Sandipan Haldar commited on
Commit
dc6bd38
·
unverified ·
2 Parent(s): af4427fc87c8e6

Merge pull request #1 from sandipan1/feature/load-server-config-client

Browse files
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
@@ -9,7 +9,7 @@ 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
15
  from mcp.client.session import (
@@ -416,7 +416,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.
@@ -449,7 +449,40 @@ def infer_transport(
449
  # the transport is a websocket URL
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}")
 
9
  from typing import (
10
  TypedDict,
11
  )
12
+ from typing import Any
13
  from exceptiongroup import BaseExceptionGroup, catch
14
  from mcp import ClientSession, McpError, StdioServerParameters
15
  from mcp.client.session import (
 
416
 
417
 
418
  def infer_transport(
419
+ transport: ClientTransport | FastMCPServer | AnyUrl | Path | dict[str, Any] | str,
420
  ) -> ClientTransport:
421
  """
422
  Infer the appropriate transport type from the given transport argument.
 
449
  # the transport is a websocket URL
450
  elif isinstance(transport, AnyUrl | str) and str(transport).startswith("ws"):
451
  return WSTransport(url=transport)
452
+
453
+ ## if the transport is a config dict
454
+ elif isinstance(transport, dict):
455
+ if "mcpServers" not in transport:
456
+ raise ValueError("Invalid transport dictionary: missing 'mcpServers' key")
457
+ else:
458
+ server = transport["mcpServers"]
459
+ if len(list(server.keys())) > 1:
460
+ raise ValueError("Invalid transport dictionary: multiple servers found - only one expected")
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}")