You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add TokenProvider for composable bearer-token auth (non-breaking)
Adds a minimal `() => Promise<string | undefined>` function type as a
lightweight alternative to OAuthClientProvider, for scenarios where
bearer tokens are managed externally (gateway/proxy patterns, service
accounts, API keys).
- New TokenProvider type + withBearerAuth(getToken, fetchFn?) helper
- New tokenProvider option on StreamableHTTPClientTransport and
SSEClientTransport, used as fallback after authProvider in
_commonHeaders(). authProvider takes precedence when both set.
- On 401 with tokenProvider (no authProvider), transports throw
UnauthorizedError — no retry, since tokenProvider() is already
called before every request and would likely return the same
rejected token. Callers catch UnauthorizedError, invalidate
external cache, reconnect.
- Exported previously-internal auth helpers for building custom
flows: applyBasicAuth, applyPostAuth, applyPublicAuth,
executeTokenRequest.
- Tests, example, docs, changeset.
Zero breakage. Bughunter fleet review: 28 findings submitted,
2 confirmed, both addressed.
Add `TokenProvider` for simple bearer-token authentication and export composable auth primitives
6
+
7
+
- New `TokenProvider` type — a minimal `() => Promise<string | undefined>` function interface for supplying bearer tokens. Use this instead of `OAuthClientProvider` when tokens are managed externally (gateway/proxy patterns, service accounts, upfront API tokens, or any scenario where the full OAuth redirect flow is not needed).
8
+
- New `tokenProvider` option on `StreamableHTTPClientTransport` and `SSEClientTransport`. Called before every request to obtain a fresh token. If both `authProvider` and `tokenProvider` are set, `authProvider` takes precedence.
9
+
- New `withBearerAuth(getToken, fetchFn?)` helper that wraps a fetch function to inject `Authorization: Bearer` headers — useful for composing with other fetch middleware.
10
+
- Exported previously-internal auth helpers for building custom auth flows: `applyBasicAuth`, `applyPostAuth`, `applyPublicAuth`, `executeTokenRequest`.
MCP servers can require OAuth 2.0 authentication before accepting client connections (see [Authorization](https://modelcontextprotocol.io/specification/latest/basic/authorization) in the MCP specification). Pass an `authProvider` to {@linkcode@modelcontextprotocol/client!client/streamableHttp.StreamableHTTPClientTransport | StreamableHTTPClientTransport} to enable this — the SDK provides built-in providers for common machine-to-machine flows, or you can implement the full {@linkcode@modelcontextprotocol/client!client/auth.OAuthClientProvider | OAuthClientProvider} interface for user-facing OAuth.
116
+
MCP servers can require authentication before accepting client connections (see [Authorization](https://modelcontextprotocol.io/specification/latest/basic/authorization) in the MCP specification). For servers that accept plain bearer tokens, pass a `tokenProvider` function to {@linkcode@modelcontextprotocol/client!client/streamableHttp.StreamableHTTPClientTransport | StreamableHTTPClientTransport}. For servers that require OAuth 2.0, pass an `authProvider` — the SDK provides built-in providers for common machine-to-machine flows, or you can implement the full {@linkcode@modelcontextprotocol/client!client/auth.OAuthClientProvider | OAuthClientProvider} interface for user-facing OAuth.
117
+
118
+
### Token provider
119
+
120
+
For servers that accept bearer tokens managed outside the SDK — API keys, tokens from a gateway or proxy, service-account credentials, or tokens obtained through a separate auth flow — pass a {@linkcode@modelcontextprotocol/client!client/tokenProvider.TokenProvider | TokenProvider} function. It is called before every request, so it can handle expiry and refresh internally. If the server rejects the token with 401, the transport throws {@linkcode@modelcontextprotocol/client!client/auth.UnauthorizedError | UnauthorizedError} without retrying — catch it to invalidate any external cache and reconnect:
const transport =newStreamableHTTPClientTransport(newURL('http://localhost:3000/mcp'), { tokenProvider });
126
+
```
127
+
128
+
See [`simpleTokenProvider.ts`](https://github.com/modelcontextprotocol/typescript-sdk/blob/main/examples/client/src/simpleTokenProvider.ts) for a complete runnable example. For finer control, {@linkcode@modelcontextprotocol/client!client/tokenProvider.withBearerAuth | withBearerAuth} wraps a fetch function directly.
0 commit comments