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.
2
2
3
3
This module provides the MultiServerMCPClient class for managing connections to multiple
4
4
MCP servers and loading tools, prompts, and resources from them.
29
29
from langchain_mcp_adapters .tools import load_mcp_tools
30
30
31
31
ASYNC_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(...)). "
33
34
"Instead, you can do one of the following:\n "
34
35
"1. client = MultiServerMCPClient(...)\n "
35
36
" tools = await client.get_tools()\n "
40
41
41
42
42
43
class 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
+ """
44
48
45
49
def __init__ (self , connections : dict [str , Connection ] | None = None ) -> None :
46
50
"""Initialize a MultiServerMCPClient with MCP servers connections.
@@ -58,7 +62,8 @@ def __init__(self, connections: dict[str, Connection] | None = None) -> None:
58
62
{
59
63
"math": {
60
64
"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
62
67
"args": ["/path/to/math_server.py"],
63
68
"transport": "stdio",
64
69
},
@@ -84,7 +89,9 @@ def __init__(self, connections: dict[str, Connection] | None = None) -> None:
84
89
```
85
90
86
91
"""
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
+ )
88
95
89
96
@asynccontextmanager
90
97
async def session (
@@ -107,7 +114,10 @@ async def session(
107
114
108
115
"""
109
116
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
+ )
111
121
raise ValueError (msg )
112
122
113
123
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]:
130
140
"""
131
141
if server_name is not None :
132
142
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
+ )
134
147
raise ValueError (msg )
135
148
return await load_mcp_tools (None , connection = self .connections [server_name ])
136
149
137
150
all_tools : list [BaseTool ] = []
138
151
load_mcp_tool_tasks = []
139
152
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
+ )
141
156
load_mcp_tool_tasks .append (load_mcp_tool_task )
142
157
tools_list = await asyncio .gather (* load_mcp_tool_tasks )
143
158
for tools in tools_list :
@@ -165,7 +180,8 @@ async def get_resources(
165
180
166
181
Args:
167
182
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.
169
185
170
186
Returns:
171
187
A list of LangChain Blobs
0 commit comments