Class McpServer.SyncSpecification

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

public static class McpServer.SyncSpecification extends Object
Synchronous server specification.
  • Method Details

    • uriTemplateManagerFactory

      public McpServer.SyncSpecification uriTemplateManagerFactory(McpUriTemplateManagerFactory uriTemplateManagerFactory)
      Sets the URI template manager factory to use for creating URI templates. This allows for custom URI template parsing and variable extraction.
      Parameters:
      uriTemplateManagerFactory - The factory to use. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if uriTemplateManagerFactory is null
    • requestTimeout

      public McpServer.SyncSpecification requestTimeout(Duration requestTimeout)
      Sets the duration to wait for server responses before timing out requests. This timeout applies to all requests made through the client, including tool calls, resource access, and prompt operations.
      Parameters:
      requestTimeout - The duration to wait before timing out requests. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if requestTimeout is null
    • serverInfo

      public McpServer.SyncSpecification 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.SyncSpecification 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:
    • instructions

      public McpServer.SyncSpecification instructions(String instructions)
      Sets the server instructions that will be shared with clients during connection initialization. These instructions provide guidance to the client on how to interact with this server.
      Parameters:
      instructions - The instructions text. Can be null or empty.
      Returns:
      This builder instance for method chaining
    • capabilities

      public McpServer.SyncSpecification 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.SyncToolSpecification explicitly.

      Example usage:

      
       .tool(
           new Tool("calculator", "Performs calculations", schema),
           (exchange, args) -> 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. The function's first argument is an McpSyncServerExchange upon which the server can interact with the connected client. The second argument is the list 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 ToolSpecification(calculatorTool, calculatorHandler),
           new ToolSpecification(weatherTool, weatherHandler),
           new ToolSpecification(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.SyncSpecification resources(McpServerFeatures.SyncResourceSpecification... resourceSpecifications)
      Registers multiple resources with their handlers using varargs. This method provides a convenient way to register multiple resources inline.

      Example usage:

      
       .resources(
           new ResourceSpecification(fileResource, fileHandler),
           new ResourceSpecification(dbResource, dbHandler),
           new ResourceSpecification(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.SyncSpecification 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.SyncSpecification 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:

      
       Map<String, PromptSpecification> prompts = new HashMap<>();
       prompts.put("analysis", new PromptSpecification(
           new Prompt("analysis", "Code analysis template"),
           (exchange, request) -> new GetPromptResult(generateAnalysisPrompt(request))
       ));
       .prompts(prompts)
       
      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 PromptSpecification(analysisPrompt, analysisHandler),
           new PromptSpecification(summaryPrompt, summaryHandler),
           new PromptSpecification(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
    • completions

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

      Registers multiple completions with their handlers using varargs. This method is useful when completions are defined inline and added directly.
      Parameters:
      completions - Array of completion specifications. Must not be null.
      Returns:
      This builder instance for method chaining
      Throws:
      IllegalArgumentException - if completions is null
    • rootsChangeHandler

      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 McpSyncServerExchange 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

      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

      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.SyncSpecification 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 McpSyncServer build()
      Builds a synchronous MCP server that provides blocking operations.
      Returns:
      A new instance of McpSyncServer configured with this builder's settings.