langchain-mcp-adapters¶
Reference documentation for the langchain-mcp-adapters package.
langchain_mcp_adapters.client
¶
Client for connecting to multiple MCP servers and loading LangChain tools/resources.
This module provides the MultiServerMCPClient class for managing connections to multiple MCP servers and loading tools, prompts, and resources from them.
MultiServerMCPClient
¶
Client for connecting to multiple MCP servers.
Loads LangChain-compatible tools, prompts and resources from MCP servers.
| METHOD | 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. |
__aenter__ |
Async context manager entry point. |
__aexit__ |
Async context manager exit point. |
__init__
¶
__init__(
connections: dict[str, Connection] | None = None,
*,
callbacks: Callbacks | None = None,
hooks: Hooks | None = None,
) -> None
Initialize a MultiServerMCPClient with MCP servers connections.
| PARAMETER | DESCRIPTION |
|---|---|
connections
|
A dictionary mapping server names to connection configurations. If None, no initial connections are established. |
callbacks
|
Optional callbacks for handling notifications and events.
TYPE:
|
hooks
|
Optional hooks for before/after tool call processing.
TYPE:
|
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.
| PARAMETER | DESCRIPTION |
|---|---|
server_name
|
Name to identify this server connection
TYPE:
|
auto_initialize
|
Whether to automatically initialize the session
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the server name is not found in the connections |
| YIELDS | DESCRIPTION |
|---|---|
AsyncIterator[ClientSession]
|
An initialized ClientSession |
get_tools
async
¶
Get a list of all tools from all connected servers.
| PARAMETER | DESCRIPTION |
|---|---|
server_name
|
Optional name of the server to get tools from. If None, all tools from all servers will be returned (default).
TYPE:
|
NOTE: a new session will be created for each tool call
| RETURNS | 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
¶
__aenter__
async
¶
__aenter__() -> MultiServerMCPClient
Async context manager entry point.
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
Context manager support has been removed. |
__aexit__
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Async context manager exit point.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
Exception type if an exception occurred.
TYPE:
|
exc_val
|
Exception value if an exception occurred.
TYPE:
|
exc_tb
|
Exception traceback if an exception occurred.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
Context manager support has been removed. |
langchain_mcp_adapters.tools
¶
Tools adapter for converting MCP tools to LangChain tools.
This module provides functionality to convert MCP tools into LangChain-compatible tools, handle tool execution, and manage tool conversion between the two formats.
| FUNCTION | 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,
callbacks: Callbacks | None = None,
hooks: Hooks | None = None,
server_name: str | None = None,
) -> list[BaseTool]
Load all available MCP tools and convert them to LangChain tools.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
The MCP client session. If None, connection must be provided.
TYPE:
|
connection
|
Connection config to create a new session if session is None.
TYPE:
|
callbacks
|
Optional callbacks for handling notifications and events.
TYPE:
|
hooks
|
Optional hooks for before/after tool call processing.
TYPE:
|
server_name
|
Name of the server these tools belong to.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[BaseTool]
|
List of LangChain tools. Tool annotations are returned as part |
list[BaseTool]
|
of the tool metadata object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither session nor connection is provided. |
langchain_mcp_adapters.prompts
¶
Prompts adapter for converting MCP prompts to LangChain messages.
This module provides functionality to convert MCP prompt messages into LangChain message objects, handling both user and assistant message types.
| FUNCTION | DESCRIPTION |
|---|---|
load_mcp_prompt |
Load MCP prompt and convert to LangChain messages. |
load_mcp_prompt
async
¶
load_mcp_prompt(
session: ClientSession, name: str, *, arguments: dict[str, Any] | None = None
) -> list[HumanMessage | AIMessage]
Load MCP prompt and convert to LangChain messages.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
The MCP client session.
TYPE:
|
name
|
Name of the prompt to load.
TYPE:
|
arguments
|
Optional arguments to pass to the prompt. |
| RETURNS | DESCRIPTION |
|---|---|
list[HumanMessage | AIMessage]
|
A list of LangChain messages converted from the MCP prompt. |
langchain_mcp_adapters.resources
¶
Resources adapter for converting MCP resources to LangChain Blobs.
This module provides functionality to convert MCP resources into LangChain Blob objects, handling both text and binary resource content types.
| FUNCTION | 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.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
MCP client session.
TYPE:
|
uris
|
List of URIs to load. If None, all resources will be loaded. Note: Dynamic resources will NOT be loaded when None is specified, as they require parameters and are ignored by the MCP SDK's session.list_resources() method. |
| RETURNS | DESCRIPTION |
|---|---|
list[Blob]
|
A list of LangChain Blobs. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If an error occurs while fetching a resource. |