1- """Client for connecting to multiple MCP servers and loading LangChain-compatible resources.
1+ """Client for connecting to multiple MCP servers and loading LangChain tools/ resources.
22
33This module provides the MultiServerMCPClient class for managing connections to multiple
44MCP servers and loading tools, prompts, and resources from them.
2929from langchain_mcp_adapters .tools import load_mcp_tools
3030
3131ASYNC_CONTEXT_MANAGER_ERROR = (
32- "As of langchain-mcp-adapters 0.1.0, MultiServerMCPClient cannot be used as a context manager (e.g., async with MultiServerMCPClient(...)). "
32+ "As of langchain-mcp-adapters 0.1.0, MultiServerMCPClient cannot be used as a "
33+ "context manager (e.g., async with MultiServerMCPClient(...)). "
3334 "Instead, you can do one of the following:\n "
3435 "1. client = MultiServerMCPClient(...)\n "
3536 " tools = await client.get_tools()\n "
4041
4142
4243class MultiServerMCPClient :
43- """Client for connecting to multiple MCP servers and loading LangChain-compatible tools, prompts and resources from them."""
44+ """Client for connecting to multiple MCP servers.
45+
46+ Loads LangChain-compatible tools, prompts and resources from MCP servers.
47+ """
4448
4549 def __init__ (self , connections : dict [str , Connection ] | None = None ) -> None :
4650 """Initialize a MultiServerMCPClient with MCP servers connections.
@@ -58,7 +62,8 @@ def __init__(self, connections: dict[str, Connection] | None = None) -> None:
5862 {
5963 "math": {
6064 "command": "python",
61- # Make sure to update to the full absolute path to your math_server.py file
65+ # Make sure to update to the full absolute path to your
66+ # math_server.py file
6267 "args": ["/path/to/math_server.py"],
6368 "transport": "stdio",
6469 },
@@ -84,7 +89,9 @@ def __init__(self, connections: dict[str, Connection] | None = None) -> None:
8489 ```
8590
8691 """
87- self .connections : dict [str , Connection ] = connections if connections is not None else {}
92+ self .connections : dict [str , Connection ] = (
93+ connections if connections is not None else {}
94+ )
8895
8996 @asynccontextmanager
9097 async def session (
@@ -107,7 +114,10 @@ async def session(
107114
108115 """
109116 if server_name not in self .connections :
110- msg = f"Couldn't find a server with name '{ server_name } ', expected one of '{ list (self .connections .keys ())} '"
117+ msg = (
118+ f"Couldn't find a server with name '{ server_name } ', "
119+ f"expected one of '{ list (self .connections .keys ())} '"
120+ )
111121 raise ValueError (msg )
112122
113123 async with create_session (self .connections [server_name ]) as session :
@@ -130,14 +140,19 @@ async def get_tools(self, *, server_name: str | None = None) -> list[BaseTool]:
130140 """
131141 if server_name is not None :
132142 if server_name not in self .connections :
133- msg = f"Couldn't find a server with name '{ server_name } ', expected one of '{ list (self .connections .keys ())} '"
143+ msg = (
144+ f"Couldn't find a server with name '{ server_name } ', "
145+ f"expected one of '{ list (self .connections .keys ())} '"
146+ )
134147 raise ValueError (msg )
135148 return await load_mcp_tools (None , connection = self .connections [server_name ])
136149
137150 all_tools : list [BaseTool ] = []
138151 load_mcp_tool_tasks = []
139152 for connection in self .connections .values ():
140- load_mcp_tool_task = asyncio .create_task (load_mcp_tools (None , connection = connection ))
153+ load_mcp_tool_task = asyncio .create_task (
154+ load_mcp_tools (None , connection = connection )
155+ )
141156 load_mcp_tool_tasks .append (load_mcp_tool_task )
142157 tools_list = await asyncio .gather (* load_mcp_tool_tasks )
143158 for tools in tools_list :
@@ -165,7 +180,8 @@ async def get_resources(
165180
166181 Args:
167182 server_name: Name of the server to get resources from
168- uris: Optional resource URI or list of URIs to load. If not provided, all resources will be loaded.
183+ uris: Optional resource URI or list of URIs to load. If not provided,
184+ all resources will be loaded.
169185
170186 Returns:
171187 A list of LangChain Blobs
0 commit comments