MCP TypeScript SDK (V2)
    Preparing search index...
    • Unified token fetching that works with any grant type via prepareTokenRequest().

      This function provides a single entry point for obtaining tokens regardless of the OAuth grant type. The provider's prepareTokenRequest() method determines which grant to use and supplies the grant-specific parameters.

      Parameters

      • provider: OAuthClientProvider

        OAuth client provider that implements prepareTokenRequest()

      • authorizationServerUrl: string | URL

        The authorization server's base URL

      • options: {
            authorizationCode?: string;
            fetchFn?: FetchLike;
            metadata?: AuthorizationServerMetadata;
            resource?: URL;
        } = {}

        Configuration for the token request

        • OptionalauthorizationCode?: string

          Authorization code for the default authorization_code grant flow

        • OptionalfetchFn?: FetchLike
        • Optionalmetadata?: AuthorizationServerMetadata
        • Optionalresource?: URL

      Returns Promise<
          {
              access_token: string;
              expires_in?: number;
              id_token?: string;
              refresh_token?: string;
              scope?: string;
              token_type: string;
          },
      >

      Promise resolving to OAuth tokens

      When provider doesn't implement prepareTokenRequest or token fetch fails

      // Provider for client_credentials:
      class MyProvider extends MyProviderBase implements OAuthClientProvider {
      prepareTokenRequest(scope?: string) {
      const params = new URLSearchParams({ grant_type: 'client_credentials' });
      if (scope) params.set('scope', scope);
      return params;
      }
      }

      const tokens = await fetchToken(new MyProvider(), authServerUrl, { metadata });