Skip to content

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

fix(mcp): keep OAuth scopes typed without a client ID and send them in sign-in

AdityaUmale8f0a3e13081f
diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts
index b0d7a3577b..aafbcd3bb8 100644
--- a/server/mcp-oauth.test.ts
+++ b/server/mcp-oauth.test.ts
@@ -430,6 +430,16 @@ describe("a sign-in app registered in advance", () => {
expect(new URL(started.authorizationUrl!).searchParams.get("scope")).toBe("mcp");
});
+ it("registers itself with the scopes typed without a client ID", async () => {
+ await setup({ offlineAccess: true });
+ registered = { scopes: ["api://mcp/read"] };
+ const started = await manager.start("docs", mcp.url);
+ expect(new URL(started.authorizationUrl!).searchParams.get("scope")).toBe("api://mcp/read");
+ expect(oauth.counts.register).toBe(1);
+ await approve(started);
+ expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
+ });
+
it("uses the app in place of registration even when the server offers it", async () => {
await setup({ preRegistered: { mine: null } });
registered = { clientId: "mine" };
diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts
index 8c976a6748..a147505210 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -579,7 +579,9 @@ export class McpOAuthManager {
if (previous) this.finish(previous, "cancelled", "Sign-in cancelled.");
const meta = await discoverMcpAuth(url, wwwAuthenticate ?? await this.challenge(url));
if (!meta) throw new McpOAuthError("not-oauth", "This server does not offer an OAuth sign-in. Add its token as a header instead.");
- const registered = this.clients(name, url);
+ const configured = this.clients(name, url);
+ const registered = configured?.clientId ? { ...configured, clientId: configured.clientId } : undefined;
+ const scopes = configured?.scopes?.length ? configured.scopes : meta.scopes;
/** An app with a configured return address always returns there: it is the one address the provider has registered,
* and the only one a browser on another computer can reach. The public route completes it, so no listener runs. */
@@ -589,7 +591,6 @@ export class McpOAuthManager {
...(registered.clientSecret ? { secret: registered.clientSecret } : {}),
method: tokenAuthMethod(meta, registered.clientSecret),
};
- const scopes = registered.scopes?.length ? registered.scopes : meta.scopes;
return { ...this.open(name, url, starting, meta, registered.redirectUri, client, null, scopes).status };
}
@@ -601,7 +602,7 @@ export class McpOAuthManager {
} catch (error) {
if (!(error instanceof RegistrationRefused)) throw error;
}
- if (client) return { ...this.open(name, url, starting, meta, redirectUri, client, null, meta.scopes).status };
+ if (client) return { ...this.open(name, url, starting, meta, redirectUri, client, null, scopes).status };
}
const listener = await this.listener(preferredPort(url), registered !== undefined);
@@ -610,7 +611,6 @@ export class McpOAuthManager {
const client: ClientAuth = registered
? { id: registered.clientId, ...(registered.clientSecret ? { secret: registered.clientSecret } : {}), method: tokenAuthMethod(meta, registered.clientSecret) }
: await this.register(meta, redirectUri);
- const scopes = registered?.scopes?.length ? registered.scopes : meta.scopes;
const flow = this.open(name, url, starting, meta, redirectUri, client, listener, scopes);
listener.reserved -= 1;
listener.flows.add(flow);
diff --git a/server/mcp-registry.test.ts b/server/mcp-registry.test.ts
index 59cc7c8dd3..3593141539 100644
--- a/server/mcp-registry.test.ts
+++ b/server/mcp-registry.test.ts
@@ -303,6 +303,18 @@ describe("a url server's sign-in app", () => {
expect(JSON.stringify(listings)).not.toContain("app-secret-value");
});
+ it("stores scopes typed without a client id, and refuses a secret or return address without one", () => {
+ expect(parseStoredMcpServer("docs", { url, oauth: { scopes: ["api://mcp/read"] } })).toEqual({
+ ok: true,
+ server: { type: "http", url, headers: {}, oauth: { scopes: ["api://mcp/read"] }, enabled: true },
+ });
+ expect(listMcpServers({ docs: { type: "http", url, headers: {}, enabled: true, oauth: { scopes: ["api://mcp/read"] } } })[0]).toMatchObject({
+ oauth: { scopes: ["api://mcp/read"], clientSecretConfigured: false },
+ });
+ expect(parseStoredMcpServer("docs", { url, oauth: { clientSecret: "s", scopes: ["a"] } })).toMatchObject({ ok: false });
+ expect(parseStoredMcpServer("docs", { url, oauth: { redirectUri: "https://docs.example/mcp-oauth/callback", scopes: ["a"] } })).toMatchObject({ ok: false });
+ });
+
it("keeps a configured redirect uri for an app registered on an https address, and refuses other addresses", () => {
expect(parseStoredMcpServer("docs", { url, oauth: { clientId: "corp-app", redirectUri: " https://docs.example/mcp-oauth/callback " } })).toEqual({
ok: true,
diff --git a/server/mcp-registry.ts b/server/mcp-registry.ts
index d1295ba21d..da2217dd71 100644
--- a/server/mcp-registry.ts
+++ b/server/mcp-registry.ts
@@ -30,9 +30,10 @@ export interface StoredStdioMcpServer extends StdioMcpSpec {
/** "Show its apps in chat": stored only as `false`; on by default. */
apps?: false;
}
-/** An app pre-registered with the server's authorization server; its secret is a credential, never listed. */
+/** An app pre-registered with the server's authorization server; its secret is a credential, never listed. Without a
+ * client ID it holds only the scopes the sign-in asks for, and the app registers itself. */
export interface McpOAuthClientConfig {
- clientId: string;
+ clientId?: string;
clientSecret?: string;
scopes?: string[];
/** Where the app is registered to return, when it is an https address rather than this computer's loopback: the only
@@ -85,7 +86,7 @@ export interface RemoteMcpServerListing {
enabled: boolean;
resultBudget?: number;
apps: boolean;
- oauth?: { clientId: string; scopes: string[]; clientSecretConfigured: boolean; redirectUri?: string };
+ oauth?: { clientId?: string; scopes: string[]; clientSecretConfigured: boolean; redirectUri?: string };
runsOn?: McpRunsOn;
bot?: string;
}
@@ -250,8 +251,8 @@ const SCOPE = /^[\x21\x23-\x5b\x5d-\x7e]+$/;
const CLIENT_SECRET_MAX = 4_096;
const oauthClientSchema = z.object({
- clientId: z.string().trim().min(1, "Enter the client ID of the app registered with this server's sign-in provider.").max(512)
- .regex(CLIENT_ID, "A client ID is printable text on one line."),
+ clientId: z.string().trim().max(512)
+ .refine((value) => !value || CLIENT_ID.test(value), "A client ID is printable text on one line.").optional(),
clientSecret: z.string().min(1).max(CLIENT_SECRET_MAX).regex(CLIENT_ID, "A client secret is printable text on one line.").optional(),
scopes: z.array(z.string().max(256).regex(SCOPE, "Scopes are single words, like offline_access or api://my-app/mcp.read.")).max(32).optional(),
redirectUri: z.string().trim().max(2_048).optional(),
@@ -284,6 +285,13 @@ function resolveOAuthClient(
saved: McpOAuthClientConfig | undefined,
): { ok: true; value: McpOAuthClientConfig | undefined } | { ok: false; error: string } {
if (!incoming) return { ok: true, value: undefined };
+ const scopes = [...new Set(incoming.scopes ?? [])];
+ if (!incoming.clientId) {
+ if (!scopes.length || incoming.clientSecret !== undefined || incoming.redirectUri) {
+ return { ok: false, error: "Enter the client ID of the app registered with this server's sign-in provider." };
+ }
+ return { ok: true, value: { scopes } };
+ }
let clientSecret: string | undefined;
if (incoming.clientSecret === true) {
if (!saved?.clientSecret || saved.clientId !== incoming.clientId) {
@@ -293,7 +301,6 @@ function resolveOAuthClient(
} else {
clientSecret = incoming.clientSecret;
}
- const scopes = [...new Set(incoming.scopes ?? [])];
if (incoming.redirectUri !== undefined) {
const redirectError = mcpConfiguredRedirectError(incoming.redirectUri);
if (redirectError) return { ok: false, error: redirectError };
@@ -511,7 +518,7 @@ export function listMcpServers(raw: Record<string, unknown> | undefined): McpSer
apps: server.apps !== false,
...(server.oauth ? {
oauth: {
- clientId: server.oauth.clientId,
+ ...(server.oauth.clientId ? { clientId: server.oauth.clientId } : {}),
scopes: server.oauth.scopes ?? [],
clientSecretConfigured: Boolean(server.oauth.clientSecret),
...(server.oauth.redirectUri ? { redirectUri: server.oauth.redirectUri } : {}),
diff --git a/src/components/McpServersPanel.test.ts b/src/components/McpServersPanel.test.ts
index 2d316ab746..a396091099 100644
--- a/src/components/McpServersPanel.test.ts
+++ b/src/components/McpServersPanel.test.ts
@@ -73,13 +73,15 @@ describe("MCP server form", () => {
expect(draftBody({ ...draft, url: "https://<tenant>.example.com/mcp" }, undefined)).toEqual({ ok: false, error: { key: "mcp.paste.urlMissing" } });
});
- it("sends a sign-in app only with a client id, keeping a saved secret behind a blank", () => {
+ it("sends a sign-in app with a client id, or just the scopes typed without one, keeping a saved secret behind a blank", () => {
const blank = { clientId: "", clientSecret: "", scopes: "", forgetSecret: false };
expect(parseMcpOAuthClient(blank)).toBeUndefined();
expect(parseMcpOAuthClient({ ...blank, clientId: " corp-app ", scopes: "offline_access, api://x/read " })).toEqual({
clientId: "corp-app",
scopes: ["offline_access", "api://x/read"],
});
+ expect(parseMcpOAuthClient({ ...blank, scopes: " offline_access api://x/read" })).toEqual({ scopes: ["offline_access", "api://x/read"] });
+ expect(parseMcpOAuthClient({ ...blank, clientSecret: "orphan" }, { clientSecretConfigured: true })).toBeUndefined();
expect(parseMcpOAuthClient({ ...blank, clientId: "corp-app" }, { clientSecretConfigured: true })).toEqual({ clientId: "corp-app", clientSecret: true });
expect(parseMcpOAuthClient({ ...blank, clientId: "corp-app", forgetSecret: true }, { clientSecretConfigured: true })).toEqual({ clientId: "corp-app" });
expect(parseMcpOAuthClient({ ...blank, clientId: "corp-app", clientSecret: "new" }, { clientSecretConfigured: true })).toEqual({ clientId: "corp-app", clientSecret: "new" });
diff --git a/src/components/McpServersPanel.tsx b/src/components/McpServersPanel.tsx
index 909d880dd4..2fe03c90d4 100644
--- a/src/components/McpServersPanel.tsx
+++ b/src/components/McpServersPanel.tsx
@@ -734,7 +734,7 @@ export function McpServersPanel({ embedded = false, hostedCards, hideHosted = fa
{isRemoteMcpListing(server)
? server.headerKeys.length > 0 && <div className="mt-1 truncate text-[11px] text-ink-secondary">{t("mcp.headersSaved", { keys: server.headerKeys.join(", ") })}</div>
: server.envKeys.length > 0 && <div className="mt-1 truncate text-[11px] text-ink-secondary">{t("mcp.secretsSaved", { keys: server.envKeys.join(", ") })}</div>}
- {isRemoteMcpListing(server) && server.oauth && <div className="mt-1 truncate text-[11px] text-ink-secondary">{t(server.oauth.clientSecretConfigured ? "mcp.oauth.savedWithSecret" : "mcp.oauth.saved", { clientId: server.oauth.clientId })}</div>}
+ {isRemoteMcpListing(server) && server.oauth?.clientId && <div className="mt-1 truncate text-[11px] text-ink-secondary">{t(server.oauth.clientSecretConfigured ? "mcp.oauth.savedWithSecret" : "mcp.oauth.saved", { clientId: server.oauth.clientId })}</div>}
</div>
<div data-mcp-row-actions className="ml-auto flex flex-wrap items-center justify-end gap-1">
{signingIn === server.name ? (
diff --git a/src/components/mcp-draft.ts b/src/components/mcp-draft.ts
index ca29083bc3..33e060441e 100644
--- a/src/components/mcp-draft.ts
+++ b/src/components/mcp-draft.ts
@@ -20,7 +20,7 @@ export interface RemoteMcpListing extends McpResultBudgetListing {
oauth?: McpOAuthClientListing;
}
export interface McpOAuthClientListing {
- clientId: string;
+ clientId?: string;
scopes: string[];
clientSecretConfigured: boolean;
redirectUri: string;
@@ -160,11 +160,11 @@ export function parseMcpArguments(value: string): string[] {
export function parseMcpOAuthClient(
input: { clientId: string; clientSecret: string; scopes: string; forgetSecret: boolean },
saved?: { clientSecretConfigured: boolean },
-): { clientId: string; clientSecret?: string | true; scopes?: string[] } | undefined {
+): { clientId?: string; clientSecret?: string | true; scopes?: string[] } | undefined {
const clientId = input.clientId.trim();
- if (!clientId) return undefined;
- const secret = input.clientSecret.trim();
const scopes = input.scopes.split(/[\s,]+/).filter(Boolean);
+ if (!clientId) return scopes.length ? { scopes } : undefined;
+ const secret = input.clientSecret.trim();
return {
clientId,
...(secret ? { clientSecret: secret } : saved?.clientSecretConfigured && !input.forgetSecret ? { clientSecret: true as const } : {}),