public interface McpServer
This class serves as the main entry point for implementing the server-side of the MCP specification. The server's responsibilities include:
Thread Safety: Both synchronous and asynchronous server implementations are thread-safe. The synchronous server processes requests sequentially, while the asynchronous server can handle concurrent requests safely through its reactive programming model.
Error Handling: The server implementations provide robust error handling through the McpError class. Errors are properly propagated to clients while maintaining the server's stability. Server implementations should use appropriate error codes and provide meaningful error messages to help diagnose issues.
The class provides factory methods to create either:
McpAsyncServer
for non-blocking operations with reactive responses
McpSyncServer
for blocking operations with direct responses
Example of creating a basic synchronous server:
McpServer.sync(transportProvider)
.serverInfo("my-server", "1.0.0")
.tool(new Tool("calculator", "Performs calculations", schema),
(exchange, args) -> new CallToolResult("Result: " + calculate(args)))
.build();
Example of creating a basic asynchronous server:
McpServer.async(transportProvider)
.serverInfo("my-server", "1.0.0")
.tool(new Tool("calculator", "Performs calculations", schema),
(exchange, args) -> Mono.fromSupplier(() -> calculate(args))
.map(result -> new CallToolResult("Result: " + result)))
.build();
Example with comprehensive asynchronous configuration:
McpServer.async(transportProvider)
.serverInfo("advanced-server", "2.0.0")
.capabilities(new ServerCapabilities(...))
// Register tools
.tools(
new McpServerFeatures.AsyncToolSpecification(calculatorTool,
(exchange, args) -> Mono.fromSupplier(() -> calculate(args))
.map(result -> new CallToolResult("Result: " + result))),
new McpServerFeatures.AsyncToolSpecification(weatherTool,
(exchange, args) -> Mono.fromSupplier(() -> getWeather(args))
.map(result -> new CallToolResult("Weather: " + result)))
)
// Register resources
.resources(
new McpServerFeatures.AsyncResourceSpecification(fileResource,
(exchange, req) -> Mono.fromSupplier(() -> readFile(req))
.map(ReadResourceResult::new)),
new McpServerFeatures.AsyncResourceSpecification(dbResource,
(exchange, req) -> Mono.fromSupplier(() -> queryDb(req))
.map(ReadResourceResult::new))
)
// Add resource templates
.resourceTemplates(
new ResourceTemplate("file://{path}", "Access files"),
new ResourceTemplate("db://{table}", "Access database")
)
// Register prompts
.prompts(
new McpServerFeatures.AsyncPromptSpecification(analysisPrompt,
(exchange, req) -> Mono.fromSupplier(() -> generateAnalysisPrompt(req))
.map(GetPromptResult::new)),
new McpServerFeatures.AsyncPromptRegistration(summaryPrompt,
(exchange, req) -> Mono.fromSupplier(() -> generateSummaryPrompt(req))
.map(GetPromptResult::new))
)
.build();
限定符和类型 | 接口和说明 |
---|---|
static class |
McpServer.AsyncSpecification
Asynchronous server specification.
|
static class |
McpServer.SyncSpecification
Synchronous server specification.
|
限定符和类型 | 方法和说明 |
---|---|
static McpServer.AsyncSpecification |
async(McpServerTransportProvider transportProvider)
Starts building an asynchronous MCP server that provides non-blocking operations.
|
static McpServer.SyncSpecification |
sync(McpServerTransportProvider transportProvider)
Starts building a synchronous MCP server that provides blocking operations.
|
static McpServer.SyncSpecification sync(McpServerTransportProvider transportProvider)
transportProvider
- The transport layer implementation for MCP communication.McpServer.SyncSpecification
for configuring the server.static McpServer.AsyncSpecification async(McpServerTransportProvider transportProvider)
transportProvider
- The transport layer implementation for MCP communication.McpServer.AsyncSpecification
for configuring the server.