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 call specification at runtime.
      Parameters:
      toolSpecification - The tool specification to add
      Returns:
      Mono that completes when clients have been notified of the change
    • listTools

      public reactor.core.publisher.Flux<McpSchema.Tool> listTools()
      List all registered tools.
      Returns:
      A Flux stream of all registered tools
    • 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
    • listResources

      public reactor.core.publisher.Flux<McpSchema.Resource> listResources()
      List all registered resources.
      Returns:
      A Flux stream of all registered resources
    • 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
    • addResourceTemplate

      public reactor.core.publisher.Mono<Void> addResourceTemplate(McpServerFeatures.AsyncResourceTemplateSpecification resourceTemplateSpecification)
      Add a new resource template at runtime.
      Parameters:
      resourceTemplateSpecification - The resource template to add
      Returns:
      Mono that completes when clients have been notified of the change
    • listResourceTemplates

      public reactor.core.publisher.Flux<McpSchema.ResourceTemplate> listResourceTemplates()
      List all registered resource templates.
      Returns:
      A Flux stream of all registered resource templates
    • removeResourceTemplate

      public reactor.core.publisher.Mono<Void> removeResourceTemplate(String uriTemplate)
      Remove a resource template at runtime.
      Parameters:
      uriTemplate - The URI template of the resource template 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
    • notifyResourcesUpdated

      public reactor.core.publisher.Mono<Void> notifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification)
      Notifies clients that the resources have updated.
      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
    • listPrompts

      public reactor.core.publisher.Flux<McpSchema.Prompt> listPrompts()
      List all registered prompts.
      Returns:
      A Flux stream of all registered prompts
    • 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