LangChain Model Context Protocol (MCP) Adapters¶
Classes:
Name | Description |
---|---|
MultiServerMCPClient |
Client for connecting to multiple MCP servers and loading LangChain-compatible tools, prompts and resources from them. |
MultiServerMCPClient
¶
Client for connecting to multiple MCP servers and loading LangChain-compatible tools, prompts and resources from them.
Methods:
Name | Description |
---|---|
__init__ |
Initialize a MultiServerMCPClient with MCP servers connections. |
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. |
__init__
¶
Initialize a MultiServerMCPClient with MCP servers connections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
connections
|
dict[str, Connection] | None
|
A dictionary mapping server names to connection configurations. If None, no initial connections are established. |
None
|
Example: basic usage (starting a new session on each tool call)
from langchain_mcp_adapters.client import MultiServerMCPClient
client = MultiServerMCPClient(
{
"math": {
"command": "python",
# Make sure to update to the full absolute path to your math_server.py file
"args": ["/path/to/math_server.py"],
"transport": "stdio",
},
"weather": {
# make sure you start your weather server on port 8000
"url": "http://localhost:8000/mcp",
"transport": "streamable_http",
}
}
)
all_tools = await client.get_tools()
Example: explicitly starting a session
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 |
Functions:
Name | Description |
---|---|
load_mcp_tools |
Load all available MCP tools and convert them to LangChain tools. |
load_mcp_tools
async
¶
load_mcp_tools(
session: ClientSession | None,
*,
connection: Connection | None = None
) -> list[BaseTool]
Load all available MCP tools and convert them to LangChain tools.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
ClientSession | None
|
MCP client session |
required |
connection
|
Connection | None
|
Optional connection config to use to create a new session if a |
None
|
Returns:
Type | Description |
---|---|
list[BaseTool]
|
a list of LangChain tools |
Functions:
Name | Description |
---|---|
load_mcp_prompt |
Load MCP prompt and convert to LangChain messages. |
Functions:
Name | Description |
---|---|
load_mcp_resources |
Load MCP resources and convert them to LangChain Blobs. |
load_mcp_resources
async
¶
Load MCP resources and convert them to LangChain Blobs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
ClientSession
|
MCP client session |
required |
uris
|
str | list[str] | None
|
List of URIs to load. If None, all resources will be loaded. NOTE: if you specify None, dynamic resources will NOT be loaded, as they need the parameters to be provided, and are ignored by MCP SDK's session.list_resources() method. |
None
|
Returns:
Type | Description |
---|---|
list[Blob]
|
A list of LangChain Blobs |