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.
|
限定符和类型 | 方法和说明 |
---|---|
void |
addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification)
Add a new prompt handler.
|
void |
addResource(McpServerFeatures.SyncResourceSpecification resourceHandler)
Add a new resource handler.
|
void |
addResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceHandler)
Add a new resource template handler.
|
void |
addTool(McpServerFeatures.SyncToolSpecification toolHandler)
Add a new tool handler.
|
void |
close()
Close the server immediately.
|
void |
closeGracefully()
Close the server gracefully.
|
McpSchema.CreateMessageResult |
createMessage(McpSchema.CreateMessageRequest createMessageRequest)
已过时。
This method will be removed in 0.9.0. Use
McpSyncServerExchange.createMessage(McpSchema.CreateMessageRequest) . |
McpAsyncServer |
getAsyncServer()
Get the underlying async server instance.
|
McpSchema.ClientCapabilities |
getClientCapabilities()
已过时。
This method will be removed in 0.9.0. Use
McpSyncServerExchange.getClientCapabilities() . |
McpSchema.Implementation |
getClientInfo()
已过时。
This method will be removed in 0.9.0. Use
McpSyncServerExchange.getClientInfo() . |
McpSchema.ServerCapabilities |
getServerCapabilities()
Get the server capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getServerInfo()
Get the server implementation information.
|
void |
loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
Send a logging message notification to all clients.
|
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 |
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 resourceUri)
Remove a resource template handler.
|
void |
removeTool(java.lang.String toolName)
Remove a tool handler.
|
public McpSyncServer(McpAsyncServer asyncServer)
asyncServer
- The async server to wrappublic void addTool(McpServerFeatures.SyncToolSpecification toolHandler)
toolHandler
- The tool handler to addpublic void removeTool(java.lang.String toolName)
toolName
- The name of the tool handler to removepublic void addResource(McpServerFeatures.SyncResourceSpecification resourceHandler)
resourceHandler
- The resource handler to addpublic void removeResource(java.lang.String resourceUri)
resourceUri
- The URI of the resource handler to removepublic void addResourceTemplate(McpServerFeatures.SyncResourceTemplateSpecification resourceHandler)
resourceHandler
- The resource handler to addpublic void removeResourceTemplate(java.lang.String resourceUri)
resourceUri
- The URI of the resource template handler to removepublic void addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification)
promptSpecification
- The prompt specification to addpublic 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()
@Deprecated public McpSchema.ClientCapabilities getClientCapabilities()
McpSyncServerExchange.getClientCapabilities()
.@Deprecated public McpSchema.Implementation getClientInfo()
McpSyncServerExchange.getClientInfo()
.public void notifyResourcesListChanged()
public void notifyPromptsListChanged()
public void loggingNotification(McpSchema.LoggingMessageNotification loggingMessageNotification)
loggingMessageNotification
- The logging message notification to sendpublic void closeGracefully()
public void close()
public McpAsyncServer getAsyncServer()
@Deprecated public McpSchema.CreateMessageResult createMessage(McpSchema.CreateMessageRequest createMessageRequest)
McpSyncServerExchange.createMessage(McpSchema.CreateMessageRequest)
.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.
Unlike its async counterpart, this method blocks until the message creation is complete, making it easier to use in synchronous code paths.
createMessageRequest
- The request to create a new messageMcpError
- if the client has not been initialized or does not support
sampling capabilitiesMcpError
- if the client does not support the createMessage methodMcpSchema.CreateMessageRequest
,
McpSchema.CreateMessageResult
,
Sampling
Specification