Class McpAsyncServer

java.lang.Object
io.modelcontextprotocol.server.McpAsyncServer

public class McpAsyncServer extends Object
The Model Context Protocol (MCP) server implementation that provides asynchronous communication using Project Reactor's Mono and Flux types.

This server implements the MCP specification, enabling AI models to expose tools, resources, and prompts through a standardized interface. Key features include:

  • Asynchronous communication using reactive programming patterns
  • Dynamic tool registration and management
  • Resource handling with URI-based addressing
  • Prompt template management
  • Real-time client notifications for state changes
  • Structured logging with configurable severity levels
  • Support for client-side AI model sampling

The server follows a lifecycle:

  1. Initialization - Accepts client connections and negotiates capabilities
  2. Normal Operation - Handles client requests and sends notifications
  3. Graceful Shutdown - Ensures clean connection termination

This implementation uses Project Reactor for non-blocking operations, making it suitable for high-throughput scenarios and reactive applications. All operations return Mono or Flux types that can be composed into reactive pipelines.

The server supports runtime modification of its capabilities through methods like addTool(io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification), addResource(io.modelcontextprotocol.server.McpServerFeatures.AsyncResourceSpecification), and addPrompt(io.modelcontextprotocol.server.McpServerFeatures.AsyncPromptSpecification), automatically notifying connected clients of changes when configured to do so.

Author:
Christian Tzolov, Dariusz Jędrzejczyk, Jihoon Kim
See Also:
  • Method Details

    • 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
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Gracefully closes the server, allowing any in-progress operations to complete.
      Returns:
      A Mono that completes when the server has been closed
    • close

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

      public reactor.core.publisher.Mono<Void> addTool(McpServerFeatures.AsyncToolSpecification toolSpecification)
      Add a new tool specification at runtime.
      Parameters:
      toolSpecification - The tool specification to add
      Returns:
      Mono that completes when clients have been notified of the change
    • removeTool

      public reactor.core.publisher.Mono<Void> removeTool(String toolName)
      Remove a tool handler at runtime.
      Parameters:
      toolName - The name of the tool handler to remove
      Returns:
      Mono that completes when clients have been notified of the change
    • notifyToolsListChanged

      public reactor.core.publisher.Mono<Void> notifyToolsListChanged()
      Notifies clients that the list of available tools has changed.
      Returns:
      A Mono that completes when all clients have been notified
    • addResource

      public reactor.core.publisher.Mono<Void> addResource(McpServerFeatures.AsyncResourceSpecification resourceSpecification)
      Add a new resource handler at runtime.
      Parameters:
      resourceSpecification - The resource handler to add
      Returns:
      Mono that completes when clients have been notified of the change
    • removeResource

      public reactor.core.publisher.Mono<Void> removeResource(String resourceUri)
      Remove a resource handler at runtime.
      Parameters:
      resourceUri - The URI of the resource handler to remove
      Returns:
      Mono that completes when clients have been notified of the change
    • notifyResourcesListChanged

      public reactor.core.publisher.Mono<Void> notifyResourcesListChanged()
      Notifies clients that the list of available resources has changed.
      Returns:
      A Mono that completes when all clients have been notified
    • addPrompt

      public reactor.core.publisher.Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpecification)
      Add a new prompt handler at runtime.
      Parameters:
      promptSpecification - The prompt handler to add
      Returns:
      Mono that completes when clients have been notified of the change
    • removePrompt

      public reactor.core.publisher.Mono<Void> removePrompt(String promptName)
      Remove a prompt handler at runtime.
      Parameters:
      promptName - The name of the prompt handler to remove
      Returns:
      Mono that completes when clients have been notified of the change
    • notifyPromptsListChanged

      public reactor.core.publisher.Mono<Void> notifyPromptsListChanged()
      Notifies clients that the list of available prompts has changed.
      Returns:
      A Mono that completes when all clients have been notified
    • loggingNotification

      @Deprecated public reactor.core.publisher.Mono<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
      Returns:
      A Mono that completes when the notification has been sent