Skip to content

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

mcp: let a pre-registered app return to an https redirect URI

koeriadb92fd885272
diff --git a/docs/custom-mcp-servers.md b/docs/custom-mcp-servers.md
index 3baafaf953..ca9374f556 100644
--- a/docs/custom-mcp-servers.md
+++ b/docs/custom-mcp-servers.md
@@ -221,6 +221,17 @@ the server's address. Sign-in with a registered app always uses that port; if
another program holds it, sign-in stops and says so. Changing the client ID
or the scopes signs the server out; a new secret for the same app does not.
+When you sign in from a browser on another computer, a return to
+`127.0.0.1` lands on the wrong machine and only the paste-back step is left.
+If the provider lets the app register an https address instead, set
+**Redirect URI** in the app's entry in `config.json` to this server's public
+https address at `/mcp-oauth/callback` (for example
+`https://maus.example/mcp-oauth/callback`) — the same address the editor
+shows once it is set. The sign-in then returns straight to that page and
+says **Signed in**, with nothing to paste; a browser on this computer uses
+the same address. Only that exact address is accepted, over https, with no
+query, credentials or fragment.
+
### Example: give your bots web search
A good first URL server is You.com's search server, because the free profile
diff --git a/server/host/mcp-chat.ts b/server/host/mcp-chat.ts
index a6a16a6dff..6826360be8 100644
--- a/server/host/mcp-chat.ts
+++ b/server/host/mcp-chat.ts
@@ -132,7 +132,7 @@ export function createMcpChat(deps: McpChatDeps) {
const servers = listMcpServers(cfg.mcpServers).map((server) => {
if (!("url" in server)) return server;
const auth = mcpOAuth.authState(server.name, server.url);
- const listed = server.oauth ? { ...server, oauth: { ...server.oauth, redirectUri: mcpOAuthRedirectUri(server.url) } } : server;
+ const listed = server.oauth ? { ...server, oauth: { ...server.oauth, redirectUri: server.oauth.redirectUri ?? mcpOAuthRedirectUri(server.url) } } : server;
return auth === "none" ? listed : { ...listed, auth };
}).map(mcpServersHost.listing).map((server) => {
const managedBy = mcpManagedBy(server.name, server);
diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts
index 0bed8ffeff..1ad5247843 100644
--- a/server/mcp-oauth.test.ts
+++ b/server/mcp-oauth.test.ts
@@ -832,6 +832,20 @@ describe("McpOAuthManager sign-in from a browser on another computer", () => {
expect(oauth.counts.register).toBe(0);
});
+ it("returns a configured app to its configured https address, on any browser, with no loopback listener", async () => {
+ await setup({ noRegistration: true, preRegistered: { corp: null } });
+ registered = { clientId: "corp", redirectUri: "https://cloud.example/mcp-oauth/callback" };
+ const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
+ expect(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")).toBe("https://cloud.example/mcp-oauth/callback");
+ expect(started.pasteBack).toBeUndefined();
+ expect(oauth.counts.register).toBe(0);
+ expect(await portFree(Number(new URL(mcpOAuthRedirectUri(mcp.url)).port))).toBe(true);
+ const callback = await returnAddress(started);
+ expect((await manager.publicCallback("cloud.example", callback.search)).status).toBe(200);
+ expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("succeeded");
+ expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
+ });
+
it("asks a browser on this machine to paste nothing", async () => {
await setup();
const started = await manager.start("docs", mcp.url, undefined, "loopback", { remote: false, origin: "https://cloud.example" });
diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts
index a79c350035..e397478a41 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -573,6 +573,18 @@ export class McpOAuthManager {
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);
+ /** 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. */
+ if (registered?.redirectUri) {
+ const client: ClientAuth = {
+ id: registered.clientId,
+ ...(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 };
+ }
+
if (!registered && starting.browser.remote && starting.browser.origin) {
const redirectUri = `${starting.browser.origin}${CALLBACK_PATH}`;
let client: ClientAuth | null = null;
diff --git a/server/mcp-registry.test.ts b/server/mcp-registry.test.ts
index da347f5b0a..e4246a24f6 100644
--- a/server/mcp-registry.test.ts
+++ b/server/mcp-registry.test.ts
@@ -303,6 +303,20 @@ describe("a url server's sign-in app", () => {
expect(JSON.stringify(listings)).not.toContain("app-secret-value");
});
+ 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,
+ server: { type: "http", url, headers: {}, oauth: { clientId: "corp-app", redirectUri: "https://docs.example/mcp-oauth/callback" }, enabled: true },
+ });
+ expect(parseStoredMcpServer("docs", { url, oauth: { clientId: "corp-app", redirectUri: "http://127.0.0.1:20000/mcp-oauth/callback" } })).toMatchObject({
+ ok: false, error: expect.stringMatching(/must be an https address/),
+ });
+ expect(parseStoredMcpServer("docs", { url, oauth: { clientId: "corp-app", redirectUri: "https://docs.example/other" } })).toMatchObject({
+ ok: false, error: expect.stringMatching(/must be an https address/),
+ });
+ expect(parseStoredMcpServer("docs", { url, oauth: { clientId: "corp-app", redirectUri: "https://user:[email protected]/mcp-oauth/callback" } })).toMatchObject({ ok: false });
+ });
+
it("keeps a saved secret behind a placeholder only for the same client id", () => {
expect(parseMcpServerMutation("docs", { url, oauth: { clientId: "corp-app", clientSecret: true } }, saved)).toEqual({
ok: true,
diff --git a/server/mcp-registry.ts b/server/mcp-registry.ts
index 825c2d429f..e6935ffc59 100644
--- a/server/mcp-registry.ts
+++ b/server/mcp-registry.ts
@@ -34,6 +34,23 @@ export interface McpOAuthClientConfig {
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
+ * return a browser on another computer can reach. Absent: the loopback address, pasted back. */
+ redirectUri?: string;
+}
+
+/** The return address a configured app may name: this server's https address, at the callback path. */
+export function mcpConfiguredRedirectError(value: string): string | null {
+ let url: URL;
+ try {
+ url = new URL(value);
+ } catch {
+ return "The redirect URI must be an https address ending in /mcp-oauth/callback.";
+ }
+ if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash || url.pathname !== "/mcp-oauth/callback") {
+ return "The redirect URI must be an https address ending in /mcp-oauth/callback.";
+ }
+ return null;
}
export interface StoredRemoteMcpServer extends RemoteMcpSpec {
enabled: boolean;
@@ -67,7 +84,7 @@ export interface RemoteMcpServerListing {
enabled: boolean;
resultBudget?: number;
apps: boolean;
- oauth?: { clientId: string; scopes: string[]; clientSecretConfigured: boolean };
+ oauth?: { clientId: string; scopes: string[]; clientSecretConfigured: boolean; redirectUri?: string };
runsOn?: McpRunsOn;
bot?: string;
}
@@ -232,6 +249,7 @@ const oauthClientSchema = z.object({
.regex(CLIENT_ID, "A client ID is printable text on one line."),
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(),
}).strict();
const remoteEntrySchema = z.object({
@@ -271,9 +289,18 @@ function resolveOAuthClient(
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 };
+ }
return {
ok: true,
- value: { clientId: incoming.clientId, ...(clientSecret ? { clientSecret } : {}), ...(scopes.length ? { scopes } : {}) },
+ value: {
+ clientId: incoming.clientId,
+ ...(clientSecret ? { clientSecret } : {}),
+ ...(scopes.length ? { scopes } : {}),
+ ...(incoming.redirectUri ? { redirectUri: incoming.redirectUri } : {}),
+ },
};
}
@@ -474,7 +501,12 @@ export function listMcpServers(raw: Record<string, unknown> | undefined): McpSer
...(server.resultBudget !== undefined ? { resultBudget: server.resultBudget } : {}),
apps: server.apps !== false,
...(server.oauth ? {
- oauth: { clientId: server.oauth.clientId, scopes: server.oauth.scopes ?? [], clientSecretConfigured: Boolean(server.oauth.clientSecret) },
+ oauth: {
+ clientId: server.oauth.clientId,
+ scopes: server.oauth.scopes ?? [],
+ clientSecretConfigured: Boolean(server.oauth.clientSecret),
+ ...(server.oauth.redirectUri ? { redirectUri: server.oauth.redirectUri } : {}),
+ },
} : {}),
...(server.runsOn ? { runsOn: server.runsOn } : {}),
...(server.bot ? { bot: server.bot } : {}),