Open Source MCP CLient Library
 ](https://pypi.org/project/mcp_use/) ](https://pypi.org/project/mcp_use/) ](https://pypi.org/project/mcp_use/)    ](https://github.com/pietrozullo/mcp-use/stargazers)
🌐 MCP Use是一种开源方式,可以将任何LLM连接到MCP工具,并构建具有工具访问权限的自定义代理,而无需使用闭源或应用程序客户端。
💡 让开发人员轻松地将任何LLM连接到web浏览、文件操作等工具。
特性
✨ 主要特点
| 特性 | 描述 |
|---|---|
| 🔄 易用性 | 创建您的第一个支持MCP的代理,您只需要6行代码 |
| 🤖 LLM灵活性 | 适用于任何支持工具调用的语言链支持的LLM(OpenAI、Anthropic、Groq、LLama等) |
| 🌐 HTTP支持 | 直接连接到在特定HTTP端口上运行的MCP服务器 |
| 🧩 多服务器支持 | 在单个代理中同时使用多个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与各种LLM提供商合作。您需要为您选择的LLM安装相应的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一起使用。确保您选择的模型支持函数调用或工具使用。
启动您的代理:
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服务器,允许您在单个代理中组合来自不同服务器的工具。这对于需要多种功能的复杂任务非常有用,例如网页浏览与文件操作或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使用允许您限制代理可用的工具,从而提供更好的安全性和对代理功能的控制:
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] Multiple Servers at once
[x] Test remote connectors (http, ws)
[ ] ...
贡献
我们热爱贡献!对于bug或功能请求,请随时打开问题。
需求
- 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}
}许可证
麻省理工学院
