Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mcp): register as a confidential client when the server can't sign in public ones

AdityaUmalee21999db2669
diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts
index 1ad5247843..b0d7a3577b 100644
--- a/server/mcp-oauth.test.ts
+++ b/server/mcp-oauth.test.ts
@@ -354,6 +354,28 @@ describe("review fixes", () => {
expect(stored.clientSecret).toMatch(/^secret_/);
});
+ it("asks to be a confidential client when the server's methods leave out none", async () => {
+ await setup({ noPublicClients: true, tokenAuthMethods: ["client_secret_basic", "client_secret_post"] });
+ const started = await manager.start("docs", mcp.url);
+ await approve(started);
+ expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
+ expect(oauth.lastRegistration?.token_endpoint_auth_method).toBe("client_secret_basic");
+ expect(oauth.clientAuths).toEqual([{ method: "basic", clientId: "client_1" }]);
+ expect(manager.authState("docs", mcp.url)).toBe("signed-in");
+ });
+
+ it.each([
+ ["no list", undefined],
+ ["a list with none", ["none", "client_secret_basic"]],
+ ])("still asks to be public when the server advertises %s", async (_, tokenAuthMethods) => {
+ await setup(tokenAuthMethods ? { tokenAuthMethods } : {});
+ const started = await manager.start("docs", mcp.url);
+ await approve(started);
+ expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
+ expect(oauth.lastRegistration?.token_endpoint_auth_method).toBe("none");
+ expect(oauth.clientAuths).toEqual([{ method: "none", clientId: "client_1" }]);
+ });
+
it("registers a fresh client for every sign-in", async () => {
await setup();
for (let i = 0; i < 2; i++) {
diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts
index e27cd6e705..8c976a6748 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -147,6 +147,12 @@ function tokenAuthMethod(meta: McpAuthMetadata, secret: string | undefined): Tok
: "client_secret_post";
}
+/** RFC 7591 §2: a server whose list leaves out "none" can't sign in a public client, so ask it for a secret. */
+function registrationAuthMethod(meta: McpAuthMetadata): TokenAuthMethod {
+ const supported = meta.tokenEndpointAuthMethods;
+ return !supported || supported.includes("none") ? "none" : tokenAuthMethod(meta, "requested");
+}
+
/** RFC 6749 §2.3.1: Basic credentials are form-encoded first. */
const formEncoded = (value: string) => new URLSearchParams({ v: value }).toString().slice(2);
@@ -532,7 +538,7 @@ export class McpOAuthManager {
}
/** A fresh client per sign-in: authorization servers prune idle clients, so a cached id goes stale. It asks to be
- * public; a server that issues a secret anyway (Supabase) gets it back on every token request. */
+ * public when the server allows that; a server that issues a secret (Supabase) gets it back on every token request. */
private async register(meta: McpAuthMetadata, redirectUri: string): Promise<ClientAuth> {
if (!meta.registrationEndpoint) {
throw new McpOAuthError("no-registration", meta.clientIdMetadataDocument
@@ -547,7 +553,7 @@ export class McpOAuthManager {
redirect_uris: [redirectUri],
grant_types: ["authorization_code", "refresh_token"],
response_types: ["code"],
- token_endpoint_auth_method: "none",
+ token_endpoint_auth_method: registrationAuthMethod(meta),
}),
redirect: "error",
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
diff --git a/server/testing/fake-oauth-server.ts b/server/testing/fake-oauth-server.ts
index 07a6db9801..845ee585e8 100644
--- a/server/testing/fake-oauth-server.ts
+++ b/server/testing/fake-oauth-server.ts
@@ -38,6 +38,8 @@ export interface FakeOAuthOptions {
tokenAuthMethods?: string[];
/** registration issues a secret with this method, whatever the client asked for, and the token endpoint requires it */
registerConfidential?: "client_secret_basic" | "client_secret_post";
+ /** registration issues a secret only when asked for client_secret_basic or client_secret_post; the token endpoint refuses a public client */
+ noPublicClients?: boolean;
/** Require offline_access to issue a refresh token and advertise it only on the authorization server. */
offlineAccess?: boolean;
}
@@ -57,6 +59,8 @@ export interface FakeOAuth {
mint(): string;
counts: { register: number; token: number; refresh: number; revoke: number };
lastAuthorize: URLSearchParams | null;
+ /** the body of the latest client registration */
+ lastRegistration: { token_endpoint_auth_method?: string } | null;
/** every /token and /revoke client identification, in order */
clientAuths: FakeClientAuth[];
options: FakeOAuthOptions;
@@ -80,7 +84,7 @@ export async function startFakeOAuth(options: FakeOAuthOptions = {}): Promise<Fa
const counts = { register: 0, token: 0, refresh: 0, revoke: 0 };
let issuer = "";
const clientAuths: FakeClientAuth[] = [];
- const fake: Partial<FakeOAuth> = { lastAuthorize: null, clientAuths, options };
+ const fake: Partial<FakeOAuth> = { lastAuthorize: null, lastRegistration: null, clientAuths, options };
const preRegistered = { ...options.preRegistered };
/** The authenticated client, or null when its secret is missing or wrong (RFC 6749 §2.3: one method per request). */
@@ -105,7 +109,7 @@ export async function startFakeOAuth(options: FakeOAuthOptions = {}): Promise<Fa
const expected = preRegistered[auth.clientId];
return expected === null ? (secret === null ? auth.clientId : null) : (secret === expected ? auth.clientId : null);
}
- return clients.has(auth.clientId) && secret === null ? auth.clientId : null;
+ return clients.has(auth.clientId) && secret === null && !options.noPublicClients ? auth.clientId : null;
};
const issue = (offline = true) => {
@@ -144,13 +148,17 @@ export async function startFakeOAuth(options: FakeOAuthOptions = {}): Promise<Fa
res.writeHead(options.registerRefusal.status, { "content-type": "application/json", ...options.registerRefusal.headers });
return res.end(JSON.stringify(options.registerRefusal.body));
}
- const body = JSON.parse(await readBody(req)) as { redirect_uris?: string[] };
+ const body = JSON.parse(await readBody(req)) as { redirect_uris?: string[]; token_endpoint_auth_method?: string };
if (body.redirect_uris?.some((uri) => options.refuseRedirect?.(uri))) return json(res, 400, { error: "invalid_redirect_uri" });
const clientId = `client_${counts.register}`;
- if (options.registerConfidential) {
+ fake.lastRegistration = body;
+ const asked = body.token_endpoint_auth_method;
+ const confidential = options.registerConfidential
+ ?? (options.noPublicClients && (asked === "client_secret_basic" || asked === "client_secret_post") ? asked : undefined);
+ if (confidential) {
const secret = `secret_${randomBytes(8).toString("hex")}`;
preRegistered[clientId] = secret;
- return json(res, 201, { client_id: clientId, client_secret: secret, token_endpoint_auth_method: options.registerConfidential, redirect_uris: body.redirect_uris });
+ return json(res, 201, { client_id: clientId, client_secret: secret, token_endpoint_auth_method: confidential, redirect_uris: body.redirect_uris });
}
clients.add(clientId);
return json(res, 201, { client_id: clientId, redirect_uris: body.redirect_uris });