/** * MCP OAuth Provider * * Implementation of the MCP SDK's OAuthClientProvider interface. * Handles OAuth client registration, token storage, and authorization redirection. */ import { type AddClientAuthentication, type OAuthClientProvider, type OAuthDiscoveryState } from "@modelcontextprotocol/client"; import type { OAuthClientInformationMixed, OAuthClientMetadata, OAuthTokens } from "@modelcontextprotocol/client"; import { type AuthStorageOptions } from "./mcp-auth.ts"; declare const DEFAULT_OAUTH_CALLBACK_PORT = 19876; declare const DEFAULT_OAUTH_CALLBACK_PATH = "/callback"; export declare function getConfiguredOAuthCallbackPort(): number; export declare function getOAuthCallbackPort(): number; export declare function setOAuthCallbackPort(port: number): void; export declare function getOAuthCallbackPath(): string; export declare function setOAuthCallbackPath(path: string): void; /** Configuration options for OAuth */ export interface McpOAuthConfig { grantType?: "authorization_code" | "client_credentials"; clientId?: string; clientSecret?: string; scope?: string; authorizationParams?: Record; redirectUri?: string; clientName?: string; clientUri?: string; } /** Callbacks for OAuth flow interactions */ export interface McpOAuthCallbacks { onRedirect: (url: URL) => void | Promise; } /** * OAuth provider implementation for MCP servers. * Implements the OAuthClientProvider interface from the MCP SDK. */ export declare class McpOAuthProvider implements OAuthClientProvider { private serverName; private serverUrl; private config; private callbacks; private storageOptions; private runtimeSignal?; private readonly redirectUrlSnapshot; private active; private flowClientInfo; private flowCodeVerifier; private flowDiscoveryState; private flowIssuerMismatch; private flowState; constructor(serverName: string, serverUrl: string, config: McpOAuthConfig, callbacks: McpOAuthCallbacks, storageOptions?: AuthStorageOptions, runtimeSignal?: AbortSignal | undefined, initialState?: string); private get usesClientCredentials(); private get discoveredIssuer(); deactivate(): void; private assertStoredIssuerBindings; private throwIfInactive; /** * The redirect URL for OAuth callbacks. * This must match the redirect_uri in client metadata. */ get redirectUrl(): string | undefined; /** * Client metadata for dynamic registration. * Describes this client to the OAuth authorization server. */ get clientMetadata(): OAuthClientMetadata; /** * Get client information (for pre-registered or dynamically registered clients). * Returns undefined if no client info exists or if the server URL has changed. */ clientInformation(): Promise; /** * Save client information from dynamic registration. */ saveClientInformation(info: OAuthClientInformationMixed): Promise; /** * Get stored OAuth tokens. * Returns undefined if no tokens exist or if the server URL has changed. */ tokens(): Promise; /** * Save OAuth tokens. */ saveTokens(tokens: OAuthTokens): Promise; /** * Redirect the user to the authorization URL. * This opens the browser for the user to authenticate. * * Throws UnauthorizedError when called outside of a user-initiated flow * (no oauthState saved by startAuth). That path is reached when the SDK * falls through from a failed refresh into a fresh authorization_code * flow, which library hosts cannot complete in-process. */ redirectToAuthorization(authorizationUrl: URL): Promise; /** * Save the PKCE code verifier. */ saveCodeVerifier(codeVerifier: string): Promise; /** * Get the stored PKCE code verifier. * @throws Error if no code verifier is stored */ codeVerifier(): Promise; /** * Keep discovery with the in-flight PKCE verifier. The callback leg uses it * to validate the authorization response issuer before token exchange. */ saveDiscoveryState(state: OAuthDiscoveryState): Promise; discoveryState(): Promise; /** * Save the OAuth state parameter for CSRF protection. */ saveState(state: string): Promise; /** * Get the stored OAuth state parameter. * @throws UnauthorizedError if no flow is in progress (see redirectToAuthorization) */ state(): Promise; /** * Invalidate credentials when authentication fails. * Clears tokens, client info, or all credentials based on the type. */ invalidateCredentials(type: "all" | "client" | "tokens" | "verifier" | "discovery"): Promise; /** * Adds configured authorization-code scope without replacing the SDK's * default token endpoint authentication behavior. */ addClientAuthentication: AddClientAuthentication; prepareTokenRequest(scope?: string): URLSearchParams | undefined; } export { DEFAULT_OAUTH_CALLBACK_PORT, DEFAULT_OAUTH_CALLBACK_PATH };