java.lang.Object
io.modelcontextprotocol.server.McpSyncServer

public class McpSyncServer extends Object
A synchronous implementation of the Model Context Protocol (MCP) server that wraps 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 Details

    • McpSyncServer

      public McpSyncServer(McpAsyncServer asyncServer)
      Creates a new synchronous server that wraps the provided async server.
      Parameters:
      asyncServer - The async server to wrap
  • Method Details

    • addTool

      public void addTool(McpServerFeatures.SyncToolSpecification toolHandler)
      Add a new tool handler.
      Parameters:
      toolHandler - The tool handler to add
    • removeTool

      public void removeTool(String toolName)
      Remove a tool handler.
      Parameters:
      toolName - The name of the tool handler to remove
    • addResource

      public void addResource(McpServerFeatures.SyncResourceSpecification resourceHandler)
      Add a new resource handler.
      Parameters:
      resourceHandler - The resource handler to add
    • removeResource

      public void removeResource(String resourceUri)
      Remove a resource handler.
      Parameters:
      resourceUri - The URI of the resource handler to remove
    • addPrompt

      public void addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification)
      Add a new prompt handler.
      Parameters:
      promptSpecification - The prompt specification to add
    • removePrompt

      public void removePrompt(String promptName)
      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

      public McpSchema.ServerCapabilities getServerCapabilities()
      Get the server capabilities that define the supported features and functionality.
      Returns:
      The server capabilities
    • getServerInfo

      public McpSchema.Implementation 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.
    • notifyPromptsListChanged

      public void notifyPromptsListChanged()
      Notify clients that the list of available prompts has changed.
    • loggingNotification

      @Deprecated public void loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
      This implementation would, incorrectly, broadcast the logging message to all connected clients, using a single minLoggingLevel for all of them. Similar to the sampling and roots, the logging level should be set per client session and use the ServerExchange to send the logging message to the right client.
      Parameters:
      loggingMessageNotification - The logging message to send
    • closeGracefully

      public void closeGracefully()
      Close the server gracefully.
    • close

      public void close()
      Close the server immediately.
    • getAsyncServer

      public McpAsyncServer getAsyncServer()
      Get the underlying async server instance.
      Returns:
      The wrapped async server