$npx -y skills add rrezartprebreza/spring-boot-skills --skill mcp-serverUse when building MCP (Model Context Protocol) servers in Java/Spring Boot. Covers tool registration, resource exposure, prompt templates, and production deployment using the official MCP Java SDK. Use when user mentions MCP, AI agent integration, or tool calling.
| 1 | # MCP Server — Java SDK |
| 2 | |
| 3 | Official Java SDK: https://github.com/modelcontextprotocol/java-sdk |
| 4 | Maintained by Anthropic in collaboration with Spring AI. |
| 5 | |
| 6 | ## Dependency |
| 7 | |
| 8 | The standalone SDK reached **1.0.0 GA** (`io.modelcontextprotocol.sdk:mcp`). Most Spring Boot |
| 9 | apps should use the **Spring AI MCP starter** instead — it auto-configures the server, transport, |
| 10 | and tool scanning. The starter coordinates were renamed at Spring AI 1.0 GA to `spring-ai-starter-mcp-server-*`. |
| 11 | |
| 12 | ```xml |
| 13 | <!-- Recommended for Spring Boot: pick ONE transport starter --> |
| 14 | <!-- stdio (Claude Desktop / Claude Code launching the jar locally) --> |
| 15 | <dependency> |
| 16 | <groupId>org.springframework.ai</groupId> |
| 17 | <artifactId>spring-ai-starter-mcp-server</artifactId> |
| 18 | </dependency> |
| 19 | <!-- OR remote HTTP (SSE + Streamable-HTTP) over Spring MVC --> |
| 20 | <dependency> |
| 21 | <groupId>org.springframework.ai</groupId> |
| 22 | <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId> |
| 23 | </dependency> |
| 24 | <!-- OR reactive: spring-ai-starter-mcp-server-webflux --> |
| 25 | ``` |
| 26 | |
| 27 | ```xml |
| 28 | <!-- Or drive the raw SDK directly (no Spring AI), now at 1.0.0 GA --> |
| 29 | <dependency> |
| 30 | <groupId>io.modelcontextprotocol.sdk</groupId> |
| 31 | <artifactId>mcp</artifactId> |
| 32 | <version>1.0.0</version> |
| 33 | </dependency> |
| 34 | ``` |
| 35 | |
| 36 | > The old `spring-ai-mcp-server-spring-boot-starter` name is dead. GA is `spring-ai-starter-mcp-server` |
| 37 | > (stdio), `-webmvc` (servlet SSE / Streamable-HTTP), and `-webflux` (reactive). |
| 38 | |
| 39 | ## Minimal MCP Server (stdio transport) |
| 40 | |
| 41 | ```java |
| 42 | @SpringBootApplication |
| 43 | public class OrderMcpServer { |
| 44 | public static void main(String[] args) { |
| 45 | var transport = new StdioServerTransportProvider(); |
| 46 | |
| 47 | var server = McpServer.sync(transport) |
| 48 | .serverInfo("order-service-mcp", "1.0.0") |
| 49 | .capabilities(ServerCapabilities.builder().tools(true).resources(true).build()) |
| 50 | .tools(getOrderTool(), listOrdersTool()) |
| 51 | .build(); |
| 52 | |
| 53 | Runtime.getRuntime().addShutdownHook(new Thread(server::close)); |
| 54 | } |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ## Defining Tools |
| 59 | |
| 60 | ```java |
| 61 | // Tool with typed input/output |
| 62 | private static McpServerFeatures.SyncToolSpecification getOrderTool() { |
| 63 | var schema = """ |
| 64 | { |
| 65 | "type": "object", |
| 66 | "properties": { |
| 67 | "orderId": { "type": "string", "description": "UUID of the order" } |
| 68 | }, |
| 69 | "required": ["orderId"] |
| 70 | } |
| 71 | """; |
| 72 | |
| 73 | return McpServerFeatures.SyncToolSpecification.builder() |
| 74 | .tool(Tool.builder() |
| 75 | .name("get_order") |
| 76 | .description("Get a single order by ID including all line items and status history") |
| 77 | .inputSchema(schema) |
| 78 | .build()) |
| 79 | .callHandler((exchange, args) -> { |
| 80 | String orderId = (String) args.get("orderId"); |
| 81 | try { |
| 82 | Order order = orderService.findById(UUID.fromString(orderId)); |
| 83 | return new CallToolResult(List.of( |
| 84 | new TextContent(objectMapper.writeValueAsString(order)) |
| 85 | ), false); |
| 86 | } catch (EntityNotFoundException e) { |
| 87 | return new CallToolResult(List.of( |
| 88 | new TextContent("Order not found: " + orderId) |
| 89 | ), true); // isError = true |
| 90 | } |
| 91 | }) |
| 92 | .build(); |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | ## Spring Boot Integration (recommended) |
| 97 | |
| 98 | ```java |
| 99 | @Configuration |
| 100 | public class McpToolsConfig { |
| 101 | |
| 102 | @Bean |
| 103 | public ToolCallbackProvider orderTools(OrderService orderService, ObjectMapper objectMapper) { |
| 104 | return MethodToolCallbackProvider.builder() |
| 105 | .toolObjects(new OrderMcpTools(orderService, objectMapper)) |
| 106 | .build(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Component |
| 111 | public class OrderMcpTools { |
| 112 | private final OrderService orderService; |
| 113 | private final ObjectMapper objectMapper; |
| 114 | |
| 115 | // Spring AI annotation-based tool registration |
| 116 | @Tool(description = "Get order by ID with full line items and status history") |
| 117 | public String getOrder(@ToolParam(description = "UUID of the order") String orderId) { |
| 118 | try { |
| 119 | Order order = orderService.findById(UUID.fromString(orderId)); |
| 120 | return objectMapper.writeValueAsString(OrderResponse.from(order)); |
| 121 | } catch (Exception e) { |
| 122 | return "Error: " + e.getMessage(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Tool(description = "List orders for a customer, optionally filtered by status") |
| 127 | public String listOrders( |
| 128 | @ToolParam(description = "Customer email address") String email, |
| 129 | @ToolParam(description = "Filter by status: PENDING, PROCESSING, SHIPPED, DELIVERED", required = false) String status |
| 130 | ) { |
| 131 | List<Order> orders = status != null |
| 132 | ? orderService.findByEmailAndStatus(email, OrderStatus.valueOf(status)) |
| 133 | : o |