Class McpServer.AsyncSpecification

java.lang.Object
io.modelcontextprotocol.server.McpServer.AsyncSpecification
Enclosing interface:
McpServer

public static class McpServer.AsyncSpecification extends Object
Asynchronous server specification.
  • Method Details

    • serverInfo

      Sets the server implementation information that will be shared with clients during connection initialization. This helps with version compatibility, debugging, and server identification.
      Parameters:
      serverInfo - The server implementation details including name and version. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if serverInfo is null
    • serverInfo

      public McpServer.AsyncSpecification serverInfo(String name, String version)
      Sets the server implementation information using name and version strings. This is a convenience method alternative to serverInfo(McpSchema.Implementation).
      Parameters:
      name - The server name. Must not be null or empty.
      version - The server version. Must not be null or empty.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if name or version is null or empty
      See Also:
    • capabilities

      public McpServer.AsyncSpecification capabilities(McpSchema.ServerCapabilities serverCapabilities)
      Sets the server capabilities that will be advertised to clients during connection initialization. Capabilities define what features the server supports, such as:
      • Tool execution
      • Resource access
      • Prompt handling
      Parameters:
      serverCapabilities - The server capabilities configuration. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if serverCapabilities is null
    • tool

      Adds a single tool with its implementation handler to the server. This is a convenience method for registering individual tools without creating a McpServerFeatures.AsyncToolSpecification explicitly.

      Example usage:

      
       .tool(
           new Tool("calculator", "Performs calculations", schema),
           (exchange, args) -> Mono.fromSupplier(() -> calculate(args))
               .map(result -> new CallToolResult("Result: " + result))
       )
       
      Parameters:
      tool - The tool definition including name, description, and schema. Must not be null.
      handler - The function that implements the tool's logic. Must not be null. The function's first argument is an McpAsyncServerExchange upon which the server can interact with the connected client. The second argument is the map of arguments passed to the tool.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if tool or handler is null
    • tools

      Adds multiple tools with their handlers to the server using a List. This method is useful when tools are dynamically generated or loaded from a configuration source.
      Parameters:
      toolSpecifications - The list of tool specifications to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if toolSpecifications is null
      See Also:
    • tools

      Adds multiple tools with their handlers to the server using varargs. This method provides a convenient way to register multiple tools inline.

      Example usage:

      
       .tools(
           new McpServerFeatures.AsyncToolSpecification(calculatorTool, calculatorHandler),
           new McpServerFeatures.AsyncToolSpecification(weatherTool, weatherHandler),
           new McpServerFeatures.AsyncToolSpecification(fileManagerTool, fileManagerHandler)
       )
       
      Parameters:
      toolSpecifications - The tool specifications to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if toolSpecifications is null
      See Also:
    • resources

      Registers multiple resources with their handlers using a Map. This method is useful when resources are dynamically generated or loaded from a configuration source.
      Parameters:
      resourceSpecifications - Map of resource name to specification. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceSpecifications is null
      See Also:
    • resources

      Registers multiple resources with their handlers using a List. This method is useful when resources need to be added in bulk from a collection.
      Parameters:
      resourceSpecifications - List of resource specifications. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceSpecifications is null
      See Also:
    • resources

      public McpServer.AsyncSpecification resources(McpServerFeatures.AsyncResourceSpecification... resourceSpecifications)
      Registers multiple resources with their handlers using varargs. This method provides a convenient way to register multiple resources inline.

      Example usage:

      
       .resources(
           new McpServerFeatures.AsyncResourceSpecification(fileResource, fileHandler),
           new McpServerFeatures.AsyncResourceSpecification(dbResource, dbHandler),
           new McpServerFeatures.AsyncResourceSpecification(apiResource, apiHandler)
       )
       
      Parameters:
      resourceSpecifications - The resource specifications to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceSpecifications is null
    • resourceTemplates

      public McpServer.AsyncSpecification resourceTemplates(List<McpSchema.ResourceTemplate> resourceTemplates)
      Sets the resource templates that define patterns for dynamic resource access. Templates use URI patterns with placeholders that can be filled at runtime.

      Example usage:

      
       .resourceTemplates(
           new ResourceTemplate("file://{path}", "Access files by path"),
           new ResourceTemplate("db://{table}/{id}", "Access database records")
       )
       
      Parameters:
      resourceTemplates - List of resource templates. If null, clears existing templates.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceTemplates is null.
      See Also:
    • resourceTemplates

      public McpServer.AsyncSpecification resourceTemplates(McpSchema.ResourceTemplate... resourceTemplates)
      Sets the resource templates using varargs for convenience. This is an alternative to resourceTemplates(List).
      Parameters:
      resourceTemplates - The resource templates to set.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceTemplates is null.
      See Also:
    • prompts

      Registers multiple prompts with their handlers using a Map. This method is useful when prompts are dynamically generated or loaded from a configuration source.

      Example usage:

      
       .prompts(Map.of("analysis", new McpServerFeatures.AsyncPromptSpecification(
           new Prompt("analysis", "Code analysis template"),
           request -> Mono.fromSupplier(() -> generateAnalysisPrompt(request))
               .map(GetPromptResult::new)
       )));
       
      Parameters:
      prompts - Map of prompt name to specification. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if prompts is null
    • prompts

      Registers multiple prompts with their handlers using a List. This method is useful when prompts need to be added in bulk from a collection.
      Parameters:
      prompts - List of prompt specifications. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if prompts is null
      See Also:
    • prompts

      Registers multiple prompts with their handlers using varargs. This method provides a convenient way to register multiple prompts inline.

      Example usage:

      
       .prompts(
           new McpServerFeatures.AsyncPromptSpecification(analysisPrompt, analysisHandler),
           new McpServerFeatures.AsyncPromptSpecification(summaryPrompt, summaryHandler),
           new McpServerFeatures.AsyncPromptSpecification(reviewPrompt, reviewHandler)
       )
       
      Parameters:
      prompts - The prompt specifications to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if prompts is null
    • rootsChangeHandler

      public McpServer.AsyncSpecification rootsChangeHandler(BiFunction<McpAsyncServerExchange,List<McpSchema.Root>,reactor.core.publisher.Mono<Void>> handler)
      Registers a consumer that will be notified when the list of roots changes. This is useful for updating resource availability dynamically, such as when new files are added or removed.
      Parameters:
      handler - The handler to register. Must not be null. The function's first argument is an McpAsyncServerExchange upon which the server can interact with the connected client. The second argument is the list of roots.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumer is null
    • rootsChangeHandlers

      public McpServer.AsyncSpecification rootsChangeHandlers(List<BiFunction<McpAsyncServerExchange,List<McpSchema.Root>,reactor.core.publisher.Mono<Void>>> handlers)
      Registers multiple consumers that will be notified when the list of roots changes. This method is useful when multiple consumers need to be registered at once.
      Parameters:
      handlers - The list of handlers to register. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumers is null
      See Also:
    • rootsChangeHandlers

      public McpServer.AsyncSpecification rootsChangeHandlers(BiFunction<McpAsyncServerExchange,List<McpSchema.Root>,reactor.core.publisher.Mono<Void>>... handlers)
      Registers multiple consumers that will be notified when the list of roots changes using varargs. This method provides a convenient way to register multiple consumers inline.
      Parameters:
      handlers - The handlers to register. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumers is null
      See Also:
    • objectMapper

      public McpServer.AsyncSpecification objectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Sets the object mapper to use for serializing and deserializing JSON messages.
      Parameters:
      objectMapper - the instance to use. Must not be null.
      Returns:
      This builder instance for method chaining.
      Throws:
      IllegalArgumentException - if objectMapper is null
    • build

      public McpAsyncServer build()
      Builds an asynchronous MCP server that provides non-blocking operations.
      Returns:
      A new instance of McpAsyncServer configured with this builder's settings.