Class HttpTransport

java.lang.Object
com.tnsai.mcp.client.HttpTransport
All Implemented Interfaces:
McpTransport

public class HttpTransport extends Object implements McpTransport
HTTP-based MCP transport implementation. Connects to MCP servers via HTTP POST with JSON-RPC 2.0. Supports API key authentication via headers. ┌──────────────────────────────────────────────────────────────────────────┐ │ HTTP Transport Architecture │ └──────────────────────────────────────────────────────────────────────────┘ TnsAI Agent HttpTransport MCP Server (Remote) │ │ │ │ executeTool() │ │ │ ───────────────────►│ │ │ │ │ │ │ POST /mcp │ │ │ Authorization: Bearer xxx │ │ │ Content-Type: application │ │ │ /json │ │ │ ─────────────────────────► │ │ │ │ │ │ JSON-RPC Response │ │ │ ◄───────────────────────── │ │ │ │ │ JsonNode result │ │ │ ◄───────────────────│ │ │ │ │ Usage:
 HttpTransport transport = new HttpTransport(
     "https://api.example.com/mcp",
     Map.of("Authorization", "Bearer sk-xxx")
 );
 McpClient client = new McpClient(transport);
 client.connect();
 
  • Constructor Details

    • HttpTransport

      public HttpTransport(String endpoint, Map<String,String> headers)
      Creates HTTP transport with endpoint and custom headers.
      Parameters:
      endpoint - MCP server endpoint URL
      headers - Custom headers (e.g., Authorization)
    • HttpTransport

      public HttpTransport(String endpoint, Map<String,String> headers, Duration timeout)
      Creates HTTP transport with endpoint, headers, and timeout.
      Parameters:
      endpoint - MCP server endpoint URL
      headers - Custom headers (e.g., Authorization)
      timeout - Request timeout duration
    • HttpTransport

      public HttpTransport(String endpoint, Map<String,String> headers, Duration timeout, List<HttpInterceptor> interceptors)
      Creates HTTP transport with endpoint, headers, timeout, and an ordered list of HttpInterceptors.

      Interceptors execute in registration order; each call to HttpInterceptor.Chain.proceed(HttpRequest) advances to the next interceptor and finally to the network. See HttpInterceptor for the contract and composition rules.

      Parameters:
      endpoint - MCP server endpoint URL
      headers - Custom headers (e.g., Authorization)
      timeout - Request timeout duration
      interceptors - Ordered interceptor chain (may be null or empty)
      Since:
      0.10.5
  • Method Details

    • withApiKey

      public static HttpTransport withApiKey(String endpoint, String apiKey)
      Convenience constructor with API key.
      Parameters:
      endpoint - MCP server endpoint URL
      apiKey - API key for Bearer authentication
    • withApiKey

      public static HttpTransport withApiKey(String endpoint, String apiKey, String headerName)
      Convenience constructor with API key header name.
      Parameters:
      endpoint - MCP server endpoint URL
      apiKey - API key value
      headerName - Header name for API key (e.g., "X-API-Key", "Authorization")
    • from

      public static HttpTransport from(McpServerConfig server)
      Creates HttpTransport from McpServerConfig.
      Parameters:
      server - MCP server configuration
    • 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
    • getEndpoint

      public String getEndpoint()
      Returns the configured endpoint.
    • getHeaders

      public Map<String,String> getHeaders()
      Returns the configured headers (excluding sensitive values).
    • getInterceptors

      public List<HttpInterceptor> getInterceptors()
      Returns the configured interceptor chain in registration order. Returned list is immutable.
      Since:
      0.10.5