Mcp Use
pietrozullo
README
<h1 align="center">开源 MCP 客户端库</h1>
🌐 MCP-Use 是一种开源方式,可以将任何 LLM 连接到 MCP 工具,并构建具有工具访问权限的自定义代理,而无需使用闭源或应用程序客户端。
💡 让开发者轻松地将任何 LLM 连接到诸如网页浏览、文件操作等工具。
快速开始
使用 pip 安装:
pip install mcp-use
或者从源码安装:
git clone https://github.com/pietrozullo/mcp-use.git
cd mcp-use
pip install -e .
安装 LangChain 提供程序
mcp_use 通过 LangChain 与各种 LLM 提供程序一起工作。您需要为所选的 LLM 安装相应的 LangChain 提供程序包。例如:
# 对于 OpenAI
pip install langchain-openai
# 对于 Anthropic
pip install langchain-anthropic
# 对于其他提供程序,请查看 [LangChain 聊天模型文档](https://python.langchain.com/docs/integrations/chat/)
并将您要使用的提供程序的 API 密钥添加到您的 .env 文件中。
OPENAI_API_KEY=
ANTHROPIC_API_KEY=
重要提示:只有具有工具调用功能的模型才能与 mcp_use 一起使用。请确保您选择的模型支持函数调用或工具使用。
启动您的代理:
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
# 加载环境变量
load_dotenv()
# 创建配置字典
config = {
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
}
# 从配置字典创建 MCPClient
client = MCPClient.from_dict(config)
# 创建 LLM
llm = ChatOpenAI(model="gpt-4o")
# 使用客户端创建代理
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# 运行查询
result = await agent.run(
"Find the best restaurant in San Francisco",
)
print(f"\nResult: {result}")
if __name__ == "__main__":
asyncio.run(main())
您还可以从配置文件中添加服务器配置,如下所示:
client = MCPClient.from_config_file(
os.path.join("browser_mcp.json")
)
示例配置文件 (browser_mcp.json):
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
}
有关其他设置、模型等,请查看文档。
示例用例
使用 Playwright 进行网页浏览
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
# 加载环境变量
load_dotenv()
# 从配置文件创建 MCPClient
client = MCPClient.from_config_file(
os.path.join(os.path.dirname(__file__), "browser_mcp.json")
)
# 创建 LLM
llm = ChatOpenAI(model="gpt-4o")
# 备选模型:
# llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# llm = ChatGroq(model="llama3-8b-8192")
# 使用客户端创建代理
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# 运行查询
result = await agent.run(
"Find the best restaurant in San Francisco USING GOOGLE SEARCH",
max_steps=30,
)
print(f"\nResult: {result}")
if __name__ == "__main__":
asyncio.run(main())
Airbnb 搜索
import asyncio
import os
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from mcp_use import MCPAgent, MCPClient
async def run_airbnb_example():
# 加载环境变量
load_dotenv()
# 使用 Airbnb 配置创建 MCPClient
client = MCPClient.from_config_file(
os.path.join(os.path.dirname(__file__), "airbnb_mcp.json")
)
# 创建 LLM - 您可以在不同的模型之间进行选择
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# 使用客户端创建代理
agent = MCPAgent(llm=llm, client=client, max_steps=30)
try:
# 运行查询以搜索住宿
result = await agent.run(
"Find me a nice place to stay in Barcelona for 2 adults "
"for a week in August. I prefer places with a pool and "
"good reviews. Show me the top 3 options.",
max_steps=30,
)
print(f"\nResult: {result}")
finally:
# 确保我们正确清理资源
if client.sessions:
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(run_airbnb_example())
示例配置文件 (airbnb_mcp.json):
{
"mcpServers": {
"airbnb": {
"command": "npx",
"args": ["-y", "@openbnb/mcp-server-airbnb"]
}
}
}
Blender 3D 创建
import asyncio
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from mcp_use import MCPAgent, MCPClient
async def run_blender_example():
# 加载环境变量
load_dotenv()
# 使用 Blender MCP 配置创建 MCPClient
config = {"mcpServers": {"blender": {"command": "uvx", "args": ["blender-mcp"]}}}
client = MCPClient.from_dict(config)
# 创建 LLM
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# 使用客户端创建代理
agent = MCPAgent(llm=llm, client=client, max_steps=30)
try:
# 运行查询
result = await agent.run(
"Create an inflatable cube with soft material and a plane as ground.",
max_steps=30,
)
print(f"\nResult: {result}")
finally:
# 确保我们正确清理资源
if client.sessions:
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(run_blender_example())
配置文件支持
MCP-Use 支持从配置文件进行初始化,从而可以轻松管理和切换不同的 MCP 服务器设置:
import asyncio
from mcp_use import create_session_from_config
async def main():
# 从配置文件创建 MCP 会话
session = create_session_from_config("mcp-config.json")
# 初始化会话
await session.initialize()
# 使用会话...
# 完成后断开连接
await session.disconnect()
if __name__ == "__main__":
asyncio.run(main())
多服务器支持
MCP-Use 支持同时使用多个 MCP 服务器,允许您在单个代理中组合来自不同服务器的工具。 这对于需要多种功能的复杂任务非常有用,例如将网页浏览与文件操作或 3D 建模相结合。
配置
您可以在配置文件中配置多个服务器:
{
"mcpServers": {
"airbnb": {
"command": "npx",
"args": ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"]
},
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
}
用法
MCPClient 类提供了几种用于管理多个服务器的方法:
import asyncio
from mcp_use import MCPClient, MCPAgent
from langchain_anthropic import ChatAnthropic
async def main():
# 创建具有多个服务器的客户端
client = MCPClient.from_config_file("multi_server_config.json")
# 使用客户端创建代理
agent = MCPAgent(
llm=ChatAnthropic(model="claude-3-5-sonnet-20240620"),
client=client
)
try:
# 运行使用来自多个服务器的工具的查询
result = await agent.run(
"Search for a nice place to stay in Barcelona on Airbnb, "
"then use Google to find nearby restaurants and attractions."
)
print(result)
finally:
# 清理所有会话
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
工具访问控制
MCP-Use 允许您限制代理可用的工具,从而提供更好的安全性和对代理功能的控制:
import asyncio
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI
async def main():
# 创建客户端
client = MCPClient.from_config_file("config.json")
# 创建具有受限工具的代理
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-4"),
client=client,
disallowed_tools=["file_system", "network"] # 限制潜在的危险工具
)
# 运行具有受限工具访问权限的查询
result = await agent.run(
"Find the best restaurant in San Francisco"
)
print(result)
# 清理
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
路线图
<ul> <li>[x] 同时使用多个服务器</li> <li>[x] 测试远程连接器 (http, ws)</li> <li>[ ] ... </li> </ul>
贡献
我们欢迎贡献! 随时打开问题以报告错误或提出功能请求。
要求
- Python 3.11+
- MCP 实现(如 Playwright MCP)
- LangChain 和适当的模型库(OpenAI、Anthropic 等)
引用
如果您在您的研究或项目中使用 MCP-Use,请引用:
@software{mcp_use2025,
author = {Zullo, Pietro},
title = {MCP-Use: MCP Library for Python},
year = {2025},
publisher = {GitHub},
url = {https://github.com/pietrozullo/mcp-use}
}
许可证
MIT
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。
Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。
Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。