MCP客户端工具

一个强大的Python客户端和REST API,用于Model Context Protocol (MCP),实现与MCP服务器和AI提供商的无缝集成。

  • by alejoair
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 alejoair/mcp-open-client-v2

使用说明

MCP开放客户端

用于模型上下文协议(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 8001

API服务器将在以下位置提供:

  • 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 <server-id>

4.列出可用工具

mcp-open-client api tools <server-id>

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 <name> --command <cmd> --args <arg>
mcp-open-client api list [--format json|table]
mcp-open-client api start <server-id>
mcp-open-client api stop <server-id>
mcp-open-client api tools <server-id> [--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 <provider-id>

# Update provider
mcp-open-client api providers update <provider-id> \
  --name "New Name" \
  --api-key "new-key"

# Test connection
mcp-open-client api providers test <provider-id>

# Set as default
mcp-open-client api providers set-default <provider-id>

# Enable/disable
mcp-open-client api providers enable <provider-id>
mcp-open-client api providers disable <provider-id>

模型配置

为提供商配置小型和主要模型:

# Set model
mcp-open-client api providers models set <provider-id> small \
  --name "gpt-3.5-turbo" \
  --max-tokens 4096

# List models
mcp-open-client api providers models list <provider-id>

# Remove model
mcp-open-client api providers models remove <provider-id> small

REST 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())

贡献

欢迎投稿!请随时提交拉取请求。

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/AmazingFeature)
  3. 提交您的更改(git commit -m 'Add some AmazingFeature')
  4. 推到分支(git push origin feature/AmazingFeature)
  5. 打开拉取请求

许可证

此项目根据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

致谢