Class McpTokenProvider
McpAuthInterceptor token seam (Supplier<String>).
This is the client-side token-manager: it acquires, caches, and refreshes an access token and returns the current
valid bearer credential from get(). Three modes are supported:
- Pre-provisioned / static (
ofStaticToken(String)) — always returns a fixed token. - Client-credentials (
clientCredentials()) — RFC 6749 §4.4 grant, re-acquired on expiry. - Refresh-token (
refreshToken(String), SEP-2207) — RFC 6749 §6 grant; rotated refresh tokens returned by the IdP are captured and used for the next refresh without re-consent, and the refreshed access token is fed back to the supplier.
The dynamic modes (client-credentials, refresh-token) require McpTokenProvider.Builder.resource(URI) and carry that RFC 8707
resource indicator on every token request, binding the token's audience to the target MCP server. The static
mode issues no token request, so it takes no resource indicator.
get() never returns a blank token: dynamic modes return the IdP-issued (non-blank) access token or throw
McpAuthException/OAuthFlowException on failure (which aborts the intercepted request), and the static
mode rejects a blank token at construction. This satisfies the McpAuthInterceptor "supply a non-blank token
or suppress the header" contract.
Thread-safe. Never logged: toString() discloses only non-secret configuration.
- Since:
- 10.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic McpTokenProvider.BuilderCreates a builder for the client-credentials grant.Returns the refresh token currently held (the most recently rotated value in refresh mode).get()Returns the current valid bearer token, acquiring or refreshing one on demand.Creates anMcpAuthInterceptorbacked by this provider, ready to attach to an MCP client builder.static McpTokenProviderofStaticToken(String token) Creates a provider that always returns the supplied pre-provisioned bearer token.static McpTokenProvider.BuilderrefreshToken(String initialRefreshToken) Creates a builder for the refresh-token grant, seeded with the initial refresh token.toString()Redacts token/secret material so nothing sensitive reaches logs via this provider'stoString().
-
Constructor Details
-
McpTokenProvider
Constructor.- Parameters:
b- The builder.
-
-
Method Details
-
ofStaticToken
Creates a provider that always returns the supplied pre-provisioned bearer token.- Parameters:
token- The static bearer token. Must not benull or blank.- Returns:
- A new provider.
-
clientCredentials
Creates a builder for the client-credentials grant.- Returns:
- A new builder.
-
refreshToken
Creates a builder for the refresh-token grant, seeded with the initial refresh token.- Parameters:
initialRefreshToken- The initial refresh token. Must not benull or blank.- Returns:
- A new builder.
-
get
Returns the current valid bearer token, acquiring or refreshing one on demand.Dynamic modes acquire under a single-flight monitor so concurrent callers don't stampede the IdP; the per-request HTTP timeout (
McpTokenProvider.Builder.httpTimeout(Duration), default 10s) is the backstop that prevents one hung IdP from wedging the monitor indefinitely. In refresh mode a terminalinvalid_grantlatches the provider: every subsequent call throws a distinguishableMcpAuthExceptionrather than re-hammering the IdP with a dead refresh token (SEP-2207).- Specified by:
getin interfaceSupplier<String>- Returns:
- A non-blank bearer token. Never
nullfor a successfully configured provider. - Throws:
OAuthFlowException- If a token acquisition/refresh HTTP round-trip fails or the IdP returns an error.McpAuthException- If a prior refresh was terminally rejected (invalid_grant); re-authorization is required.
-
currentRefreshToken
Returns the refresh token currently held (the most recently rotated value in refresh mode).Callers persisting refresh tokens across process restarts read this after each
get()to capture a rotated token (SEP-2207).- Returns:
- The current refresh token, or
Optional.empty()if none is held.
-
interceptor
Creates anMcpAuthInterceptorbacked by this provider, ready to attach to an MCP client builder.- Returns:
- A new interceptor.
-
toString
Redacts token/secret material so nothing sensitive reaches logs via this provider'stoString().
-