Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

modified to load transport from config

Sandipan Haldar2be377f24b0c
diff --git a/src/fastmcp/client/base.py b/src/fastmcp/client/base.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.py
index 8437c8436..426cb7e84 100644
--- a/src/fastmcp/client/transports.py
+++ b/src/fastmcp/client/transports.py
@@ -9,7 +9,7 @@ from pathlib import Path
from typing import (
TypedDict,
)
-
+from typing import Any
from exceptiongroup import BaseExceptionGroup, catch
from mcp import ClientSession, McpError, StdioServerParameters
from mcp.client.session import (
@@ -449,30 +449,37 @@ def infer_transport(
# the transport is a websocket URL
elif isinstance(transport, AnyUrl | str) and str(transport).startswith("ws"):
return WSTransport(url=transport)
+
+ ## if the transport is a config dict
elif isinstance(transport, dict):
+ if "mcpServers" not in transport:
+ raise ValueError("Invalid transport dictionary: missing 'mcpServers' key")
+ else:
+ server = transport["mcpServers"]
+ server_name = list(server.keys())[0]
# Stdio transport
- if "command" in transport and "args" in transport:
- return StdioTransport(
- command=transport["command"],
- args=transport["args"],
- env=transport.get("env", None),
- cwd=transport.get("cwd", None),
- )
-
- # HTTP transport
- elif "url" in transport:
- return SSETransport(
- url=transport["url"],
- headers=transport.get("headers", None),
- )
-
- # WebSocket transport
- elif "ws_url" in transport:
- return WSTransport(
- url=transport["ws_url"],
- )
-
- raise ValueError("Cannot determine transport type from dictionary")
+ if "command" in server[server_name] and "args" in server[server_name]:
+ return StdioTransport(
+ command=server[server_name]["command"],
+ args=server[server_name]["args"],
+ env=server[server_name].get("env", None),
+ cwd=server[server_name].get("cwd", None),
+ )
+
+ # HTTP transport
+ elif "url" in server:
+ return SSETransport(
+ url=server["url"],
+ headers=server.get("headers", None),
+ )
+
+ # WebSocket transport
+ elif "ws_url" in server:
+ return WSTransport(
+ url=server["ws_url"],
+ )
+
+ raise ValueError("Cannot determine transport type from dictionary")
# the transport is an unknown type
else: