Skip to content

Commit 3cea898

Browse files
committed
Add MCP annotations sample project
Demonstrates the use of Spring AI MCP annotations for creating an MCP server. - Add @McpComplete, @McpArg, @McpResource, @McpPrompt handlers The project showcases how to use Spring AI's annotation-based approach to easily expose tools, resources, prompts, and completions through an MCP server. Signed-off-by: Christian Tzolov <[email protected]>
1 parent 4a54643 commit 3cea898

File tree

15 files changed

+1612
-0
lines changed

15 files changed

+1612
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
# Spring AI MCP Weather Server Sample with WebMVC Starter
2+
3+
This sample project demonstrates how to create an MCP server using the Spring AI MCP Server Boot Starter with WebMVC transport. It implements a weather service that exposes tools for retrieving weather information using the National Weather Service API.
4+
5+
For more information, see the [MCP Server Boot Starter](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs.html) reference documentation.
6+
7+
## Overview
8+
9+
The sample showcases:
10+
- Integration with `spring-ai-mcp-server-webmvc-spring-boot-starter`
11+
- Support for both SSE (Server-Sent Events) and STDIO transports
12+
- Automatic tool registration using Spring AI's `@Tool` annotation
13+
- Two weather-related tools:
14+
- Get weather forecast by location (latitude/longitude)
15+
- Get weather alerts by US state
16+
17+
## Dependencies
18+
19+
The project requires the Spring AI MCP Server WebMVC Boot Starter:
20+
21+
```xml
22+
<dependency>
23+
<groupId>org.springframework.ai</groupId>
24+
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
25+
</dependency>
26+
```
27+
28+
This starter provides:
29+
- HTTP-based transport using Spring MVC (`WebMvcSseServerTransport`)
30+
- Auto-configured SSE endpoints
31+
- Optional STDIO transport
32+
- Included `spring-boot-starter-web` and `mcp-spring-webmvc` dependencies
33+
34+
## Building the Project
35+
36+
Build the project using Maven:
37+
```bash
38+
./mvnw clean install -DskipTests
39+
```
40+
41+
## Running the Server
42+
43+
The server supports two transport modes:
44+
45+
### WebMVC SSE Mode (Default)
46+
```bash
47+
java -jar target/mcp-weather-starter-webmvc-server-0.0.1-SNAPSHOT.jar
48+
```
49+
50+
### STDIO Mode
51+
To enable STDIO transport, set the appropriate properties:
52+
```bash
53+
java -Dspring.ai.mcp.server.stdio=true -Dspring.main.web-application-type=none -jar target/mcp-weather-starter-webmvc-server-0.0.1-SNAPSHOT.jar
54+
```
55+
56+
## Configuration
57+
58+
Configure the server through `application.properties`:
59+
60+
```properties
61+
# Server identification
62+
spring.ai.mcp.server.name=my-weather-server
63+
spring.ai.mcp.server.version=0.0.1
64+
65+
# Server type (SYNC/ASYNC)
66+
spring.ai.mcp.server.type=SYNC
67+
68+
# Transport configuration
69+
spring.ai.mcp.server.stdio=false
70+
spring.ai.mcp.server.sse-message-endpoint=/mcp/message
71+
72+
# Change notifications
73+
spring.ai.mcp.server.resource-change-notification=true
74+
spring.ai.mcp.server.tool-change-notification=true
75+
spring.ai.mcp.server.prompt-change-notification=true
76+
77+
# Logging (required for STDIO transport)
78+
spring.main.banner-mode=off
79+
logging.file.name=./target/starter-webmvc-server.log
80+
```
81+
82+
## Available Tools
83+
84+
### Weather Forecast Tool
85+
- Name: `getWeatherForecastByLocation`
86+
- Description: Get weather forecast for a specific latitude/longitude
87+
- Parameters:
88+
- `latitude`: double - Latitude coordinate
89+
- `longitude`: double - Longitude coordinate
90+
91+
### Weather Alerts Tool
92+
- Name: `getAlerts`
93+
- Description: Get weather alerts for a US state
94+
- Parameters:
95+
- `state`: String - Two-letter US state code (e.g., CA, NY)
96+
97+
## Server Implementation
98+
99+
The server uses Spring Boot and Spring AI's tool annotations for automatic tool registration:
100+
101+
```java
102+
@SpringBootApplication
103+
public class McpServerApplication {
104+
public static void main(String[] args) {
105+
SpringApplication.run(McpServerApplication.class, args);
106+
}
107+
108+
@Bean
109+
public ToolCallbackProvider weatherTools(WeatherService weatherService){
110+
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
111+
}
112+
}
113+
```
114+
115+
The `WeatherService` implements the weather tools using the `@Tool` annotation:
116+
117+
```java
118+
@Service
119+
public class WeatherService {
120+
@Tool(description = "Get weather forecast for a specific latitude/longitude")
121+
public String getWeatherForecastByLocation(double latitude, double longitude) {
122+
// Implementation using weather.gov API
123+
}
124+
125+
@Tool(description = "Get weather alerts for a US state. Input is Two-letter US state code (e.g., CA, NY)")
126+
public String getAlerts(String state) {
127+
// Implementation using weather.gov API
128+
}
129+
}
130+
```
131+
132+
## MCP Clients
133+
134+
You can connect to the weather server using either STDIO or SSE transport:
135+
136+
### Manual Clients
137+
138+
#### WebMVC SSE Client
139+
140+
For servers using SSE transport:
141+
142+
```java
143+
var transport = new HttpClientSseClientTransport("http://localhost:8080");
144+
var client = McpClient.sync(transport).build();
145+
```
146+
147+
#### STDIO Client
148+
149+
For servers using STDIO transport:
150+
151+
```java
152+
var stdioParams = ServerParameters.builder("java")
153+
.args("-Dspring.ai.mcp.server.stdio=true",
154+
"-Dspring.main.web-application-type=none",
155+
"-Dspring.main.banner-mode=off",
156+
"-Dlogging.pattern.console=",
157+
"-jar",
158+
"target/mcp-weather-starter-webmvc-server-0.0.1-SNAPSHOT.jar")
159+
.build();
160+
161+
var transport = new StdioClientTransport(stdioParams);
162+
var client = McpClient.sync(transport).build();
163+
```
164+
165+
The sample project includes example client implementations:
166+
- [SampleClient.java](src/test/java/org/springframework/ai/mcp/sample/client/SampleClient.java): Manual MCP client implementation
167+
- [ClientStdio.java](src/test/java/org/springframework/ai/mcp/sample/client/ClientStdio.java): STDIO transport connection
168+
- [ClientSse.java](src/test/java/org/springframework/ai/mcp/sample/client/ClientSse.java): SSE transport connection
169+
170+
For a better development experience, consider using the [MCP Client Boot Starters](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html). These starters enable auto-configuration of multiple STDIO and/or SSE connections to MCP servers. See the [starter-default-client](../../client-starter/starter-default-client) project for examples.
171+
172+
### Boot Starter Clients
173+
174+
Let's use the [starter-default-client](../../client-starter/starter-default-client) client to connect to our weather `starter-webmvc-server`.
175+
176+
Follow the `starter-default-client` readme instruction to build a `mcp-starter-default-client-0.0.1-SNAPSHOT.jar` client application.
177+
178+
#### STDIO Transport
179+
180+
1. Create a `mcp-servers-config.json` configuration file with this content:
181+
182+
```json
183+
{
184+
"mcpServers": {
185+
"weather-starter-webmvc-server": {
186+
"command": "java",
187+
"args": [
188+
"-Dspring.ai.mcp.server.stdio=true",
189+
"-Dspring.main.web-application-type=none",
190+
"-Dlogging.pattern.console=",
191+
"-jar",
192+
"/absolute/path/to/mcp-weather-starter-webmvc-server-0.0.1-SNAPSHOT.jar"
193+
]
194+
}
195+
}
196+
}
197+
```
198+
199+
2. Run the client using the configuration file:
200+
201+
```bash
202+
java -Dspring.ai.mcp.client.stdio.servers-configuration=file:mcp-servers-config.json \
203+
-Dai.user.input='What is the weather in NY?' \
204+
-Dlogging.pattern.console= \
205+
-jar mcp-starter-default-client-0.0.1-SNAPSHOT.jar
206+
```
207+
208+
#### SSE (WebMVC) Transport
209+
210+
1. Start the `mcp-weather-starter-webmvc-server`:
211+
212+
```bash
213+
java -jar mcp-weather-starter-webmvc-server-0.0.1-SNAPSHOT.jar
214+
```
215+
216+
starts the MCP server on port 8080.
217+
218+
2. In another console start the client configured with SSE transport:
219+
220+
```bash
221+
java -Dspring.ai.mcp.client.sse.connections.weather-server.url=http://localhost:8080 \
222+
-Dlogging.pattern.console= \
223+
-Dai.user.input='What is the weather in NY?' \
224+
-jar mcp-starter-default-client-0.0.1-SNAPSHOT.jar
225+
```
226+
227+
## Additional Resources
228+
229+
* [Spring AI Documentation](https://docs.spring.io/spring-ai/reference/)
230+
* [MCP Server Boot Starter](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs.html)
231+
* [MCP Client Boot Starter](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-client-docs.html)
232+
* [Model Context Protocol Specification](https://modelcontextprotocol.github.io/specification/)
233+
* [Spring Boot Auto-configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration)

0 commit comments

Comments
 (0)