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.1.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.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: context7_mcp_python-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 d72a33576d857fb2db683fffb66d12d0f914798ff4cca244329cb9df4c843f86
MD5 85d352a7a111c89c374a121f3fb9dcae
BLAKE2b-256 aab69004419d6cef707c335d5b42979baa24000296ea2d7a77f084025d171713

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for context7_mcp_python-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f74c02dd5cbd01d33de86d12272b15971ea1a37d7c631842f355f35996d936c
MD5 f268455d172043daf44456f671da9846
BLAKE2b-256 dab2768312d6d77564dd733125384d946d1cab3bd3579772849feacf9678bcd0

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