
Langchain-MCP-Tools-Py
MCP to Langchain Tools Conversion Utility / Python
3 years
Works with Finder
18
Github Watches
8
Github Forks
18
Github Stars
MCP To LangChain Tools Conversion Utility

NOTE
LangChain's official LangChain MCP Adapters library has been released at:
- pypi: https://pypi.org/project/langchain-mcp-adapters/
- github: https://github.com/langchain-ai/langchain-mcp-adapters
You may want to consider using the above if you don't have specific needs for using this library...
Introduction
This package is intended to simplify the use of Model Context Protocol (MCP) server tools with LangChain / Python.
Model Context Protocol (MCP), an open standard announced by Anthropic, dramatically expands LLM’s scope by enabling external tool and resource integration, including GitHub, Google Drive, Slack, Notion, Spotify, Docker, PostgreSQL, and more…
MCP is likely to become the de facto industry standard as OpenAI has announced its adoption.
Over 2000 functional components available as MCP servers:
- MCP Server Listing on the Official Site
- MCP.so - Find Awesome MCP Servers and Clients
- Smithery: MCP Server Registry
The goal of this utility is to make these 2000+ MCP servers readily accessible from LangChain.
It contains a utility function convert_mcp_to_langchain_tools()
.
This async function handles parallel initialization of specified multiple MCP servers
and converts their available tools into a list of LangChain-compatible tools.
For detailed information on how to use this library, please refer to the following document:
A typescript equivalent of this utility is available here
Prerequisites
- Python 3.11+
Installation
pip install langchain-mcp-tools
API docs
Can be found here
Quick Start
A minimal but complete working usage example can be found in this example in the langchain-mcp-tools-py-usage repo
convert_mcp_to_langchain_tools()
utility function accepts MCP server configurations
that follow the same structure as
Claude for Desktop,
but only the contents of the mcpServers
property,
and is expressed as a dict
, e.g.:
mcp_servers = {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "."]
},
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
}
tools, cleanup = await convert_mcp_to_langchain_tools(
mcp_servers
)
This utility function initializes all specified MCP servers in parallel,
and returns LangChain Tools
(tools: list[BaseTool]
)
by gathering available MCP tools from the servers,
and by wrapping them into LangChain tools.
It also returns an async callback function (cleanup: McpServerCleanupFn
)
to be invoked to close all MCP server sessions when finished.
The returned tools can be used with LangChain, e.g.:
# from langchain.chat_models import init_chat_model
llm = init_chat_model(
model="claude-3-7-sonnet-latest",
model_provider="anthropic"
)
# from langgraph.prebuilt import create_react_agent
agent = create_react_agent(
llm,
tools
)
For hands-on experimentation with MCP server integration, try this LangChain application built with the utility
For detailed information on how to use this library, please refer to the following document:
"Supercharging LangChain: Integrating 2000+ MCP with ReAct"
Experimental Features
Remote MCP Server Support
mcp_servers
configuration for SSE and Websocket servers are as follows:
"sse-server-name": {
"url": f"http://{sse_server_host}:{sse_server_port}/..."
},
"ws-server-name": {
"url": f"ws://{ws_server_host}:{ws_server_port}/..."
},
Note that the key "url"
may be changed in the future to match
the MCP server configurations used by Claude for Desktop once
it introduces remote server support.
A usage example can be found here
Authentication Support for SSE Connections
A new key "headers"
has been introduced to pass HTTP headers to the SSE (Server-Sent Events) connection.
It takes dict[str, str]
and is primarily intended to support SSE MCP servers
that require authentication via bearer tokens or other custom headers.
"sse-server-name": {
"url": f"http://{sse_server_host}:{sse_server_port}/..."
"headers": {"Authorization": f"Bearer {bearer_token}"}
},
The key name header
is derived from the Python SDK
sse_client()
argument name.
A simple example showing how to implement MCP SSE server and client with authentication can be found in sse-auth-test-client.py and in sse-auth-test-server.py of this usage examples repo.
Working Directory Configuration for Local MCP Servers
The working directory that is used when spawning a local (stdio) MCP server
can be specified with the "cwd"
key as follows:
"local-server-name": {
"command": "...",
"args": [...],
"cwd": "/working/directory" # the working dir to be use by the server
},
The key name cwd
is derived from
Python SDK's StdioServerParameters
.
stderr Redirection for Local MCP Server
A new key "errlog"
has been introduced to specify a file-like object
to which local (stdio) MCP server's stderr is redirected.
log_path = f"mcp-server-{server_name}.log"
log_file = open(log_path, "w")
mcp_servers[server_name]["errlog"] = log_file
A usage example can be found here
NOTE: Why the key name errlog
was chosen:
Unlike TypeScript SDK's StdioServerParameters
, the Python
SDK's StdioServerParameters
doesn't include stderr: int
.
Instead, it calls stdio_client()
with a separate argument
errlog: TextIO
.
I once included stderr: int
for
compatibility with the TypeScript version, but decided to
follow the Python SDK more closely.
Limitations
- Currently, only text results of tool calls are supported.
- MCP features other than Tools are not supported.
Change Log
Can be found here
相关推荐
🧑🚀 全世界最好的 llm 资料总结(数据处理、模型训练、模型部署、 O1 模型、 MCP 、小语言模型、视觉语言模型) | Zusammenfassung der weltbesten LLM -Ressourcen.
🔥 1Panel bietet eine intuitive Weboberfläche und einen MCP -Server, um Websites, Dateien, Container, Datenbanken und LLMs auf einem Linux -Server zu verwalten.
⛓️Rugele ist ein leichter, leistungsstarker, leistungsstarker, eingebetteter Komponenten-Orchestrierungsregel-Motor-Rahmen für GO.
Dieses Projekt wurde erstellt, um zu demonstrieren, wie wir uns mit verschiedenen Modellkontextprotokollen (MCPs) verbinden können.
Reviews

