Langchain Adapter
This module implements the LangChain adapter.
LangChain tools support both sync and async functions for their tools so we can leverage both in our implementation.
Example Usage:
with MCPAdapt(StdioServerParameters(command="uv", args=["run", "src/echo.py"]), LangChainAdapter()) as tools: print(tools)
LangChainAdapter
Bases: ToolAdapter
Adapter for langchain
.
Note that langchain
support both sync and async tools so we
write adapt for both methods.
Warning: if the mcp tool name is a python keyword, starts with digits or contains dashes, the tool name will be sanitized to become a valid python function name.
Source code in src/mcpadapt/langchain_adapter.py
adapt
Adapt a MCP tool to a LangChain tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[[dict | None], CallToolResult]
|
The function to adapt. |
required |
mcp_tool
|
Tool
|
The MCP tool to adapt. |
required |
Returns:
Type | Description |
---|---|
BaseTool
|
A LangChain tool. |
Source code in src/mcpadapt/langchain_adapter.py
async_adapt
async_adapt(
afunc: Callable[
[dict | None], Coroutine[Any, Any, CallToolResult]
],
mcp_tool: Tool,
) -> BaseTool
Adapt a MCP tool to a LangChain tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
afunc
|
Callable[[dict | None], Coroutine[Any, Any, CallToolResult]]
|
The function to adapt. |
required |
mcp_tool
|
Tool
|
The MCP tool to adapt. |
required |
Returns:
Type | Description |
---|---|
BaseTool
|
A LangChain tool. |