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.AsyncToolRegistration), addResource(io.modelcontextprotocol.server.McpServerFeatures.AsyncResourceRegistration), and addPrompt(io.modelcontextprotocol.server.McpServerFeatures.AsyncPromptRegistration), automatically notifying connected clients of changes when configured to do so.

Author:
Christian Tzolov, Dariusz Jędrzejczyk
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
    • getClientCapabilities

      public McpSchema.ClientCapabilities getClientCapabilities()
      Get the client capabilities that define the supported features and functionality.
      Returns:
      The client capabilities
    • getClientInfo

      public McpSchema.Implementation getClientInfo()
      Get the client implementation information.
      Returns:
      The client 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.
    • listRoots

      public reactor.core.publisher.Mono<McpSchema.ListRootsResult> listRoots()
      Retrieves the list of all roots provided by the client.
      Returns:
      A Mono that emits the list of roots result.
    • listRoots

      public reactor.core.publisher.Mono<McpSchema.ListRootsResult> listRoots(String cursor)
      Retrieves a paginated list of roots provided by the server.
      Parameters:
      cursor - Optional pagination cursor from a previous list request
      Returns:
      A Mono that emits the list of roots result containing
    • addTool

      public reactor.core.publisher.Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistration)
      Add a new tool registration at runtime.
      Parameters:
      toolRegistration - The tool registration 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.AsyncResourceRegistration resourceHandler)
      Add a new resource handler at runtime.
      Parameters:
      resourceHandler - 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.AsyncPromptRegistration promptRegistration)
      Add a new prompt handler at runtime.
      Parameters:
      promptRegistration - 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

      public reactor.core.publisher.Mono<Void> loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
      Send a logging message notification to all connected clients. Messages below the current minimum logging level will be filtered out.
      Parameters:
      loggingMessageNotification - The logging message to send
      Returns:
      A Mono that completes when the notification has been sent
    • createMessage

      public reactor.core.publisher.Mono<McpSchema.CreateMessageResult> createMessage(McpSchema.CreateMessageRequest createMessageRequest)
      Create a new message using the sampling capabilities of the client. The Model Context Protocol (MCP) provides a standardized way for servers to request LLM sampling (“completions” or “generations”) from language models via clients. This flow allows clients to maintain control over model access, selection, and permissions while enabling servers to leverage AI capabilities—with no server API keys necessary. Servers can request text or image-based interactions and optionally include context from MCP servers in their prompts.
      Parameters:
      createMessageRequest - The request to create a new message
      Returns:
      A Mono that completes when the message has been created
      Throws:
      McpError - if the client has not been initialized or does not support sampling capabilities
      McpError - if the client does not support the createMessage method
      See Also: