Package com.tnsai.mcp.client
Interface HttpInterceptor
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Pluggable hook in the
HttpTransport request/response path.
An interceptor sits between the transport and the underlying
HttpClient. It receives the outbound request via
HttpInterceptor.Chain.request(), may inspect or replace it, calls
HttpInterceptor.Chain.proceed(HttpRequest) to forward through any later
interceptors and ultimately the network, and finally returns (and may
transform) the response.
What it enables
- Auth refresh — catch 401, mint a fresh token, retry once.
- Rate limiting — block / queue requests when a token bucket is empty.
- Tracing / metrics — wrap each call in a span; record latency and status histograms.
- Payment-aware transport — catch HTTP 402, settle a micropayment, replay the request with payment proof (tracked under TNS-449).
Composition
Interceptors are invoked in the order they were registered with the transport. Each interceptor must callchain.proceed(...)
exactly zero or more times:
- Zero — short-circuit: return a synthetic response (offline cache hit, fast-fail on circuit breaker, etc.).
- Once — typical pass-through.
- More than once — retry: examine the response, decide it
should be reattempted, call
proceedagain with a different request.
Threading
The chain executes on the calling thread ofHttpTransport.send(String).
Implementations must be thread-safe if shared across transports.
Example — payment interceptor sketch
HttpInterceptor x402 = chain -> {
HttpResponse<String> response = chain.proceed(chain.request());
if (response.statusCode() != 402) {
return response;
}
Quote quote = parsePaymentRequirement(response.body());
Settlement settlement = broker.settle(quote);
HttpRequest paid = HttpRequest.newBuilder(chain.request(), (n, v) -> true)
.header("X-PAYMENT", settlement.paymentToken())
.build();
return chain.proceed(paid);
};
- Since:
- 0.10.5
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceForwards a request through the remaining interceptor chain and ultimately the network. -
Method Summary
Modifier and TypeMethodDescriptionintercept(HttpInterceptor.Chain chain) Invokes this interceptor with the given chain.
-
Method Details
-
intercept
Invokes this interceptor with the given chain.- Parameters:
chain- provides access to the outbound request and the ability to forward it through later interceptors- Returns:
- the response to return to the transport (typically the
result of
HttpInterceptor.Chain.proceed(HttpRequest), optionally transformed) - Throws:
IOException- if the interceptor cannot produce a response
-