Skip to main content

MCP客户端库,用于在对话中调用MCP工具

Project description

Chat-MCP

一个用于在对话中调用MCP(Model Context Protocol)工具的Python客户端库

Chat-MCP 提供了一个简洁而强大的接口,让您能够轻松地将MCP服务器集成到AI agent项目中。通过两个简单的api,并配置好.env文件,就可以在开发agent和chat助手时高效的调用mcp服务。作为agentic项目的底层脚手架,分享大家使用。

核心特性

  • 简洁的API设计: 二个主要接口覆盖所有使用场景,提高agent应用开发效率
  • 优化提示词: 提高tools调用的成功率,持续优化中
  • 异步架构: 完全基于asyncio的高性能处理
  • 智能工具管理: 自动工具收集、过滤和并行执行
  • 多模型支持: 通过LiteLLM支持多种LLM提供商,支持本地模型配置

关键技术栈

  • MCP Python SDK: Model Context Protocol的官方Python实现
  • LiteLLM: 统一的LLM API接口库,支持100+模型
  • Pydantic: 数据验证和设置管理框架

项目结构

chat-mcp/
├── docs/                 # 详细的开发文档和规范
├── src/chat_mcp/         # 主要源代码包
│   ├── tests/            # 测试套件
│   ├── __init__.py       # 公共API导出
│   ├── easy_chat.py      # 简化API接口
│   ├── ai_provider.py    # LLM提供商集成
│   ├── mcp_service.py    # MCP服务器管理
│   ├── mcp_chat_handler.py # 聊天处理逻辑
│   ├── ipc_handler.py    # 进程间通信处理
│   └── mcp_types.py      # 数据类型定义
├── temp_arxiv_storage/   # ArXiv演示数据存储
├── pyproject.toml        # uv项目配置
├── uv.lock              # 依赖锁定文件
└── main.py              # 演示入口

核心API

MCPChatTool - 主要接口

from chat_mcp import MCPChatTool

# 创建聊天工具实例
chat_tool = MCPChatTool()

# 启动MCP服务器,这里使用arxiv-mcp-server作为演示样例,可以添加多个不同的mcp server,只需要多次调用
server = await chat_tool.start_mcp_server(
    server_id="arxiv",
    name="ArXiv Research",
    command="uv",
    args=["tool", "run", "arxiv-mcp-server"]
)

# 进行对话
result = await chat_tool.chat_with_mcp(
    user_message="搜索关于机器学习的最新论文",
    enabled_server_ids=["arxiv"] # 这里可以不设置enabled_server_ids,默认所有server都会被添加
)

安装

使用 uv (推荐):

uv add chat-mcp

或使用 pip:

pip install chat-mcp

快速开始

  1. 配置环境变量 (创建 .env 文件):

LLM配置:使用litellm规范,可以自行配置,支持大部分模型包括自定义大模型

OPENAI_API_KEY=your_openai_api_key
OPENAI_API_BASE=https://api.openai.com/v1
LLM_MODEL=gpt-3.5-turbo

如果是vllm本地模型可以如下配置

MODEL_NAME="openai//{VLLM_MODEL_NAME(PATH)}"
OPENAI_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
OPENAI_API_BASE="http://xxx.xxx.xxx.xxx:xxxx/v1"

其他模型都已自行尝试配置,具体参考litellm支持的模型

  1. 基础使用:
import asyncio
from chat_mcp import MCPChatTool

async def main():
    # 创建聊天工具
    chat_tool = MCPChatTool()
    
    # 添加ArXiv服务器
    await chat_tool.start_mcp_server(
        server_id="arxiv",
        name="ArXiv Research", 
        command="uv",
        args=["tool", "run", "arxiv-mcp-server"]
    )
    
    # 开始对话
    result = await chat_tool.chat_with_mcp(
        user_message="帮我搜索最新的深度学习论文",
        system_prompt="你是一个研究助手,专门帮助用户搜索和分析学术论文。"
    )
    
    print(f"AI回答: {result['content']}")
    print(f"工具调用: {len(result['tool_calls'])} 次")

# 运行
asyncio.run(main())

许可证

MIT License - 详见 LICENSE 文件


Chat-MCP: 让AI对话更智能,让工具调用更简单

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chat_mcp-0.1.1.tar.gz (35.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chat_mcp-0.1.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file chat_mcp-0.1.1.tar.gz.

File metadata

  • Download URL: chat_mcp-0.1.1.tar.gz
  • Upload date:
  • Size: 35.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for chat_mcp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9c28e8d8b0eb43411aca4c41de281b6fde80bfe394e738bd5b006de2fd890c20
MD5 fad57d8ea92c86e560374e77148245bb
BLAKE2b-256 fd365905fc1e888b9c8640e594de745928ab681c13c8cb1e20149da9610240d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for chat_mcp-0.1.1.tar.gz:

Publisher: publish.yml on zjmwqx/chat-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chat_mcp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chat_mcp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for chat_mcp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cf595a38003b8644d211a553797350d5574d18de0d04c0f7ec9649c2b971cc0c
MD5 51aed64e7014eb252b08e48f0ec5d8ed
BLAKE2b-256 b9e6b313628559f619e56e2a29dc68b1e409bf435c58f138745ab9ec1bf37b2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chat_mcp-0.1.1-py3-none-any.whl:

Publisher: publish.yml on zjmwqx/chat-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page