类 McpClientBuilder
Supports three transport types:
- StdIO - for local process communication
- SSE - for HTTP Server-Sent Events (stateful)
- StreamableHTTP - for HTTP streaming (stateless)
Example usage:
// StdIO transport
McpClientWrapper client = McpClientBuilder.create("git-mcp")
.stdioTransport("python", "-m", "mcp_server_git")
.buildAsync()
.block();
// SSE transport with headers and query parameters
McpClientWrapper client = McpClientBuilder.create("remote-mcp")
.sseTransport("https://mcp.example.com/sse")
.header("Authorization", "Bearer " + token)
.queryParam("queryKey", "queryValue")
.timeout(Duration.ofSeconds(60))
.buildAsync()
.block();
// HTTP transport with multiple query parameters
McpClientWrapper client = McpClientBuilder.create("http-mcp")
.streamableHttpTransport("https://mcp.example.com/http")
.queryParams(Map.of("token", "abc123", "env", "prod"))
.buildSync();
-
方法概要
修饰符和类型方法说明asyncElicitation(Function<io.modelcontextprotocol.spec.McpSchema.ElicitRequest, reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ElicitResult>> handler) Registers an asynchronous elicitation handler for processing elicit requests from the server.reactor.core.publisher.Mono<McpClientWrapper> Builds an asynchronous MCP client wrapper.Builds a synchronous MCP client wrapper (blocking operations).static McpClientBuilderCreates a new MCP client builder with the specified name.customizeSseClient(Consumer<HttpClient.Builder> customizer) Customizes the HTTP client for SSE transport (only applicable after calling sseTransport).customizeStreamableHttpClient(Consumer<HttpClient.Builder> customizer) Customizes the HTTP client for StreamableHTTP transport (only applicable after calling streamableHttpTransport).Adds an HTTP header (only applicable for HTTP transports).Sets multiple HTTP headers (only applicable for HTTP transports).initializationTimeout(Duration timeout) Sets the initialization timeout duration.queryParam(String key, String value) Adds a query parameter to the URL (only applicable for HTTP transports).queryParams(Map<String, String> queryParams) Sets multiple query parameters (only applicable for HTTP transports).sseTransport(String url) Configures HTTP SSE (Server-Sent Events) transport for stateful connections.stdioTransport(String command, String... args) Configures StdIO transport for local process communication.Configures StdIO transport with environment variables.Configures HTTP StreamableHTTP transport for stateless connections.syncElicitation(Function<io.modelcontextprotocol.spec.McpSchema.ElicitRequest, io.modelcontextprotocol.spec.McpSchema.ElicitResult> handler) Registers a synchronous elicitation handler for processing elicit requests from the server.Sets the request timeout duration.
-
方法详细资料
-
create
Creates a new MCP client builder with the specified name.- 参数:
name- unique identifier for the MCP client- 返回:
- new builder instance
-
stdioTransport
Configures StdIO transport for local process communication.- 参数:
command- the executable commandargs- command arguments- 返回:
- this builder
-
stdioTransport
Configures StdIO transport with environment variables.- 参数:
command- the executable commandargs- command arguments listenv- environment variables- 返回:
- this builder
-
sseTransport
Configures HTTP SSE (Server-Sent Events) transport for stateful connections.- 参数:
url- the server URL- 返回:
- this builder
-
customizeSseClient
Customizes the HTTP client for SSE transport (only applicable after calling sseTransport). This allows advanced HTTP client configuration like HTTP/2, custom timeouts, SSL settings, etc.Example usage for HTTP/2:
McpClientWrapper client = McpClientBuilder.create("mcp") .sseTransport("https://example.com/sse") .customizeSseClient(clientBuilder -> clientBuilder.version(java.net.http.HttpClient.Version.HTTP_2)) .buildAsync() .block();- 参数:
customizer- consumer to customize the HttpClient.Builder- 返回:
- this builder
-
streamableHttpTransport
Configures HTTP StreamableHTTP transport for stateless connections.- 参数:
url- the server URL- 返回:
- this builder
-
customizeStreamableHttpClient
Customizes the HTTP client for StreamableHTTP transport (only applicable after calling streamableHttpTransport). This allows advanced HTTP client configuration like HTTP/2, custom timeouts, SSL settings, etc.Example usage for HTTP/2:
McpClientWrapper client = McpClientBuilder.create("mcp") .streamableHttpTransport("https://example.com/http") .customizeStreamableHttpClient( clientBuilder -> clientBuilder.version(java.net.http.HttpClient.Version.HTTP_2)) .buildAsync() .block();- 参数:
customizer- consumer to customize the HttpClient.Builder- 返回:
- this builder
-
header
Adds an HTTP header (only applicable for HTTP transports).- 参数:
key- header namevalue- header value- 返回:
- this builder
-
headers
Sets multiple HTTP headers (only applicable for HTTP transports).- 参数:
headers- map of header name-value pairs- 返回:
- this builder
-
queryParam
Adds a query parameter to the URL (only applicable for HTTP transports).Query parameters added via this method will be merged with any existing query parameters in the URL. If the same parameter key exists in both the URL and the added parameters, the added parameter will take precedence.
- 参数:
key- query parameter namevalue- query parameter value- 返回:
- this builder
-
queryParams
Sets multiple query parameters (only applicable for HTTP transports).This method replaces any previously added query parameters. Query parameters in the original URL are still preserved and merged.
- 参数:
queryParams- map of query parameter name-value pairs- 返回:
- this builder
-
timeout
Sets the request timeout duration.- 参数:
timeout- timeout duration- 返回:
- this builder
-
initializationTimeout
Sets the initialization timeout duration.- 参数:
timeout- timeout duration- 返回:
- this builder
-
asyncElicitation
public McpClientBuilder asyncElicitation(Function<io.modelcontextprotocol.spec.McpSchema.ElicitRequest, reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ElicitResult>> handler) Registers an asynchronous elicitation handler for processing elicit requests from the server.When an elicitation handler is registered, the client will automatically enable the elicitation capability in ClientCapabilities.
This method is for use with
buildAsync(). The handler returns aMonofor asynchronous processing.Example usage:
McpClientWrapper client = McpClientBuilder.create("mcp") .stdioTransport("python", "-m", "mcp_server") .asyncElicitation(request -> { // Handle elicitation request asynchronously return Mono.just(ElicitResult.builder()...build()); }) .buildAsync() .block();- 参数:
handler- function to handle elicit requests asynchronously- 返回:
- this builder
-
syncElicitation
public McpClientBuilder syncElicitation(Function<io.modelcontextprotocol.spec.McpSchema.ElicitRequest, io.modelcontextprotocol.spec.McpSchema.ElicitResult> handler) Registers a synchronous elicitation handler for processing elicit requests from the server.When an elicitation handler is registered, the client will automatically enable the elicitation capability in ClientCapabilities.
This method is for use with
buildSync(). The handler returns anMcpSchema.ElicitResultdirectly for synchronous processing.Example usage:
McpClientWrapper client = McpClientBuilder.create("mcp") .stdioTransport("python", "-m", "mcp_server") .syncElicitation(request -> { // Handle elicitation request synchronously return ElicitResult.builder()...build(); }) .buildSync();- 参数:
handler- function to handle elicit requests synchronously- 返回:
- this builder
-
buildAsync
Builds an asynchronous MCP client wrapper.- 返回:
- Mono emitting the async client wrapper
-
buildSync
Builds a synchronous MCP client wrapper (blocking operations).- 返回:
- synchronous client wrapper
-