$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill spring-ai-mcp-server-patternsProvides Spring Boot MCP server patterns that create Model Context Protocol servers with Spring AI by defining tool handlers, exposing resources, configuring prompt templates, and setting up transports for AI function calling and tool calling. Use when building MCP servers to ext
| 1 | # Spring AI MCP Server Implementation Patterns |
| 2 | |
| 3 | Implements MCP servers with Spring AI for AI function calling, tool handlers, and MCP transport configuration. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | Production-ready MCP server patterns: `@Tool` functions, `@PromptTemplate` resources, and stdio/HTTP/SSE transports with Spring AI security. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | MCP servers, Spring AI function calling, AI tools, tool calling, custom tool handlers, Spring Boot MCP, resource endpoints, or MCP transport configuration. |
| 12 | |
| 13 | ## Quick Reference |
| 14 | |
| 15 | ### Core Annotations |
| 16 | |
| 17 | | Annotation | Target | Purpose | |
| 18 | |-----------|--------|---------| |
| 19 | | `@EnableMcpServer` | Class | Enable MCP server auto-configuration | |
| 20 | | `@Tool(description)` | Method | Declare AI-callable tool | |
| 21 | | `@ToolParam(value)` | Parameter | Document tool parameter for AI | |
| 22 | | `@PromptTemplate(name)` | Method | Declare reusable prompt template | |
| 23 | | `@PromptParam(value)` | Parameter | Document prompt parameter | |
| 24 | |
| 25 | ### Transport Types |
| 26 | |
| 27 | | Transport | Use Case | Config | |
| 28 | |-----------|----------|--------| |
| 29 | | `stdio` | Local process / Claude Desktop | Default | |
| 30 | | `http` | Remote HTTP clients | `port`, `path` | |
| 31 | | `sse` | Real-time streaming clients | `port`, `path` | |
| 32 | |
| 33 | ### Key Dependencies |
| 34 | |
| 35 | ```xml |
| 36 | <!-- Maven --> |
| 37 | <dependency> |
| 38 | <groupId>org.springframework.ai</groupId> |
| 39 | <artifactId>spring-ai-mcp-server</artifactId> |
| 40 | <version>1.0.0</version> |
| 41 | </dependency> |
| 42 | <dependency> |
| 43 | <groupId>org.springframework.ai</groupId> |
| 44 | <artifactId>spring-ai-starter-model-openai</artifactId> |
| 45 | <version>1.0.0</version> |
| 46 | </dependency> |
| 47 | ``` |
| 48 | |
| 49 | ```gradle |
| 50 | // Gradle |
| 51 | implementation 'org.springframework.ai:spring-ai-mcp-server:1.0.0' |
| 52 | implementation 'org.springframework.ai:spring-ai-starter-model-openai:1.0.0' |
| 53 | ``` |
| 54 | |
| 55 | ## Instructions |
| 56 | |
| 57 | ### 1. Project Setup |
| 58 | |
| 59 | Add Spring AI MCP dependencies (see Quick Reference above), configure the AI model in `application.properties`, and enable MCP with `@EnableMcpServer`: |
| 60 | |
| 61 | ```java |
| 62 | @SpringBootApplication |
| 63 | @EnableMcpServer |
| 64 | public class MyMcpApplication { |
| 65 | public static void main(String[] args) { |
| 66 | SpringApplication.run(MyMcpApplication.class, args); |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ```properties |
| 72 | spring.ai.openai.api-key=${OPENAI_API_KEY} |
| 73 | spring.ai.mcp.enabled=true |
| 74 | spring.ai.mcp.transport.type=stdio |
| 75 | ``` |
| 76 | |
| 77 | ### 2. Define Tools |
| 78 | |
| 79 | Annotate methods with `@Tool` inside `@Component` beans. Use `@ToolParam` to document parameters: |
| 80 | |
| 81 | ```java |
| 82 | @Component |
| 83 | public class WeatherTools { |
| 84 | |
| 85 | @Tool(description = "Get current weather for a city") |
| 86 | public WeatherData getWeather(@ToolParam("City name") String city) { |
| 87 | return weatherService.getCurrentWeather(city); |
| 88 | } |
| 89 | |
| 90 | @Tool(description = "Get 5-day forecast for a city") |
| 91 | public ForecastData getForecast( |
| 92 | @ToolParam("City name") String city, |
| 93 | @ToolParam(value = "Unit: celsius or fahrenheit", required = false) String unit) { |
| 94 | return weatherService.getForecast(city, unit != null ? unit : "celsius"); |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | See [references/implementation-patterns.md](references/implementation-patterns.md) for database tools, API integration tools, and the `FunctionCallback` low-level pattern. |
| 100 | |
| 101 | ### 3. Create Prompt Templates |
| 102 | |
| 103 | ```java |
| 104 | @Component |
| 105 | public class CodeReviewPrompts { |
| 106 | |
| 107 | @PromptTemplate( |
| 108 | name = "java-code-review", |
| 109 | description = "Review Java code for best practices and issues" |
| 110 | ) |
| 111 | public Prompt createCodeReviewPrompt( |
| 112 | @PromptParam("code") String code, |
| 113 | @PromptParam(value = "focusAreas", required = false) List<String> focusAreas) { |
| 114 | |
| 115 | String focus = focusAreas != null ? String.join(", ", focusAreas) : "general best practices"; |
| 116 | return Prompt.builder() |
| 117 | .system("You are an expert Java code reviewer with 20 years of experience.") |
| 118 | .user("Review the following Java code for " + focus + ":\n```java\n" + code + "\n```") |
| 119 | .build(); |
| 120 | } |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | See [references/implementation-patterns.md](references/implementation-patterns.md) for additional prompt template patterns. |
| 125 | |
| 126 | ### 4. Configure Transport |
| 127 | |
| 128 | ```yaml |
| 129 | spring: |
| 130 | ai: |
| 131 | mcp: |
| 132 | enabled: true |
| 133 | transport: |
| 134 | type: stdio # stdio | http | sse |
| 135 | http: |
| 136 | port: 8080 |
| 137 | path: /mcp |
| 138 | server: |
| 139 | name: my-mcp-server |
| 140 | version: 1.0.0 |
| 141 | ``` |
| 142 | |
| 143 | ### 5. Add Security |
| 144 | |
| 145 | ```java |
| 146 | @Configuration |
| 147 | public class McpSecurityConfig { |
| 148 | |
| 149 | @Bean |
| 150 | public ToolFilter toolFilter(SecurityService securityService) { |
| 151 | return (tool, context) -> { |
| 152 | User user = |