MCP开放客户端
](https://badge.fury.io/py/mcp-open-client)  
用于模型上下文协议(MCP)的强大Python客户端和REST API,实现与MCP服务器和AI提供商的无缝集成。
特性
- FastAPI REST API -用于管理MCP服务器的全功能API服务器
- 丰富的CLI -带有彩色输出的漂亮命令行界面
- AI提供商集成 -支持OpenAI、Anthropic和自定义提供者
- 进程管理 -MCP服务器的自动生命周期管理
- 工具发现 -MCP服务器工具的自动检测和执行
- 会话管理 -持久会话存储和检索
- 类型安全 -Pydantic v2的完整类型提示
安装
来自PyPI(推荐)
pip install mcp-open-client来源
git clone https://github.com/alejoair/mcp-open-client-v2.git
cd mcp-open-client-v2
pip install -e .快速开始
1.启动API服务器
mcp-open-client api serve --port 8001API服务器将在以下位置提供:
- API: http://localhost:8001
- Swagger文档: http://localhost:8001/docs
- ReDoc: http://localhost:8001/redoc
2.添加MCP服务器
mcp-open-client api add \
--name "filesystem" \
--command "npx" \
--args "-y" \
--args "@modelcontextprotocol/server-filesystem" \
--args "."3.启动服务器
mcp-open-client api start 4.列出可用工具
mcp-open-client api tools CLI参考
直接连接命令
在不使用API的情况下直接连接到MCP服务器:
# Connect and test
mcp-open-client connect http://localhost:3000
# List resources
mcp-open-client list-resources http://localhost:3000
# List tools
mcp-open-client list-tools http://localhost:3000
# Call custom method
mcp-open-client call http://localhost:3000 method-name --params '{"key": "value"}'API服务器管理
通过REST API管理MCP服务器:
# Start API server
mcp-open-client api serve --port 8001 --host 127.0.0.1
# Server operations
mcp-open-client api add --name --command --args
mcp-open-client api list [--format json|table]
mcp-open-client api start
mcp-open-client api stop
mcp-open-client api tools [--format json|table]人工智能提供商管理
为聊天完成配置AI提供商:
# Add provider
mcp-open-client api providers add \
--name "OpenAI" \
--type openai \
--base-url "https://api.openai.com/v1" \
--api-key "sk-..."
# List providers
mcp-open-client api providers list
# Show provider details
mcp-open-client api providers show
# Update provider
mcp-open-client api providers update
\
--name "New Name" \
--api-key "new-key"
# Test connection
mcp-open-client api providers test
# Set as default
mcp-open-client api providers set-default
# Enable/disable
mcp-open-client api providers enable
mcp-open-client api providers disable
模型配置
为提供商配置小型和主要模型:
# Set model
mcp-open-client api providers models set
small \
--name "gpt-3.5-turbo" \
--max-tokens 4096
# List models
mcp-open-client api providers models list
# Remove model
mcp-open-client api providers models remove
smallREST API端点
服务器管理
POST /servers/ # Add server
GET /servers/ # List servers
POST /servers/{id}/start # Start server
POST /servers/{id}/stop # Stop server
GET /servers/{id}/tools # Get server tools
DELETE /servers/{id} # Remove server供应商管理
POST /providers/ # Add provider
GET /providers/ # List providers
GET /providers/{id} # Get provider
PUT /providers/{id} # Update provider
PATCH /providers/{id} # Partial update
DELETE /providers/{id} # Delete provider
POST /providers/{id}/enable # Enable provider
POST /providers/{id}/disable # Disable provider
POST /providers/{id}/set-default # Set as default
POST /providers/{id}/test # Test connection会话管理
POST /conversations # Create conversation
GET /conversations # List conversations
GET /conversations/{id} # Get conversation
PUT /conversations/{id} # Update conversation
DELETE /conversations/{id} # Delete conversation
POST /conversations/{id}/chat # Send message
GET /conversations/{id}/messages # Get messages
POST /conversations/{id}/context # Add context
GET /conversations/{id}/tools # Get enabled tools聊天完成(兼容OpenAI)
POST /v1/chat/completions # Create chat completion
POST /v1/chat/stream # Stream completion
GET /v1/models # List models配置
配置文件存储在 ~/.mcp-open-client/:
mcp_servers.json-MCP服务器配置ai_providers.json-AI提供商配置conversations/-对话存储目录
发展
安装开发依赖项
pip install -e ".[dev]"代码质量工具
# Format code
black mcp_open_client/
isort mcp_open_client/
# Lint
flake8 mcp_open_client/
# Type check
mypy mcp_open_client/
# Run tests
pytest预提交钩子
# Install hooks
pre-commit install
# Run on all files
pre-commit run --all-files建筑
┌─────────────────────────────────────────────┐
│ mcp-open-client CLI │
├─────────────────────────────────────────────┤
│ │
│ Direct Commands API Commands │
│ ├─ connect ├─ api serve │
│ ├─ list-resources ├─ api add │
│ ├─ list-tools ├─ api start │
│ └─ call └─ api providers │
│ │
└─────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────┐
│ FastAPI REST API │
│ (Port 8001) │
└─────────────┬───────────┘
│
┌─────────┴─────────┐
▼ ▼
┌───────────────┐ ┌──────────────┐
│ MCP Servers │ │ AI Providers │
│ (FastMCP) │ │ (OpenAI API) │
└───────────────┘ └──────────────┘聊天完成示例
import requests
response = requests.post("http://localhost:8001/v1/chat/completions", json={
"model": "gpt-4",
"messages": [
{"role": "user", "content": "List files in current directory"}
],
"model_type": "main"
})
print(response.json())贡献
欢迎投稿!请随时提交拉取请求。
- 分叉存储库
- 创建功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推到分支(
git push origin feature/AmazingFeature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
链接
- PyPI: https://pypi.org/project/mcp-open-client/
- GitHub: https://github.com/alejoair/mcp-open-client-v2
- 问题: https://github.com/alejoair/mcp-open-client-v2/issues
