Class OAuthMcpTransport

java.lang.Object
com.tnsai.mcp.auth.OAuthMcpTransport
All Implemented Interfaces:
McpTransport

public class OAuthMcpTransport extends Object implements 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:
  • 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 to
      tokenManager - the token manager for token storage and refresh
      serverUrl - the MCP server URL (used as token lookup key)
      tokenEndpoint - the OAuth token endpoint for refresh
      clientId - the OAuth client ID for refresh
  • Method Details

    • builder

      public static OAuthMcpTransport.Builder builder()
      Creates a new builder for OAuthMcpTransport.
      Returns:
      a new Builder instance
    • connect

      public void connect() throws IOException
      Description copied from interface: McpTransport
      Connects to the MCP server.
      Specified by:
      connect in interface McpTransport
      Throws:
      IOException - if connection fails
    • send

      public void send(String message) throws IOException
      Description copied from interface: McpTransport
      Sends a message to the MCP server.
      Specified by:
      send in interface McpTransport
      Parameters:
      message - the message to send (JSON-RPC request)
      Throws:
      IOException - if sending fails
    • setOnMessage

      public void setOnMessage(Consumer<String> messageConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for incoming messages from the MCP server.
      Specified by:
      setOnMessage in interface McpTransport
      Parameters:
      messageConsumer - the consumer for incoming messages (JSON-RPC response/notification)
    • setOnError

      public void setOnError(Consumer<Throwable> errorConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for transport errors.
      Specified by:
      setOnError in interface McpTransport
      Parameters:
      errorConsumer - the consumer for transport errors
    • disconnect

      public void disconnect() throws IOException
      Description copied from interface: McpTransport
      Disconnects from the MCP server.
      Specified by:
      disconnect in interface McpTransport
      Throws:
      IOException - if disconnection fails
    • isConnected

      public boolean isConnected()
      Description copied from interface: McpTransport
      Checks if the transport is currently connected.
      Specified by:
      isConnected in interface McpTransport
      Returns:
      true if connected, false otherwise
    • getBearerToken

      public Optional<String> 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

      public McpTransport getDelegate()
      Returns the underlying delegate transport.
      Returns:
      the delegate transport
    • getServerUrl

      public String getServerUrl()
      Returns the server URL this transport is configured for.
      Returns:
      the server URL