一个全面的Python工具包,用于与远程模型上下文协议(MCP)端点进行交互。当前支持服务器发送事件(SSE),并计划支持可流式传输的HTTP协议。
MCP Playground 特别设计用于远程MCP客户端能力,提供强大的工具,通过网络协议连接到并与MCP服务器进行交互:
几分钟内启动运行:
# 克隆仓库
git clone https://github.com/zanetworker/mcp-playground.git
cd mcp-playground
# 安装包
pip install -e .
# 尝试交互式的Streamlit应用
cd mcp-streamlit-app
pip install -r requirements.txt
streamlit run app.py

🚨 重要提示: 连接到MCP服务器时,请始终使用以
/sse结尾的URL。 示例:http://localhost:8000/sse(而不是http://localhost:8000)
为了方便起见,您可以通过环境变量设置API密钥和OpenRouter配置:
# 对于LLM提供商是必需的
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"
export OPENROUTER_API_KEY="your-openrouter-key"
# 可选的OpenRouter配置,以便更好地排名
export OPENROUTER_SITE_URL="https://your-site.com"
export OPENROUTER_SITE_NAME="Your App Name"
Streamlit界面突出显示了对/sse URL的要求,并提供了有用的提示和验证。
MCP Playground集成了多个LLM提供商,用于智能工具选择:
轻松连接到任何远程MCP端点并与可用工具进行交互:
import asyncio
from mcp_playground import MCPClient
async def main():
# 使用可选超时和重试设置连接到远程MCP端点
# 重要提示:URL必须以/sse结尾,用于服务器发送事件
client = MCPClient(
"http://localhost:8000/sse", # 注意/sse后缀!
timeout=30.0, # 连接超时时间(秒)
max_retries=3 # 最大重试次数
)
# 列出可用工具
tools = await client.list_tools()
print(f"找到 {len(tools)} 个工具")
# 调用计算器工具
result = await client.invoke_tool(
"calculator",
{"x": 10, "y": 5, "operation": "add"}
)
print(f"结果: {result.content}") # 输出: 结果: 15
print(f"成功: {result.error_code == 0}")
asyncio.run(main())
让AI根据自然语言查询选择正确的工具:
import os
from mcp_playground import MCPClient, OpenAIBridge
# 连接到MCP端点并创建LLM桥接器
client = MCPClient("http://localhost:8000/sse")
bridge = OpenAIBridge(
client,
api_key=os.environ.get("OPENAI_API_KEY"),
model="gpt-4o"
)
# 处理自然语言查询
result = await bridge.process_query(
"将此PDF转换为文本: https://example.com/document.pdf"
)
# LLM自动选择适当的工具和参数
if result["tool_call"]:
print(f"工具: {result['tool_call']['name']}")
print(f"结果: {result['tool_result'].content}")
该包包括一个强大的CLI工具,用于交互式测试和分析:
# 运行CLI工具(注意端点URL中的/sse后缀)
python -m mcp_playground.examples.llm_example --provider openai --endpoint http://localhost:8000/sse
配置选项:
usage: llm_example.py [-h] [--provider {openai,anthropic,ollama}]
[--openai-model {gpt-4o,gpt-4-turbo,gpt-4,gpt-3.5-turbo}]
[--anthropic-model {claude-3-opus-20240229,claude-3-sonnet-20240229,claude-3-haiku-20240307}]
[--ollama-model OLLAMA_MODEL] [--ollama-host OLLAMA_HOST]
[--endpoint ENDPOINT] [--openai-key OPENAI_KEY]
[--anthropic-key ANTHROPIC_KEY]
包含的Streamlit应用提供了一个全面的测试界面:
关键特性:
要运行Streamlit应用:
cd mcp-streamlit-app
pip install -r requirements.txt
streamlit run app.py
git clone https://github.com/zanetworker/mcp-playground.git
cd mcp-playground
pip install -e .
pip install mcp-playground
client = MCPClient(endpoint, timeout=30.0, max_retries=3)
参数:
endpoint:MCP端点URL(必须是http或https,并且以/sse结尾)timeout:连接超时时间(秒,默认值:30.0)max_retries:最大重试次数(默认值:3)⚠️ URL要求:
/sse结尾,用于服务器发送事件通信http://localhost:8000/ssehttps://my-mcp-server.com/ssehttp://192.168.1.100:3000/sseasync list_tools() -> List[ToolDef]列出来自MCP端点的可用工具。
async invoke_tool(tool_name: str, kwargs: Dict[str, Any]) -> ToolInvocationResult调用特定工具及其参数。
async check_connection() -> bool检查MCP端点是否可达。
get_endpoint_info() -> Dict[str, Any]获取有关已配置端点的信息。
客户端包括具有特定异常类型的强大错误处理:
from mcp_playground import MCPClient, MCPConnectionError, MCPTimeoutError
try:
client = MCPClient("http://localhost:8000/sse")
tools = await client.list_tools()
except MCPConnectionError as e:
print(f"连接失败: {e}")
except MCPTimeoutError as e:
print(f"操作超时: {e}")
bridge = OpenAIBridge(mcp_client, api_key, model="gpt-4o")
bridge = AnthropicBridge(mcp_client, api_key, model="claude-3-opus-20240229")
bridge = OllamaBridge(mcp_client, model="llama3", host=None)
bridge = OpenRouterBridge(mcp_client, api_key, model="anthropic/claude-3-opus")
客户端包括带有指数退避的自动重试逻辑:
# 配置自定义重试行为
client = MCPClient(
"http://localhost:8000/sse",
timeout=60.0, # 更长的超时时间,适用于慢速服务器
max_retries=5 # 更多的重试次数
)
# 客户端自动重试失败的操作
# 指数退避:1s, 2s, 4s, 8s, 16s
# 在操作之前检查端点是否可达
if await client.check_connection():
tools = await client.list_tools()
else:
print("服务器不可达")
# 获取详细的端点信息
info = client.get_endpoint_info()
print(f"连接到: {info['hostname']}:{info['port']}")
mcp>=0.1.0(模型上下文协议库)pydantic>=2.0.0(数据验证)openai>=1.70.0(用于OpenAI集成)anthropic>=0.15.0(用于Anthropic集成)ollama>=0.1.7(用于Ollama集成)streamlit(用于交互式测试应用)“任务组中的未处理错误”错误: 这通常发生在asyncio兼容性问题上。Streamlit应用会自动处理这种情况,但对于自定义实现,请确保正确管理异步上下文。
连接超时:
MCPClient(endpoint, timeout=60.0)/sse结尾导入错误:
pip install -e .LLM集成问题:
关于开发设置、贡献指南和可用的make命令,请参阅DEVELOPMENT.md。
本项目采用MIT许可 - 详情请参阅LICENSE文件。