Package io.modelcontextprotocol.server
Class McpServer.StatelessAsyncSpecification
java.lang.Object
io.modelcontextprotocol.server.McpServer.StatelessAsyncSpecification
- Enclosing interface:
- McpServer
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()capabilities(McpSchema.ServerCapabilities serverCapabilities) Sets the server capabilities that will be advertised to clients during connection initialization.completions(McpStatelessServerFeatures.AsyncCompletionSpecification... completions) Registers multiple completions with their handlers using varargs.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.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(McpStatelessServerFeatures.AsyncResourceSpecification... resourceSpecifications) Registers multiple resources with their handlers using varargs.resources(List<McpStatelessServerFeatures.AsyncResourceSpecification> resourceSpecifications) Registers multiple resources with their handlers using a List.resources(Map<String, McpStatelessServerFeatures.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.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.toolCall(McpSchema.Tool tool, BiFunction<McpTransportContext, McpSchema.CallToolRequest, reactor.core.publisher.Mono<McpSchema.CallToolResult>> callHandler) Adds a single tool with its implementation handler to the server.tools(McpStatelessServerFeatures.AsyncToolSpecification... toolSpecifications) Adds multiple tools with their handlers to the server using varargs.tools(List<McpStatelessServerFeatures.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
-
StatelessAsyncSpecification
-
-
Method Details
-
uriTemplateManagerFactory
public McpServer.StatelessAsyncSpecification 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.StatelessAsyncSpecification 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
-
toolCall
public McpServer.StatelessAsyncSpecification toolCall(McpSchema.Tool tool, BiFunction<McpTransportContext, 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.StatelessAsyncSpecification tools(List<McpStatelessServerFeatures.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.StatelessAsyncSpecification tools(McpStatelessServerFeatures.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.StatelessAsyncSpecification resources(Map<String, McpStatelessServerFeatures.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.StatelessAsyncSpecification resources(List<McpStatelessServerFeatures.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.StatelessAsyncSpecification resources(McpStatelessServerFeatures.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.StatelessAsyncSpecification 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.StatelessAsyncSpecification 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.StatelessAsyncSpecification prompts(Map<String, McpStatelessServerFeatures.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.StatelessAsyncSpecification prompts(List<McpStatelessServerFeatures.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.StatelessAsyncSpecification prompts(McpStatelessServerFeatures.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.StatelessAsyncSpecification completions(List<McpStatelessServerFeatures.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.StatelessAsyncSpecification completions(McpStatelessServerFeatures.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
-
objectMapper
public McpServer.StatelessAsyncSpecification 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
public McpServer.StatelessAsyncSpecification jsonSchemaValidator(JsonSchemaValidator 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
-
build
-