代码搜索服务

一个通过GraphQL API提供Sourcegraph强大代码搜索功能的MCP服务器。

  • by 0xb8001
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 0xb8001/mcp-sourcegraph

使用说明

MCP源图

模型上下文协议(MCP)服务器,通过GraphQL API提供对Sourcegraph强大的代码搜索功能的访问。

特性

  • 🔍 代码搜索:使用Sourcegraph的高级搜索语法跨存储库搜索
  • 🎯 存储库筛选:按特定存储库筛选 repo: 模式
  • 📁 文件筛选:使用搜索特定文件类型 file: 模式
  • 🔤 语言过滤:按编程语言筛选 lang:/language: 模式
  • 🔧 图案类型:支持关键字和正则表达式搜索
  • ⚙️ 搜索修改器:区分大小写、结果限制、超时等
  • 🌐 通用载体:适用于Sourcegraph.com和私有Sourcegraph实例

安装

uvx快速入门(推荐)

# Set your Sourcegraph credentials
export SOURCEGRAPH_URL="https://sourcegraph.com"
export SOURCEGRAPH_TOKEN="your-access-token"

# Run directly from GitHub
uvx --from git+https://github.com/0xb8001/mcp-sourcegraph mcp-sourcegraph

一个衬垫

SOURCEGRAPH_URL="https://sourcegraph.com" SOURCEGRAPH_TOKEN="your-token" uvx --from git+https://github.com/0xb8001/mcp-sourcegraph mcp-sourcegraph

地方发展

# Clone and run locally
git clone https://github.com/0xb8001/mcp-sourcegraph.git
cd mcp-sourcegraph
uvx --from . mcp-sourcegraph

获取源图访问令牌

  1. 首选 源代码图设置>访问令牌
  2. 点击“生成新令牌”
  3. 为其命名并赋予适当的权限
  4. 复制生成的令牌

配置

克劳德代码(CLI)

将MCP服务器添加到Claude代码中:

export SOURCEGRAPH_URL="https://sourcegraph.com"
export SOURCEGRAPH_TOKEN="your-access-token"

claude mcp add sourcegraph -e SOURCEGRAPH_URL="$SOURCEGRAPH_URL" -e SOURCEGRAPH_TOKEN="$SOURCEGRAPH_TOKEN" -- uvx --from git+https://github.com/0xb8001/mcp-sourcegraph mcp-sourcegraph

用法

服务器提供 search 工具具有以下参数:

参数 类型 默认值 说明
query 字符串 (必填) 使用Sourcegraph语法搜索查询
pattern_type 字符串 "keyword" "keyword" 或 "regexp"
count 整数 10 最大结果数(1-1000)
timeout 整数 10 搜索超时(秒)(5-60)

搜索查询示例

# Basic keyword search
hello world

# Repository filtering  
repo:facebook/react useState

# File filtering by extension
file:\.ts$ interface lang:typescript

# Language filtering
lang:python import requests

# Complex multi-filter query
repo:^github\.com/microsoft/ file:\.cs$ async lang:csharp

# Symbol search
type:symbol main lang:go

# Case-sensitive search
case:yes TODO

# Search with result limit
repo:kubernetes file:\.go$ context count:50

高级搜索语法

Sourcegraph支持许多强大的搜索运算符:

  • 存储库筛选器: repo:pattern, -repo:pattern
  • 文件筛选器: file:pattern, -file:pattern
  • 语言过滤器: lang:name, -lang:name
  • 内容过滤器: content:"text", -content:"text"
  • 符号搜索: type:symbol
  • 区分大小写: case:yes
  • 布尔运算符: AND, OR, NOT
  • 正则表达式模式: patterntype:regexp

有关完整的参考,请参阅 源代码图搜索文档.

发展

测试

运行测试脚本以验证您的设置:

python test_server.py

项目结构

mcp-sourcegraph/
├── mcp_sourcegraph/
│   ├── __init__.py
│   └── server.py          # Main MCP server implementation
├── test_server.py         # Test script
├── pyproject.toml         # Package configuration
├── README.md
├── LICENSE
└── .env.example           # Environment variables template

贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add some amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

致谢