M

MCP上下文模型文档

一种模型上下文协议服务器,它从库中直接获取最新、特定版本的文档和代码示例并将其引入LLM提示中,帮助开发人员在无需过时或虚构信息的情况下获得准确的答案。

developer-toolssearch

6.0w 查看 · 2026-07-07 更新

简介

一种模型上下文协议服务器,它从库中直接获取最新、特定版本的文档和代码示例并将其引入LLM提示中,帮助开发人员在无需过时或虚构信息的情况下获得准确的答案。

简介

一种模型上下文协议服务器,它从库中直接获取最新、特定版本的文档和代码示例并将其引入LLM提示中,帮助开发人员在无需过时或虚构信息的情况下获得准确的答案。

Context7 MCP - 任何提示的最新文档

Website smithery badge

❌ 没有 Context7

LLM 依赖于关于您使用的库的过时或通用信息。您将得到:

  • ❌ 代码示例已过时,基于一年前的训练数据
  • ❌ 幻想出来的 API 甚至不存在
  • ❌ 针对旧版本包的通用答案

✅ 使用 Context7

Context7 MCP 直接从源代码中提取最新的、特定版本的文档和代码示例,并直接放入您的提示中。

在 Cursor 中向您的提示添加 use context7

Create a basic Next.js project with app router. use context7
Create a script to delete the rows where the city is "" given PostgreSQL credentials. use context7

Context7 将最新的代码示例和文档直接提取到 LLM 的上下文中。

  • 1️⃣ 自然地编写您的提示
  • 2️⃣ 告诉 LLM use context7
  • 3️⃣ 获取可用的代码答案

无需切换标签页,没有不存在的幻想 API,也没有过时的代码生成。

🛠️ 开始使用

要求

  • Node.js >= v18.0.0
  • Cursor, Windsurf, Claude Desktop 或其他 MCP 客户端

通过 Smithery 安装

要通过 Smithery 自动为 Claude Desktop 安装 Context7 MCP 服务器:

npx -y @smithery/cli install @upstash/context7-mcp --client claude

在 Cursor 中安装

转到:设置 -> Cursor 设置 -> MCP -> 添加新的全局 MCP 服务器

将以下配置粘贴到您的 Cursor ~/.cursor/mcp.json 文件中是推荐的方法。更多信息请参阅 Cursor MCP 文档

{ "mcpServers": { "context7": { "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }
替代方法:使用 Bun
{ "mcpServers": { "context7": { "command": "bunx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }
替代方法:使用 Deno
{ "mcpServers": { "context7": { "command": "deno", "args": ["run", "--allow-net", "npm:@upstash/context7-mcp"] } } }

在 Windsurf 中安装

将此内容添加到您的 Windsurf MCP 配置文件中。更多信息请参阅 Windsurf MCP 文档

{ "mcpServers": { "context7": { "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }

在 VSCode 中安装

将此内容添加到您的 VSCode MCP 配置文件中。更多信息请参阅 VSCode MCP 文档

{ "servers": { "Context7": { "type": "stdio", "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }

在 Claude Code 中安装

运行此命令。更多信息请参阅 Claude Code MCP 文档

claude mcp add context7 -- npx -y @upstash/context7-mcp@latest

在 Claude Desktop 中安装

将此内容添加到您的 Claude Desktop claude_desktop_config.json 文件中。更多信息请参阅 Claude Desktop MCP 文档

{ "mcpServers": { "Context7": { "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }

可用工具

  • resolve-library-id: 将通用库名称解析为与 Context7 兼容的库 ID。
    • libraryName (可选): 搜索并重新排序结果
  • get-library-docs: 使用与 Context7 兼容的库 ID 获取库的文档。
    • context7CompatibleLibraryID (必需)
    • topic (可选): 专注于特定主题(例如,“路由”,“钩子”)
    • tokens (可选,默认 5000): 返回的最大令牌数

开发

克隆项目并安装依赖项:

bun i

构建:

bun run build

本地配置示例

{ "mcpServers": { "context7": { "command": "npx", "args": ["tsx", "/path/to/folder/context7-mcp/src/index.ts"] } } }

使用 MCP Inspector 测试

npx -y @modelcontextprotocol/inspector npx @upstash/context7-mcp@latest

故障排除

ERR_MODULE_NOT_FOUND

如果你看到这个错误,尝试使用 bunx 而不是 npx

{ "mcpServers": { "context7": { "command": "bunx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }

这通常可以解决模块解析问题,特别是在 npx 无法正确安装或解析包的环境中。

Context7 在媒体中

许可证

MIT

工具列表

  • resolve-library-id: Resolves a package/product name to a Context7-compatible library ID and returns a list of matching libraries.

You MUST call this function before 'get-library-docs' to obtain a valid Context7-compatible library ID UNLESS the user explicitly provides a library ID in the format '/org/project' or '/org/project/version' in their query.

Selection Process:

  1. Analyze the query to understand what library/package the user is looking for
  2. Return the most relevant match based on:
  • Name similarity to the query (exact matches prioritized)
  • Description relevance to the query's intent
  • Documentation coverage (prioritize libraries with higher Code Snippet counts)
  • Source reputation (consider libraries with High or Medium reputation more authoritative)
  • Benchmark Score: Quality indicator (100 is the highest score)

Response Format:

  • Return the selected library ID in a clearly marked section
  • Provide a brief explanation for why this library was chosen
  • If multiple good matches exist, acknowledge this but proceed with the most relevant one
  • If no good matches exist, clearly state this and suggest query refinements

For ambiguous queries, request clarification before proceeding with a best-guess match.

  • get-library-docs: Fetches up-to-date documentation for a library. You must call 'resolve-library-id' first to obtain the exact Context7-compatible library ID required to use this tool, UNLESS the user explicitly provides a library ID in the format '/org/project' or '/org/project/version' in their query.

服务配置

[{'mcpServers': {'context7': {'args': ['-y', '@upstash/context7-mcp@latest'], 'command': 'npx'}}}, {'mcpServers': {'context7': {'args': ['-y', '@upstash/context7-mcp@latest'], 'command': 'npx'}}}, {'mcpServers': {'context7': {'args': ['tsx', '/path/to/folder/context7-mcp/src/index.ts'], 'command': 'npx'}}}, {'mcpServers': {'context7': {'args': ['-y', '@upstash/context7-mcp@latest'], 'command': 'bunx'}}}, {'mcpServers': {'context7': {'args': ['run', '--allow-net', 'npm:@upstash/context7-mcp'], 'command': 'deno'}}}, {'mcpServers': {'context7': {'args': ['-y', '@upstash/context7-mcp@latest'], 'command': 'bunx'}}}]

来源