use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct McpToolInfo {
pub name: String,
pub description: String,
pub input_schema: serde_json::Value,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum McpTransport {
Http {
url: String,
#[serde(default)]
headers: BTreeMap<String, String>,
},
Stdio {
command: String,
args: Vec<String>,
#[serde(default)]
env: BTreeMap<String, String>,
},
}
impl McpTransport {
pub fn http(url: impl Into<String>) -> Self {
Self::Http {
url: url.into(),
headers: BTreeMap::new(),
}
}
pub fn stdio(
command: impl Into<String>,
args: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self::Stdio {
command: command.into(),
args: args.into_iter().map(Into::into).collect(),
env: BTreeMap::new(),
}
}
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
if let Self::Http { headers, .. } = &mut self {
headers.insert(name.into(), value.into());
}
self
}
pub fn with_env(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
if let Self::Stdio { env, .. } = &mut self {
env.insert(name.into(), value.into());
}
self
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpServerConfig {
pub name: String,
pub transport: McpTransport,
#[serde(default)]
pub auto_reconnect: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn transport_serde_matches_connections_json_shape() {
let stdio: McpTransport = serde_json::from_value(json!({
"type": "stdio",
"command": "npx",
"args": ["-y", "@notionhq/notion-mcp-server"],
"env": { "NOTION_TOKEN": "tok-1" }
}))
.unwrap();
match &stdio {
McpTransport::Stdio { command, args, env } => {
assert_eq!(command, "npx");
assert_eq!(args, &["-y", "@notionhq/notion-mcp-server"]);
assert_eq!(env["NOTION_TOKEN"], "tok-1");
}
other => panic!("wrong variant: {other:?}"),
}
let http: McpTransport = serde_json::from_value(json!({
"type": "http",
"url": "https://example.com/mcp",
"headers": { "Authorization": "Bearer tok-2" }
}))
.unwrap();
match &http {
McpTransport::Http { url, headers } => {
assert_eq!(url, "https://example.com/mcp");
assert_eq!(headers["Authorization"], "Bearer tok-2");
}
other => panic!("wrong variant: {other:?}"),
}
}
#[test]
fn transport_serde_tolerates_missing_env_and_headers() {
let stdio: McpTransport = serde_json::from_value(json!({
"type": "stdio", "command": "npx", "args": []
}))
.unwrap();
assert!(matches!(stdio, McpTransport::Stdio { env, .. } if env.is_empty()));
let http: McpTransport =
serde_json::from_value(json!({"type": "http", "url": "https://x"})).unwrap();
assert!(matches!(http, McpTransport::Http { headers, .. } if headers.is_empty()));
}
#[test]
fn transport_serde_round_trips() {
let t = McpTransport::stdio("npx", ["-y", "pkg"]).with_env("TOKEN", "v");
let raw = serde_json::to_value(&t).unwrap();
assert_eq!(raw["type"], "stdio");
assert_eq!(raw["env"]["TOKEN"], "v");
let back: McpTransport = serde_json::from_value(raw).unwrap();
assert_eq!(back, t);
let h = McpTransport::http("https://x").with_header("Authorization", "Bearer v");
let back: McpTransport = serde_json::from_value(serde_json::to_value(&h).unwrap()).unwrap();
assert_eq!(back, h);
}
#[test]
fn header_and_env_builders_only_touch_their_variant() {
let t = McpTransport::stdio("sh", ["-c", "x"]).with_header("X", "y");
assert!(matches!(t, McpTransport::Stdio { env, .. } if env.is_empty()));
let t = McpTransport::http("https://x").with_env("X", "y");
assert!(matches!(t, McpTransport::Http { headers, .. } if headers.is_empty()));
}
}