Sourcegraph DocsSourcegraph Docs
Changelog↗
Theme
  1. Docs
  2. api
  3. mcp
  4. authentication

MCP Authentication

Authenticate MCP clients with OAuth or a Sourcegraph access token.

Supported on Enterprise plans.

The Sourcegraph MCP server supports OAuth 2.0 and access token authentication.

MethodWhen to use
OAuth with Dynamic Client RegistrationYour MCP client supports OAuth and your organization allows clients to register automatically
OAuth with a pre-registered clientYour organization requires administrators to approve OAuth clients in advance
Access tokenYour MCP client does not support OAuth

OAuth with Dynamic Client Registration

Sourcegraph implements Dynamic Client Registration (DCR) as defined by RFC 7591. Compatible MCP clients can register automatically and authenticate through a browser without a pre-configured client ID.

DCR is enabled by default. Applications created through DCR are restricted to the mcp and mcp:write scopes, which limit access to MCP endpoints.

OAuth with a Pre-registered Client

Organizations that require administrators to approve OAuth clients can use pre-registered OAuth clients, also called static OAuth clients. This method also works with MCP clients that do not support DCR.

Configure and test pre-registered clients before disabling DCR to avoid interrupting users.

Create an OAuth Client

  1. In Sourcegraph, navigate to Administration > OAuth clients.
  2. Click Create OAuth client and configure it with:
    • A descriptive name and optional description.
    • The redirect URI required by your MCP client. For mcp-remote, use http://localhost:3334/oauth/callback.
    • Public as the client type.
    • The mcp scope. For Agentic Batch Changes, also mcp:write.
  3. Create the client and copy its client ID.

See OAuth Apps for more information about creating and managing OAuth clients.

Configure the MCP Client

If your MCP client supports a pre-configured OAuth client ID, add the client ID using the client's OAuth settings.

If it does not, use mcp-remote as a local proxy and add the following configuration to your client's MCP server configuration:

JSON
{ "sourcegraph": { "type": "stdio", "command": "npx", "args": [ "mcp-remote", "https://sourcegraph.example.com/.api/mcp", "3334", "--static-oauth-client-info", "{\"client_id\":\"YOUR_CLIENT_ID\"}", "--static-oauth-client-metadata", "{\"scope\":\"mcp\"}" ] } }

Replace sourcegraph.example.com with your Sourcegraph instance URL and YOUR_CLIENT_ID with the client ID you copied. Start the MCP client and complete authorization in your browser.

Disable Dynamic Client Registration

After configuring and testing pre-registered clients, disable DCR while leaving MCP available by setting:

JSON
{ "auth.idpDynamicClientRegistrationEnabled": false }

When DCR is disabled:

  • Requests to /.auth/idp/oauth/register return 404 not found.
  • Existing DCR-registered clients and their tokens stop working.
  • Pre-registered OAuth clients and access tokens continue to work.

If mcp.enabled is false, DCR is also unavailable and the MCP endpoints are disabled.

Access Tokens

For clients that do not support OAuth, include a Sourcegraph access token in the Authorization header:

SHELL
Authorization: token YOUR_ACCESS_TOKEN

Access tokens can use the mcp scope to restrict access to MCP endpoints only. Add mcp:write for the Agentic Batch Changes tools that start or change an agent.

Agentic Batch Changes

The Agentic Batch Changes MCP server at /.api/mcp/abc lists its read tools with the mcp scope. Starting agents and approving their publications also needs mcp:write, plus the Batch Changes and Agent write permissions. OAuth clients request mcp and mcp:write when they connect to /.api/mcp/abc; the other MCP endpoints request only mcp. For unattended starts and what revoking access leaves running, see Authentication and tool access and Security.

Troubleshooting

Most MCP connection issues come from OAuth or access-control configuration rather than the MCP endpoint itself.

If you seeLikely causeWhat to check
invalid_scope during OAuth sign-inThe OAuth client requested a scope it is not configured to useFor Agentic Batch Changes, see Agentic Batch Changes sign-in fails with invalid_scope. For other MCP connections, request the mcp scope. DCR clients cannot request general API scopes such as user:all. For an integration that also needs GraphQL access, create a pre-registered OAuth App configured with both mcp and user:all.
redirect_uri_mismatch or a token exchange failure after browser authorizationThe OAuth client is missing the callback URL expected by the MCP clientRegister the exact redirect URI your client uses. For the mcp-remote configuration above, add http://localhost:3334/oauth/callback to the OAuth App.
/.auth/idp/oauth/register returns 404 not foundDynamic Client Registration is disabledConfirm mcp.enabled and auth.idpDynamicClientRegistrationEnabled are both true. To keep MCP available without DCR, use a pre-registered OAuth client instead.
Users must sign in again frequently after the integration previously workedThe client is not refreshing tokens correctlyOAuth access tokens expire after one hour. Store the newest refresh_token returned by each refresh because refresh tokens are one-time use. M2M flows do not return refresh tokens; request a new access token when the current one expires.
403 forbidden from /.api/mcpThe token lacks the mcp scope or the user lacks MCP accessConfirm the OAuth client is configured with mcp and the user has the MCP#ACCESS permission. If MCP is disabled, the endpoint returns 404 instead.

Agentic Batch Changes sign-in fails with invalid_scope

An OAuth client registered with only mcp fails when it asks for mcp:write. Until it signs in again, its cached mcp credential lists only the read tools.

For Claude Code, log out before logging in again so it registers with both scopes:

SHELL
claude mcp logout <name> && claude mcp login <name>

A plain claude mcp login <name> can fail once with invalid_scope; run it again.

For a pre-registered OAuth app, a site admin adds mcp:write to its scopes in Administration > OAuth clients > Edit. Users still approve write access on the consent screen.

Previous
MCP Server
Next
Client integrations

On this page

  1. MCP Authentication

    1. OAuth with Dynamic Client Registration
    1. OAuth with a Pre-registered Client
    1. Disable Dynamic Client Registration
    1. Access Tokens
    1. Agentic Batch Changes
    1. Troubleshooting

Edit this page on GitHub
View as Markdown