- Implement MCP server with async/sync support and builder pattern configuration - Add Server stdio/SSE transport layers with robust error handling - Convert McpClient and McpServer to interfaces for better extensibility - Add sampling capabilities for LLM interactions (text/image) - Implement dynamic root management and runtime tool management - Add comprehensive prompt and resource capabilities - Add support for dynamic root management (add/remove roots at runtime) Architecture Changes - Introduce registration records for tools, resources, and prompts - Improve thread safety with concurrent collections - Enhance resource template handling - Implement builder patterns for Server/Client capabilities - Add support for server capabilities and implementation info Transport Layer: - Implement SseServerTransport with SSE endpoints - Add StdioServerTransport for process-based communication - Reorganize transport packages for better organization - Improve transport error handling and shutdown processes Documentation: - Add detailed MCP reference documentation - Include architecture diagrams and API documentation - Add comprehensive test coverage for all components - Include sample projects demonstrating stdio/SSE transports BREAKING CHANGES: Transport Package Reorganization - Move StdioClientTransport to o.s.ai.mcp.client.transport - Move SseClientTransport to o.s.ai.mcp.client.transport - Move ServerParameters to o.s.ai.mcp.client.transport - StdioClientTransport.errorHandler changed to private with setter method Migration: Use setErrorHandler() method instead of direct field access API Changes - Replace static factory methods with builder pattern - Update ServerCapabilities to use builder pattern - Make StdioClientTransport.errorHandler private with setter - Replace rootsListProvider with direct root management - Remove deprecated static factory methods Resolves #27,#25,#24,#11 Related to #12
4.6 KiB
MCP Server Reference
McpServer Factory
org.springframework.ai.mcp.server.McpServer
The McpServer class serves as a factory for creating Model Context Protocol (MCP) servers. It provides the main entry point for establishing MCP servers, offering both synchronous and asynchronous server implementations.
Overview
The server factory uses a builder pattern for flexible configuration, allowing you to customize various aspects of the server including server information, capabilities, tools, resource providers, and prompt providers.
Server Types
The factory can create two types of servers:
McpAsyncServerfor non-blocking operationsMcpSyncServerfor blocking operations
Builder Configuration
Basic Usage
McpServer.using(transport)
.serverInfo("my-server", "1.0.0")
.addTool(new MyToolHandler())
.async(); // or .sync()
Builder Methods
using(McpTransport transport)
public static Builder using(McpTransport transport)
Start building an MCP server with the specified transport.
serverInfo(Implementation serverInfo)
public Builder serverInfo(McpSchema.Implementation serverInfo)
Set the server implementation information.
serverInfo(String name, String version)
public Builder serverInfo(String name, String version)
Set the server implementation information using name and version.
serverCapabilities(ServerCapabilities capabilities)
public Builder serverCapabilities(McpSchema.ServerCapabilities serverCapabilities)
Set the server capabilities configuration.
addTool(ToolHandler handler)
public <T extends McpAsyncServer.ToolHandler> Builder addTool(T toolHandler)
Add a tool handler to the server.
tool(Tool tool, Function<Map<String, Object>, CallToolResult> handler)
public Builder tool(McpSchema.Tool tool, Function<Map<String, Object>, McpSchema.CallToolResult> handler)
Add a tool with its handler function.
addTools(List handlers)
public Builder addTools(List<ToolHandler> toolHandlers)
Add multiple tool handlers to the server.
resourcesProvider(Function<String, List> provider)
public Builder resourcesProvider(Function<String, List<McpSchema.Resource>> resourcesProvider)
Set the resources provider function.
promptsProvider(Function<String, List> provider)
public Builder promptsProvider(Function<String, List<McpSchema.Prompt>> promptsProvider)
Set the prompts provider function.
sync()
public McpSyncServer sync()
Build and return a synchronous MCP server.
async()
public AsyncServerBuilder async()
Build and return an asynchronous server builder.
Usage Examples
Creating a Basic Server
McpTransport transport = // obtain transport instance
McpServer.using(transport)
.serverInfo("my-server", "1.0.0")
.sync();
Creating a Server with Tools
McpServer.using(transport)
.serverInfo("calculation-server", "1.0.0")
.tool(
new McpSchema.Tool("calculate", "Performs calculations", Map.of(
"operation", Map.of("type", "string"),
"numbers", Map.of("type", "array")
)),
args -> {
// Handle calculation
return new McpSchema.CallToolResult(/* result */);
}
)
.async();
Creating a Server with Resources and Prompts
McpServer.using(transport)
.serverInfo("content-server", "1.0.0")
.resourcesProvider(cursor -> {
// Return list of resources based on cursor
return List.of(/* resources */);
})
.promptsProvider(cursor -> {
// Return list of prompts based on cursor
return List.of(/* prompts */);
})
.sync();
Implementation Notes
- The server factory uses a builder pattern for configuration
- Default server info is "mcp-server" version "1.0.0" if not specified
- Tool handlers must be thread-safe as they may be called concurrently
- The synchronous server is implemented as a wrapper around the asynchronous server
- Resource and prompt providers use cursor-based pagination
- The AsyncServerBuilder allows adding tools after server creation
Tool Handler Interface
public interface ToolHandler {
String getName();
String getDescription();
Map<String, Object> getInputSchema();
McpSchema.CallToolResult call(Map<String, Object> arguments);
}
Tool handlers implement this interface to define:
- Tool name and description
- Input schema for validation
- Execution logic in the call method