MCP¶
DistributedMCPClient
¶
Client for connecting to multiple MCP servers and loading LangChain-compatible tools, prompts and resources from them.
Methods:
Name | Description |
---|---|
session |
Connect to an MCP server and initialize a session. |
get_tools |
Get a list of all tools from all connected servers. |
get_prompt |
Get a prompt from a given MCP server. |
get_resources |
Get resources from a given MCP server. |
session
async
¶
session(server_name: str, *, auto_initialize: bool = True) -> AsyncIterator[ClientSession]
Connect to an MCP server and initialize a session.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server_name
|
str
|
Name to identify this server connection |
required |
auto_initialize
|
bool
|
Whether to automatically initialize the session |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
If the server name is not found in the connections |
Yields:
Type | Description |
---|---|
AsyncIterator[ClientSession]
|
An initialized ClientSession |
get_tools
async
¶
Get a list of all tools from all connected servers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server_name
|
str | None
|
Optional name of the server to get tools from. If None, all tools from all servers will be returned (default). |
None
|
NOTE: a new session will be created for each tool call
Returns:
Type | Description |
---|---|
list[BaseTool]
|
A list of LangChain tools |
get_prompt
async
¶
get_prompt(server_name: str, prompt_name: str, *, arguments: dict[str, Any] | None = None) -> list[HumanMessage | AIMessage]
Get a prompt from a given MCP server.
get_resources
async
¶
Get resources from a given MCP server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server_name
|
str
|
Name of the server to get resources from |
required |
uris
|
str | list[str] | None
|
Optional resource URI or list of URIs to load. If not provided, all resources will be loaded. |
None
|
Returns:
Type | Description |
---|---|
list[Blob]
|
A list of LangChain Blobs |