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(
McpServerFeatures.AsyncToolSpecification.builder()
.tool(calculatorTool)
.callTool((exchange, args) -> Mono.fromSupplier(() -> calculate(args.arguments()))
.map(result -> new CallToolResult("Result: " + result))))
. .build(),
McpServerFeatures.AsyncToolSpecification.builder()
.tool((weatherTool)
.callTool((exchange, args) -> Mono.fromSupplier(() -> getWeather(args.arguments()))
.map(result -> new CallToolResult("Weather: " + result))))
.build()
)
// 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<S extends McpServer.AsyncSpecification<S>>
Asynchronous server specification.
|
static class |
McpServer.SingleSessionAsyncSpecification |
static class |
McpServer.SingleSessionSyncSpecification |
static class |
McpServer.StatelessAsyncSpecification |
static class |
McpServer.StatelessSyncSpecification |
static class |
McpServer.StreamableServerAsyncSpecification |
static class |
McpServer.StreamableSyncSpecification |
static class |
McpServer.SyncSpecification<S extends McpServer.SyncSpecification<S>>
Synchronous server specification.
|
限定符和类型 | 字段和说明 |
---|---|
static McpSchema.Implementation |
DEFAULT_SERVER_INFO |
限定符和类型 | 方法和说明 |
---|---|
static McpServer.AsyncSpecification<?> |
async(McpServerTransportProvider transportProvider)
Starts building an asynchronous MCP server that provides non-blocking operations.
|
static McpServer.StatelessAsyncSpecification |
async(McpStatelessServerTransport transport)
Starts building an asynchronous MCP server that provides non-blocking operations.
|
static McpServer.AsyncSpecification<?> |
async(McpStreamableServerTransportProvider transportProvider)
Starts building an asynchronous MCP server that provides non-blocking operations.
|
static McpServer.SingleSessionSyncSpecification |
sync(McpServerTransportProvider transportProvider)
Starts building a synchronous MCP server that provides blocking operations.
|
static McpServer.StatelessSyncSpecification |
sync(McpStatelessServerTransport transport)
Starts building a synchronous MCP server that provides blocking operations.
|
static McpServer.StreamableSyncSpecification |
sync(McpStreamableServerTransportProvider transportProvider)
Starts building a synchronous MCP server that provides blocking operations.
|
static final McpSchema.Implementation DEFAULT_SERVER_INFO
static McpServer.SingleSessionSyncSpecification 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.static McpServer.StreamableSyncSpecification sync(McpStreamableServerTransportProvider transportProvider)
transportProvider
- The transport layer implementation for MCP communication.McpServer.SyncSpecification
for configuring the server.static McpServer.AsyncSpecification<?> async(McpStreamableServerTransportProvider transportProvider)
transportProvider
- The transport layer implementation for MCP communication.McpServer.AsyncSpecification
for configuring the server.static McpServer.StatelessAsyncSpecification async(McpStatelessServerTransport transport)
transport
- The transport layer implementation for MCP communication.McpServer.AsyncSpecification
for configuring the server.static McpServer.StatelessSyncSpecification sync(McpStatelessServerTransport transport)
transport
- The transport layer implementation for MCP communication.McpServer.SyncSpecification
for configuring the server.