UNPKG

@modelcontextprotocol/sdk

Version:

Model Context Protocol implementation for TypeScript

535 lines 22.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnauthorizedError = void 0; exports.auth = auth; exports.selectResourceURL = selectResourceURL; exports.extractResourceMetadataUrl = extractResourceMetadataUrl; exports.discoverOAuthProtectedResourceMetadata = discoverOAuthProtectedResourceMetadata; exports.discoverOAuthMetadata = discoverOAuthMetadata; exports.startAuthorization = startAuthorization; exports.exchangeAuthorization = exchangeAuthorization; exports.refreshAuthorization = refreshAuthorization; exports.registerClient = registerClient; const pkce_challenge_1 = __importDefault(require("pkce-challenge")); const types_js_1 = require("../types.js"); const auth_js_1 = require("../shared/auth.js"); const auth_utils_js_1 = require("../shared/auth-utils.js"); class UnauthorizedError extends Error { constructor(message) { super(message !== null && message !== void 0 ? message : "Unauthorized"); } } exports.UnauthorizedError = UnauthorizedError; /** * Determines the best client authentication method to use based on server support and client configuration. * * Priority order (highest to lowest): * 1. client_secret_basic (if client secret is available) * 2. client_secret_post (if client secret is available) * 3. none (for public clients) * * @param clientInformation - OAuth client information containing credentials * @param supportedMethods - Authentication methods supported by the authorization server * @returns The selected authentication method */ function selectClientAuthMethod(clientInformation, supportedMethods) { const hasClientSecret = clientInformation.client_secret !== undefined; // If server doesn't specify supported methods, use RFC 6749 defaults if (supportedMethods.length === 0) { return hasClientSecret ? "client_secret_post" : "none"; } // Try methods in priority order (most secure first) if (hasClientSecret && supportedMethods.includes("client_secret_basic")) { return "client_secret_basic"; } if (hasClientSecret && supportedMethods.includes("client_secret_post")) { return "client_secret_post"; } if (supportedMethods.includes("none")) { return "none"; } // Fallback: use what we have return hasClientSecret ? "client_secret_post" : "none"; } /** * Applies client authentication to the request based on the specified method. * * Implements OAuth 2.1 client authentication methods: * - client_secret_basic: HTTP Basic authentication (RFC 6749 Section 2.3.1) * - client_secret_post: Credentials in request body (RFC 6749 Section 2.3.1) * - none: Public client authentication (RFC 6749 Section 2.1) * * @param method - The authentication method to use * @param clientInformation - OAuth client information containing credentials * @param headers - HTTP headers object to modify * @param params - URL search parameters to modify * @throws {Error} When required credentials are missing */ function applyClientAuthentication(method, clientInformation, headers, params) { const { client_id, client_secret } = clientInformation; switch (method) { case "client_secret_basic": applyBasicAuth(client_id, client_secret, headers); return; case "client_secret_post": applyPostAuth(client_id, client_secret, params); return; case "none": applyPublicAuth(client_id, params); return; default: throw new Error(`Unsupported client authentication method: ${method}`); } } /** * Applies HTTP Basic authentication (RFC 6749 Section 2.3.1) */ function applyBasicAuth(clientId, clientSecret, headers) { if (!clientSecret) { throw new Error("client_secret_basic authentication requires a client_secret"); } const credentials = btoa(`${clientId}:${clientSecret}`); headers.set("Authorization", `Basic ${credentials}`); } /** * Applies POST body authentication (RFC 6749 Section 2.3.1) */ function applyPostAuth(clientId, clientSecret, params) { params.set("client_id", clientId); if (clientSecret) { params.set("client_secret", clientSecret); } } /** * Applies public client authentication (RFC 6749 Section 2.1) */ function applyPublicAuth(clientId, params) { params.set("client_id", clientId); } /** * Orchestrates the full auth flow with a server. * * This can be used as a single entry point for all authorization functionality, * instead of linking together the other lower-level functions in this module. */ async function auth(provider, { serverUrl, authorizationCode, scope, resourceMetadataUrl }) { let resourceMetadata; let authorizationServerUrl = serverUrl; try { resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl }); if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) { authorizationServerUrl = resourceMetadata.authorization_servers[0]; } } catch (_a) { // Ignore errors and fall back to /.well-known/oauth-authorization-server } const resource = await selectResourceURL(serverUrl, provider, resourceMetadata); const metadata = await discoverOAuthMetadata(serverUrl, { authorizationServerUrl }); // Handle client registration if needed let clientInformation = await Promise.resolve(provider.clientInformation()); if (!clientInformation) { if (authorizationCode !== undefined) { throw new Error("Existing OAuth client information is required when exchanging an authorization code"); } if (!provider.saveClientInformation) { throw new Error("OAuth client information must be saveable for dynamic registration"); } const fullInformation = await registerClient(authorizationServerUrl, { metadata, clientMetadata: provider.clientMetadata, }); await provider.saveClientInformation(fullInformation); clientInformation = fullInformation; } // Exchange authorization code for tokens if (authorizationCode !== undefined) { const codeVerifier = await provider.codeVerifier(); const tokens = await exchangeAuthorization(authorizationServerUrl, { metadata, clientInformation, authorizationCode, codeVerifier, redirectUri: provider.redirectUrl, resource, addClientAuthentication: provider.addClientAuthentication, }); await provider.saveTokens(tokens); return "AUTHORIZED"; } const tokens = await provider.tokens(); // Handle token refresh or new authorization if (tokens === null || tokens === void 0 ? void 0 : tokens.refresh_token) { try { // Attempt to refresh the token const newTokens = await refreshAuthorization(authorizationServerUrl, { metadata, clientInformation, refreshToken: tokens.refresh_token, resource, addClientAuthentication: provider.addClientAuthentication, }); await provider.saveTokens(newTokens); return "AUTHORIZED"; } catch (_b) { // Could not refresh OAuth tokens } } const state = provider.state ? await provider.state() : undefined; // Start new authorization flow const { authorizationUrl, codeVerifier } = await startAuthorization(authorizationServerUrl, { metadata, clientInformation, state, redirectUrl: provider.redirectUrl, scope: scope || provider.clientMetadata.scope, resource, }); await provider.saveCodeVerifier(codeVerifier); await provider.redirectToAuthorization(authorizationUrl); return "REDIRECT"; } async function selectResourceURL(serverUrl, provider, resourceMetadata) { const defaultResource = (0, auth_utils_js_1.resourceUrlFromServerUrl)(serverUrl); // If provider has custom validation, delegate to it if (provider.validateResourceURL) { return await provider.validateResourceURL(defaultResource, resourceMetadata === null || resourceMetadata === void 0 ? void 0 : resourceMetadata.resource); } // Only include resource parameter when Protected Resource Metadata is present if (!resourceMetadata) { return undefined; } // Validate that the metadata's resource is compatible with our request if (!(0, auth_utils_js_1.checkResourceAllowed)({ requestedResource: defaultResource, configuredResource: resourceMetadata.resource })) { throw new Error(`Protected resource ${resourceMetadata.resource} does not match expected ${defaultResource} (or origin)`); } // Prefer the resource from metadata since it's what the server is telling us to request return new URL(resourceMetadata.resource); } /** * Extract resource_metadata from response header. */ function extractResourceMetadataUrl(res) { const authenticateHeader = res.headers.get("WWW-Authenticate"); if (!authenticateHeader) { return undefined; } const [type, scheme] = authenticateHeader.split(' '); if (type.toLowerCase() !== 'bearer' || !scheme) { return undefined; } const regex = /resource_metadata="([^"]*)"/; const match = regex.exec(authenticateHeader); if (!match) { return undefined; } try { return new URL(match[1]); } catch (_a) { return undefined; } } /** * Looks up RFC 9728 OAuth 2.0 Protected Resource Metadata. * * If the server returns a 404 for the well-known endpoint, this function will * return `undefined`. Any other errors will be thrown as exceptions. */ async function discoverOAuthProtectedResourceMetadata(serverUrl, opts) { const response = await discoverMetadataWithFallback(serverUrl, 'oauth-protected-resource', { protocolVersion: opts === null || opts === void 0 ? void 0 : opts.protocolVersion, metadataUrl: opts === null || opts === void 0 ? void 0 : opts.resourceMetadataUrl, }); if (!response || response.status === 404) { throw new Error(`Resource server does not implement OAuth 2.0 Protected Resource Metadata.`); } if (!response.ok) { throw new Error(`HTTP ${response.status} trying to load well-known OAuth protected resource metadata.`); } return auth_js_1.OAuthProtectedResourceMetadataSchema.parse(await response.json()); } /** * Helper function to handle fetch with CORS retry logic */ async function fetchWithCorsRetry(url, headers) { try { return await fetch(url, { headers }); } catch (error) { if (error instanceof TypeError) { if (headers) { // CORS errors come back as TypeError, retry without headers return fetchWithCorsRetry(url); } else { // We're getting CORS errors on retry too, return undefined return undefined; } } throw error; } } /** * Constructs the well-known path for OAuth metadata discovery */ function buildWellKnownPath(wellKnownPrefix, pathname) { let wellKnownPath = `/.well-known/${wellKnownPrefix}${pathname}`; if (pathname.endsWith('/')) { // Strip trailing slash from pathname to avoid double slashes wellKnownPath = wellKnownPath.slice(0, -1); } return wellKnownPath; } /** * Tries to discover OAuth metadata at a specific URL */ async function tryMetadataDiscovery(url, protocolVersion) { const headers = { "MCP-Protocol-Version": protocolVersion }; return await fetchWithCorsRetry(url, headers); } /** * Determines if fallback to root discovery should be attempted */ function shouldAttemptFallback(response, pathname) { return !response || response.status === 404 && pathname !== '/'; } /** * Generic function for discovering OAuth metadata with fallback support */ async function discoverMetadataWithFallback(serverUrl, wellKnownType, opts) { var _a, _b; const issuer = new URL(serverUrl); const protocolVersion = (_a = opts === null || opts === void 0 ? void 0 : opts.protocolVersion) !== null && _a !== void 0 ? _a : types_js_1.LATEST_PROTOCOL_VERSION; let url; if (opts === null || opts === void 0 ? void 0 : opts.metadataUrl) { url = new URL(opts.metadataUrl); } else { // Try path-aware discovery first const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pathname); url = new URL(wellKnownPath, (_b = opts === null || opts === void 0 ? void 0 : opts.metadataServerUrl) !== null && _b !== void 0 ? _b : issuer); url.search = issuer.search; } let response = await tryMetadataDiscovery(url, protocolVersion); // If path-aware discovery fails with 404 and we're not already at root, try fallback to root discovery if (!(opts === null || opts === void 0 ? void 0 : opts.metadataUrl) && shouldAttemptFallback(response, issuer.pathname)) { const rootUrl = new URL(`/.well-known/${wellKnownType}`, issuer); response = await tryMetadataDiscovery(rootUrl, protocolVersion); } return response; } /** * Looks up RFC 8414 OAuth 2.0 Authorization Server Metadata. * * If the server returns a 404 for the well-known endpoint, this function will * return `undefined`. Any other errors will be thrown as exceptions. */ async function discoverOAuthMetadata(issuer, { authorizationServerUrl, protocolVersion, } = {}) { if (typeof issuer === 'string') { issuer = new URL(issuer); } if (!authorizationServerUrl) { authorizationServerUrl = issuer; } if (typeof authorizationServerUrl === 'string') { authorizationServerUrl = new URL(authorizationServerUrl); } protocolVersion !== null && protocolVersion !== void 0 ? protocolVersion : (protocolVersion = types_js_1.LATEST_PROTOCOL_VERSION); const response = await discoverMetadataWithFallback(issuer, 'oauth-authorization-server', { protocolVersion, metadataServerUrl: authorizationServerUrl, }); if (!response || response.status === 404) { return undefined; } if (!response.ok) { throw new Error(`HTTP ${response.status} trying to load well-known OAuth metadata`); } return auth_js_1.OAuthMetadataSchema.parse(await response.json()); } /** * Begins the authorization flow with the given server, by generating a PKCE challenge and constructing the authorization URL. */ async function startAuthorization(authorizationServerUrl, { metadata, clientInformation, redirectUrl, scope, state, resource, }) { const responseType = "code"; const codeChallengeMethod = "S256"; let authorizationUrl; if (metadata) { authorizationUrl = new URL(metadata.authorization_endpoint); if (!metadata.response_types_supported.includes(responseType)) { throw new Error(`Incompatible auth server: does not support response type ${responseType}`); } if (!metadata.code_challenge_methods_supported || !metadata.code_challenge_methods_supported.includes(codeChallengeMethod)) { throw new Error(`Incompatible auth server: does not support code challenge method ${codeChallengeMethod}`); } } else { authorizationUrl = new URL("/authorize", authorizationServerUrl); } // Generate PKCE challenge const challenge = await (0, pkce_challenge_1.default)(); const codeVerifier = challenge.code_verifier; const codeChallenge = challenge.code_challenge; authorizationUrl.searchParams.set("response_type", responseType); authorizationUrl.searchParams.set("client_id", clientInformation.client_id); authorizationUrl.searchParams.set("code_challenge", codeChallenge); authorizationUrl.searchParams.set("code_challenge_method", codeChallengeMethod); authorizationUrl.searchParams.set("redirect_uri", String(redirectUrl)); if (state) { authorizationUrl.searchParams.set("state", state); } if (scope) { authorizationUrl.searchParams.set("scope", scope); } if (resource) { authorizationUrl.searchParams.set("resource", resource.href); } return { authorizationUrl, codeVerifier }; } /** * Exchanges an authorization code for an access token with the given server. * * Supports multiple client authentication methods as specified in OAuth 2.1: * - Automatically selects the best authentication method based on server support * - Falls back to appropriate defaults when server metadata is unavailable * * @param authorizationServerUrl - The authorization server's base URL * @param options - Configuration object containing client info, auth code, etc. * @returns Promise resolving to OAuth tokens * @throws {Error} When token exchange fails or authentication is invalid */ async function exchangeAuthorization(authorizationServerUrl, { metadata, clientInformation, authorizationCode, codeVerifier, redirectUri, resource, addClientAuthentication }) { var _a; const grantType = "authorization_code"; const tokenUrl = (metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl); if ((metadata === null || metadata === void 0 ? void 0 : metadata.grant_types_supported) && !metadata.grant_types_supported.includes(grantType)) { throw new Error(`Incompatible auth server: does not support grant type ${grantType}`); } // Exchange code for tokens const headers = new Headers({ "Content-Type": "application/x-www-form-urlencoded", }); const params = new URLSearchParams({ grant_type: grantType, code: authorizationCode, code_verifier: codeVerifier, redirect_uri: String(redirectUri), }); if (addClientAuthentication) { addClientAuthentication(headers, params, authorizationServerUrl, metadata); } else { // Determine and apply client authentication method const supportedMethods = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint_auth_methods_supported) !== null && _a !== void 0 ? _a : []; const authMethod = selectClientAuthMethod(clientInformation, supportedMethods); applyClientAuthentication(authMethod, clientInformation, headers, params); } if (resource) { params.set("resource", resource.href); } const response = await fetch(tokenUrl, { method: "POST", headers, body: params, }); if (!response.ok) { throw new Error(`Token exchange failed: HTTP ${response.status}`); } return auth_js_1.OAuthTokensSchema.parse(await response.json()); } /** * Exchange a refresh token for an updated access token. * * Supports multiple client authentication methods as specified in OAuth 2.1: * - Automatically selects the best authentication method based on server support * - Preserves the original refresh token if a new one is not returned * * @param authorizationServerUrl - The authorization server's base URL * @param options - Configuration object containing client info, refresh token, etc. * @returns Promise resolving to OAuth tokens (preserves original refresh_token if not replaced) * @throws {Error} When token refresh fails or authentication is invalid */ async function refreshAuthorization(authorizationServerUrl, { metadata, clientInformation, refreshToken, resource, addClientAuthentication, }) { var _a; const grantType = "refresh_token"; let tokenUrl; if (metadata) { tokenUrl = new URL(metadata.token_endpoint); if (metadata.grant_types_supported && !metadata.grant_types_supported.includes(grantType)) { throw new Error(`Incompatible auth server: does not support grant type ${grantType}`); } } else { tokenUrl = new URL("/token", authorizationServerUrl); } // Exchange refresh token const headers = new Headers({ "Content-Type": "application/x-www-form-urlencoded", }); const params = new URLSearchParams({ grant_type: grantType, refresh_token: refreshToken, }); if (addClientAuthentication) { addClientAuthentication(headers, params, authorizationServerUrl, metadata); } else { // Determine and apply client authentication method const supportedMethods = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint_auth_methods_supported) !== null && _a !== void 0 ? _a : []; const authMethod = selectClientAuthMethod(clientInformation, supportedMethods); applyClientAuthentication(authMethod, clientInformation, headers, params); } if (resource) { params.set("resource", resource.href); } const response = await fetch(tokenUrl, { method: "POST", headers, body: params, }); if (!response.ok) { throw new Error(`Token refresh failed: HTTP ${response.status}`); } return auth_js_1.OAuthTokensSchema.parse({ refresh_token: refreshToken, ...(await response.json()) }); } /** * Performs OAuth 2.0 Dynamic Client Registration according to RFC 7591. */ async function registerClient(authorizationServerUrl, { metadata, clientMetadata, }) { let registrationUrl; if (metadata) { if (!metadata.registration_endpoint) { throw new Error("Incompatible auth server: does not support dynamic client registration"); } registrationUrl = new URL(metadata.registration_endpoint); } else { registrationUrl = new URL("/register", authorizationServerUrl); } const response = await fetch(registrationUrl, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(clientMetadata), }); if (!response.ok) { throw new Error(`Dynamic client registration failed: HTTP ${response.status}`); } return auth_js_1.OAuthClientInformationFullSchema.parse(await response.json()); } //# sourceMappingURL=auth.js.map