user_fuUsJGfG
As a dedicated user of MCP applications, I was thrilled to discover the langchain-mcp-tools-py by hideya. This toolset has significantly streamlined my workflow, making complex tasks more manageable with its intuitive interface. The seamless integration with other tools and robust functionality have made it indispensable. Highly recommend!

user_TPIAOQsI
I've been using langchain-mcp-tools-py, and it's a fantastic toolset for MCP applications! Created by the talented hideya, this product provides seamless integration and robust functionality in Python, enhancing workflow efficiency significantly. Highly recommend it for anyone looking to streamline their processes!

user_f4h6I8Zx
As a loyal user of the mcp application, I must say that langchain-mcp-tools-py by hideya is a game-changer. This tool is efficient, user-friendly, and seamlessly integrates with my workflow. It has significantly boosted my productivity and enhanced the overall experience. Highly recommended for anyone looking to optimize their mcp application usage!

user_WeQtFyFP
I recently started using langchain-mcp-tools-py by hideya, and it has significantly streamlined my workflow. The tools are intuitive and robust, making it easy to handle complex tasks efficiently. I'm particularly impressed with the seamless integration and the comprehensive documentation. Highly recommended for anyone looking to enhance their MCP applications!

user_Kf0hX9ND
As a dedicated MCP application user, I am thoroughly impressed with the Langchain-MCP-Tools-Py created by hideya. This toolset significantly enhances productivity and efficiency with its seamless integration and user-friendly interface. It has become an indispensable part of my workflow, streamlining complex tasks and providing reliable performance. Highly recommend to all who are looking to optimize their MCP application usage!

user_tiVOW2ut
As an avid user of the MCP application, I have found langchain-mcp-tools-py by hideya to be an exceptional tool. Its integration and functionality seamlessly enhance my workflow. The product is reliable and intuitive, making it a must-have for MCP users. I highly recommend it to anyone looking to optimize their productivity.

user_L8zICGIT
Langchain-mcp-tools-py is an exceptional tool created by hideya. It's highly user-friendly and integrates seamlessly into various workflows. The language processing capabilities truly enhance productivity and simplify complex tasks. Highly recommend checking out the product for anyone looking to optimize their processes!

user_tzrNlIqI
I've been using langchain-mcp-tools-py by hideya, and it's been a game-changer! The tool seamlessly integrates with my current MCP applications, making my workflow efficient and effective. The setup was straightforward, and the functionality is robust and reliable. Highly recommend it for anyone looking to enhance their MCP experience!