关于 MCPAdapt
MCPAdapt 是一个开源 Python 库,可将 MCP 服务的工具接入多种 Agent 框架。它面向希望在 smolagents、LangChain 等框架中调用现有 MCP 服务的开发者,通过统一的适配器完成转换。
主要功能
- 01将 MCP 工具转换为框架工具
- 02支持 stdio 与基于 SSE 的远程服务
- 03可同时挂载多个 MCP 服务
- 04支持异步调用(取决于框架)
- 05提供可扩展的适配器接口
适用场景
- 01在 LangChain 智能体中调用 MCP 服务工具
- 02在 smolagents 中使用现有 MCP 服务
- 03为新的 Agent 框架编写自定义适配器
MCP 相关
MCPAdapt 作为 MCP 客户端侧的适配层,连接 MCP 服务并将其工具转换为各 Agent 框架可用的工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
MCPAdapt
如需了解更多背景信息、指南和 API 参考,请访问我们的新版文档。
在你喜欢的 Agent 框架中解锁 650 多个 MCP 服务的工具。
Model Context Protocol 是 Anthropic 推出的开源协议,让任何人都能轻松、快速地将工具和资源作为“MCP 服务”提供出来。
自发布以来,已有 650 多个 MCP 服务诞生,为受支持的“MCP 客户端”提供了对大量数据和工具的访问权限。
本项目让你能够在任何 Agent 框架中无缝调用任意 MCP 服务的工具,几乎可以让你的 Agent 工作流访问 650 多个 MCP 服务的工具。
访问 glama.ai 或 smithery.ai,了解你的 Agent 可以访问哪些内容。
安装说明
Smolagents
Smolagents 1.4.1 及更高版本已在其工具集合对象中直接集成 mcpadapt。 这意味着你可以直接在 smolagents 中使用它:
uv add smolagents[mcp]
其他框架
每个 Agent 框架都有自己的一组可选依赖项,以免引入不必要的依赖。 在安装命令中将框架名放在方括号内,即可选择所需版本。
# with uv
uv add mcpadapt[langchain]
# or with pip
pip install mcpadapt[langchain]
目前支持的框架:smolagents、langchain、crewAI、google-genai。
如有需要,也可以用逗号分隔,添加多个框架。
用法
⚠️ 安全警告:使用 MCP 服务时,尤其是通过 SSE(Server-Sent Events)连接时,务必格外谨慎,只连接可信且经过验证的服务。连接前,请始终核实 MCP 服务的来源和安全性。
Smolagents
由于 mcpadapt 已集成到 smolagents 中,只需像下面这样使用 smolagents 的工具集合:
from mcp import StdioServerParameters
from smolagents.tools import ToolCollection
serverparams = StdioServerParameters(command="uv", args=["run", "src/echo.py"])
with ToolCollection.from_mcp(serverparams) as tool_collection:
... # enjoy your tools!
其他框架
MCPAdapt 可将任意 MCP 服务适配为工具,供你直接在 Agent 工作流中使用:
from mcp import StdioServerParameters
from mcpadapt.core import MCPAdapt
from mcpadapt.smolagents_adapter import SmolAgentsAdapter
with MCPAdapt(
# specify the command to run your favorite MCP server (support also smithery and co.)
StdioServerParameters(command="uv", args=["run", "src/echo.py"]),
# or a dict of sse server parameters e.g. {"url": http://localhost:8000, "headers": ...}
# specify the adapter you want to use to adapt MCP into your tool in this case smolagents.
SmolAgentsAdapter(),
) as tools:
# enjoy your smolagents tools as if you wrote them yourself
...
MCP Adapt 支持 Smolagents、Langchain、CrewAI、google-genai [pydantic.dev、Llammaindex 等]。 *即将推出。
注意:你也可以像这样指定多个 MCP 服务:
from mcp import StdioServerParameters
from mcpadapt.core import MCPAdapt
from mcpadapt.smolagents_adapter import SmolAgentsAdapter
with MCPAdapt(
[
StdioServerParameters(command="uv", args=["run", "src/echo1.py"]),
StdioServerParameters(command="uv", args=["run", "src/echo2.py"]),
],
SmolAgentsAdapter(),
) as tools:
# tools is now a flattened list of tools from the 2 MCP servers.
...
如果底层 Agent 框架支持异步,我们也支持异步。
有关用法的更多详情,请参阅我们的示例。
参与贡献
如果缺少你喜欢的 Agent 框架,也没关系,你可以自行添加,过程非常简单:
- 在
src/mcpadapt/{name_of_your_framework}_adapter.py中创建一个新模块:
class YourFrameworkAdapter(ToolAdapter):
def adapt(
self,
func: Callable[[dict | None], mcp.types.CallToolResult],
mcp_tool: mcp.types.Tool,
) -> YourFramework.Tool:
# HERE implement how the adapter should convert a simple function and mcp_tool (JSON Schema)
# into your framework tool. see smolagents_adapter.py for an example
def async_adapt(
self,
afunc: Callable[[dict | None], Coroutine[Any, Any, mcp.types.CallToolResult]],
mcp_tool: mcp.types.Tool,
) -> YourFramework.Tool:
# if your framework supports async function even better use async_adapt.
- 就这样,测试你的适配器是否正常工作,然后向我们提交 PR,与大家分享。
路线图
- 初始框架,供任何人开始创建适配器
- 支持 smolagents
- 支持 pydantic-ai
- 支持 langchain
- 支持 llamaindex
- 支持 swarm
- 支持 crewAI
- 支持 google genai
- 支持通过 SSE 使用远程 MCP 服务
- 支持 jupyter notebook
- 添加测试
贡献者
我们感谢所有贡献者和维护者所做的工作及贡献。
核心维护者:
贡献者:
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。