import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'; import { OAuthClientMetadata, OAuthClientInformationMixed, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js'; /** * Generic OAuth 2.1 for imported MCP servers (RFC 9728 discovery, RFC 7591 * dynamic client registration, PKCE). Works for any remote MCP that answers * 401 + `WWW-Authenticate: Bearer resource_metadata=…`. * * Two modes share one provider: * - daemon (default): reads the stored client + tokens, refreshes them * through the SDK's `auth()`, and NEVER starts an interactive flow — when * credentials are missing/revoked it throws `McpOAuthLoginRequiredError` * naming `agentproto mcp login `. * - interactive (`loginMcpOAuth`, used by `agentproto mcp login`): runs the * browser consent on a loopback redirect and stores the result. * * Everything (DCR client, tokens, PKCE verifier) lives in ONE keychain item * per import (`mcpOAuthRef`), reached through the same `resolveMcpSecret` / * `storeMcpSecret` seam as header secrets — nothing secret is written to * `imported-mcps.json`, logs or errors. */ declare const MCP_OAUTH_CLIENT_NAME = "agentproto"; /** Keychain ref of an import's OAuth state (same `#` shape as * `mcpSecretRef`). */ declare function mcpOAuthRef(importId: string): string; /** What a connection config carries to opt into OAuth. Serializable on * purpose: `mcpConfigKey` hashes the whole config. */ interface McpOAuthConfig { importId: string; alias: string; scope?: string; } interface McpOAuthState { /** Origin the credentials were obtained from. Never presented elsewhere. */ serverOrigin: string; clientInformation?: OAuthClientInformationMixed; tokens?: OAuthTokens; codeVerifier?: string; state?: string; } interface McpOAuthSecretIO { read(ref: string): Promise; write(ref: string, value: string): Promise; } declare class McpOAuthLoginRequiredError extends Error { constructor(alias: string, reason?: string); } interface McpOAuthProviderOptions { importId: string; alias: string; serverUrl: string; scope?: string; io: McpOAuthSecretIO; /** Present only for the interactive login; absent = daemon mode. */ interactive?: { redirectUrl: string; onAuthorize: (url: URL) => void | Promise; }; } declare class McpOAuthProvider implements OAuthClientProvider { private readonly opts; private readonly ref; private readonly origin; private cache; private loaded; constructor(opts: McpOAuthProviderOptions); get redirectUrl(): string; get clientMetadata(): OAuthClientMetadata; /** Current stored state, or undefined when absent or bound to another origin. */ private load; private update; /** Drop everything stored for this import (fresh login starts here). */ reset(): Promise; expectedState(): Promise; state(): Promise; clientInformation(): Promise; saveClientInformation(info: OAuthClientInformationMixed): Promise; tokens(): Promise; saveTokens(tokens: OAuthTokens): Promise; redirectToAuthorization(url: URL): Promise; saveCodeVerifier(codeVerifier: string): Promise; codeVerifier(): Promise; invalidateCredentials(scope: "all" | "client" | "tokens" | "verifier" | "discovery"): Promise; } /** The daemon-mode provider for a connection config, backed by the keychain * seam the daemon registered via `setMcpCredentialDeps`. * * `io` lets a caller that already holds the resolved secret functions (the * CLI's own `KeychainStore`-backed IO, or a daemon call site that already * fetched `getMcpCredentialDeps()`) pass them in directly. Prefer this over * the `getMcpCredentialDeps()` fallback: `@agentproto/runtime`'s multi-entry * tsup build (`splitting: false`) gives `./mcp-oauth`, `./mcp-client-pool` * and `./mcp-credential-deps` each their OWN bundled copy of * `mcp-credential-deps.ts`'s module state, so a `setMcpCredentialDeps` call * reached through one subpath is invisible to `getMcpCredentialDeps()` * reached through another — the fallback only actually works when both ends * happen to be inlined into the same chunk (true for `./index`, false for a * direct subpath import like the CLI's). */ declare function createDaemonOAuthProvider(oauth: McpOAuthConfig, serverUrl: string, io?: McpOAuthSecretIO): McpOAuthProvider; interface McpOAuthLoginOptions { importId: string; alias: string; serverUrl: string; scope?: string; io: McpOAuthSecretIO; /** Called with the consent URL once discovery + registration are done; * print and/or open it. The scope being requested is in `url.searchParams`. */ onAuthorizationUrl: (url: URL) => void | Promise; timeoutMs?: number; /** * Second path to the same result: resolve with the full callback URL the * user pasted (`http://127.0.0.1:PORT/callback?code=…&state=…`). The * loopback listener stays open (the registered `redirect_uri` is still the * listener's), and whichever arrives first settles the login. Called after * the consent URL was shown; `signal` aborts when the login finished. * The URL's code/state are validated like a loopback hit and never logged. */ manualCallback?: (signal: AbortSignal) => Promise; } interface McpOAuthLoginResult { /** Scope string the server actually granted, when it reports one. */ grantedScope?: string; } declare const DEFAULT_LOGIN_TIMEOUT_MS: number; /** * Extract and validate `code` + `state` from a pasted callback URL. Errors * never echo the URL, code or state (they are credentials in flight). */ declare function parseAuthorizationCallback(raw: string, stateOk: (state: string) => Promise): Promise<{ code: string; state: string; }>; /** * Interactive login: fresh DCR + PKCE consent on a one-shot loopback * listener (127.0.0.1, ephemeral port, `state`-checked). Starts from a clean * slate so the registered `redirect_uri` always matches the listener. */ declare function loginMcpOAuth(opts: McpOAuthLoginOptions): Promise; export { DEFAULT_LOGIN_TIMEOUT_MS, MCP_OAUTH_CLIENT_NAME, type McpOAuthConfig, type McpOAuthLoginOptions, McpOAuthLoginRequiredError, type McpOAuthLoginResult, McpOAuthProvider, type McpOAuthProviderOptions, type McpOAuthSecretIO, type McpOAuthState, createDaemonOAuthProvider, loginMcpOAuth, mcpOAuthRef, parseAuthorizationCallback };