0xb8001/mcp-sourcegraph
If you are the rightful owner of mcp-sourcegraph and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
MCP Sourcegraph is a server that leverages the Model Context Protocol to provide access to Sourcegraph's code search capabilities via the GraphQL API.
MCP Sourcegraph
A Model Context Protocol (MCP) server that provides access to Sourcegraph's powerful code search capabilities through the GraphQL API.
Features
- š Code Search: Search across repositories using Sourcegraph's advanced search syntax
- šÆ Repository Filtering: Filter by specific repositories with
repo:
patterns - š File Filtering: Search specific file types with
file:
patterns - š¤ Language Filtering: Filter by programming language with
lang:
/language:
patterns - š§ Pattern Types: Support for both keyword and regular expression search
- āļø Search Modifiers: Case sensitivity, result limits, timeouts, and more
- š Universal Support: Works with Sourcegraph.com and private Sourcegraph instances
Installation
Quick Start with uvx (Recommended)
# 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
One-liner
SOURCEGRAPH_URL="https://sourcegraph.com" SOURCEGRAPH_TOKEN="your-token" uvx --from git+https://github.com/0xb8001/mcp-sourcegraph mcp-sourcegraph
Local Development
# Clone and run locally
git clone https://github.com/0xb8001/mcp-sourcegraph.git
cd mcp-sourcegraph
uvx --from . mcp-sourcegraph
Getting a Sourcegraph Access Token
- Go to Sourcegraph Settings > Access tokens
- Click "Generate new token"
- Give it a name and appropriate permissions
- Copy the generated token
Configuration
Claude Code (CLI)
Add the MCP server to Claude Code:
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
Usage
The server provides a search
tool with the following parameters:
Parameter | Type | Default | Description |
---|---|---|---|
query | string | (required) | Search query using Sourcegraph syntax |
pattern_type | string | "keyword" | "keyword" or "regexp" |
count | integer | 10 | Maximum number of results (1-1000) |
timeout | integer | 10 | Search timeout in seconds (5-60) |
Search Query Examples
# 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
Advanced Search Syntax
Sourcegraph supports many powerful search operators:
- Repository filters:
repo:pattern
,-repo:pattern
- File filters:
file:pattern
,-file:pattern
- Language filters:
lang:name
,-lang:name
- Content filters:
content:"text"
,-content:"text"
- Symbol search:
type:symbol
- Case sensitivity:
case:yes
- Boolean operators:
AND
,OR
,NOT
- Regex patterns:
patterntype:regexp
For a complete reference, see the Sourcegraph search documentation.
Development
Testing
Run the test script to verify your setup:
python test_server.py
Project Structure
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
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the file for details.
Acknowledgments
- Sourcegraph for providing the powerful code search API
- Model Context Protocol for the integration standard
- Anthropic for Claude and the MCP specification