Context7 MCP - 为所有Prompt获取最新文档

1- Context7 MCP - 为所有 Prompt 获取最新文档

网站 smithery徽章 在VS Code中安装 (npx)

1.1- ❌ 不使用 Context7

大语言模型 (LLM) 可能依赖过时或通用的库信息。你可能会遇到:

  • ❌ 代码示例已过时,或基于一年前的训练数据
  • ❌ 幻觉产生的 API 根本不存在
  • ❌ 针对旧版本包的通用回答

1.2- ✅ 使用 Context7

Context7 MCP 直接从源头获取最新的、特定版本的文档和代码示例 — 并将它们直接放入你的提示中。

在 Cursor 中添加 使用 context7 到你的提示:

创建一个使用app router的基本Next.js项目。使用 context7
创建一个脚本,删除PostgreSQL数据库中city字段为""的行。使用 context7

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

  • 1️⃣ 按照往常,自然地编写你的提示
  • 2️⃣ 告诉 LLM使用 context7
  • 3️⃣ 获取可用的代码回复

无需在标签间切换,不存在幻觉 API,不会生成过时的代码。

1.3- 🛠️ 开始使用

1.3.1- 要求

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

1.3.2- 通过 Smithery 安装

要通过 Smithery 自动安装 Context7 MCP Server for Claude Desktop:

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

1.3.3- 在 Cursor 中安装

前往:Settings -> Cursor Settings -> MCP -> Add new global MCP server

推荐的方法是将以下配置粘贴到你的 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"]
    }
  }
}

1.3.4- 在 Windsurf 中安装

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

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

1.3.5- 在 VSCode 中安装

在VS Code中安装 (npx)
在VS Code Insiders中安装 (npx)

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

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

1.3.6- 在 Zed 中安装

可以通过 Zed扩展 安装,或者你可以将以下内容添加到你的 Zed settings.json 文件中。更多信息请参见 Zed Context Server文档

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

1.3.7- 在 Claude Code 中安装

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

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

1.3.8- 在 Claude Desktop 中安装

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

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

1.3.9- 使用 Docker(容器部署)

如果你希望使用 Docker 容器运行 MCP 服务器:

  1. 构建 Docker 镜像:

    确保 Docker Desktop(或 Docker 守护进程)正在运行。 在项目根目录运行:

    docker build -t context7-mcp .
    
  2. 配置 MCP 客户端:

    更新 MCP 客户端配置以使用 Docker 命令。

    cline_mcp_settings.json 配置示例:

    {
      "mcpServers": {
        "Сontext7": {
        "autoApprove": [],
        "disabled": false,
        "timeout": 60,
          "command": "docker",
          "args": ["run", "-i", "--rm", "context7-mcp"],
          "transportType": "stdio"
        }
      }
    }
    

    注意事项:

    • 此为示例配置。请参考前面 README 中针对具体 MCP 客户端(如 Cursor、VS Code 等)的示例来调整结构(如 mcpServersservers)。同时确保 args 中的镜像名称与 docker build 命令使用的标签一致。
    • 当前 Cursor 版本 (0.49.5), 请不要使用本方式启动 MCP server,详情:Cursor官方说明

1.3.10- 可用工具

  • resolve-library-id: 将通用库名称解析为 Context7 兼容的库 ID。
    • libraryName (必需)
  • get-library-docs: 使用 Context7 兼容的库 ID 获取库的文档。
    • context7CompatibleLibraryID (必需)
    • topic (可选): 将文档集中在特定主题上(例如 “routing”、“hooks”)
    • tokens (可选,默认 10000): 返回的最大令牌数。小于 10000 的值会自动增加到 10000。

1.4- 开发

克隆项目并安装依赖:

bun i

构建:

bun run build

1.4.1- 本地配置示例

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

1.4.2- 使用 MCP Inspector 测试

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

1.5- 故障排除

1.5.1- ERR_MODULE_NOT_FOUND

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

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

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

1.5.2- MCP 客户端错误

  1. 尝试从包名中删除 @latest

  2. 尝试使用 bunx 作为替代方案。

  3. 尝试使用 deno 作为替代方案。

  4. 确保你使用的是 Node v18 或更高版本,以便使用 npx 时获得原生 fetch 支持。

1.6- Context7 媒体报道

1.7- Star 历史

Star历史图表

1.8- 许可证

MIT