You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk/java/mcp-client.mdx
+177Lines changed: 177 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,6 +186,71 @@ The transport layer handles the communication between MCP clients and servers, p
186
186
```
187
187
</Tab>
188
188
</Tabs>
189
+
190
+
**HttpClient: Customizing HTTP requests**
191
+
192
+
To customize the base HTTP request builder used for every request, provide a custom `HttpRequestBuilder`.
193
+
This is available in both Streamable HTTP and SSE transports.
194
+
When using a custom `HttpRequest.Builder`, every HTTP request issued by the transport
195
+
will use the hardcoded configuration from the builder.
196
+
For example, this is useful for providing a never-expiring security token in a header.
197
+
To add a `X-Custom-Header` header to every request, use:
198
+
199
+
```java
200
+
var requestBuilder =HttpRequest
201
+
.newBuilder()
202
+
.header("X-Custom-Header", "some header value");
203
+
204
+
HttpClientStreamableHttpTransport
205
+
.builder("https://mcp.example.com")
206
+
.requestBuilder(requestBuilder)
207
+
.build();
208
+
```
209
+
210
+
To dynamically modify HTTP request before they are issued, implement either `McpSyncHttpClientRequestCustomizer` or `McpAsyncHttpClientRequestCustomizer`.
211
+
Choose the request customizer matching the MCP Client type, sync or async.
212
+
Note that thread-locals may not be available in the customizers, context-related information
213
+
must be accessed through `McpTransportContext` (see [adding context information](#adding-context-information)).
// Ensure you call next.exchange to execute the HTTP request
317
+
return next.exchange(newRequest);
318
+
});
319
+
});
320
+
});
321
+
```
322
+
323
+
To learn how to populate context information, see [adding context information](#adding-context-information).
324
+
217
325
</Tab>
218
326
219
327
</Tabs>
@@ -525,3 +633,72 @@ Mono<CompleteResult> result = mcpClient.completeCompletion(request);
525
633
526
634
</Tab>
527
635
</Tabs>
636
+
637
+
### Adding context information
638
+
639
+
HTTP request sent through SSE or Streamable HTTP transport can be customized with
640
+
dedicated APIs (see [client transport](#client-transport)). These customizers may need
641
+
additional context-specific information that must be injected at the client level.
642
+
643
+
<Tabs>
644
+
<Tabtitle="Sync API">
645
+
646
+
The `McpSyncClient` is used in a blocking environment, and may rely on thread-locals to share information.
647
+
For example, some frameworks store the current server request or security tokens in a thread-local.
648
+
To make this type of information available to underlying transports, use `SyncSpec#transportContextProvider`:
649
+
650
+
```java
651
+
McpClient.sync(transport)
652
+
.transportContextProvider(() -> {
653
+
var data = obtainDataFromThreadLocals();
654
+
returnMcpTransportContext.create(
655
+
Map.of("some-data", data)
656
+
);
657
+
})
658
+
.build();
659
+
```
660
+
661
+
This `McpTransportContext` will be available in HttpClient-based `McpSyncHttpClientRequestCustomizer`
662
+
and WebClient-based `ExchangeFilterFunction`.
663
+
664
+
</Tab>
665
+
666
+
<Tabtitle="Async API">
667
+
668
+
The `McpAsyncClient` is used in a reactive environment.
669
+
Information is passed through the reactive chain using the [Reactor context](https://projectreactor.io/docs/core/release/reference/advancedFeatures/context.html).
670
+
To insert data in the Reactor context, use `contextWrite` when calling client operations:
0 commit comments