内容
# Sourcegraph MCP Server
一个模型上下文协议 (MCP) 服务器,允许 AI 助手通过 Sourcegraph API 使用自然语言查询搜索代码仓库。该服务器还提供高级代码模式研究功能。
## Key Features
- **Natural Language Code Search**:使用简单的英语查询进行搜索
- **Code Search**:跨 Sourcegraph 仓库搜索代码
- **Commit Search**:使用各种过滤器查找提交
- **Diff Search**:查找代码更改/PR
- **GitHub-specific Search**:在特定的 GitHub 仓库中搜索
- **Deep Code Research**:分析跨仓库的代码模式和架构
## Quick Start
### Installation & Usage
```bash
# Run directly with npx (simplest way)
npx sourcegraph-mcp-server
# Or install globally
npm install -g sourcegraph-mcp-server
sourcegraph-mcp-server
```
### Configuration
在运行之前,使用您的 Sourcegraph 凭据创建一个 `.env` 文件:
```
SOURCEGRAPH_URL=https://your-sourcegraph-instance.com
SOURCEGRAPH_TOKEN=your_api_token
```
## Using with MCP-Capable AI Assistants
### Amp
1. 在 Amp 扩展中,单击“设置”图标并找到 MCP Servers 部分
2. 添加 MCP Server
- 对于 stdio:
- Command: `npx`
- Arguments: `-y sourcegraph-mcp-server`
- 对于 http:
- URL: `http://localhost:3002`
- Environment Variables: `SOURCEGRAPH_URL` 和 `SOURCEGRAPH_TOKEN`
### MCP Inspector (for testing)
npx @modelcontextprotocol/inspector node dist/stdio-server.js
## Available Tools
### Basic Search Tools
- `search-code`:使用 Sourcegraph 查询语法搜索代码
- `search-commits`:使用过滤器查找提交
- `search-diffs`:查找代码更改/PR
- `search-github-repos`:在特定的 GitHub 仓库中搜索
### Code Intelligence Tools
- `get-definition`:查找代码中符号的定义
- `get-references`:查找对符号的所有引用
- `get-implementations`:查找接口或方法的实现
- `get-hover-documentation`:获取符号的文档
- `get-document-symbols`:列出文件中的所有符号
### Repository Tools
- `get-file-content`:从仓库中获取文件的内容
- `get-file-blame`:获取文件的 git blame 信息
### Security Tools
- `lookup-cve`:搜索影响仓库或软件包的 CVE
- `lookup-package-vulnerability`:检查软件包是否存在漏洞
- `search-exploits`:查找已知漏洞的利用代码
- `find-vendor-advisory`:查找供应商安全公告
### Utility Tools
- `test-connection`:测试与 Sourcegraph API 的连接
- `echo`:简单的测试工具
- `debug`:显示可用的工具
## Code Intelligence Features
Sourcegraph MCP Server 提供对高级代码智能功能的访问:
- **Symbol Navigation**:跳转到定义并在仓库中查找引用
- **Code Documentation**:获取函数、类和变量的悬停文档
- **Repository Analysis**:检查文件内容和 git blame 历史记录
- **Security Analysis**:查找漏洞和安全公告
这些工具通过提供有关代码符号、关系和漏洞的上下文,帮助您更有效地理解代码库。
## Advanced Search Syntax
Sourcegraph 具有强大的搜索语法,您可以将其与搜索工具一起使用:
- **Repository filtering**:`repo:^github\.com/owner/repo$`
- **Language filtering**:`lang:javascript`
- **File path filtering**:`file:\.js$`
- **Content filtering**:`content:"exact phrase"`
- **Boolean operators**:`term1 AND term2`, `term1 OR term2`, `term1 NOT term2`
- **Regular expressions**:`/pattern/`
- **Commit search**:`type:commit message:"fix bug" author:username`
- **Diff search**:`type:diff select:commit.diff.added term`
## Troubleshooting
如果您遇到问题:
1. **Environment Variables**:确保正确设置了 SOURCEGRAPH_URL 和 SOURCEGRAPH_TOKEN
2. **Connectivity**:验证您的 Sourcegraph 实例是否可访问
3. **Tool Errors**:使用 `debug` 工具验证可用的工具
有关详细的故障排除帮助,请参见 [DEBUGGING.md](./DEBUGGING.md) 文件。
## Project Structure
```
.
├── src/ - TypeScript source files
│ ├── index.ts - Main Express server entry point
│ ├── mcp-server.ts - Model Context Protocol implementation
│ ├── mcp-server-debug.ts - Debug version of MCP server
│ ├── mcp-server-connect.ts - Connection testing implementation
│ ├── http-server.ts - HTTP transport for MCP
│ └── stdio-server.ts - STDIO transport for MCP
├── scripts/ - Utility scripts for testing and development
│ ├── debug-server.js - Debug server utility
│ └── test-search.js - Test search functionality
├── dist/ - Compiled JavaScript output
├── docs/ - Documentation files
└── .env.example - Example environment configuration
```
## Running Manually (for Development)
```bash
# Clone the repository
git clone https://github.com/madhukarkumar/sg-ts-mcp-server.git
cd sg-ts-mcp-server
# Install dependencies
npm install
# Build the project
npm run build
# Start the HTTP MCP server
npm run start:mcp
# Or start the STDIO server
npm run start:stdio
```
## License
MIT