AI文档工具服务器

为AI代理提供的文档工具服务,包括库文档获取、语义搜索和网页内容抓取功能,适用于开发者快速获取技术文档。

  • by ain3sh
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 ain3sh/context-mcp

使用说明

上下文工具MCP服务器

AI代理的文档工具——获取库文档、语义搜索、干净地抓取网站。

特性

  • 📚 库文档 -通过Context7 API即时访问1000多个库
  • 🔍 语义搜索 -关于文档目标的自然语言问答
  • 🌐 Web剪贴 -从任何网站中提取干净的降价
  • 💾 智能缓存 -自动重复数据删除和内容组织
  • ⚡ 代币高效 -优化响应以避免上下文膨胀
  • 🎯 资源发现 -MCP资源为代理暴露可用目标

快速开始

推荐: npx

npx -y github:ain3sh/context-mcp

无需安装。始终使用最新版本。

来源

git clone https://github.com/ain3sh/context-mcp.git
cd context-mcp
npm install
npm run build
npm start

安装

克劳德桌面

增添 ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "context-mcp": {
      "command": "npx",
      "args": ["-y", "github:ain3sh/context-mcp"],
      "env": {
        "GEMINI_API_KEY": "your_api_key_here"
      }
    }
  }
}

克劳德代码(项目级)

创建 .mcp.json 在项目根目录中:

{
  "mcpServers": {
    "context-mcp": {
      "type": "stdio", 
      "command": "npx",
      "args": ["-y", "github:ain3sh/context-mcp"],
      "env": {
        "GEMINI_API_KEY": "${GEMINI_API_KEY}"
      }
    }
  }
}

工具概述

工具 目的 令牌使用
fetch_docs 获取库/框架文档 约5k-50k代币
fetch_site 废弃网页→ 清洁降价 约2k-20k代币
ask_docs_agent 文档语义问答 约500-3k个令牌

fetch_docs

从Context7的海量索引中获取任何库或框架的文档。

使用示例

// Simple library fetch
fetch_docs({
  target: "react"
})

// Specific topic within a library
fetch_docs({
  target: "pytorch",
  tag: "autograd"
})

// Control documentation depth
fetch_docs({
  target: "pandas",
  depth: "high"  // ~50k tokens for comprehensive docs
})

// Browse available versions
fetch_docs({
  target: "tensorflow",
  browse_index: true
})

参数

参数 类型 默认值 描述
target string 必填 库名(支持模糊匹配)
tag string - 库中的主题筛选器
depth "low" "medium" "high"
version string - 要获取的特定版本
browse_index 布尔值 false 列出可用库而不是获取文档

运作原理

Query → Context7 API → Smart matching → Filtered docs → Markdown response

Context7为1000多个库维护预索引文档。该工具使用模糊匹配,因此“tf”找到“tensorflow”,“pd”找到“pandas”等。


fetch_site

使用Mozilla的可读性算法从任何网站提取干净、可读的内容。

使用示例

// Single page extraction
fetch_site({
  url: "https://arxiv.org/abs/2301.00234"
})

// Include images
fetch_site({
  url: "https://example.com/tutorial",
  images: true
})

// Batch fetch multiple URLs
fetch_site({
  url: [
    "https://site.com/page1",
    "https://site.com/page2",
    "https://site.com/page3"
  ]
})

// Force refresh cached content
fetch_site({
  url: "https://news.site/article",
  refresh: true
})

参数

参数 类型 默认值 描述
url string string[] 必填
images 布尔值 false 获取图像并将其与内容一起保存
refresh 布尔值 false 绕过缓存并重新获取

输出结构

内容保存到磁盘并返回:

./context/
└── understanding-react-hooks/   # Auto-named from page title
    ├── CONTENT.md               # Markdown with YAML frontmatter
    └── images/
        └── diagram.jpg

运作原理

URL → Fetch → JSDOM → Readability → Turndown → Clean Markdown

使用与Firefox阅读器视图相同的提取算法来删除广告、导航和杂乱。


ask_docs_agent

使用Gemini文件搜索对文档存储进行人工智能语义搜索。

需要: GEMINI_API_KEY 环境变量(在这里买一个)

使用示例

// Simple question
ask_docs_agent({
  target: "context",
  query: "How does chunking work in File Search?"
})

// Include source chunks for verification
ask_docs_agent({
  target: "my-docs",
  query: "authentication setup",
  include_chunks: true,
  top_k: 5
})

// Get structured JSON response
ask_docs_agent({
  target: "api-docs",
  query: "rate limits",
  format: "json"
})

参数

参数 类型 默认值 描述
query string 必填 自然语言问题(5-500个字符)
target string 必填 文档存储名称
top_k int 3 要检索的相关块数(1-20)
include_chunks 布尔值 false 在响应中包含块预览
format "markdown" "json" "markdown"
metadata_filter string - 高级:用于文件筛选的列表筛选器语法

发现可用目标

查询 docs://targets MCP资源查看可用资源:

{
  "targets": [
    { "target": "my-api-docs", "id": "...", "createTime": "...", "updateTime": "..." }
  ],
  "total": 1
}

使用 docs://targets/{name} 了解特定目标的详细信息。

响应示例

默认值(标记,无块):约500-1000个代币

# Search Results: context

**Query**: How does chunking work?

**Response**:
Files are automatically chunked when imported into a file search store...

---

**Sources** (2 files):
  - ai.google.dev_gemini-api_docs_file-search.md
  - technical_spec.md

有大块:约2000-3000个代币

[... same as above, plus ...]

---

## Retrieved Context Chunks

### [1] ai.google.dev_gemini-api_docs_file-search.md

Files are automatically chunked when imported...
[truncated to 500 chars]

运作原理

Query → Gemini File Search API → Semantic retrieval → LLM synthesis → Cited response

查询云中预先索引的Gemini FileSearchStores。必须单独创建存储(例如,通过GitHub Actions工作流)。


配置

环境变量

变量 必填 描述
GEMINI_API_KEY For ask_docs_agent 用于语义搜索的Gemini API密钥
CONTEXT7_API_KEY 否 更高的费率限制 fetch_docs (可选)
FETCH_SITE_CONTENT_DIR 否 内容存储目录(默认: ./context)
LOG_LEVEL 没有 debug, info,或 error (默认值: info)

代币效率

此服务器经过优化,可最大限度地减少上下文使用:

工具 典型用法 最大值
fetch_docs 随着 depth="low" 约5k个代币 10k
fetch_docs 随着 depth="medium" 约15k个代币 30k
fetch_docs 随着 depth="high" 约5万个代币 10万个
fetch_site (单页) ~2-5k代币 20k
ask_docs_agent (默认) ~500-1k个令牌 2k
ask_docs_agent 使用区块 ~2-3k代币 5k

故障排除

fetch_docs:“未找到库”

  • 尝试名称的变体(例如,“tf”vs“tensorflow”)
  • 使用 browse_index: true 查看可用库
  • 检查您是否需要特定版本

fetch_site:“提取内容失败”

  • 一些网站阻止自动访问
  • 尝试 refresh: true 绕过缓存
  • 检查网站是否需要身份验证

ask_docs_agent:“找不到目标”

  • 查询 docs://targets 查看可用目标的资源
  • 验证目标名称是否完全匹配
  • 检查一下 GEMINI_API_KEY 设置正确

ask_docs_agent:费率限制

  • 免费等级:每分钟约15次查询
  • 在两次爆发之间等待60秒
  • 如果需要,考虑升级Gemini API层

MCP客户端不显示工具

  • 跑 npm run build 编译TypeScript
  • 检查MCP配置JSON是否有效
  • 查看客户端日志(例如。, ~/Library/Logs/Claude/mcp*.log)
  • 手动测试: GEMINI_API_KEY=key npx -y github:ain3sh/context-mcp

发展

本地开发

# Install dependencies
npm install

# Build TypeScript
npm run build

# Run development mode (auto-reload)
npm run dev

# Test with environment
GEMINI_API_KEY=your_key npm start

快速测试

npm run build
timeout 5s GEMINI_API_KEY=your_key npx .

项目结构

context-mcp/
├── src/
│   └── index.ts         # MCP server implementation
├── dist/
│   └── index.js         # Compiled output
├── context/             # Cached content (git-ignored)
├── package.json
└── tsconfig.json

许可证

MIT许可证——见 许可证