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 server.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 server.py

# Linux/Mac
export HTTP_PROXY=http://100.108.35.118:12080
export HTTPS_PROXY=http://100.108.35.118:12080
python server.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.2.tar.gz (8.6 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.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for context7_mcp_python-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9f1c39b87f9574490a627e21ce7a7846abc2546d56b73a5d2c7f4070d1fbc61e
MD5 7610510ec90b915fedba370df1df6903
BLAKE2b-256 1f9aab12df858b114d65be82d233339e3fd15dbf9310d42e43f3d018a07ce917

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for context7_mcp_python-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8569f3cee9ca66a83fe5ce09b464a9034fc6a743a31ae2ba3e4f8ae0aff00f57
MD5 b1e76abf60353e65600bc8159f9b217a
BLAKE2b-256 e0ce3415b607ca6ae17b27d8e39cf948ed32cf97134ed9fb7693aa5802f346e2

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