Package com.tnsai.mcp.client
Class HttpTransport
java.lang.Object
com.tnsai.mcp.client.HttpTransport
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionHttpTransport(String endpoint, Map<String, String> headers) Creates HTTP transport with endpoint and custom headers.Creates HTTP transport with endpoint, headers, and timeout.HttpTransport(String endpoint, Map<String, String> headers, Duration timeout, List<HttpInterceptor> interceptors) Creates HTTP transport with endpoint, headers, timeout, and an ordered list ofHttpInterceptors. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()Connects to the MCP server.voidDisconnects from the MCP server.static HttpTransportfrom(McpServerConfig server) Creates HttpTransport from McpServerConfig.Returns the configured endpoint.Returns the configured headers (excluding sensitive values).Returns the configured interceptor chain in registration order.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.static HttpTransportwithApiKey(String endpoint, String apiKey) Convenience constructor with API key.static HttpTransportwithApiKey(String endpoint, String apiKey, String headerName) Convenience constructor with API key header name.
-
Constructor Details
-
HttpTransport
Creates HTTP transport with endpoint and custom headers.- Parameters:
endpoint- MCP server endpoint URLheaders- Custom headers (e.g., Authorization)
-
HttpTransport
Creates HTTP transport with endpoint, headers, and timeout.- Parameters:
endpoint- MCP server endpoint URLheaders- 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 ofHttpInterceptors.Interceptors execute in registration order; each call to
HttpInterceptor.Chain.proceed(HttpRequest)advances to the next interceptor and finally to the network. SeeHttpInterceptorfor the contract and composition rules.- Parameters:
endpoint- MCP server endpoint URLheaders- Custom headers (e.g., Authorization)timeout- Request timeout durationinterceptors- Ordered interceptor chain (may be null or empty)- Since:
- 0.10.5
-
-
Method Details
-
withApiKey
Convenience constructor with API key.- Parameters:
endpoint- MCP server endpoint URLapiKey- API key for Bearer authentication
-
withApiKey
Convenience constructor with API key header name.- Parameters:
endpoint- MCP server endpoint URLapiKey- API key valueheaderName- Header name for API key (e.g., "X-API-Key", "Authorization")
-
from
Creates HttpTransport from McpServerConfig.- Parameters:
server- MCP server configuration
-
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
-
getEndpoint
Returns the configured endpoint. -
getHeaders
Returns the configured headers (excluding sensitive values). -
getInterceptors
Returns the configured interceptor chain in registration order. Returned list is immutable.- Since:
- 0.10.5
-