Skip to main content

让 MCP 工具像本地函数一样简单的桥接库

Project description

🚀 Easy MCP — 让 MCP 工具像本地函数一样简单

一行代码集成任意 MCP 服务 自动管理子进程生命周期 · 零配置接入 LangChain / LangGraph Agent

✨ 特性

  • 自动资源管理:无需手动启动/关闭 MCP 服务
  • 零侵入集成:返回标准 LangChain Tool 列表
  • 多服务支持:高德地图、天气、数据库 MCP 一次性接入
  • 生产可用:异常安全、子进程隔离、异步非阻塞

🚀 快速开始

1. 安装

pip install easy-mcp

2. 🧪「5 行代码」调用高德地图 MCP

from src.easy_mcp import MCPToolLoader
import asyncio


async def main():
    async with MCPToolLoader([{
        "command": "npx",
        "args": ["-y", "@amap/amap-maps-mcp-server"],
        "env": {"AMAP_MAPS_API_KEY": "你的密钥"}
    }]) as tools:
        search = next(t for t in tools if t.name == "maps_text_search")
        print(await search.ainvoke({"keywords": "西湖", "city": "杭州"}))


asyncio.run(main())

输出示例

{
  "pois": [
    {"name": "杭州西湖", "location": "120.1551,30.2741", ...}
  ]
}

🤖 构建 MCP-Powered Agent

from langgraph.graph import StateGraph
from src.easy_mcp import MCPToolLoader

async with MCPToolLoader([高德配置]) as tools:
    graph = StateGraph(MessagesState)
    graph.add_node("agent", create_agent_node(tools))
    # ... 添加更多工具结点 ...
    app = graph.compile()
    await app.ainvoke({"messages": [("user", "西湖附近有什么酒店?")]})

👉 查看完整示例:examples/full_agent_demo.py

🔧 支持的 MCP 服务

服务 安装命令 必需环境变量
高德地图 npx @amap/amap-maps-mcp-server AMAP_MAPS_API_KEY
天气 API npx @weather/mcp-server WEATHER_API_KEY
自定义 MCP 任意符合 MCP 协议的进程 -

📂 项目结构

easy-mcp/
├── examples/
│   ├── quickstart.py          # 5 行上手示例
│   └── full_agent_demo.py     # 完整 LangGraph Agent 示例
├── src/easy_mcp/
│   ├── __init__.py            # 导出 MCPToolLoader
│   ├── bridge.py              # MCP ↔ LangChain 适配器
│   └── client.py              # MCP 客户端封装
└── README.md

❓ 常见问题(FAQ)

Q: 如何添加新的 MCP 服务?

只需在 MCPToolLoader([...]) 的列表中添加一个新的配置:

{
    "command": "python",
    "args": ["my_mcp_server.py"],
    "env": {"API_KEY": "..."}
}

Q: 子进程会残留吗?

不会。 MCPToolLoader 使用 AsyncExitStack 和自动回收机制保证:

  • MCP 子进程 100% 自动退出
  • 不留下僵尸进程
  • 异常情况下仍可安全清理

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_mcp_langchain-0.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_mcp_langchain-0.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file easy_mcp_langchain-0.1.tar.gz.

File metadata

  • Download URL: easy_mcp_langchain-0.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for easy_mcp_langchain-0.1.tar.gz
Algorithm Hash digest
SHA256 49578ef08867ec0992ad031cf153323d4d19f4f24292d3ca34b14afcc32a6893
MD5 0999345837d51e66d5ac4a9c1117c6df
BLAKE2b-256 7c90f1b50b560044217f773d29faf8c928b5c68b0554860c849dbdf713b72d84

See more details on using hashes here.

File details

Details for the file easy_mcp_langchain-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_mcp_langchain-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a70709d13c8ef6c4a3481f5794b42fdbd151c4a79ad48cc9b2bebec7ebe743b
MD5 f2ff77598619a4674729df2a4744731f
BLAKE2b-256 1e065728a6cae4ad87bf3a82a31f162478db7786129c70f9e18597dd695ce7b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page