> promptsChangeConsumer) {
Assert.notNull(promptsChangeConsumer, "Prompts change consumer must not be null");
this.promptsChangeConsumers.add(promptsChangeConsumer);
@@ -160,16 +290,25 @@ public interface McpClient {
}
/**
- * Build a synchronous MCP client.
- * @return A new instance of {@link McpSyncClient}
+ * Builds a synchronous MCP client that provides blocking operations.
+ * Synchronous clients wait for each operation to complete before returning,
+ * making them simpler to use but potentially less performant for
+ * concurrent operations.
+ *
+ * @return A new instance of {@link McpSyncClient} configured with this
+ * builder's settings
*/
public McpSyncClient sync() {
return new McpSyncClient(async());
}
/**
- * Build an asynchronous MCP client.
- * @return A new instance of {@link McpAsyncClient}
+ * Builds an asynchronous MCP client that provides non-blocking operations.
+ * Asynchronous clients return CompletableFuture objects immediately,
+ * allowing for concurrent operations and reactive programming patterns.
+ *
+ * @return A new instance of {@link McpAsyncClient} configured with this
+ * builder's settings
*/
public McpAsyncClient async() {
return new McpAsyncClient(transport, requestTimeout, clientInfo, capabilities, roots, toolsChangeConsumers,
diff --git a/mcp/src/main/java/org/springframework/ai/mcp/server/McpServer.java b/mcp/src/main/java/org/springframework/ai/mcp/server/McpServer.java
index cbabff4..4cc051c 100644
--- a/mcp/src/main/java/org/springframework/ai/mcp/server/McpServer.java
+++ b/mcp/src/main/java/org/springframework/ai/mcp/server/McpServer.java
@@ -30,49 +30,162 @@ import org.springframework.ai.mcp.spec.McpTransport;
import org.springframework.ai.mcp.util.Assert;
/**
- * Factory class providing static methods for creating Model Context Protocol (MCP)
- * servers. This class serves as the main entry point for establishing MCP servers,
- * offering both synchronous and asynchronous server implementations.
+ * Factory class for creating Model Context Protocol (MCP) servers. MCP servers expose
+ * tools, resources, and prompts to AI models through a standardized interface.
*
- *
- * The class provides factory methods to create either:
+ *
This class serves as the main entry point for implementing the server-side of
+ * the MCP specification. The server's responsibilities include:
*
- * - {@link McpAsyncServer} for non-blocking operations
- *
- {@link McpSyncServer} for blocking operations
+ *
- 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
*
*
- *
- * Use the builder pattern for flexible server configuration:
+ *
The class provides factory methods to create either:
+ *
+ * - {@link McpAsyncServer} for non-blocking operations with CompletableFuture responses
+ *
- {@link McpSyncServer} for blocking operations with direct responses
+ *
*
+ * Example of creating a basic server:
*
{@code
* McpServer.using(transport)
- * .serverInfo(new McpSchema.Implementation("my-server", "1.0.0"))
- * .tool(new MyToolHandler())
- * .async(); // or .sync()
+ * .info("my-server", "1.0.0")
+ * .tool(new Tool("calculator", "Performs calculations", schema),
+ * args -> new CallToolResult("Result: " + calculate(args)))
+ * .sync(); // or .async()
+ * }
+ *
+ * Example with comprehensive configuration:
+ *
{@code
+ * McpServer.using(transport)
+ * .info("advanced-server", "2.0.0")
+ * .capabilities(new ServerCapabilities(...))
+ * // Register tools
+ * .tools(
+ * new ToolRegistration(calculatorTool, calculatorHandler),
+ * new ToolRegistration(weatherTool, weatherHandler)
+ * )
+ * // Register resources
+ * .resources(
+ * new ResourceRegistration(fileResource, fileHandler),
+ * new ResourceRegistration(dbResource, dbHandler)
+ * )
+ * // Add resource templates
+ * .resourceTemplates(
+ * new ResourceTemplate("file://{path}", "Access files"),
+ * new ResourceTemplate("db://{table}", "Access database")
+ * )
+ * // Register prompts
+ * .prompts(
+ * new PromptRegistration(analysisPrompt, analysisHandler),
+ * new PromptRegistration(summaryPrompt, summaryHandler)
+ * )
+ * .async();
* }
*
* @author Christian Tzolov
+ * @see McpAsyncServer
+ * @see McpSyncServer
+ * @see McpTransport
*/
public interface McpServer {
/**
- * Registration of a tool with its handler function. MCP allows servers to expose
- * tools that can be invoked by language models. Tools enable models to interact with
- * external systems, such as querying databases, calling APIs, or performing
- * computations. Each tool is uniquely identified by a name and includes metadata
- * describing its schema.
+ * Registration of a tool with its handler function. Tools are the primary way for
+ * MCP servers to expose functionality to AI models. Each tool represents a specific
+ * capability, such as:
+ *
+ * - Performing calculations
+ *
- Accessing external APIs
+ *
- Querying databases
+ *
- Manipulating files
+ *
- Executing system commands
+ *
*
- * @param tool The tool definition
- * @param call The function to handle tool execution
+ * Example tool registration:
+ *
{@code
+ * new ToolRegistration(
+ * new Tool(
+ * "calculator",
+ * "Performs mathematical calculations",
+ * new JsonSchemaObject()
+ * .required("expression")
+ * .property("expression", JsonSchemaType.STRING)
+ * ),
+ * args -> {
+ * String expr = (String) args.get("expression");
+ * return new CallToolResult("Result: " + evaluate(expr));
+ * }
+ * )
+ * }
+ *
+ * @param tool The tool definition including name, description, and parameter schema
+ * @param call The function that implements the tool's logic, receiving arguments
+ * and returning results
*/
public static record ToolRegistration(Tool tool, Function