Package com.tnsai.mcp.auth
Class McpTokenManager
java.lang.Object
com.tnsai.mcp.auth.McpTokenManager
Thread-safe token manager for multiple MCP server connections.
Manages OAuth tokens for MCP servers, providing:
- Thread-safe token storage using
ConcurrentHashMap - Automatic token refresh before expiry
- Per-server token management
Usage:
McpTokenManager manager = new McpTokenManager(Duration.ofMinutes(5));
manager.storeToken("https://mcp.example.com", tokenResponse);
Optional<PkceFlow.TokenResponse> token = manager.getValidToken(
"https://mcp.example.com",
"https://auth.example.com/token",
"my-client-id"
);
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a token manager with the default refresh threshold (5 minutes).McpTokenManager(Duration refreshThreshold) Creates a token manager with a custom refresh threshold. -
Method Summary
Modifier and TypeMethodDescriptionvoidclearAll()Clears all stored tokens.Returns the set of all server URLs with stored tokens.Gets the stored token for a server URL without validation or refresh.getValidToken(String serverUrl, String tokenEndpoint, String clientId) Gets a valid token for a server, auto-refreshing if the token is expiring soon.booleanChecks if a token is stored for the given server URL.voidremoveToken(String serverUrl) Removes the token for a given server URL.voidstoreToken(String serverUrl, PkceFlow.TokenResponse token) Stores a token for a given server URL.
-
Constructor Details
-
McpTokenManager
public McpTokenManager()Creates a token manager with the default refresh threshold (5 minutes). -
McpTokenManager
Creates a token manager with a custom refresh threshold.- Parameters:
refreshThreshold- how far before expiry to trigger a refresh
-
-
Method Details
-
storeToken
Stores a token for a given server URL.- Parameters:
serverUrl- the MCP server URLtoken- the token response to store
-
getToken
Gets the stored token for a server URL without validation or refresh.- Parameters:
serverUrl- the MCP server URL- Returns:
- the token if present
-
getValidToken
public Optional<PkceFlow.TokenResponse> getValidToken(String serverUrl, String tokenEndpoint, String clientId) Gets a valid token for a server, auto-refreshing if the token is expiring soon.If the stored token is expiring within the configured threshold and a refresh token is available, attempts to refresh the token automatically. If refresh fails, the existing token is returned if it has not yet expired.
- Parameters:
serverUrl- the MCP server URLtokenEndpoint- the OAuth token endpoint for refreshclientId- the OAuth client ID for refresh- Returns:
- a valid token if available
-
hasToken
Checks if a token is stored for the given server URL.- Parameters:
serverUrl- the MCP server URL- Returns:
- true if a token exists for the server
-
removeToken
Removes the token for a given server URL.- Parameters:
serverUrl- the MCP server URL
-
clearAll
public void clearAll()Clears all stored tokens. -
getServerUrls
Returns the set of all server URLs with stored tokens.- Returns:
- an unmodifiable set of server URLs
-