diff --git a/mcp/src/main/java/org/springframework/ai/mcp/client/McpClient.java b/mcp/src/main/java/org/springframework/ai/mcp/client/McpClient.java index 9f49924..bea9478 100644 --- a/mcp/src/main/java/org/springframework/ai/mcp/client/McpClient.java +++ b/mcp/src/main/java/org/springframework/ai/mcp/client/McpClient.java @@ -34,33 +34,37 @@ import org.springframework.ai.mcp.spec.McpTransport; import org.springframework.ai.mcp.util.Assert; /** - * Factory class for creating Model Context Protocol (MCP) clients. MCP is a protocol that enables - * AI models to interact with external tools and resources through a standardized interface. - * - *
This class serves as the main entry point for establishing connections with MCP servers, - * implementing the client-side of the MCP specification. The protocol follows a client-server - * architecture where: + * Factory class for creating Model Context Protocol (MCP) clients. MCP is a protocol that + * enables AI models to interact with external tools and resources through a standardized + * interface. + * + *
+ * This class serves as the main entry point for establishing connections with MCP + * servers, implementing the client-side of the MCP specification. The protocol follows a + * client-server architecture where: *
The class provides factory methods to create either: + *
+ * The class provides factory methods to create either: *
Example of creating a basic client: - *
{@code
+ *
+ * Example of creating a basic client:
{@code
* McpClient.using(transport)
* .requestTimeout(Duration.ofSeconds(5))
* .sync(); // or .async()
* }
*
- * Example with advanced configuration:
- *
{@code
+ *
+ * Example with advanced configuration:
{@code
* McpClient.using(transport)
* .requestTimeout(Duration.ofSeconds(10))
* .capabilities(new ClientCapabilities(...))
@@ -70,7 +74,8 @@ import org.springframework.ai.mcp.util.Assert;
* .async();
* }
*
- * The client supports:
+ *
+ * The client supports:
*
The builder supports configuration of: + *
+ * The builder supports configuration of: *
This class serves as the main entry point for implementing the server-side of - * the MCP specification. The server's responsibilities include: + *
+ * This class serves as the main entry point for implementing the server-side of the MCP + * specification. The server's responsibilities include: *
The class provides factory methods to create either: + *
+ * The class provides factory methods to create either: *
Example of creating a basic server: - *
{@code
+ *
+ * Example of creating a basic server:
{@code
* McpServer.using(transport)
* .info("my-server", "1.0.0")
* .tool(new Tool("calculator", "Performs calculations", schema),
@@ -58,8 +60,8 @@ import org.springframework.ai.mcp.util.Assert;
* .sync(); // or .async()
* }
*
- * Example with comprehensive configuration:
- *
{@code
+ *
+ * Example with comprehensive configuration:
{@code
* McpServer.using(transport)
* .info("advanced-server", "2.0.0")
* .capabilities(new ServerCapabilities(...))
@@ -94,8 +96,8 @@ import org.springframework.ai.mcp.util.Assert;
public interface McpServer {
/**
- * 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
+ * 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:
* Example tool registration: - *
{@code
+ *
+ * Example tool registration:
{@code
* new ToolRegistration(
* new Tool(
* "calculator",
@@ -123,15 +125,15 @@ public interface McpServer {
* }
*
* @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
+ * @param call The function that implements the tool's logic, receiving arguments and
+ * returning results
*/
public static record ToolRegistration(Tool tool, FunctionExample prompt registration: - *
{@code
+ *
+ * Example prompt registration:
{@code
* new PromptRegistration(
* new Prompt("analyze", "Code analysis template"),
* request -> {
@@ -184,7 +186,7 @@ public interface McpServer {
*
* @param prompt The prompt definition including name and description
* @param promptHandler The function that processes prompt requests and returns
- * formatted templates
+ * formatted templates
*/
public static record PromptRegistration(McpSchema.Prompt propmpt,
Function promptHandler) {
@@ -248,9 +250,8 @@ public interface McpServer {
* Sets the server implementation information that will be shared with clients
* during connection initialization. This helps with version compatibility,
* debugging, and server identification.
- *
* @param serverInfo The server implementation details including name and version.
- * Must not be null.
+ * Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if serverInfo is null
*/
@@ -261,9 +262,8 @@ public interface McpServer {
}
/**
- * Sets the server implementation information using name and version strings.
- * This is a convenience method alternative to {@link #info(McpSchema.Implementation)}.
- *
+ * Sets the server implementation information using name and version strings. This
+ * is a convenience method alternative to {@link #info(McpSchema.Implementation)}.
* @param name The server name. Must not be null or empty.
* @param version The server version. Must not be null or empty.
* @return This builder instance for method chaining
@@ -288,8 +288,8 @@ public interface McpServer {
* - Streaming responses
*
- Batch operations
*
Example usage: - *
{@code
+ *
+ * Example usage:
{@code
* .tool(
* new Tool("calculator", "Performs calculations", schema),
* args -> new CallToolResult("Result: " + calculate(args))
* )
* }
- *
- * @param tool The tool definition including name, description, and schema.
- * Must not be null.
+ * @param tool The tool definition including name, description, and schema. Must
+ * not be null.
* @param handler The function that implements the tool's logic. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if tool or handler is null
@@ -327,12 +326,11 @@ public interface McpServer {
}
/**
- * Adds multiple tools with their handlers to the server using a List.
- * This method is useful when tools are dynamically generated or loaded
- * from a configuration source.
- *
- * @param toolRegistrations The list of tool registrations to add.
- * Must not be null.
+ * Adds multiple tools with their handlers to the server using a List. This method
+ * is useful when tools are dynamically generated or loaded from a configuration
+ * source.
+ * @param toolRegistrations The list of tool registrations to add. Must not be
+ * null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if toolRegistrations is null
* @see #tools(ToolRegistration...)
@@ -344,18 +342,17 @@ public interface McpServer {
}
/**
- * Adds multiple tools with their handlers to the server using varargs.
- * This method provides a convenient way to register multiple tools inline.
+ * Adds multiple tools with their handlers to the server using varargs. This
+ * method provides a convenient way to register multiple tools inline.
*
- * Example usage:
- *
{@code
+ *
+ * Example usage:
{@code
* .tools(
* new ToolRegistration(calculatorTool, calculatorHandler),
* new ToolRegistration(weatherTool, weatherHandler),
* new ToolRegistration(fileManagerTool, fileManagerHandler)
* )
* }
- *
* @param toolRegistrations The tool registrations to add. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if toolRegistrations is null
@@ -369,12 +366,11 @@ public interface McpServer {
}
/**
- * Registers multiple resources with their handlers using a Map.
- * This method is useful when resources are dynamically generated
- * or loaded from a configuration source.
- *
- * @param resourceRegsitrations Map of resource name to registration.
- * Must not be null.
+ * Registers multiple resources with their handlers using a Map. This method is
+ * useful when resources are dynamically generated or loaded from a configuration
+ * source.
+ * @param resourceRegsitrations Map of resource name to registration. Must not be
+ * null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if resourceRegsitrations is null
* @see #resources(ResourceRegistration...)
@@ -386,12 +382,9 @@ public interface McpServer {
}
/**
- * Registers multiple resources with their handlers using a List.
- * This method is useful when resources need to be added in bulk
- * from a collection.
- *
- * @param resourceRegsitrations List of resource registrations.
- * Must not be null.
+ * Registers multiple resources with their handlers using a List. This method is
+ * useful when resources need to be added in bulk from a collection.
+ * @param resourceRegsitrations List of resource registrations. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if resourceRegsitrations is null
* @see #resources(ResourceRegistration...)
@@ -405,21 +398,19 @@ public interface McpServer {
}
/**
- * Registers multiple resources with their handlers using varargs.
- * This method provides a convenient way to register multiple
- * resources inline.
+ * Registers multiple resources with their handlers using varargs. This method
+ * provides a convenient way to register multiple resources inline.
*
- * Example usage:
- *
{@code
+ *
+ * Example usage:
{@code
* .resources(
* new ResourceRegistration(fileResource, fileHandler),
* new ResourceRegistration(dbResource, dbHandler),
* new ResourceRegistration(apiResource, apiHandler)
* )
* }
- *
- * @param resourceRegistrations The resource registrations to add.
- * Must not be null.
+ * @param resourceRegistrations The resource registrations to add. Must not be
+ * null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if resourceRegistrations is null
*/
@@ -432,20 +423,18 @@ public interface McpServer {
}
/**
- * Sets the resource templates that define patterns for dynamic resource
- * access. Templates use URI patterns with placeholders that can be
- * filled at runtime.
+ * Sets the resource templates that define patterns for dynamic resource access.
+ * Templates use URI patterns with placeholders that can be filled at runtime.
*
- * Example usage:
- *
{@code
+ *
+ * Example usage:
{@code
* .resourceTemplates(
* new ResourceTemplate("file://{path}", "Access files by path"),
* new ResourceTemplate("db://{table}/{id}", "Access database records")
* )
* }
- *
- * @param resourceTemplates List of resource templates. If null, clears
- * existing templates.
+ * @param resourceTemplates List of resource templates. If null, clears existing
+ * templates.
* @return This builder instance for method chaining
* @see #resourceTemplates(ResourceTemplate...)
*/
@@ -455,9 +444,8 @@ public interface McpServer {
}
/**
- * Sets the resource templates using varargs for convenience.
- * This is an alternative to {@link #resourceTemplates(List)}.
- *
+ * Sets the resource templates using varargs for convenience. This is an
+ * alternative to {@link #resourceTemplates(List)}.
* @param resourceTemplates The resource templates to set.
* @return This builder instance for method chaining
* @see #resourceTemplates(List)
@@ -470,12 +458,12 @@ public interface McpServer {
}
/**
- * Registers multiple prompts with their handlers using a Map.
- * This method is useful when prompts are dynamically generated
- * or loaded from a configuration source.
+ * Registers multiple prompts with their handlers using a Map. This method is
+ * useful when prompts are dynamically generated or loaded from a configuration
+ * source.
*
- * Example usage:
- *
{@code
+ *
+ * Example usage:
{@code
* Map prompts = new HashMap<>();
* prompts.put("analysis", new PromptRegistration(
* new Prompt("analysis", "Code analysis template"),
@@ -483,7 +471,6 @@ public interface McpServer {
* ));
* .prompts(prompts)
* }
- *
* @param prompts Map of prompt name to registration. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if prompts is null
@@ -494,10 +481,8 @@ public interface McpServer {
}
/**
- * Registers multiple prompts with their handlers using a List.
- * This method is useful when prompts need to be added in bulk
- * from a collection.
- *
+ * Registers multiple prompts with their handlers using a List. This method is
+ * useful when prompts need to be added in bulk from a collection.
* @param prompts List of prompt registrations. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if prompts is null
@@ -511,19 +496,17 @@ public interface McpServer {
}
/**
- * Registers multiple prompts with their handlers using varargs.
- * This method provides a convenient way to register multiple
- * prompts inline.
+ * Registers multiple prompts with their handlers using varargs. This method
+ * provides a convenient way to register multiple prompts inline.
*
- * Example usage:
- *
{@code
+ *
+ * Example usage:
{@code
* .prompts(
* new PromptRegistration(analysisPrompt, analysisHandler),
* new PromptRegistration(summaryPrompt, summaryHandler),
* new PromptRegistration(reviewPrompt, reviewHandler)
* )
* }
- *
* @param prompts The prompt registrations to add. Must not be null.
* @return This builder instance for method chaining
* @throws IllegalArgumentException if prompts is null
@@ -536,13 +519,12 @@ public interface McpServer {
}
/**
- * Builds a synchronous MCP server that provides blocking operations.
- * Synchronous servers process each request to completion before handling
- * the next one, making them simpler to implement but potentially less
- * performant for concurrent operations.
- *
- * @return A new instance of {@link McpSyncServer} configured with this
- * builder's settings
+ * Builds a synchronous MCP server that provides blocking operations. Synchronous
+ * servers process each request to completion before handling the next one, making
+ * them simpler to implement but potentially less performant for concurrent
+ * operations.
+ * @return A new instance of {@link McpSyncServer} configured with this builder's
+ * settings
*/
public McpSyncServer sync() {
return new McpSyncServer(async());
@@ -551,11 +533,10 @@ public interface McpServer {
/**
* Builds an asynchronous MCP server that provides non-blocking operations.
* Asynchronous servers can handle multiple requests concurrently using
- * CompletableFuture, making them more efficient for high-concurrency
- * scenarios but more complex to implement.
- *
- * @return A new instance of {@link McpAsyncServer} configured with this
- * builder's settings
+ * CompletableFuture, making them more efficient for high-concurrency scenarios
+ * but more complex to implement.
+ * @return A new instance of {@link McpAsyncServer} configured with this builder's
+ * settings
*/
public McpAsyncServer async() {
if (serverInfo == null) {