/** Tokens that an upstream identity provider issued for one user. */ export interface UpstreamTokenSet { /** Bearer token for the upstream API. */ accessToken: string; /** Refresh token, when the provider issued one. */ refreshToken?: string; /** Access-token expiry as Unix time in seconds, when the provider gave one. */ expiresAt?: number; /** Scopes that the provider granted. */ scopes: string[]; /** OpenID Connect ID token, when the provider issued one. */ idToken?: string; } /** The signed-in user, as the upstream identity provider describes them. */ export interface UpstreamUser { /** * Stable user identifier at the provider, for example the numeric GitHub * user id or the OpenID Connect `sub`. Never an email address or a login * name, which can change. */ subject: string; /** Profile fields from the provider, for example `login` or `email`. */ profile: Record; } /** * An upstream identity provider with OAuth 2.0 authorization and token * endpoints, such as GitHub or Google. The proxy sends the browser to its * authorization endpoint as one confidential OAuth app, and exchanges and * refreshes codes on the server, so `clientSecret` never leaves the server. * * Use {@link github}, {@link google}, or {@link upstream} to create one. */ export interface RedirectUpstream { /** Discriminator for a redirect upstream. */ kind: "redirect"; /** Short identifier for the provider, used in storage keys, for example `github`. */ id: string; /** Provider name shown on the consent page, for example `GitHub`. */ name: string; /** * Issuer identifier. Required when the token endpoint returns OpenID * Connect ID tokens, whose `iss` claim must match it. Defaults to the * origin of the authorization endpoint. */ issuer?: string; /** Authorization endpoint URL. */ authorizationEndpoint: string; /** Token endpoint URL. */ tokenEndpoint: string; /** Client identifier of the OAuth app registered at the provider. */ clientId: string; /** Client secret of the OAuth app. The proxy sends it only to the token endpoint. */ clientSecret: string; /** Scopes that every sign-in requests from the provider. */ scopes: readonly string[]; /** * Whether to send an RFC 7636 S256 code challenge upstream. * * @defaultValue `true` */ pkce?: boolean; /** Extra query parameters for the authorization request, for example `access_type`. */ authorizeParams?: Readonly>; /** * Extra authorization parameters for one more round when a sign-in * returns no refresh token and none is stored for the user, for example * Google's `prompt=consent`. Leave it unset for providers whose tokens do * not expire. */ retryWithoutRefreshToken?: Readonly>; /** * Reads the signed-in user from the provider. * * @param tokens - Tokens from the code exchange. * @param fetch - Fetch implementation to use for the request. * @returns The user's stable subject and profile. */ fetchUser(tokens: UpstreamTokenSet, fetch: typeof globalThis.fetch): Promise; } /** Identity provider behind an OAuth proxy. */ export type Upstream = RedirectUpstream; /** Options for {@link upstream}. */ export type UpstreamOptions = Omit; /** * Creates a redirect upstream for any OAuth 2.0 provider with authorization * and token endpoints. * * @param options - Endpoints, OAuth app credentials, scopes, and user lookup. * @returns An upstream for `oauthProxy`. * @throws A `TypeError` if a field is missing or an endpoint is not HTTPS * (HTTP is accepted on localhost). * * @example * ```ts * import { oauthProxy, upstream } from "mcp-use/oauth"; * * const oauth = oauthProxy({ * upstream: upstream({ * id: "acme", * name: "Acme", * authorizationEndpoint: "https://id.acme.dev/oauth/authorize", * tokenEndpoint: "https://id.acme.dev/oauth/token", * clientId: process.env.ACME_CLIENT_ID!, * clientSecret: process.env.ACME_CLIENT_SECRET!, * scopes: ["profile"], * fetchUser: async (tokens, fetch) => { * const response = await fetch("https://api.acme.dev/me", { * headers: { authorization: `Bearer ${tokens.accessToken}` }, * }); * const me = await response.json(); * return { subject: String(me.id), profile: me }; * }, * }), * }); * ``` */ export declare function upstream(options: UpstreamOptions): RedirectUpstream; /** @internal Validates a redirect upstream. */ export declare function assertRedirectUpstream(value: RedirectUpstream): void; /** * @internal Validates an upstream endpoint URL: HTTPS, or HTTP on localhost, * without credentials or a fragment. */ export declare function assertEndpoint(value: unknown, name: string): void; /** * @internal Failure of an upstream callback or token request. Its message * and `detail` are safe to log: they never hold token, code, or body values. */ export declare class UpstreamError extends Error { /** RFC 6749 error code, for example `invalid_grant`. */ readonly code: string; /** Whether a retry can succeed (network errors, 5xx responses). */ readonly transient: boolean; /** The provider's own error code, for example GitHub's `bad_verification_code`. */ readonly upstreamCode: string | undefined; /** The provider's `error_description`, cleaned for logs. */ readonly description: string | undefined; /** Diagnostics for logs, such as the HTTP status and content type. */ readonly detail: string | undefined; /** * @param code - RFC 6749 error code. * @param message - Human-readable message. * @param transient - Whether a retry can succeed. * @param options - Underlying error and diagnostics. */ constructor(code: string, message: string, transient: boolean, options?: { cause?: unknown; upstreamCode?: string; description?: string; detail?: string; }); } /** * @internal Makes a value from a provider or a browser safe to log: no * control characters, at most `max` characters. */ export declare function printable(value: string, max?: number): string; /** @internal Exchanges an upstream authorization code on the server. */ export declare function exchangeUpstreamCode(provider: RedirectUpstream, fetchImpl: typeof globalThis.fetch, input: { callbackParams: URLSearchParams; redirectUri: string; codeVerifier: string | undefined; }): Promise; /** @internal Refreshes upstream tokens on the server. */ export declare function refreshUpstreamTokens(provider: RedirectUpstream, fetchImpl: typeof globalThis.fetch, refreshToken: string): Promise;