MCP SSE Client Python
用于远程 MCP 服务器的简单 MCP 客户端 🌐
zanetworker
README
MCP SSE Client Python
一个使用服务器发送事件 (SSE) 与模型上下文协议 (MCP) 端点交互的 Python 客户端。
快速开始
几分钟内即可启动并运行:
# 克隆仓库
git clone https://github.com/zanetworker/mcp-sse-client-python.git
cd mcp-sse-client-python
# 安装包
pip install -e .
# 尝试交互式 Streamlit 应用
cd mcp-streamlit-app
pip install -r requirements.txt
streamlit run app.py
什么是 MCP SSE Client?
此客户端提供了一个简单的接口,用于:
- 通过服务器发送事件连接到 MCP 端点
- 发现和调用带有参数的工具
- 与 LLM(OpenAI、Anthropic、Ollama)集成,实现 AI 驱动的工具选择
- 通过 Streamlit UI 交互式测试工具
核心功能
1. 简单 MCP 客户端
轻松连接到任何 MCP 端点并与可用工具交互:
import asyncio
from mcp_sse_client import MCPClient
async def main():
# 连接到 MCP 端点
client = MCPClient("http://localhost:8000/sse")
# 列出可用工具
tools = await client.list_tools()
print(f"Found {len(tools)} tools")
# 调用计算器工具
result = await client.invoke_tool(
"calculator",
{"x": 10, "y": 5, "operation": "add"}
)
print(f"Result: {result.content}") # 输出: Result: 15
asyncio.run(main())
2. LLM 驱动的工具选择
让 AI 根据自然语言查询选择合适的工具:
import os
from mcp_sse_client 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(
"Convert this PDF to text: https://example.com/document.pdf"
)
# LLM 自动选择合适的工具和参数
if result["tool_call"]:
print(f"Tool: {result['tool_call']['name']}")
print(f"Result: {result['tool_result'].content}")
3. 命令行界面
该软件包包含一个强大的 CLI 工具,用于交互式测试和分析:
# 运行 CLI 工具
python -m mcp_sse_client.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]
示例输出:
Starting MCP-LLM Integration Example...
Connecting to MCP server at: http://localhost:8000/sse
Using OpenAI LLM bridge with model: gpt-4o
Fetching tools from server...
=== Available Tools Summary ===
3 tools available:
1. calculator: Perform basic arithmetic operations
Required parameters:
- x (number): First operand
- y (number): Second operand
- operation (string): Operation to perform (add, subtract, multiply, divide)
2. weather: Get current weather for a location
Required parameters:
- location (string): City or location name
3. convert_document: Convert a document to text
Required parameters:
- source (string): URL or file path to the document
Optional parameters:
- enable_ocr (boolean): Whether to use OCR for scanned documents
Entering interactive mode. Type 'quit' to exit.
Enter your query: What's the weather in Berlin?
=== User Query ===
What's the weather in Berlin?
Processing query...
=== LLM Reasoning ===
I need to check the weather in Berlin. Looking at the available tools, there's a "weather" tool that can get the current weather for a location. I'll use this tool with "Berlin" as the location parameter.
=== Tool Selection Decision ===
Selected: weather
Description: Get current weather for a location
Parameters provided:
- location (string, required): Berlin
Description: City or location name
Query to Tool Mapping:
Query: "What's the weather in Berlin?"
Tool: weather
Key parameters: location
=== Tool Execution Result ===
Success: True
Content: {"temperature": 18.5, "conditions": "Partly cloudy", "humidity": 65, "wind_speed": 12}
4. 交互式测试 UI
包含的 Streamlit 应用程序提供了一个用户友好的界面,用于:
- 连接到任何 MCP 端点
- 在 OpenAI、Anthropic 或 Ollama LLM 之间进行选择
- 查看可用工具及其参数
- 通过聊天界面中的自然语言测试工具
- 可视化工具选择的推理和结果
要运行 Streamlit 应用程序:
cd mcp-streamlit-app
streamlit run app.py
安装
从源码安装
git clone https://github.com/zanetworker/mcp-sse-client-python.git
cd mcp-sse-client-python
pip install -e .
使用 pip (发布后)
pip install mcp-sse-client
支持的 LLM 提供商
该客户端支持多个 LLM 提供商,用于 AI 驱动的工具选择:
- OpenAI: GPT-4o, GPT-4, GPT-3.5-Turbo
- Anthropic: Claude 3 Opus, Claude 3 Sonnet, Claude 3 Haiku
- Ollama: Llama 3, Mistral 和其他本地托管模型
API 参考
MCPClient
client = MCPClient(endpoint)
endpoint
: MCP 端点 URL(必须是 http 或 https)
方法
async list_tools() -> List[ToolDef]
列出 MCP 端点提供的可用工具。
async invoke_tool(tool_name: str, kwargs: Dict[str, Any]) -> ToolInvocationResult
使用参数调用特定工具。
LLM 桥
OpenAIBridge
bridge = OpenAIBridge(mcp_client, api_key, model="gpt-4o")
AnthropicBridge
bridge = AnthropicBridge(mcp_client, api_key, model="claude-3-opus-20240229")
OllamaBridge
bridge = OllamaBridge(mcp_client, model="llama3", host=None)
通用桥方法
async process_query(query: str) -> Dict[str, Any]
通过 LLM 处理用户查询并执行任何工具调用。
要求
- Python 3.7+
requests
sseclient-py
pydantic
openai
(用于 OpenAI 集成)anthropic
(用于 Anthropic 集成)ollama
(用于 Ollama 集成)streamlit
(用于交互式测试应用程序)
开发
有关开发设置、贡献指南和可用 make 命令的信息,请参阅 DEVELOPMENT.md。
许可证
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。