Skip to main content

Context7 MCP 服务器 - Python 实现,提供最新的库文档和代码示例

Project description

Context7 MCP 服务器 - Python 版本

这是 Context7 MCP 服务器的 Python 实现,提供最新的库文档和代码示例。

功能特点

  • 🔍 库搜索: 通过 resolve-library-id 工具搜索并获取 Context7 兼容的库 ID
  • 📚 文档获取: 通过 get-library-docs 工具获取特定库的最新文档
  • 🌐 代理支持: 支持通过环境变量配置 HTTP/HTTPS 代理
  • FastMCP: 使用 FastMCP 框架,性能优异
  • 📦 单文件实现: 所有功能集成在一个文件中,部署简单

安装依赖

pip install -r requirements.txt

或手动安装:

pip install httpx pydantic mcp

使用方法

基本运行(stdio 传输)

python main.py

SSE 传输模式

python main.py --transport sse --host 0.0.0.0 --port 8088

配置代理

通过环境变量设置代理,服务器会自动检测和使用代理配置:

# Windows
set HTTP_PROXY=http://100.108.35.118:12080
set HTTPS_PROXY=http://100.108.35.118:12080
python main.py

# Linux/Mac
export HTTP_PROXY=http://100.108.35.118:12080
export HTTPS_PROXY=http://100.108.35.118:12080
python main.py

MCP 工具

resolve-library-id

搜索库并获取 Context7 兼容的库 ID。

参数:

  • library_name: 要搜索的库名称

示例:

resolve-library-id("react")

get-library-docs

获取特定库的文档。

参数:

  • context7_compatible_library_id: Context7 兼容的库 ID
  • topic: 可选,文档主题
  • tokens: 可选,最大令牌数量(默认 10000)

示例:

get-library-docs("/facebook/react", topic="hooks", tokens=15000)

集成到 MCP 客户端

Cursor 配置

~/.cursor/mcp.json 中添加:

{
  "mcpServers": {
    "context7-python": {
      "command": "python",
      "args": ["/path/to/python-mcp/main.py"],
      "env": {
        "HTTP_PROXY": "http://100.108.35.118:12080",
        "HTTPS_PROXY": "http://100.108.35.118:12080"
      }
    }
  }
}

Claude Desktop 配置

在配置文件中添加:

{
  "mcpServers": {
    "context7-python": {
      "command": "python",
      "args": ["/path/to/python-mcp/main.py"]
    }
  }
}

错误处理

  • 自动处理速率限制(429 错误)
  • 网络错误重试
  • 代理连接失败处理
  • 详细的错误日志记录

技术架构

  • HTTP 客户端: 使用 httpx 进行异步 HTTP 请求
  • 数据验证: 使用 pydantic 进行数据模型验证
  • MCP 框架: 基于 FastMCP 构建
  • 代理支持: 自动检测环境变量中的代理配置

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

context7_mcp_python-1.0.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

context7_mcp_python-1.0.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file context7_mcp_python-1.0.0.tar.gz.

File metadata

  • Download URL: context7_mcp_python-1.0.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for context7_mcp_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8b2f886ddbc3dd04e1afb277af93d4550f33206239d32b1249a23ce6c9ad3dcc
MD5 acbef64adbc40b46a748f843118bdfe0
BLAKE2b-256 3da3c6a77866309ddb0ca16ab158c600171595c4a24cfcdf0d0f76f1740dd118

See more details on using hashes here.

File details

Details for the file context7_mcp_python-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for context7_mcp_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a96de4eed40b953b9f7ff3af6fea31a9c93bd23ee64fc5ac0c485d72d342bdf9
MD5 af806024d38d953f02423094b40fcd15
BLAKE2b-256 841817be223a29720ed12117ba8cb93fbf19ada932ed41935ec4387e8b1f6853

See more details on using hashes here.

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