关于 Open Source MCP CLient Library
MCP-Use 是面向 Python 开发者的开源客户端库,用于将支持工具调用的 LangChain LLM 连接到 MCP 工具并构建 Agent。它支持同时连接多个 MCP 服务,并可限制 Agent 可用的工具。
主要功能
- 01连接支持工具调用的 LangChain LLM
- 02同时使用多个 MCP 服务
- 03支持 HTTP 连接
- 04从配置文件创建会话
- 05限制 Agent 可用工具
适用场景
- 01构建可使用浏览器工具的 Agent
- 02组合多个 MCP 服务处理复杂任务
- 03通过 HTTP 连接 Web MCP 服务
- 04限制文件系统或网络工具访问
MCP 相关
MCP-Use 作为客户端读取 MCP 服务配置并连接服务,将其工具提供给 Agent 使用。它支持多个服务以及 HTTP 连接。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
开源 MCP 客户端库
🌐 MCP-Use 是一种开源方式,可将任意大模型连接到 MCP 工具,并构建具备工具访问权限的自定义 Agent,无需使用闭源客户端或应用客户端。
💡 让开发者轻松将任意大模型连接到网页浏览、文件操作等工具。
功能
✨ 主要功能
| 功能 | 描述 |
|---|---|
| 🔄 易于使用 | 只需 6 行代码,即可创建你的第一个支持 MCP 的 Agent |
| 🤖 大模型灵活性 | 兼容 LangChain 支持的、具备工具调用能力的任意大模型(OpenAI、Anthropic、Groq、LLama 等) |
| 🌐 HTTP 支持 | 直接连接到运行在指定 HTTP 端口上的 MCP 服务 |
| 🧩 多服务支持 | 在单个 Agent 中同时使用多个 MCP 服务 |
| 🛡️ 工具限制 | 限制可能有危险的工具,例如文件系统或网络访问 |
快速开始
使用 pip:
pip install mcp-use
或从源代码安装:
git clone https://github.com/pietrozullo/mcp-use.git
cd mcp-use
pip install -e .
安装 LangChain 提供程序
mcp_use 可通过 LangChain 与各种大模型提供程序配合使用。你需要为所选的大模型安装相应的 LangChain 提供程序包。例如:
# For OpenAI
pip install langchain-openai
# For Anthropic
pip install langchain-anthropic
# For other providers, check the [LangChain chat models documentation](https://python.langchain.com/docs/integrations/chat/)
并将所需提供程序的 API 密钥添加到你的 .env 文件中。
OPENAI_API_KEY=
ANTHROPIC_API_KEY=
重要提示:只有具备工具调用能力的模型才能与 mcp_use 配合使用。请确保所选模型支持函数调用或工具使用。
启动你的 Agent:
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
# Load environment variables
load_dotenv()
# Create configuration dictionary
config = {
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
}
# Create MCPClient from configuration dictionary
client = MCPClient.from_dict(config)
# Create LLM
llm = ChatOpenAI(model="gpt-4o")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# Run the query
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 environment variables
load_dotenv()
# Create MCPClient from config file
client = MCPClient.from_config_file(
os.path.join(os.path.dirname(__file__), "browser_mcp.json")
)
# Create LLM
llm = ChatOpenAI(model="gpt-4o")
# Alternative models:
# llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# llm = ChatGroq(model="llama3-8b-8192")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# Run the query
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 environment variables
load_dotenv()
# Create MCPClient with Airbnb configuration
client = MCPClient.from_config_file(
os.path.join(os.path.dirname(__file__), "airbnb_mcp.json")
)
# Create LLM - you can choose between different models
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
try:
# Run a query to search for accommodations
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:
# Ensure we clean up resources properly
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 environment variables
load_dotenv()
# Create MCPClient with Blender MCP configuration
config = {"mcpServers": {"blender": {"command": "uvx", "args": ["blender-mcp"]}}}
client = MCPClient.from_dict(config)
# Create LLM
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
try:
# Run the query
result = await agent.run(
"Create an inflatable cube with soft material and a plane as ground.",
max_steps=30,
)
print(f"\nResult: {result}")
finally:
# Ensure we clean up resources properly
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():
# Create an MCP session from a config file
session = create_session_from_config("mcp-config.json")
# Initialize the session
await session.initialize()
# Use the session...
# Disconnect when done
await session.disconnect()
if __name__ == "__main__":
asyncio.run(main())
HTTP 连接示例
MCP-Use 现在支持 HTTP 连接,因此你可以连接到运行在指定 HTTP 端口上的 MCP 服务。此功能特别适用于与基于 Web 的 MCP 服务集成。
以下是 HTTP 连接功能的使用示例:
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
"""Run the example using a configuration file."""
# Load environment variables
load_dotenv()
config = {
"mcpServers": {
"http": {
"url": "http://localhost:8931/sse"
}
}
}
# Create MCPClient from config file
client = MCPClient.from_dict(config)
# Create LLM
llm = ChatOpenAI(model="gpt-4o")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# Run the query
result = await agent.run(
"Find the best restaurant in San Francisco USING GOOGLE SEARCH",
max_steps=30,
)
print(f"\nResult: {result}")
if __name__ == "__main__":
# Run the appropriate example
asyncio.run(main())
此示例演示如何连接到运行在指定 HTTP 端口上的 MCP 服务。请确保在运行此示例前启动 MCP 服务。
多服务支持
MCP-Use 支持同时使用多个 MCP 服务,让你可以在单个 Agent 中组合来自不同服务的工具。这适用于需要多种能力的复杂任务,例如将网页浏览与文件操作或 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():
# Create client with multiple servers
client = MCPClient.from_config_file("multi_server_config.json")
# Create agent with the client
agent = MCPAgent(
llm=ChatAnthropic(model="claude-3-5-sonnet-20240620"),
client=client
)
try:
# Run a query that uses tools from multiple servers
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:
# Clean up all sessions
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
工具访问控制
MCP-Use 支持限制 Agent 可用的工具,从而更好地保障安全并控制 Agent 的能力:
import asyncio
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI
async def main():
# Create client
client = MCPClient.from_config_file("config.json")
# Create agent with restricted tools
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-4"),
client=client,
disallowed_tools=["file_system", "network"] # Restrict potentially dangerous tools
)
# Run a query with restricted tool access
result = await agent.run(
"Find the best restaurant in San Francisco"
)
print(result)
# Clean up
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
路线图
- [x] 同时连接多个服务
- [x] 测试远程连接器(http、ws)
- [ ] ...
贡献
我们欢迎贡献!如有 Bug 或功能需求,欢迎提交 issue。
要求
- 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
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。