Spaces:
Running
Running
Merge pull request #260 from sandipan1/main
Browse filesAdded feature : Load MCP server using config
src/fastmcp/client/base.py
CHANGED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
|
|
|
|
|
|
src/fastmcp/client/client.py
CHANGED
|
@@ -35,7 +35,7 @@ class Client:
|
|
| 35 |
|
| 36 |
def __init__(
|
| 37 |
self,
|
| 38 |
-
transport: ClientTransport | FastMCP | AnyUrl | Path | str,
|
| 39 |
# Common args
|
| 40 |
roots: RootsList | RootsHandler | None = None,
|
| 41 |
sampling_handler: SamplingHandler | None = None,
|
|
|
|
| 35 |
|
| 36 |
def __init__(
|
| 37 |
self,
|
| 38 |
+
transport: ClientTransport | FastMCP | AnyUrl | Path | dict[str, Any] | str,
|
| 39 |
# Common args
|
| 40 |
roots: RootsList | RootsHandler | None = None,
|
| 41 |
sampling_handler: SamplingHandler | None = None,
|
src/fastmcp/client/transports.py
CHANGED
|
@@ -6,9 +6,7 @@ import shutil
|
|
| 6 |
import sys
|
| 7 |
from collections.abc import AsyncIterator
|
| 8 |
from pathlib import Path
|
| 9 |
-
from typing import
|
| 10 |
-
TypedDict,
|
| 11 |
-
)
|
| 12 |
|
| 13 |
from exceptiongroup import BaseExceptionGroup, catch
|
| 14 |
from mcp import ClientSession, McpError, StdioServerParameters
|
|
@@ -416,7 +414,7 @@ class FastMCPTransport(ClientTransport):
|
|
| 416 |
|
| 417 |
|
| 418 |
def infer_transport(
|
| 419 |
-
transport: ClientTransport | FastMCPServer | AnyUrl | Path | str,
|
| 420 |
) -> ClientTransport:
|
| 421 |
"""
|
| 422 |
Infer the appropriate transport type from the given transport argument.
|
|
@@ -450,6 +448,41 @@ def infer_transport(
|
|
| 450 |
elif isinstance(transport, AnyUrl | str) and str(transport).startswith("ws"):
|
| 451 |
return WSTransport(url=transport)
|
| 452 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
# the transport is an unknown type
|
| 454 |
else:
|
| 455 |
raise ValueError(f"Could not infer a valid transport from: {transport}")
|
|
|
|
| 6 |
import sys
|
| 7 |
from collections.abc import AsyncIterator
|
| 8 |
from pathlib import Path
|
| 9 |
+
from typing import Any, TypedDict
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from exceptiongroup import BaseExceptionGroup, catch
|
| 12 |
from mcp import ClientSession, McpError, StdioServerParameters
|
|
|
|
| 414 |
|
| 415 |
|
| 416 |
def infer_transport(
|
| 417 |
+
transport: ClientTransport | FastMCPServer | AnyUrl | Path | dict[str, Any] | str,
|
| 418 |
) -> ClientTransport:
|
| 419 |
"""
|
| 420 |
Infer the appropriate transport type from the given transport argument.
|
|
|
|
| 448 |
elif isinstance(transport, AnyUrl | str) and str(transport).startswith("ws"):
|
| 449 |
return WSTransport(url=transport)
|
| 450 |
|
| 451 |
+
## if the transport is a config dict
|
| 452 |
+
elif isinstance(transport, dict):
|
| 453 |
+
if "mcpServers" not in transport:
|
| 454 |
+
raise ValueError("Invalid transport dictionary: missing 'mcpServers' key")
|
| 455 |
+
else:
|
| 456 |
+
server = transport["mcpServers"]
|
| 457 |
+
if len(list(server.keys())) > 1:
|
| 458 |
+
raise ValueError(
|
| 459 |
+
"Invalid transport dictionary: multiple servers found - only one expected"
|
| 460 |
+
)
|
| 461 |
+
server_name = list(server.keys())[0]
|
| 462 |
+
# Stdio transport
|
| 463 |
+
if "command" in server[server_name] and "args" in server[server_name]:
|
| 464 |
+
return StdioTransport(
|
| 465 |
+
command=server[server_name]["command"],
|
| 466 |
+
args=server[server_name]["args"],
|
| 467 |
+
env=server[server_name].get("env", None),
|
| 468 |
+
cwd=server[server_name].get("cwd", None),
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
# HTTP transport
|
| 472 |
+
elif "url" in server:
|
| 473 |
+
return SSETransport(
|
| 474 |
+
url=server["url"],
|
| 475 |
+
headers=server.get("headers", None),
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
# WebSocket transport
|
| 479 |
+
elif "ws_url" in server:
|
| 480 |
+
return WSTransport(
|
| 481 |
+
url=server["ws_url"],
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
raise ValueError("Cannot determine transport type from dictionary")
|
| 485 |
+
|
| 486 |
# the transport is an unknown type
|
| 487 |
else:
|
| 488 |
raise ValueError(f"Could not infer a valid transport from: {transport}")
|