Class HigressMcpClientBuilder

java.lang.Object
io.agentscope.extensions.higress.HigressMcpClientBuilder

public class HigressMcpClientBuilder extends Object
Builder for creating HigressMcpClientWrapper instances.

This builder follows the same pattern as McpClientBuilder in agentscope-core, providing a fluent API for configuring and building Higress MCP clients.

Supports two transport types:

  • SSE (Server-Sent Events) - for stateful connections with server push
  • StreamableHTTP - for stateless HTTP streaming

Example usage with SSE transport:


 HigressMcpClientWrapper client = HigressMcpClientBuilder
     .create("higress-mcp")
     .sseEndpoint("http://higress-gateway/mcp-servers/union-tools-search/sse")
     .build();
 

Example usage with StreamableHTTP transport:


 HigressMcpClientWrapper client = HigressMcpClientBuilder
     .create("higress-mcp")
     .streamableHttpEndpoint("http://higress-gateway/mcp-servers/union-tools-search")
     .build();
 

Example with authentication:


 HigressMcpClientWrapper client = HigressMcpClientBuilder
     .create("higress-mcp")
     .sseEndpoint("http://higress-gateway/mcp-servers/union-tools-search/sse")
     .header("Authorization", "Bearer " + token)
     .header("X-Api-Key", apiKey)
     .timeout(Duration.ofSeconds(60))
     .buildAsync()
     .block();
 
See Also:
  • Method Details

    • create

      public static HigressMcpClientBuilder create(String clientName)
      Creates a new builder with the specified client name.
      Parameters:
      clientName - unique identifier for the MCP client
      Returns:
      new builder instance
      Throws:
      IllegalArgumentException - if clientName is null or empty
    • sseEndpoint

      public HigressMcpClientBuilder sseEndpoint(String endpoint)
      Configures SSE (Server-Sent Events) transport endpoint.

      SSE transport is recommended for scenarios requiring real-time server push and stateful connections.

      Parameters:
      endpoint - the SSE endpoint URL (e.g., "http://higress-gateway/mcp-servers/union-tools-search/sse")
      Returns:
      this builder for method chaining
    • streamableHttpEndpoint

      public HigressMcpClientBuilder streamableHttpEndpoint(String endpoint)
      Configures StreamableHTTP transport endpoint.

      StreamableHTTP transport is suitable for stateless HTTP streaming scenarios.

      Parameters:
      endpoint - the StreamableHTTP endpoint URL (e.g., "http://higress-gateway/mcp-servers/union-tools-search")
      Returns:
      this builder for method chaining
    • header

      public HigressMcpClientBuilder header(String key, String value)
      Adds an HTTP header to be sent with each request.

      Common use cases include authentication headers:

      • header("Authorization", "Bearer " + token)
      • header("X-Api-Key", apiKey)
      Parameters:
      key - header name
      value - header value
      Returns:
      this builder for method chaining
    • headers

      public HigressMcpClientBuilder headers(Map<String,String> headers)
      Sets multiple HTTP headers at once.
      Parameters:
      headers - map of header name-value pairs
      Returns:
      this builder for method chaining
    • queryParam

      public HigressMcpClientBuilder queryParam(String key, String value)
      Adds a query parameter to the URL.

      Query parameters added via this method will be merged with any existing query parameters in the URL.

      Example:

      
       HigressMcpClientWrapper client = HigressMcpClientBuilder
           .create("higress")
           .streamableHttpEndpoint("http://gateway/mcp")
           .queryParam("token", "abc123")
           .queryParam("env", "prod")
           .build();
       
      Parameters:
      key - query parameter name
      value - query parameter value
      Returns:
      this builder for method chaining
    • queryParams

      public HigressMcpClientBuilder queryParams(Map<String,String> queryParams)
      Sets multiple query parameters at once.
      Parameters:
      queryParams - map of query parameter name-value pairs
      Returns:
      this builder for method chaining
    • timeout

      public HigressMcpClientBuilder timeout(Duration timeout)
      Sets the request timeout duration.

      This timeout applies to individual MCP requests (tool calls, list tools, etc.). Default is 120 seconds.

      Parameters:
      timeout - timeout duration
      Returns:
      this builder for method chaining
    • initializationTimeout

      public HigressMcpClientBuilder initializationTimeout(Duration timeout)
      Sets the initialization timeout duration.

      This timeout applies to the client initialization process. Default is 30 seconds.

      Parameters:
      timeout - timeout duration
      Returns:
      this builder for method chaining
    • toolSearch

      public HigressMcpClientBuilder toolSearch(String query)
      Enables tool search with the specified query.

      When enabled, listTools() will call x_higress_tool_search with the query, and return only the semantically relevant tools (default top 10).

      Example:

      
       HigressMcpClientWrapper client = HigressMcpClientBuilder
           .create("higress")
           .streamableHttpEndpoint(endpoint)
           .toolSearch("查询天气")
           .build();
       
      Parameters:
      query - the search query describing what tools are needed
      Returns:
      this builder for method chaining
    • toolSearch

      public HigressMcpClientBuilder toolSearch(String query, int topK)
      Enables tool search with the specified query and topK.

      Example:

      
       HigressMcpClientWrapper client = HigressMcpClientBuilder
           .create("higress")
           .streamableHttpEndpoint(endpoint)
           .toolSearch("查询天气", 5)
           .build();
       
      Parameters:
      query - the search query describing what tools are needed
      topK - the maximum number of tools to return (must be positive)
      Returns:
      this builder for method chaining
      Throws:
      IllegalArgumentException - if topK is not positive
    • buildAsync

      public reactor.core.publisher.Mono<HigressMcpClientWrapper> buildAsync()
      Builds an asynchronous HigressMcpClientWrapper instance.

      This method returns a Mono that, when subscribed:

      1. Validates the configuration
      2. Creates the underlying MCP client using McpClientBuilder
      3. Wraps it in a HigressMcpClientWrapper
      4. Initializes the client

      Example usage:

      
       HigressMcpClientWrapper client = HigressMcpClientBuilder
           .create("higress-mcp")
           .streamableHttpEndpoint(endpoint)
           .buildAsync()
           .block();
       
      Returns:
      Mono emitting the configured HigressMcpClientWrapper instance
      Throws:
      IllegalArgumentException - if endpoint is not configured
      IllegalStateException - if transport type is not configured
    • buildSync

      public HigressMcpClientWrapper buildSync()
      Builds a synchronous HigressMcpClientWrapper instance (blocking operation).

      This method:

      1. Validates the configuration
      2. Creates the underlying synchronous MCP client using McpClientBuilder
      3. Wraps it in a HigressMcpClientWrapper
      4. Initializes the client

      Example usage:

      
       HigressMcpClientWrapper client = HigressMcpClientBuilder
           .create("higress-mcp")
           .streamableHttpEndpoint(endpoint)
           .buildSync();
       
      Returns:
      configured and initialized HigressMcpClientWrapper instance
      Throws:
      IllegalArgumentException - if endpoint is not configured
      IllegalStateException - if transport type is not configured