Complete step-by-step guide to get SourceGraph MCP server running with Claude.
- Python 3.10 or higher
- SourceGraph instance (local or cloud)
- Claude Desktop, Claude Code, or Codex (OpenAI) installed
pipx install sourcegraph-mcpThat's it! The sourcegraph-mcp command is now available globally.
which sourcegraph-mcp
# Should show: /Users/yourusername/.local/bin/sourcegraph-mcp (macOS/Linux)
# Or: C:\Users\yourusername\.local\bin\sourcegraph-mcp.exe (Windows)- Open your browser and navigate to your local instance:
http://localhost:3370 - Click your profile icon (top right) → Settings
- In the left sidebar, click Access tokens
- Click Generate new token
- Give it a descriptive name like "Claude MCP Server"
- Select scopes (minimum:
user:read,repo:read) - Click Generate token
- Copy the token immediately (you won't see it again!)
- Navigate to your SourceGraph instance:
https://sourcegraph.yourcompany.com - Follow the same steps as above
Choose one of these methods:
Add to your shell profile (~/.zshrc, ~/.bashrc, etc.):
export SOURCEGRAPH_URL="http://localhost:3370"
export SOURCEGRAPH_TOKEN="sgp_your_actual_token_here"Then reload your shell:
source ~/.zshrc # or ~/.bashrcYou can also pass the URL and token directly in your Claude Code or Claude Desktop config (see step 6).
You can test that your token and URL work:
curl -H "Authorization: token sgp_your_token_here" \
http://localhost:3370/.api/graphql \
-d '{"query": "{currentUser{username}}"}'If successful, you'll see JSON output with your username.
Alternatively, wait until step 8 to verify everything works together in Claude.
Choose Claude Code, Claude Desktop, or Codex based on your setup.
Option A: User-Wide (Recommended - No Permission Prompts)
Edit ~/.claude.json:
IMPORTANT: Replace the URL and token with your actual SourceGraph instance details.
{
"mcpServers": {
"sourcegraph": {
"command": "sourcegraph-mcp",
"env": {
"SOURCEGRAPH_URL": "http://localhost:3370", // CHANGE THIS to your SourceGraph URL
"SOURCEGRAPH_TOKEN": "sgp_your_actual_token_here" // CHANGE THIS to your actual token
}
}
},
"permissions": {
"allow": [
"mcp__sourcegraph__*"
]
}
}Note: If sourcegraph-mcp is not in your PATH, use the full path:
"command": "/Users/yourusername/.local/bin/sourcegraph-mcp"No restart needed - Claude Code will reload automatically.
Option B: Project-Specific
- Create
.mcp.jsonin your project root:
{
"mcpServers": {
"sourcegraph": {
"command": "sourcegraph-mcp",
"env": {
"SOURCEGRAPH_URL": "http://localhost:3370", // CHANGE THIS to your SourceGraph URL
per "SOURCEGRAPH_TOKEN": "sgp_your_actual_token_here" // CHANGE THIS to your actual token
}
}
}
}
On Windows, this might look like:
{
"mcpServers": {
"sourcegraph": {
"command": "C:\\Users\\YOUR_USERNAME\\.local\\bin\\sourcegraph-mcp.exe",
"args": [],
"env": {
"SOURCEGRAPH_URL": "http://192.168.0.130:7080/", // CHANGE THIS to the URL you can log into in your browser
"SOURCEGRAPH_TOKEN": "sgp_your_actual_token_here"
}
}
}
}
Important: You MUST change BOTH the URL and token to match your SourceGraph instance.
- Create
.claude/settings.local.jsonin your project root:
{
"permissions": {
"allow": [
"mcp__sourcegraph__search_sourcegraph",
"mcp__sourcegraph__search_sourcegraph_regex",
"mcp__sourcegraph__get_sourcegraph_config"
]
},
"enableAllProjectMcpServers": true,
"enabledMcpjsonServers": ["sourcegraph"]
}
And it might work better if your ~/.claude.json file simply included:
"permissions": {
"allow": [
"mcp__sourcegraph__*"
]
}Critical: Permission format must use mcp__servername__toolname (double underscores), NOT colons like MCP:servername:toolname.
Find Your Config File:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
Edit the Config File:
If the file doesn't exist, create it. If it exists and has other MCP servers, add to the mcpServers object.
IMPORTANT: Replace the URL and token with your actual SourceGraph instance details.
{
"mcpServers": {
"sourcegraph": {
"command": "sourcegraph-mcp",
"env": {
"SOURCEGRAPH_URL": "http://localhost:3370", // CHANGE THIS to your SourceGraph URL
"SOURCEGRAPH_TOKEN": "sgp_your_actual_token_here" // CHANGE THIS to your actual token
}
}
}
}For cloud SourceGraph:
{
"mcpServers": {
"sourcegraph": {
"command": "sourcegraph-mcp",
"env": {
"SOURCEGRAPH_URL": "https://sourcegraph.yourcompany.com", // CHANGE THIS to your company URL
"SOURCEGRAPH_TOKEN": "sgp_your_actual_token_here" // CHANGE THIS to your actual token
}
}
}
}Note: If sourcegraph-mcp is not in your PATH, use the full path:
- macOS/Linux:
/Users/yourusername/.local/bin/sourcegraph-mcp - Windows:
C:\Users\yourusername\.local\bin\sourcegraph-mcp.exe
Find Your Config File:
- macOS/Linux:
~/.codex/config.toml - Windows:
%USERPROFILE%\.codex\config.toml
Create the file if it doesn't exist.
Edit the Config File:
Unlike Claude Code, Codex uses a global configuration (not per-project). Add the following to your config.toml:
IMPORTANT: Replace the URL and token with your actual SourceGraph instance details.
[mcp_servers.sourcegraph]
command = "sourcegraph-mcp"
[mcp_servers.sourcegraph.env]
SOURCEGRAPH_URL = "http://localhost:3370"
SOURCEGRAPH_TOKEN = "sgp_your_actual_token_here"Alternative compact format:
[mcp_servers.sourcegraph]
command = "sourcegraph-mcp"
env = { SOURCEGRAPH_URL = "http://localhost:3370", SOURCEGRAPH_TOKEN = "sgp_your_token_here" }For cloud SourceGraph:
[mcp_servers.sourcegraph]
command = "sourcegraph-mcp"
[mcp_servers.sourcegraph.env]
SOURCEGRAPH_URL = "https://sourcegraph.yourcompany.com"
SOURCEGRAPH_TOKEN = "sgp_your_actual_token_here"Note: If sourcegraph-mcp is not in your PATH, use the full path:
- macOS/Linux:
command = "/Users/yourusername/.local/bin/sourcegraph-mcp" - Windows:
command = "C:\\Users\\yourusername\\.local\\bin\\sourcegraph-mcp.exe"
For Claude Code:
- No restart required - settings are loaded automatically
For Claude Desktop:
- Important: Completely quit Claude Desktop (don't just close the window)
- macOS: Cmd+Q or Claude → Quit Claude
- Windows: File → Exit
- Linux: File → Quit
- Then relaunch Claude Desktop
For Codex:
- No restart required - Codex automatically reloads MCP servers from
config.toml
In a new Claude conversation, try:
Can you check the SourceGraph configuration?
Claude should use the get_sourcegraph_config tool and show your setup.
Then try a search:
Search for "PlaceOrder" in my codebase
Claude should use search_sourcegraph and show results from your SourceGraph instance!
python test_connection.pyAsk Claude: "Check SourceGraph configuration"
- "Find all TODO comments"
- "Search for 'async def' in Python files"
- "Find classes ending with 'Service' in C#"
- "Search for 'PlaceOrder' in the trading-engine repo"
If you get "command not found: sourcegraph-mcp":
-
Verify installation:
which sourcegraph-mcp
-
Check if
~/.local/binis in your PATH:echo $PATH | grep .local/bin
-
Use full path in config:
"command": "/Users/yourusername/.local/bin/sourcegraph-mcp"
If you get permission errors:
chmod +x ~/.local/bin/sourcegraph-mcpStart your local SourceGraph:
docker-compose up -d # or however you run SourceGraphVerify it's accessible:
curl http://localhost:3370/If Claude Code keeps asking for permission even after selecting "Don't ask again":
- Check permission format - Must use
mcp__sourcegraph__*(double underscores), NOTMCP:sourcegraph:*(colons) - Use user-wide config - Move server to
~/.claude.jsoninstead of project.mcp.jsonfor better permission persistence - Verify settings location - Project permissions go in
.claude/settings.local.json, not.claude/settings.json - Reset if stuck:
claude mcp reset-project-choices
- Read the full README.md for query syntax and examples
- Check SourceGraph docs for advanced queries
- Join the GitHub Discussions
- Check the Troubleshooting section in README
- Open an issue on GitHub
- Review SourceGraph documentation
You will need to log into localhost with a browser and set a Site Configuration, something like this:
{ // The externally accessible URL for Sourcegraph (i.e., what you type into your browser) // This is required to be configured for Sourcegraph to work correctly. "externalURL": "http://192.168.0.130:7080", "auth.providers": [ { "allowSignup": true, "type": "builtin" } ] }