public interface McpServer
Factory class for creating Model Context Protocol (MCP) servers. MCP servers expose tools, resources, and prompts to AI models through a standardized interface.

This class serves as the main entry point for implementing the server-side of the MCP specification. The server's responsibilities include:

  • Exposing tools that models can invoke to perform actions
  • Providing access to resources that give models context
  • Managing prompt templates for structured model interactions
  • Handling client connections and requests
  • Implementing capability negotiation

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:

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();
 
Author:
Christian Tzolov, Dariusz Jędrzejczyk, Jihoon Kim
See Also:
  • Method Details

    • sync

      Starts building a synchronous MCP server that provides blocking operations. Synchronous servers block the current Thread's execution upon each request before giving the control back to the caller, making them simpler to implement but potentially less scalable for concurrent operations.
      Parameters:
      transportProvider - The transport layer implementation for MCP communication.
      Returns:
      A new instance of McpServer.SyncSpecification for configuring the server.
    • async

      static McpServer.AsyncSpecification async(McpServerTransportProvider transportProvider)
      Starts building an asynchronous MCP server that provides non-blocking operations. Asynchronous servers can handle multiple requests concurrently on a single Thread using a functional paradigm with non-blocking server transports, making them more scalable for high-concurrency scenarios but more complex to implement.
      Parameters:
      transportProvider - The transport layer implementation for MCP communication.
      Returns:
      A new instance of McpServer.AsyncSpecification for configuring the server.