Skip to content

Sourcebot MCP #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added special `*` value for `rev:` to allow searching across all branches. [#281](https://github.com/sourcebot-dev/sourcebot/pull/281)
- Added the Sourcebot Model Context Protocol (MCP) server in [packages/mcp](./packages/mcp/README.md) to allow LLMs to interface with Sourcebot. Checkout the npm package [here](https://www.npmjs.com/package/@sourcebot/mcp). [#292](https://github.com/sourcebot-dev/sourcebot/pull/292)

## [3.1.2] - 2025-04-30

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ clean:
packages/crypto/dist \
packages/error/node_modules \
packages/error/dist \
packages/mcp/node_modules \
packages/mcp/dist \
.sourcebot

soft-reset:
Expand Down
7 changes: 4 additions & 3 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"pages": [
"docs/more/syntax-reference",
"docs/more/multi-branch-indexing",
"docs/more/roles-and-permissions"
"docs/more/roles-and-permissions",
"docs/more/mcp-server",
"docs/more/search-contexts"
]
}
]
Expand All @@ -71,8 +73,7 @@
"self-hosting/more/authentication",
"self-hosting/more/tenancy",
"self-hosting/more/transactional-emails",
"self-hosting/more/declarative-config",
"self-hosting/more/search-contexts"
"self-hosting/more/declarative-config"
]
},
{
Expand Down
181 changes: 181 additions & 0 deletions docs/docs/more/mcp-server.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
---
title: Sourcebot MCP server (@sourcebot/mcp)
sidebarTitle: Sourcebot MCP server
---

<Note>
This feature is only available when [self-hosting](/self-hosting) with [authentication](/self-hosting/more/authentication) disabled.
</Note>

The [Model Context Protocol](https://modelcontextprotocol.io/introduction) (MCP) is a open standard for providing context to LLMs. The [@sourcebot/mcp](https://www.npmjs.com/package/@sourcebot/mcp) package is a MCP server that enables LLMs to interface with your Sourcebot instance, enabling MCP clients like Cursor, Vscode, and others to have context over your entire codebase.

## Getting Started

<Steps>
<Step title="Launch Sourcebot">
Follow the self-hosting [quick start guide](/self-hosting/overview#quick-start-guide) to launch Sourcebot and get your code indexed. The host url of your instance (e.g., `http://localhost:3000`) is passed to the MCP server via the `SOURCEBOT_HOST` url.

If a host is not provided, then the server will fallback to using the demo instance hosted at https://demo.sourcebot.dev. You can see the list of repositories indexed [here](https://demo.sourcebot.dev/~/repos). Add additional repositories by [opening a PR](https://github.com/sourcebot-dev/sourcebot/blob/main/demo-site-config.json).
</Step>

<Step title="Install the MCP server">
<Note>
Ensure you have [Node.js](https://nodejs.org/en) >= v18.0.0 installed.
</Note>
Next, we can install the [@sourcebot/mcp](https://www.npmjs.com/package/@sourcebot/mcp) MCP server into any supported MCP client:

<AccordionGroup>
<Accordion title="Cursor">
[Cursor MCP docs](https://docs.cursor.com/context/model-context-protocol)

Go to: `Settings` -> `Cursor Settings` -> `MCP` -> `Add new global MCP server`

Paste the following into your `~/.cursor/mcp.json` file. This will install Sourcebot globally within Cursor:

```json
{
"mcpServers": {
"sourcebot": {
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest" ],
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
```

Replace `http://localhost:3000` with wherever your Sourcebot instance is hosted.
</Accordion>
<Accordion title="Windsurf">
[Windsurf MCP docs](https://docs.windsurf.com/windsurf/mcp)

Go to: `Windsurf Settings` -> `Cascade` -> `Add Server` -> `Add Custom Server`

Paste the following into your `mcp_config.json` file:

```json
{
"mcpServers": {
"sourcebot": {
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest" ],
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
```

Replace `http://localhost:3000` with wherever your Sourcebot instance is hosted.

</Accordion>
<Accordion title="VS Code">
[VS Code MCP docs](https://code.visualstudio.com/docs/copilot/chat/mcp-servers)

Add the following to your [settings.json](https://code.visualstudio.com/docs/copilot/chat/mcp-servers):

```json
{
"mcp": {
"servers": {
"sourcebot": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest"]
},
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
```
Comment on lines +80 to +95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix VS Code configuration format - misplaced env property.

The env object should be nested under the sourcebot object, not alongside it within the servers object. This matches the format used in other client configurations and follows VS Code's MCP configuration structure.

 {
     "mcp": {
         "servers": {
             "sourcebot": {
                 "type": "stdio",
                 "command": "npx",
-                "args": ["-y", "@sourcebot/mcp@latest"]
+                "args": ["-y", "@sourcebot/mcp@latest"],
+                "env": {
+                    "SOURCEBOT_HOST": "http://localhost:3000"
+                }
             },
-            "env": {
-                "SOURCEBOT_HOST": "http://localhost:3000"
-            }
         }
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
```json
{
"mcp": {
"servers": {
"sourcebot": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest"]
},
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
```
{
"mcp": {
"servers": {
"sourcebot": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest"],
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
}


Replace `http://localhost:3000` with wherever your Sourcebot instance is hosted.
</Accordion>
<Accordion title="Claude Code">
[Claude Code MCP docs](https://docs.anthropic.com/en/docs/claude-code/tutorials#set-up-model-context-protocol-mcp)

Run the following command:

```sh
claude mcp add sourcebot -e SOURCEBOT_HOST=http://localhost:3000 -- npx -y @sourcebot/mcp@latest
```

Replace `http://localhost:3000` with wherever your Sourcebot instance is hosted.
</Accordion>
<Accordion title="Claude Desktop">
[Claude Desktop MCP docs](https://modelcontextprotocol.io/quickstart/user)

Add the following to your `claude_desktop_config.json`:

```json
{
"mcpServers": {
"sourcebot": {
"command": "npx",
"args": ["-y", "@sourcebot/mcp@latest"],
"env": {
"SOURCEBOT_HOST": "http://localhost:3000"
}
}
}
}
```

Replace `http://localhost:3000` with wherever your Sourcebot instance is hosted.
</Accordion>
</AccordionGroup>
</Step>

<Step title="Prompt the LLM">
Tell your LLM to `use sourcebot` when prompting.
</Step>

</Steps>


## Available Tools


### `search_code`

Fetches code that matches the provided regex pattern in `query`.

Parameters:
| Name | Required | Description |
|:----------------------|:---------|:----------------------------------------------------------------------------------------------------------------------------------|
| `query` | yes | Regex pattern to search for. Escape special characters and spaces with a single backslash (e.g., 'console\.log', 'console\ log'). |
| `filterByRepoIds` | no | Restrict search to specific repository IDs (from 'list_repos'). Leave empty to search all. |
| `filterByLanguages` | no | Restrict search to specific languages (GitHub linguist format, e.g., Python, JavaScript). |
| `caseSensitive` | no | Case sensitive search (default: false). |
| `includeCodeSnippets` | no | Include code snippets in results (default: false). |
| `maxTokens` | no | Max tokens to return (default: env.DEFAULT_MINIMUM_TOKENS). |


### `list_repos`

Lists all repositories indexed by Sourcebot.

### `get_file_source`

Fetches the source code for a given file.

Parameters:
| Name | Required | Description |
|:-------------|:---------|:-----------------------------------------------------------------|
| `fileName` | yes | The file to fetch the source code for. |
| `repoId` | yes | The Sourcebot repository ID. |


## Environment Variables

| Name | Default | Description |
|:-------------------------|:-----------------------|:--------------------------------------------------|
| `SOURCEBOT_HOST` | http://localhost:3000 | URL of your Sourcebot instance. |
| `DEFAULT_MINIMUM_TOKENS` | 10000 | Minimum number of tokens to return in responses. |
| `DEFAULT_MATCHES` | 10000 | Number of code matches to fetch per search. |
| `DEFAULT_CONTEXT_LINES` | 5 | Lines of context to include above/below matches. |
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebarTitle: Search contexts (EE)
import SearchContextSchema from '/snippets/schemas/v3/searchContext.schema.mdx'

<Note>
This is only available in the Enterprise Edition. Please add your [license key](/self-hosting/license-key) to activate it.
This feature is only available when [self-hosting](/self-hosting) with an active Enterprise license. Please add your [license key](/self-hosting/license-key) to activate it.
</Note>

A **search context** is a user-defined grouping of repositories that helps focus searches on specific areas of your codebase, like frontend, backend, or infrastructure code. Some example queries using search contexts:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"scripts": {
"build": "cross-env SKIP_ENV_VALIDATION=1 yarn workspaces foreach -A run build",
"test": "yarn workspaces foreach -A run test",
"dev": "yarn dev:prisma:migrate:dev && npm-run-all --print-label --parallel dev:zoekt dev:backend dev:web",
"dev": "yarn dev:prisma:migrate:dev && npm-run-all --print-label --parallel dev:zoekt dev:backend dev:web watch:mcp watch:schemas",
"with-env": "cross-env PATH=\"$PWD/bin:$PATH\" dotenv -e .env.development -c --",
"dev:zoekt": "yarn with-env zoekt-webserver -index .sourcebot/index -rpc",
"dev:backend": "yarn with-env yarn workspace @sourcebot/backend dev:watch",
"dev:web": "yarn with-env yarn workspace @sourcebot/web dev",
"watch:mcp": "yarn workspace @sourcebot/mcp build:watch",
"watch:schemas": "yarn workspace @sourcebot/schemas watch",
"dev:prisma:migrate:dev": "yarn with-env yarn workspace @sourcebot/db prisma:migrate:dev",
"dev:prisma:studio": "yarn with-env yarn workspace @sourcebot/db prisma:studio",
"dev:prisma:migrate:reset": "yarn with-env yarn workspace @sourcebot/db prisma:migrate:reset",
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"simple-git": "^3.27.0",
"strip-json-comments": "^5.0.1",
"winston": "^3.15.0",
"zod": "^3.24.2"
"zod": "^3.24.3"
}
}
2 changes: 2 additions & 0 deletions packages/mcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules/
5 changes: 5 additions & 0 deletions packages/mcp/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/*
!/dist/**
!README.md
!package.json
!CHANGELOG.md
13 changes: 13 additions & 0 deletions packages/mcp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.0.0] - 2025-05-07

### Added
- Initial release
Loading