Package io.modelcontextprotocol.server
Class McpServer.AsyncSpecification<S extends McpServer.AsyncSpecification<S>>
java.lang.Object
io.modelcontextprotocol.server.McpServer.AsyncSpecification<S>
- Direct Known Subclasses:
McpServer.SingleSessionAsyncSpecification,McpServer.StreamableServerAsyncSpecification
- Enclosing interface:
- McpServer
public abstract static class McpServer.AsyncSpecification<S extends McpServer.AsyncSpecification<S>>
extends Object
Asynchronous server specification.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract McpAsyncServerbuild()capabilities(McpSchema.ServerCapabilities serverCapabilities) Sets the server capabilities that will be advertised to clients during connection initialization.completions(McpServerFeatures.AsyncCompletionSpecification... completions) Registers multiple completions with their handlers using varargs.completions(List<McpServerFeatures.AsyncCompletionSpecification> completions) Registers multiple completions with their handlers using a List.instructions(String instructions) Sets the server instructions that will be shared with clients during connection initialization.jsonSchemaValidator(JsonSchemaValidator jsonSchemaValidator) Sets the JSON schema validator to use for validating tool and resource schemas.objectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Sets the object mapper to use for serializing and deserializing JSON messages.prompts(McpServerFeatures.AsyncPromptSpecification... prompts) Registers multiple prompts with their handlers using varargs.Registers multiple prompts with their handlers using a List.Registers multiple prompts with their handlers using a Map.requestTimeout(Duration requestTimeout) Sets the duration to wait for server responses before timing out requests.resources(McpServerFeatures.AsyncResourceSpecification... resourceSpecifications) Registers multiple resources with their handlers using varargs.resources(List<McpServerFeatures.AsyncResourceSpecification> resourceSpecifications) Registers multiple resources with their handlers using a List.resources(Map<String, McpServerFeatures.AsyncResourceSpecification> resourceSpecifications) Registers multiple resources with their handlers using a Map.resourceTemplates(McpSchema.ResourceTemplate... resourceTemplates) Sets the resource templates using varargs for convenience.resourceTemplates(List<McpSchema.ResourceTemplate> resourceTemplates) Sets the resource templates that define patterns for dynamic resource access.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.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.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.serverInfo(McpSchema.Implementation serverInfo) Sets the server implementation information that will be shared with clients during connection initialization.serverInfo(String name, String version) Sets the server implementation information using name and version strings.tool(McpSchema.Tool tool, BiFunction<McpAsyncServerExchange, Map<String, Object>, reactor.core.publisher.Mono<McpSchema.CallToolResult>> handler) Deprecated.toolCall(McpSchema.Tool tool, BiFunction<McpAsyncServerExchange, McpSchema.CallToolRequest, reactor.core.publisher.Mono<McpSchema.CallToolResult>> callHandler) Adds a single tool with its implementation handler to the server.tools(McpServerFeatures.AsyncToolSpecification... toolSpecifications) Adds multiple tools with their handlers to the server using varargs.tools(List<McpServerFeatures.AsyncToolSpecification> toolSpecifications) Adds multiple tools with their handlers to the server using a List.uriTemplateManagerFactory(McpUriTemplateManagerFactory uriTemplateManagerFactory) Sets the URI template manager factory to use for creating URI templates.
-
Constructor Details
-
AsyncSpecification
public AsyncSpecification()
-
-
Method Details
-
build
-
uriTemplateManagerFactory
public McpServer.AsyncSpecification<S> 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
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
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
Sets the server implementation information using name and version strings. This is a convenience method alternative toserverInfo(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
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.AsyncSpecification<S> 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
@Deprecated public McpServer.AsyncSpecification<S> tool(McpSchema.Tool tool, BiFunction<McpAsyncServerExchange, Map<String, Object>, reactor.core.publisher.Mono<McpSchema.CallToolResult>> handler) Deprecated.UsetoolCall(McpSchema.Tool, BiFunction)instead for tool calls that require a request object.Adds a single tool with its implementation handler to the server. This is a convenience method for registering individual tools without creating aMcpServerFeatures.AsyncToolSpecificationexplicitly.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 anMcpAsyncServerExchangeupon 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
-
toolCall
public McpServer.AsyncSpecification<S> toolCall(McpSchema.Tool tool, BiFunction<McpAsyncServerExchange, McpSchema.CallToolRequest, reactor.core.publisher.Mono<McpSchema.CallToolResult>> callHandler) Adds a single tool with its implementation handler to the server. This is a convenience method for registering individual tools without creating aMcpServerFeatures.AsyncToolSpecificationexplicitly.- Parameters:
tool- The tool definition including name, description, and schema. Must not be null.callHandler- The function that implements the tool's logic. Must not be null. The function's first argument is anMcpAsyncServerExchangeupon which the server can interact with the connected client. The second argument is theMcpSchema.CallToolRequestobject containing the tool call- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if tool or handler is null
-
tools
public McpServer.AsyncSpecification<S> tools(List<McpServerFeatures.AsyncToolSpecification> toolSpecifications) 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
public McpServer.AsyncSpecification<S> tools(McpServerFeatures.AsyncToolSpecification... toolSpecifications) 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( McpServerFeatures.AsyncToolSpecification.builder().tool(calculatorTool).callTool(calculatorHandler).build(), McpServerFeatures.AsyncToolSpecification.builder().tool(weatherTool).callTool(weatherHandler).build(), McpServerFeatures.AsyncToolSpecification.builder().tool(fileManagerTool).callTool(fileManagerHandler).build() )- Parameters:
toolSpecifications- The tool specifications to add. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if toolSpecifications is null
-
resources
public McpServer.AsyncSpecification<S> resources(Map<String, McpServerFeatures.AsyncResourceSpecification> resourceSpecifications) 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
public McpServer.AsyncSpecification<S> resources(List<McpServerFeatures.AsyncResourceSpecification> resourceSpecifications) 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<S> 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<S> 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<S> resourceTemplates(McpSchema.ResourceTemplate... resourceTemplates) Sets the resource templates using varargs for convenience. This is an alternative toresourceTemplates(List).- Parameters:
resourceTemplates- The resource templates to set.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if resourceTemplates is null.- See Also:
-
prompts
public McpServer.AsyncSpecification<S> prompts(Map<String, McpServerFeatures.AsyncPromptSpecification> 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
public McpServer.AsyncSpecification<S> prompts(List<McpServerFeatures.AsyncPromptSpecification> 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
public McpServer.AsyncSpecification<S> prompts(McpServerFeatures.AsyncPromptSpecification... 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
-
completions
public McpServer.AsyncSpecification<S> completions(List<McpServerFeatures.AsyncCompletionSpecification> 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
-
completions
public McpServer.AsyncSpecification<S> completions(McpServerFeatures.AsyncCompletionSpecification... 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
public McpServer.AsyncSpecification<S> 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 anMcpAsyncServerExchangeupon 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<S> 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<S> 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<S> 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
-
jsonSchemaValidator
Sets the JSON schema validator to use for validating tool and resource schemas. This ensures that the server's tools and resources conform to the expected schema definitions.- Parameters:
jsonSchemaValidator- The validator to use. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if jsonSchemaValidator is null
-
toolCall(McpSchema.Tool, BiFunction)instead for tool calls that require a request object.