Class McpSyncServer
McpAsyncServer to provide blocking operations. This class delegates all
operations to an underlying async server instance while providing a simpler,
synchronous API for scenarios where reactive programming is not required.
The MCP server enables AI models to expose tools, resources, and prompts through a standardized interface. Key features available through this synchronous API include:
- Tool registration and management for extending AI model capabilities
- Resource handling with URI-based addressing for providing context
- Prompt template management for standardized interactions
- Real-time client notifications for state changes
- Structured logging with configurable severity levels
- Support for client-side AI model sampling
While McpAsyncServer uses Project Reactor's Mono and Flux types for
non-blocking operations, this class converts those into blocking calls, making it more
suitable for:
- Traditional synchronous applications
- Simple scripting scenarios
- Testing and debugging
- Cases where reactive programming adds unnecessary complexity
The server supports runtime modification of its capabilities through methods like
addTool(io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification), addResource(io.modelcontextprotocol.server.McpServerFeatures.SyncResourceSpecification), and addPrompt(io.modelcontextprotocol.server.McpServerFeatures.SyncPromptSpecification), automatically notifying
connected clients of changes when configured to do so.
- Author:
- Christian Tzolov, Dariusz Jędrzejczyk
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMcpSyncServer(McpAsyncServer asyncServer) Creates a new synchronous server that wraps the provided async server.McpSyncServer(McpAsyncServer asyncServer, boolean immediateExecution) Creates a new synchronous server that wraps the provided async server. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification) Add a new prompt handler.voidaddResource(McpServerFeatures.SyncResourceSpecification resourceSpecification) Add a new resource handler.voidaddResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceTemplateSpecification) Add a new resource template.voidaddTool(McpServerFeatures.SyncToolSpecification toolHandler) Add a new tool handler.voidclose()Close the server immediately.voidClose the server gracefully.Get the underlying async server instance.Get the server capabilities that define the supported features and functionality.Get the server implementation information.List all registered prompts.List all registered resources.List all registered resource templates.List all registered tools.voidNotify clients that the list of available prompts has changed.voidNotify clients that the list of available resources has changed.voidnotifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification) Notify clients that the resources have updated.voidNotify clients that the list of available tools has changed.voidremovePrompt(String promptName) Remove a prompt handler.voidremoveResource(String resourceUri) Remove a resource handler.voidremoveResourceTemplate(String uriTemplate) Remove a resource template.voidremoveTool(String toolName) Remove a tool handler.
-
Constructor Details
-
McpSyncServer
Creates a new synchronous server that wraps the provided async server.- Parameters:
asyncServer- The async server to wrap
-
McpSyncServer
Creates a new synchronous server that wraps the provided async server.- Parameters:
asyncServer- The async server to wrapimmediateExecution- Tools, prompts, and resources handlers execute work without blocking code offloading. Do NOT set to true if theasyncServer's transport is non-blocking.
-
-
Method Details
-
addTool
Add a new tool handler.- Parameters:
toolHandler- The tool handler to add
-
listTools
List all registered tools.- Returns:
- A list of all registered tools
-
removeTool
Remove a tool handler.- Parameters:
toolName- The name of the tool handler to remove
-
addResource
Add a new resource handler.- Parameters:
resourceSpecification- The resource specification to add
-
listResources
List all registered resources.- Returns:
- A list of all registered resources
-
removeResource
Remove a resource handler.- Parameters:
resourceUri- The URI of the resource handler to remove
-
addResourceTemplate
public void addResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceTemplateSpecification) Add a new resource template.- Parameters:
resourceTemplateSpecification- The resource template specification to add
-
listResourceTemplates
List all registered resource templates.- Returns:
- A list of all registered resource templates
-
removeResourceTemplate
Remove a resource template.- Parameters:
uriTemplate- The URI template of the resource template to remove
-
addPrompt
Add a new prompt handler.- Parameters:
promptSpecification- The prompt specification to add
-
listPrompts
List all registered prompts.- Returns:
- A list of all registered prompts
-
removePrompt
Remove a prompt handler.- Parameters:
promptName- The name of the prompt handler to remove
-
notifyToolsListChanged
public void notifyToolsListChanged()Notify clients that the list of available tools has changed. -
getServerCapabilities
Get the server capabilities that define the supported features and functionality.- Returns:
- The server capabilities
-
getServerInfo
Get the server implementation information.- Returns:
- The server implementation details
-
notifyResourcesListChanged
public void notifyResourcesListChanged()Notify clients that the list of available resources has changed. -
notifyResourcesUpdated
public void notifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification) Notify clients that the resources have updated. -
notifyPromptsListChanged
public void notifyPromptsListChanged()Notify clients that the list of available prompts has changed. -
closeGracefully
public void closeGracefully()Close the server gracefully. -
close
public void close()Close the server immediately. -
getAsyncServer
Get the underlying async server instance.- Returns:
- The wrapped async server
-