langchain_mcp_tools¶
- class langchain_mcp_tools_docs.McpServerCommandBasedConfig[source]¶
Bases:
TypedDict
Configuration for an MCP server launched via command line.
This configuration is used for local MCP servers that are started as child processes using the stdio client. It defines the command to run, optional arguments, environment variables, working directory, and error logging options.
- command¶
The executable command to run (e.g., “npx”, “uvx”, “python”).
- args¶
Optional list of command-line arguments to pass to the command.
- env¶
Optional dictionary of environment variables to set for the process.
- cwd¶
Optional working directory where the command will be executed.
- errlog¶
Optional file-like object for redirecting the server’s stderr output.
Example:
{ "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."], "env": {"NODE_ENV": "production"}, "cwd": "/path/to/working/directory", "errlog": open("server.log", "w") }
-
command:
str
¶
-
args:
NotRequired
[Optional
[List
[str
]]]¶
-
env:
NotRequired
[Optional
[Dict
[str
,str
]]]¶
-
cwd:
NotRequired
[str
|None
]¶
-
errlog:
NotRequired
[TextIO
|None
]¶
- class langchain_mcp_tools_docs.McpServerUrlBasedConfig[source]¶
Bases:
TypedDict
Configuration for a remote MCP server accessed via URL.
This configuration is used for remote MCP servers that are accessed via HTTP/HTTPS (Server-Sent Events) or WebSocket connections. It defines the URL to connect to and optional HTTP headers for authentication.
- url¶
The URL of the remote MCP server. For SSE servers, use http:// or https:// prefix. For WebSocket servers, use ws:// or wss:// prefix.
- headers¶
Optional dictionary of HTTP headers to include in the request, typically used for authentication (e.g., bearer tokens).
Example for SSE server:
{ "url": "https://example.com/mcp/sse", "headers": {"Authorization": "Bearer token123"} }
Example for WebSocket server:
{ "url": "wss://example.com/mcp/ws" }
-
url:
str
¶
-
headers:
NotRequired
[Optional
[Dict
[str
,str
]]]¶
- langchain_mcp_tools_docs.SingleMcpServerConfig¶
Configuration for a single MCP server, either command-based or URL-based.
This type represents the configuration for a single MCP server, which can be either:
A local server launched via command line (McpServerCommandBasedConfig)
A remote server accessed via URL (McpServerUrlBasedConfig)
The type is determined by the presence of either the “command” key (for command-based) or the “url” key (for URL-based).
alias of
McpServerCommandBasedConfig
|McpServerUrlBasedConfig
- langchain_mcp_tools_docs.McpServersConfig¶
Configuration dictionary for multiple MCP servers.
A dictionary mapping server names (as strings) to their respective configurations. Each server name acts as a logical identifier used for logging and debugging. The configuration for each server can be either command-based or URL-based.
Example:
{ "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."] }, "fetch": { "command": "uvx", "args": ["mcp-server-fetch"] }, "remote-server": { "url": "https://example.com/mcp/sse", "headers": {"Authorization": "Bearer token123"} } }
alias of
Dict
[str
,McpServerCommandBasedConfig
|McpServerUrlBasedConfig
]
- langchain_mcp_tools_docs.McpServerCleanupFn¶
Type for the async cleanup function returned by convert_mcp_to_langchain_tools.
This represents an asynchronous function that takes no arguments and returns nothing. It’s used to properly shut down all MCP server connections and clean up resources when the tools are no longer needed.
Example usage:
tools, cleanup = await convert_mcp_to_langchain_tools(server_configs) # Use tools... await cleanup() # Clean up resources when done
alias of
Callable
[[],Awaitable
[None
]]
- async langchain_mcp_tools_docs.convert_mcp_to_langchain_tools(server_configs, logger=None)[source]¶
Initialize multiple MCP servers and convert their tools to LangChain format.
This async function manages parallel initialization of multiple MCP servers, converts their tools to LangChain format, and provides a cleanup mechanism. It orchestrates the full lifecycle of multiple servers.
- Parameters:
server_configs (
Dict
[str
,Union
[McpServerCommandBasedConfig
,McpServerUrlBasedConfig
]]) – Dictionary mapping server names to their configurations, where each configuration contains command, args, and env settingslogger (
Optional
[Logger
]) – Logger instance to use for logging events and errors. If None, uses module logger with fallback to a pre-configured logger when no root handlers exist.
- Returns:
List of converted LangChain tools from all servers
Async cleanup function to properly shutdown all server connections
- Return type:
A tuple containing
Example:
server_configs = { "fetch": { "command": "uvx", "args": ["mcp-server-fetch"] }, "weather": { "command": "npx", "args": ["-y","@h1deya/mcp-server-weather"] } } tools, cleanup = await convert_mcp_to_langchain_tools(server_configs) # Use tools... await cleanup()