Class McpAsyncServer
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:
- Initialization - Accepts client connections and negotiates capabilities
- Normal Operation - Handles client requests and sends notifications
- 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 Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpecification) Add a new prompt handler at runtime.reactor.core.publisher.Mono<Void>addResource(McpServerFeatures.AsyncResourceSpecification resourceSpecification) Add a new resource handler at runtime.reactor.core.publisher.Mono<Void>addResourceTemplate(McpServerFeatures.AsyncResourceTemplateSpecification resourceTemplateSpecification) Add a new resource template at runtime.reactor.core.publisher.Mono<Void>addTool(McpServerFeatures.AsyncToolSpecification toolSpecification) Add a new tool call specification at runtime.voidclose()Close the server immediately.reactor.core.publisher.Mono<Void>Gracefully closes the server, allowing any in-progress operations to complete.Get the server capabilities that define the supported features and functionality.Get the server implementation information.reactor.core.publisher.Flux<McpSchema.Prompt>List all registered prompts.reactor.core.publisher.Flux<McpSchema.Resource>List all registered resources.reactor.core.publisher.Flux<McpSchema.ResourceTemplate>List all registered resource templates.reactor.core.publisher.Flux<McpSchema.Tool>List all registered tools.reactor.core.publisher.Mono<Void>Notifies clients that the list of available prompts has changed.reactor.core.publisher.Mono<Void>Notifies clients that the list of available resources has changed.reactor.core.publisher.Mono<Void>notifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification) Notifies clients that the resources have updated.reactor.core.publisher.Mono<Void>Notifies clients that the list of available tools has changed.reactor.core.publisher.Mono<Void>removePrompt(String promptName) Remove a prompt handler at runtime.reactor.core.publisher.Mono<Void>removeResource(String resourceUri) Remove a resource handler at runtime.reactor.core.publisher.Mono<Void>removeResourceTemplate(String uriTemplate) Remove a resource template at runtime.reactor.core.publisher.Mono<Void>removeTool(String toolName) Remove a tool handler at runtime.
-
Method Details
-
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
-
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
List all registered tools.- Returns:
- A Flux stream of all registered tools
-
removeTool
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
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
List all registered resources.- Returns:
- A Flux stream of all registered resources
-
removeResource
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
List all registered resource templates.- Returns:
- A Flux stream of all registered resource templates
-
removeResourceTemplate
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
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
List all registered prompts.- Returns:
- A Flux stream of all registered prompts
-
removePrompt
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
Notifies clients that the list of available prompts has changed.- Returns:
- A Mono that completes when all clients have been notified
-