Package io.modelcontextprotocol.server
Class McpServer.SyncSpecification
java.lang.Object
io.modelcontextprotocol.server.McpServer.SyncSpecification
- Enclosing interface:
- McpServer
Synchronous server specification.
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds a synchronous MCP server that provides blocking operations.capabilities(McpSchema.ServerCapabilities serverCapabilities) Sets the server capabilities that will be advertised to clients during connection initialization.completions(McpServerFeatures.SyncCompletionSpecification... completions) Registers multiple completions with their handlers using varargs.completions(List<McpServerFeatures.SyncCompletionSpecification> 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.objectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Sets the object mapper to use for serializing and deserializing JSON messages.prompts(McpServerFeatures.SyncPromptSpecification... 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.SyncResourceSpecification... resourceSpecifications) Registers multiple resources with their handlers using varargs.resources(List<McpServerFeatures.SyncResourceSpecification> resourceSpecifications) Registers multiple resources with their handlers using a List.resources(Map<String, McpServerFeatures.SyncResourceSpecification> 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.Registers a consumer that will be notified when the list of roots changes.rootsChangeHandlers(BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>... handlers) Registers multiple consumers that will be notified when the list of roots changes using varargs.rootsChangeHandlers(List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> 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<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> handler) Adds a single tool with its implementation handler to the server.tools(McpServerFeatures.SyncToolSpecification... toolSpecifications) Adds multiple tools with their handlers to the server using varargs.tools(List<McpServerFeatures.SyncToolSpecification> 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.
-
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
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
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
public McpServer.SyncSpecification tool(McpSchema.Tool tool, BiFunction<McpSyncServerExchange, Map<String, Object>, 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 aMcpServerFeatures.SyncToolSpecificationexplicitly.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 anMcpSyncServerExchangeupon 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
public McpServer.SyncSpecification tools(List<McpServerFeatures.SyncToolSpecification> 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.SyncSpecification tools(McpServerFeatures.SyncToolSpecification... 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( 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
public McpServer.SyncSpecification resources(Map<String, McpServerFeatures.SyncResourceSpecification> 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.SyncSpecification resources(List<McpServerFeatures.SyncResourceSpecification> 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.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 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.SyncSpecification prompts(Map<String, McpServerFeatures.SyncPromptSpecification> 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
public McpServer.SyncSpecification completions(List<McpServerFeatures.SyncCompletionSpecification> 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
public McpServer.SyncSpecification completions(McpServerFeatures.SyncCompletionSpecification... 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.SyncSpecification rootsChangeHandler(BiConsumer<McpSyncServerExchange, List<McpSchema.Root>> 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 anMcpSyncServerExchangeupon 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.SyncSpecification rootsChangeHandlers(List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> 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.SyncSpecification rootsChangeHandlers(BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>... 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.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
Builds a synchronous MCP server that provides blocking operations.- Returns:
- A new instance of
McpSyncServerconfigured with this builder's settings.
-