Skip to content

Commit d2c61f9

Browse files
fix
1 parent 559b0ad commit d2c61f9

File tree

11 files changed

+46
-30
lines changed

11 files changed

+46
-30
lines changed

docs/docs/features/mcp-server.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Sourcebot MCP uses a [Streamable HTTP](https://modelcontextprotocol.io/specifica
2828

2929
You can read more about the options in the [authorization](#authorization) section.
3030

31+
<Note>
32+
If [anonymous access](https://docs.sourcebot.dev/docs/configuration/auth/access-settings#anonymous-access) is enabled on your Sourcebot instance, no OAuth token or API key is required. You can connect directly to the MCP endpoint without any authorization.
33+
</Note>
34+
3135
<AccordionGroup>
3236
<Accordion title="Claude Code">
3337
[Claude Code MCP docs](https://code.claude.com/docs/en/mcp#connect-claude-code-to-tools-via-mcp)
@@ -273,7 +277,7 @@ You can read more about the options in the [authorization](#authorization) secti
273277

274278
## Authorization
275279

276-
The Sourcebot MCP server supports two authorization methods, OAuth and API keys.
280+
The Sourcebot MCP server supports two authorization methods, OAuth and API keys. If [anonymous access](/docs/configuration/auth/access-settings#anonymous-access) is enabled on your instance, no authorization is required.
277281

278282
Regardless of which method you use, all MCP requests are scoped to the associated Sourcebot user and inherit the [user's role and permissions](/docs/configuration/auth/roles-and-permissions). When [permission syncing](/docs/features/permission-syncing) is configured, this includes repository permissions - the MCP server will only surface results from repositories the user has access to.
279283

packages/web/next.config.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ const nextConfig = {
3737
source: "/.well-known/oauth-protected-resource/:path*",
3838
destination: "/api/ee/.well-known/oauth-protected-resource/:path*",
3939
},
40+
// Non-spec fallback: some MCP clients (observed in Claude Code and Cursor) guess
41+
// /register as the Dynamic Client Registration endpoint (RFC 7591) when OAuth
42+
// Authorization Server Metadata discovery fails entirely. This happens when the
43+
// instance does not hold an enterprise license, causing all well-known endpoints
44+
// to return 404. Per the MCP spec, clients should only attempt Dynamic Client
45+
// Registration if the authorization server advertises a registration_endpoint in
46+
// its metadata — guessing the URL is not spec-compliant behavior. This rewrite
47+
// ensures those requests reach the actual endpoint rather than hitting a 404.
48+
{
49+
source: "/register",
50+
destination: "/api/ee/oauth/register",
51+
}
4052
];
4153
},
4254
// This is required to support PostHog trailing slash API requests
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ErrorCode } from "@/lib/errorCodes"
2+
import { ServiceError } from "@/lib/serviceError"
3+
import { StatusCodes } from "http-status-codes"
4+
import { NextResponse } from "next/server"
5+
6+
// Repeat for other methods or use a handler:
7+
const handler = () => NextResponse.json(
8+
{
9+
statusCode: StatusCodes.NOT_FOUND,
10+
errorCode: ErrorCode.NOT_FOUND,
11+
message: "This API endpoint does not exist",
12+
} satisfies ServiceError,
13+
{ status: 404 }
14+
)
15+
16+
export { handler as GET, handler as POST, handler as PUT, handler as PATCH, handler as DELETE }

packages/web/src/app/api/(server)/ee/.well-known/oauth-authorization-server/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { apiHandler } from '@/lib/apiHandler';
22
import { env, hasEntitlement } from '@sourcebot/shared';
3+
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
34

45
// RFC 8414: OAuth 2.0 Authorization Server Metadata
56
// @see: https://datatracker.ietf.org/doc/html/rfc8414
67
export const GET = apiHandler(async () => {
78
if (!hasEntitlement('oauth')) {
89
return Response.json(
9-
{ error: 'not_found', error_description: 'OAuth authorization server metadata is not available on this plan.' },
10+
{ error: 'not_found', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
1011
{ status: 404 }
1112
);
1213
}

packages/web/src/app/api/(server)/ee/.well-known/oauth-protected-resource/[...path]/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { apiHandler } from '@/lib/apiHandler';
22
import { env, hasEntitlement } from '@sourcebot/shared';
33
import { NextRequest } from 'next/server';
4+
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
45

56
// RFC 9728: OAuth 2.0 Protected Resource Metadata (path-specific form)
67
// For a resource at /api/mcp, the well-known URI is /.well-known/oauth-protected-resource/api/mcp.
@@ -12,7 +13,7 @@ const PROTECTED_RESOURCES = new Set([
1213
export const GET = apiHandler(async (_request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) => {
1314
if (!hasEntitlement('oauth')) {
1415
return Response.json(
15-
{ error: 'not_found', error_description: 'OAuth protected resource metadata is not available on this plan.' },
16+
{ error: 'not_found', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
1617
{ status: 404 }
1718
);
1819
}

packages/web/src/app/api/(server)/ee/.well-known/oauth-protected-resource/route.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/web/src/app/api/(server)/ee/oauth/register/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { prisma } from '@/prisma';
44
import { hasEntitlement } from '@sourcebot/shared';
55
import { NextRequest } from 'next/server';
66
import { z } from 'zod';
7+
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
78

89
// RFC 7591: OAuth 2.0 Dynamic Client Registration
910
// @see: https://datatracker.ietf.org/doc/html/rfc7591
@@ -16,7 +17,7 @@ const registerRequestSchema = z.object({
1617
export const POST = apiHandler(async (request: NextRequest) => {
1718
if (!hasEntitlement('oauth')) {
1819
return Response.json(
19-
{ error: 'access_denied', error_description: 'OAuth is not available on this plan. Please see https://sourcebot.dev/pricing' },
20+
{ error: 'access_denied', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
2021
{ status: 403 }
2122
);
2223
}

packages/web/src/app/api/(server)/ee/oauth/revoke/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { revokeToken } from '@/ee/features/oauth/server';
22
import { apiHandler } from '@/lib/apiHandler';
33
import { hasEntitlement } from '@sourcebot/shared';
44
import { NextRequest } from 'next/server';
5+
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
56

67
// RFC 7009: OAuth 2.0 Token Revocation
78
// Always returns 200 regardless of whether the token existed.
89
// @see: https://datatracker.ietf.org/doc/html/rfc7009
910
export const POST = apiHandler(async (request: NextRequest) => {
1011
if (!hasEntitlement('oauth')) {
1112
return Response.json(
12-
{ error: 'access_denied', error_description: 'OAuth is not available on this plan. Please see https://sourcebot.dev/pricing' },
13+
{ error: 'access_denied', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
1314
{ status: 403 }
1415
);
1516
}

packages/web/src/app/api/(server)/ee/oauth/token/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { verifyAndExchangeCode, verifyAndRotateRefreshToken, ACCESS_TOKEN_TTL_SE
22
import { apiHandler } from '@/lib/apiHandler';
33
import { hasEntitlement } from '@sourcebot/shared';
44
import { NextRequest } from 'next/server';
5+
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
56

67
// OAuth 2.0 Token Endpoint
78
// Supports grant_type=authorization_code with PKCE (RFC 7636).
89
// @see: https://datatracker.ietf.org/doc/html/rfc6749#section-3.2
910
export const POST = apiHandler(async (request: NextRequest) => {
1011
if (!hasEntitlement('oauth')) {
1112
return Response.json(
12-
{ error: 'access_denied', error_description: 'OAuth is not available on this plan. Please see https://sourcebot.dev/pricing' },
13+
{ error: 'access_denied', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
1314
{ status: 403 }
1415
);
1516
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export const OAUTH_NOT_SUPPORTED_ERROR_MESSAGE = 'OAuth is not supported on this instance. Please authenticate using a Sourcebot API key instead. See https://docs.sourcebot.dev/docs/features/mcp-server for more information.';

0 commit comments

Comments
 (0)