public class McpSyncServer
extends java.lang.Object
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:
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:
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.
McpAsyncServer,
McpSchema| 构造器和说明 |
|---|
McpSyncServer(McpAsyncServer asyncServer)
Creates a new synchronous server that wraps the provided async server.
|
McpSyncServer(McpAsyncServer asyncServer,
boolean immediateExecution)
Creates a new synchronous server that wraps the provided async server.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification)
Add a new prompt handler.
|
void |
addResource(McpServerFeatures.SyncResourceSpecification resourceSpecification)
Add a new resource handler.
|
void |
addResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceTemplateSpecification)
Add a new resource template.
|
void |
addTool(McpServerFeatures.SyncToolSpecification toolHandler)
Add a new tool handler.
|
void |
close()
Close the server immediately.
|
void |
closeGracefully()
Close the server gracefully.
|
McpAsyncServer |
getAsyncServer()
Get the underlying async server instance.
|
McpSchema.ServerCapabilities |
getServerCapabilities()
Get the server capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getServerInfo()
Get the server implementation information.
|
java.util.List<McpSchema.Prompt> |
listPrompts()
List all registered prompts.
|
java.util.List<McpSchema.Resource> |
listResources()
List all registered resources.
|
java.util.List<McpSchema.ResourceTemplate> |
listResourceTemplates()
List all registered resource templates.
|
java.util.List<McpSchema.Tool> |
listTools()
List all registered tools.
|
void |
loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
已过时。
Use
McpSyncServerExchange#loggingNotification(LoggingMessageNotification)
instead. |
void |
notifyPromptsListChanged()
Notify clients that the list of available prompts has changed.
|
void |
notifyResourcesListChanged()
Notify clients that the list of available resources has changed.
|
void |
notifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification)
Notify clients that the resources have updated.
|
void |
notifyToolsListChanged()
Notify clients that the list of available tools has changed.
|
void |
removePrompt(java.lang.String promptName)
Remove a prompt handler.
|
void |
removeResource(java.lang.String resourceUri)
Remove a resource handler.
|
void |
removeResourceTemplate(java.lang.String uriTemplate)
Remove a resource template.
|
void |
removeTool(java.lang.String toolName)
Remove a tool handler.
|
public McpSyncServer(McpAsyncServer asyncServer)
asyncServer - The async server to wrappublic McpSyncServer(McpAsyncServer asyncServer, boolean immediateExecution)
asyncServer - The async server to wrapimmediateExecution - Tools, prompts, and resources handlers execute work
without blocking code offloading. Do NOT set to true if the asyncServer's
transport is non-blocking.public void addTool(McpServerFeatures.SyncToolSpecification toolHandler)
toolHandler - The tool handler to addpublic java.util.List<McpSchema.Tool> listTools()
public void removeTool(java.lang.String toolName)
toolName - The name of the tool handler to removepublic void addResource(McpServerFeatures.SyncResourceSpecification resourceSpecification)
resourceSpecification - The resource specification to addpublic java.util.List<McpSchema.Resource> listResources()
public void removeResource(java.lang.String resourceUri)
resourceUri - The URI of the resource handler to removepublic void addResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceTemplateSpecification)
resourceTemplateSpecification - The resource template specification to addpublic java.util.List<McpSchema.ResourceTemplate> listResourceTemplates()
public void removeResourceTemplate(java.lang.String uriTemplate)
uriTemplate - The URI template of the resource template to removepublic void addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification)
promptSpecification - The prompt specification to addpublic java.util.List<McpSchema.Prompt> listPrompts()
public void removePrompt(java.lang.String promptName)
promptName - The name of the prompt handler to removepublic void notifyToolsListChanged()
public McpSchema.ServerCapabilities getServerCapabilities()
public McpSchema.Implementation getServerInfo()
public void notifyResourcesListChanged()
public void notifyResourcesUpdated(McpSchema.ResourcesUpdatedNotification resourcesUpdatedNotification)
public void notifyPromptsListChanged()
@Deprecated public void loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
McpSyncServerExchange#loggingNotification(LoggingMessageNotification)
instead.loggingMessageNotification - The logging message to sendpublic void closeGracefully()
public void close()
public McpAsyncServer getAsyncServer()