π 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.pynew file mode 100644index 000000000..e69de29bbdiff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.pyindex 8437c8436..426cb7e84 100644--- a/src/fastmcp/client/transports.py+++ b/src/fastmcp/client/transports.py@@ -9,7 +9,7 @@ from pathlib import Pathfrom typing import (TypedDict,)-+from typing import Anyfrom exceptiongroup import BaseExceptionGroup, catchfrom mcp import ClientSession, McpError, StdioServerParametersfrom mcp.client.session import (@@ -449,30 +449,37 @@ def infer_transport(# the transport is a websocket URLelif isinstance(transport, AnyUrl | str) and str(transport).startswith("ws"):return WSTransport(url=transport)++ ## if the transport is a config dictelif 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 typeelse: