Rust文档向量搜索服务

一个高性能的Rust文档向量搜索服务,通过语义搜索和智能缓存为AI代理提供即时访问Rust crate文档的能力。

  • by RimoVR
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 RimoVR/docsrs-mcp

使用说明

docsrs-mcp

一个高性能的模型上下文协议(MCP)服务器,通过矢量搜索为AI代理提供对Rust crate文档的即时访问。

快速开始

# Install and run directly from GitHub (recommended)
uvx --from git+https://github.com/RimoVR/docsrs-mcp.git docsrs-mcp

# Or install from PyPI when published
uvx docsrs-mcp

特性

  • 🚀 零安装启动 随着 uvx -无需设置
  • 🔍 语义向量搜索 响应时间<50ms
  • 📚 三级文件系统 -通过回退提取实现80%以上的机箱覆盖率
  • ⚡ 智能缓存 使用SQLite+SQLite-vec和LRU驱逐
  • 🎯 高级排名 通过MMR多样化实现平衡结果
  • 🔄 版本比较 -跟踪机箱版本之间的API变化
  • 🌍 国际支持 -处理英式/美式拼写变化
  • 🏃 零冷启动 -嵌入预热消除了首次查询延迟
  • 📦 流行的板条箱预摄入 -常用板条箱的即时响应
  • 🔗 再出口发现 -从rustdoc JSON自动解析路径别名
  • 🦀 Rust stdlib support -标准库板条箱的优先摄取和检测
  • 🔍 依赖关系过滤 -依赖关系文档的智能过滤
  • 🎯 高级搜索模式 -矢量(语义)、模糊(容忍拼写错误)、正则表达式(模式匹配)和混合搜索
  • 🛡️ ReDoS保护 -带超时和模式验证的安全正则表达式搜索
  • 🌐 跨板条箱搜索 -使用RRF排名同时搜索多达5个板条箱
  • ⚖️ 稳定性过滤 -按API稳定性水平过滤结果(稳定/不稳定/实验)
  • 📖 渐进细节级别 -获取摘要、详细或专家级别的文档
  • 🔬 使用模式提取 -从代码示例中自动提取常见模式
  • 🎓 学习路径生成 -为API生成迁移路径或入职指南

安装

macOS/Linux

# Install uv if needed
curl -LsSf https://astral.sh/uv/install.sh | sh

# Run the server
uvx --from git+https://github.com/RimoVR/docsrs-mcp.git docsrs-mcp

视窗

# Install uv
irm https://astral.sh/uv/install.ps1 | iex

# Run the server
uvx --from git+https://github.com/RimoVR/docsrs-mcp.git docsrs-mcp

码头工人

docker build -t docsrs-mcp .
docker run -p 8000:8000 -v $(pwd)/cache:/app/cache docsrs-mcp

MCP配置

克劳德桌面

添加到您的配置文件中:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 窗户: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json
{
  "mcpServers": {
    "docsrs-mcp": {
      "command": "uvx",
      "args": ["--from", "git+https://github.com/RimoVR/docsrs-mcp.git", "docsrs-mcp"],
      "env": {
        "MAX_CACHE_SIZE_GB": "2"
      }
    }
  }
}

克劳德代码CLI

# Add the server
claude mcp add docsrs -- uvx --from git+https://github.com/RimoVR/docsrs-mcp.git docsrs-mcp

# With custom settings
claude mcp add docsrs --env MAX_CACHE_SIZE_GB=5 -- uvx --from git+https://github.com/RimoVR/docsrs-mcp.git docsrs-mcp

MCP工具

核心工具

工具 说明
get_crate_summary 获取机箱元数据和模块结构
search_items 具有类型过滤和MMR多样化的语义搜索
get_item_doc 具有模糊路径匹配的完整文档
get_module_tree 导航模块层次结构
search_examples 查找具有语言检测功能的代码示例
compare_versions 分析API版本之间的变化

高级工具

工具 说明
start_pre_ingestion 预装常用板条箱,便于即时存取
ingest_cargo_file 来自Cargo.toml/Cargo.lock的批量摄取依赖关系,具有版本解析功能
list_versions 列出所有可用的机箱版本

工作流增强工具

工具 说明
getDocumentationDetail 获取具有渐进细节级别的文档(摘要/详细/专家)
extractUsagePatterns 从文档和代码示例中提取常见使用模式
generateLearningPath 为API迁移或入职生成学习路径

MCP参数要求

MCP客户端兼容性:所有参数都被接受为字符串,以确保与Claude Code等MCP客户端的最大兼容性。服务器会自动将这些转换为适当的类型。

参数格式要求

  • 数字参数 必须作为字符串传递(例如。, k="5", count="100")
  • 布尔参数 必须作为字符串传递(例如。, force="true", deprecated="false")
  • 字符串参数 保持不变
  • REST API 继续接受字符串和本机类型以实现向后兼容性

MCP工具示例

使用正确的参数格式进行搜索:

{
  "crate_name": "tokio",
  "query": "spawn async tasks",
  "k": "10",
  "item_types": "function,struct"
}

使用字符串参数进行预摄入:

{
  "count": "100",
  "concurrency": "3",
  "force": "true"
}

渐进式详细文档:

{
  "crate_name": "serde",
  "item_path": "serde::Serialize",
  "detail_level": "expert"
}

提取使用模式:

{
  "crate_name": "tokio",
  "limit": "10",
  "min_frequency": "2"
}

生成学习路径:

{
  "crate_name": "actix-web",
  "from_version": "3.0.0",
  "to_version": "4.0.0"
}

带有字符串参数的货物文件摄取:

{
  "file_path": "/path/to/Cargo.toml",
  "concurrency": "5",
  "skip_existing": "true",
  "resolve_versions": "false"
}

建筑

三级文件系统

  1. 第1级:来自docs.rs的Rustdoc JSON(最高质量,约15%的覆盖率)
  2. 第2层:从crates.io CDN中提取源代码(宏支持覆盖率超过80%)
  3. 第3层:最新版本回退(100%保证覆盖率)

演出

  • 搜索:<50ms P95热延迟
  • 模糊搜索:<300ms,对常见拼写错误的准确率为90%
  • 正则表达式搜索:<500ms,具有ReDoS保护
  • 跨板条箱搜索:5个平行板条箱小于1秒
  • 摄入:对于最大10MB的板条箱,≤3s
  • 记忆:≤1GB RSS,包括嵌入
  • 缓存:使用LRU以2GB的速度自动驱逐

关键技术

  • 快速 API + FastMCP 用于MCP协议
  • sqlite-vc 用于矢量搜索(1M+矢量<100ms)
  • 快速嵌入 带有BAAI/bge-small-en-v1.5嵌入
  • RapidFuzz 用于智能路径匹配
  • 艾森 用于内存高效流媒体

发展

# Clone and setup
git clone https://github.com/RimoVR/docsrs-mcp.git
cd docsrs-mcp
uv sync --dev

# Run locally
uv run docsrs-mcp

# Run tests
uv run pytest

# Format and lint
uv run ruff format .
uv run ruff check --fix .

环境变量

# Server configuration
PORT=8000                    # Server port
HOST=0.0.0.0                # Bind address
MAX_CACHE_SIZE_GB=2         # Cache size limit
LOG_LEVEL=INFO              # Logging level

# Performance tuning
PRE_INGEST_ENABLED=true     # Enable popular crate pre-loading
PRE_INGEST_COUNT=100        # Number of crates to pre-load
EMBEDDINGS_WARMUP=true      # Eliminate cold-start latency

# Rust stdlib support
STDLIB_PRE_CACHE=true       # Prioritize stdlib crates in pre-ingestion
RUSTUP_HOME=~/.rustup       # Override rustup location (auto-detected by default)

# Dependency filtering
DEPENDENCY_FILTER=true      # Enable dependency filtering in results
BLOOM_FILTER_CAPACITY=100000  # Expected number of dependency items
BLOOM_FILTER_ERROR_RATE=0.001 # Acceptable false positive rate

# Advanced
RANKING_DIVERSITY_WEIGHT=0.1    # MMR diversification strength
RANKING_DIVERSITY_LAMBDA=0.6    # Relevance vs diversity balance

API示例

搜索文档

import httpx

async with httpx.AsyncClient() as client:
    response = await client.post(
        "http://localhost:8000/mcp/tools/search_items",
        json={
            "crate_name": "tokio",
            "query": "spawn async tasks",
            "k": "5"  # Note: string format for MCP compatibility
        }
    )
    print(response.json())

比较版本

response = await client.post(
    "http://localhost:8000/mcp/tools/compare_versions",
    json={
        "crate_name": "serde",
        "version_a": "1.0.0",
        "version_b": "1.0.100"
    }
)

高级搜索示例

# Fuzzy search for typo tolerance
search_items(query="tokio::spwan", search_mode="fuzzy")

# Regex pattern matching
search_items(regex_pattern="async.*spawn", search_mode="regex")

# Cross-crate search
search_items(query="error handling", crates=["tokio", "anyhow", "thiserror"])

# Stability filtering
search_items(query="async runtime", stability_filter="stable")

故障排除

问题 解决方案
“未找到uvx” 安装uv: curl -LsSf https://astral.sh/uv/install.sh | sh
端口已在使用中 更改端口: PORT=8001 uvx docsrs-mcp
速率限制(429) 默认值:每个IP 30个请求/秒,实现回退
内存错误 确保≥256MB RAM可用
MCP验证错误 确保所有数字/布尔参数都以字符串形式传递(例如。, k="5", force="true")

最近的增强功能

v1.0(最新)

  • ✅ 三层回退系统,可实现80%以上的机箱覆盖率
  • ✅ API演进跟踪的版本比较
  • ✅ MMR多样化,实现平衡的搜索结果
  • ✅ 零冷启动预埋件预热
  • ✅ 流行的板条箱预摄入
  • ✅ 英美拼写规范化
  • ✅ 带有示例的增强错误消息
  • ✅ 批处理优化
  • ✅ 带后备功能的标准库支持

已知限制

  • 标准库rustdoc JSON需要通过本地生成 rustup component add rust-docs-json
  • 一些较旧的板条箱可能只有2/3级文件质量

许可证

MIT许可证-请参阅许可证文件

贡献

欢迎投稿!请向提交pull请求 GitHub存储库.

安全

  • 起源异教徒:仅从docs.rs和crates.io获取
  • 大小限制:30MB压缩/100MB解压缩
  • 输入验证:严格的Pydantic模型,经过全面验证
  • 速率限制:内置防滥用保护

向报告安全问题security@example.com