Class McpServer.AsyncSpec

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

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

    • serverInfo

      public McpServer.AsyncSpec serverInfo(McpSchema.Implementation 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.AsyncSpec 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.AsyncSpec 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
      • Streaming responses
      • Batch operations
      Parameters:
      serverCapabilities - The server capabilities configuration. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if serverCapabilities is null
    • tool

      public McpServer.AsyncSpec tool(McpSchema.Tool tool, Function<Map<String,Object>,reactor.core.publisher.Mono<McpSchema.CallToolResult>> handler)
      Adds a single tool with its implementation handler to the server. This is a convenience method for registering individual tools without creating a McpServerFeatures.AsyncToolRegistration explicitly.

      Example usage:

      
       .tool(
           new Tool("calculator", "Performs calculations", schema),
           args -> Mono.just(new CallToolResult("Result: " + calculate(args)))
       )
       
      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.
      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:
      toolRegistrations - The list of tool registrations to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if toolRegistrations is null
      See Also:
    • tools

      public McpServer.AsyncSpec tools(McpServerFeatures.AsyncToolRegistration... toolRegistrations)
      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.AsyncToolRegistration(calculatorTool, calculatorHandler),
           new McpServerFeatures.AsyncToolRegistration(weatherTool, weatherHandler),
           new McpServerFeatures.AsyncToolRegistration(fileManagerTool, fileManagerHandler)
       )
       
      Parameters:
      toolRegistrations - The tool registrations to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if toolRegistrations 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:
      resourceRegsitrations - Map of resource name to registration. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceRegsitrations is null
      See Also:
    • resources

      public McpServer.AsyncSpec resources(List<McpServerFeatures.AsyncResourceRegistration> resourceRegsitrations)
      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:
      resourceRegsitrations - List of resource registrations. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if resourceRegsitrations is null
      See Also:
    • resources

      public McpServer.AsyncSpec resources(McpServerFeatures.AsyncResourceRegistration... resourceRegistrations)
      Registers multiple resources with their handlers using varargs. This method provides a convenient way to register multiple resources inline.

      Example usage:

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

      public McpServer.AsyncSpec 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
      See Also:
    • resourceTemplates

      public McpServer.AsyncSpec 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
      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.AsyncPromptRegistration(
           new Prompt("analysis", "Code analysis template"),
           request -> Mono.just(new GetPromptResult(generateAnalysisPrompt(request)))
       )));
       
      Parameters:
      prompts - Map of prompt name to registration. 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 registrations. 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.AsyncPromptRegistration(analysisPrompt, analysisHandler),
           new McpServerFeatures.AsyncPromptRegistration(summaryPrompt, summaryHandler),
           new McpServerFeatures.AsyncPromptRegistration(reviewPrompt, reviewHandler)
       )
       
      Parameters:
      prompts - The prompt registrations to add. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if prompts is null
    • rootsChangeConsumer

      public McpServer.AsyncSpec rootsChangeConsumer(Function<List<McpSchema.Root>,reactor.core.publisher.Mono<Void>> consumer)
      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:
      consumer - The consumer to register. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumer is null
    • rootsChangeConsumers

      public McpServer.AsyncSpec rootsChangeConsumers(List<Function<List<McpSchema.Root>,reactor.core.publisher.Mono<Void>>> consumers)
      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:
      consumers - The list of consumers to register. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumers is null
    • rootsChangeConsumers

      public McpServer.AsyncSpec rootsChangeConsumers(Function<List<McpSchema.Root>,reactor.core.publisher.Mono<Void>>... consumers)
      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:
      consumers - The consumers to register. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if consumers 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