Package com.tnsai.mcp.auth
Class OAuthMcpTransport
java.lang.Object
com.tnsai.mcp.auth.OAuthMcpTransport
- All Implemented Interfaces:
McpTransport
Transport decorator that adds OAuth Bearer token authentication to any
McpTransport.
This decorator wraps an existing transport and ensures that OAuth tokens
are valid and refreshed before each operation. The Bearer token is available
via getBearerToken() for transports that need to include it in
HTTP headers (e.g., SSETransport, HttpTransport).
Since McpTransport.send(String) operates on JSON-RPC string messages
and not HTTP requests directly, this decorator primarily manages token lifecycle
and makes the current Bearer token available. HTTP-level transports should query
getBearerToken() when constructing their HTTP requests.
Usage:
McpTokenManager tokenManager = new McpTokenManager();
// ... store tokens after OAuth flow ...
OAuthMcpTransport transport = OAuthMcpTransport.builder()
.delegate(new SSETransport.Builder().sseEndpoint("...").build())
.tokenManager(tokenManager)
.serverUrl("https://mcp.example.com")
.tokenEndpoint("https://auth.example.com/token")
.clientId("my-client")
.build();
McpClient client = new McpClient(transport);
client.connect();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating OAuthMcpTransport instances. -
Constructor Summary
ConstructorsConstructorDescriptionOAuthMcpTransport(McpTransport delegate, McpTokenManager tokenManager, String serverUrl, String tokenEndpoint, String clientId) Creates a new OAuthMcpTransport. -
Method Summary
Modifier and TypeMethodDescriptionstatic OAuthMcpTransport.Builderbuilder()Creates a new builder for OAuthMcpTransport.voidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the current Bearer token if available and valid.Returns the underlying delegate transport.Returns the server URL this transport is configured for.booleanChecks if this transport has a valid (non-expired) authentication token.booleanChecks if the transport is currently connected.voidSends a message to the MCP server.voidsetOnError(Consumer<Throwable> errorConsumer) Sets a consumer for transport errors.voidsetOnMessage(Consumer<String> messageConsumer) Sets a consumer for incoming messages from the MCP server.
-
Constructor Details
-
OAuthMcpTransport
public OAuthMcpTransport(McpTransport delegate, McpTokenManager tokenManager, String serverUrl, String tokenEndpoint, String clientId) Creates a new OAuthMcpTransport.- Parameters:
delegate- the underlying transport to delegate totokenManager- the token manager for token storage and refreshserverUrl- the MCP server URL (used as token lookup key)tokenEndpoint- the OAuth token endpoint for refreshclientId- the OAuth client ID for refresh
-
-
Method Details
-
builder
Creates a new builder for OAuthMcpTransport.- Returns:
- a new Builder instance
-
connect
Description copied from interface:McpTransportConnects to the MCP server.- Specified by:
connectin interfaceMcpTransport- Throws:
IOException- if connection fails
-
send
Description copied from interface:McpTransportSends a message to the MCP server.- Specified by:
sendin interfaceMcpTransport- Parameters:
message- the message to send (JSON-RPC request)- Throws:
IOException- if sending fails
-
setOnMessage
Description copied from interface:McpTransportSets a consumer for incoming messages from the MCP server.- Specified by:
setOnMessagein interfaceMcpTransport- Parameters:
messageConsumer- the consumer for incoming messages (JSON-RPC response/notification)
-
setOnError
Description copied from interface:McpTransportSets a consumer for transport errors.- Specified by:
setOnErrorin interfaceMcpTransport- Parameters:
errorConsumer- the consumer for transport errors
-
disconnect
Description copied from interface:McpTransportDisconnects from the MCP server.- Specified by:
disconnectin interfaceMcpTransport- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Description copied from interface:McpTransportChecks if the transport is currently connected.- Specified by:
isConnectedin interfaceMcpTransport- Returns:
- true if connected, false otherwise
-
getBearerToken
Returns the current Bearer token if available and valid.- Returns:
- the Bearer token string (e.g., "Bearer eyJ..."), or empty if not authenticated
-
isAuthenticated
public boolean isAuthenticated()Checks if this transport has a valid (non-expired) authentication token.- Returns:
- true if authenticated with a valid token
-
getDelegate
Returns the underlying delegate transport.- Returns:
- the delegate transport
-
getServerUrl
Returns the server URL this transport is configured for.- Returns:
- the server URL
-