import type { OAuthMetadata } from "@modelcontextprotocol/server"; import type { OAuthUpstreamToken } from "../../context.js"; import { type OAuthProvider, type OAuthResourceOptions } from "../provider.js"; import { type OAuthProxyClient } from "./clients.js"; import type { ConsentPageProps } from "./consent.js"; import { type OAuthProxyStorage } from "./storage.js"; import { type Upstream, type UpstreamUser } from "./upstream.js"; /** Consent page settings for {@link OAuthProxyOptions.consent}. */ export interface OAuthProxyConsentOptions { /** * When the consent page appears. `"always"` asks on every sign-in. * `"remember"` asks once per browser for each client and HTTPS redirect * URI; loopback and custom-scheme redirects still ask every time, because * any local program can claim them. * * @defaultValue `"always"` */ mode?: "always" | "remember"; /** * React component that replaces the built-in consent page. It is rendered * on the server with `react-dom/server`, so it needs the `react` and * `react-dom` packages. See {@link ConsentPageProps} for the form it must * render. */ page?: (props: ConsentPageProps) => unknown; } /** * Options for {@link oauthProxy}. * * @typeParam TUser - Application user type exposed as `ctx.auth.user`. */ export interface OAuthProxyOptions extends OAuthResourceOptions { /** * The identity provider users sign in with, for example * `github({ clientId, clientSecret })`. */ upstream: Upstream; /** * Secret from which the proxy derives its encryption and cookie keys: a * random string of at least 32 characters, for example from * `openssl rand -base64 32`. Pass an array to rotate: the first secret * encrypts and signs, and the others still decrypt and verify. * * Defaults to the `MCP_USE_OAUTH_SECRET` environment variable. Required * unless the server runs with `listen()` on a localhost resource and a * loopback address (`127.0.0.1`, `localhost`, or `::1`), where a random * key is used and sign-ins end when the process stops. `mcp-use build` * does not need it. */ secret?: string | readonly string[]; /** * Shared store for clients, sign-ins, grants, and tokens. Required unless * the server runs with `listen()` on a localhost resource and a loopback * address (`127.0.0.1`, `localhost`, or `::1`), where an in-memory store is * used. `mcp-use build` does not need it. See `mcp-use/oauth/storage` for * adapters. */ storage?: OAuthProxyStorage; /** * The consent page that the proxy shows before it sends the user to the * upstream provider. It defends against a malicious client that reuses * an earlier upstream approval (the confused-deputy attack). Set `false` * only for local development: it is refused outside localhost, and on a * `listen()` that binds beyond a loopback address. * * @defaultValue `{ mode: "always" }` */ consent?: false | OAuthProxyConsentOptions; /** * The MCP clients that may connect. When unset, any client can connect: * with a client ID metadata document (an HTTPS URL as its `client_id`, * which the MCP specification prefers) or, while `dcr` is on, through * dynamic client registration. When set, only these clients can connect * and dynamic client registration is off: * * - a metadata document URL allows that one client, for example * `"https://claude.ai/oauth/mcp-client-metadata.json"`; * - an HTTPS origin allows every metadata document on that origin, for * example `"https://chatgpt.com"`; * - an {@link OAuthProxyClient} defines a client by hand, for a client * that can use neither a metadata document nor registration. * * Listing metadata documents also closes the small DNS-rebinding window * that runtimes other than Node.js and Bun leave when fetching them (see * `fetch`). * * Removing a client ends its existing sign-ins for good: the next request * with one of its tokens deletes the grant. This list controls which * apps connect, not which users sign in: any user of the upstream * provider can sign in through an allowed app. Check the user in * `mapUser` or in your tools to restrict users. */ clients?: readonly (string | OAuthProxyClient)[]; /** * Accept dynamic client registration (RFC 7591) at `/oauth/register`, for * clients that do not support metadata documents yet. Registered clients * are public clients: the proxy never issues a client secret. Turning it * off ends the sign-ins of registered clients, as removing a client from * `clients` does. It cannot be on while * `clients` is set, because a registered client names itself. * * @defaultValue `true`, or `false` when `clients` is set */ dcr?: boolean; /** * Lifetime of the proxy's access tokens in seconds. It can be longer than * the upstream token's lifetime when the upstream issues refresh tokens: * the proxy refreshes the upstream token on the server when needed. * * @defaultValue `3600` */ accessTokenTtl?: number; /** * Maps the upstream user to the application user exposed as * `ctx.auth.user`. Defaults to the {@link UpstreamUser} itself. */ mapUser?: (user: UpstreamUser) => TUser; /** * Fetch implementation for upstream and metadata-document requests. * * Without it, the proxy resolves each metadata-document host with * `node:dns` and refuses hosts that resolve to private addresses. A * runtime without `node:dns` cannot check a host, so the proxy refuses * every metadata document there (Cloudflare Workers has `node:dns` with * the `nodejs_compat` flag). When `fetch` is set, the proxy skips the * check: the custom fetch must enforce its own network policy. * * On Node.js and Bun, the proxy then connects only to the addresses it * checked. Other runtimes (Cloudflare Workers, Deno) fetch the document * with the global `fetch`, which resolves the host again, so a host that * answers DNS with a public address and then a private one (DNS * rebinding) can still reach the private one. Listing the trusted * metadata documents in `clients` closes that window. */ fetch?: typeof globalThis.fetch; } /** The provider that {@link oauthProxy} returns. */ export type OAuthProxyProvider = OAuthProvider OAuthMetadata>; /** * Creates an OAuth provider that makes the MCP server its own authorization * server, in front of an identity provider that does not support dynamic * client registration (for example GitHub or Google). * * MCP clients discover the server's own endpoints, register (or present a * client ID metadata document), and sign in through * `/oauth/authorize`. The server shows its consent page, sends * the browser to the upstream provider as one OAuth app, exchanges the code * on the server, and gives the client its own tokens. The upstream client * secret and the upstream tokens never reach the client. Tools read the * upstream token with {@link upstreamToken}. * * Register `https:///oauth/callback` (for example * `https://api.example.com/mcp/oauth/callback`) as the callback URL of the * upstream OAuth app. * * @typeParam TUser - Application user type exposed as `ctx.auth.user`. * @param options - Upstream provider, secret, storage, and consent settings. * @returns A provider for the `oauth` option of `MCPServer`. * @throws A `TypeError` if an option is invalid. When the server mounts, the * provider throws if `storage` or `secret` is missing outside a localhost * `listen()`. Inside `mcp-use build` (see `isBuilding()` in `mcp-use`), it * requires neither, nor the upstream client ID and secret. * * @example * ```ts * import { MCPServer } from "mcp-use"; * import { github, oauthProxy, upstreamToken } from "mcp-use/oauth"; * import { upstashStorage } from "mcp-use/oauth/storage"; * * const server = new MCPServer({ * name: "acme", * version: "1.0.0", * oauth: oauthProxy({ * upstream: github({ * clientId: process.env.GITHUB_CLIENT_ID!, * clientSecret: process.env.GITHUB_CLIENT_SECRET!, * scopes: ["read:user"], * }), * secret: process.env.MCP_USE_OAUTH_SECRET, * storage: upstashStorage({ * url: process.env.UPSTASH_REDIS_REST_URL!, * token: process.env.UPSTASH_REDIS_REST_TOKEN!, * }), * }), * }); * * server.tool({ name: "whoami" }, async (_input, ctx) => { * const response = await fetch("https://api.github.com/user", { * headers: { authorization: `Bearer ${upstreamToken(ctx.auth)}` }, * }); * return { content: [{ type: "text", text: (await response.json()).login }] }; * }); * ``` */ export declare function oauthProxy(options: OAuthProxyOptions): OAuthProxyProvider; /** * Reads the upstream provider's access token for the signed-in user, for * example to call the GitHub API as that user from a tool. * * Keep the token on the server: do not return it in tool results, views, * or `_meta`. * * @param auth - The request's auth, `ctx.auth` in a tool, resource, or * prompt. `undefined` on a request without a sign-in. * @returns The upstream access token. * @throws If the request is not signed in through {@link oauthProxy}. */ export declare function upstreamToken(auth: { upstream?: OAuthUpstreamToken; } | undefined): string; export type { OAuthProxyClient } from "./clients.js"; export type { ConsentPageProps } from "./consent.js"; export { memoryStorage, type OAuthProxyStorage } from "./storage.js"; export { github, google } from "./providers.js"; export type { GitHubUpstreamOptions, GoogleUpstreamOptions, } from "./providers.js"; export { upstream, type RedirectUpstream, type Upstream, type UpstreamOptions, type UpstreamTokenSet, type UpstreamUser, } from "./upstream.js";