- Notifications
You must be signed in to change notification settings - Fork0
cloud4java/my-mcp-stdio-server
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A Spring Boot starter project demonstrating how to build a Model Context Protocol (MCP) server that provides weather-related tools using the National Weather Service (weather.gov) API. This project showcases the Spring AI MCP Server Boot Starter capabilities with STDIO transport implementation.
For more information, see theMCP Server Boot Starter reference documentation.
- Java 17 or later
- Maven 3.6 or later
- Understanding of Spring Boot and Spring AI concepts
- (Optional) Claude Desktop for AI assistant integration
Thespring-ai-mcp-server-spring-boot-starter provides:
- Automatic configuration of MCP server components
- Support for both synchronous and asynchronous operation modes
- STDIO transport layer implementation
- Flexible tool registration through Spring beans
- Change notification capabilities
Thespring-ai-mcp-server-spring-boot-starter provides:
- Automatic configuration of MCP server components
- Support for both synchronous and asynchronous operation modes
- STDIO transport layer implementation
- Flexible tool registration through Spring beans
- Change notification capabilities
src/├── main/│ ├── java/│ │ └── org/springframework/ai/mcp/sample/server/│ │ ├── McpServerApplication.java # Main application class with tool registration│ │ └── WeatherService.java # Weather service implementation with MCP tools│ └── resources/│ └── application.properties # Server and transport configuration└── test/ └── java/ └── org/springframework/ai/mcp/sample/client/ └── ClientStdio.java # Test client implementationThe server uses STDIO transport mode and is typically started automatically by the client. To build the server jar:
./mvnw clean install -DskipTests
The project demonstrates how to implement and register MCP tools using Spring's dependency injection and auto-configuration:
@ServicepublicclassWeatherService {@Tool(description ="Get weather forecast for a specific latitude/longitude")publicStringgetWeatherForecastBrazil(doublelatitude,// Latitude coordinatedoublelongitude// Longitude coordinate ) {// Implementation }@Tool(description ="Get weather alerts for a US state")publicStringgetAlerts(Stringstate// Two-letter US state code (e.g., CA, NY) ) {// Implementation }}@SpringBootApplicationpublicclassMcpServerApplication {@BeanpublicList<ToolCallback>weatherTools(WeatherServiceweatherService) {returnToolCallbacks.from(weatherService); }}
The auto-configuration automatically registers these tools with the MCP server. You can have multiple beans producing lists of ToolCallbacks, and the auto-configuration will merge them.
@Tool(description ="Get weather forecast for a specific latitude/longitude")publicStringgetWeatherForecastBrazil(doublelatitude,// Latitude coordinatedoublelongitude// Longitude coordinate) {// Returns detailed forecast including:// - Temperature and unit// - Wind speed and direction// - Detailed forecast description}// Example usage:CallToolResultforecast =client.callTool(newCallToolRequest("getWeatherForecastBrazil",Map.of("latitude",47.6062,// Seattle coordinates"longitude", -122.3321 ) ));
@Tool(description ="Get weather alerts for a US state")publicStringgetAlerts(Stringstate// Two-letter US state code (e.g., CA, NY)) {// Returns active alerts including:// - Event type// - Affected area// - Severity// - Description// - Safety instructions}// Example usage:CallToolResultalerts =client.callTool(newCallToolRequest("getAlerts",Map.of("state","NY") ));
// Create server parametersServerParametersstdioParams =ServerParameters.builder("java") .args("-Dspring.ai.mcp.server.transport=STDIO","-Dspring.main.web-application-type=none","-Dlogging.pattern.console=","-jar","target/my-mcp-stdio-server-0.0.1-SNAPSHOT.jar") .build();// Initialize transport and clientvartransport =newStdioClientTransport(stdioParams);varclient =McpClient.sync(transport).build();
TheClientStdio.java demonstrates how to implement an MCP client manually.
For a better development experience, consider using theMCP Client Boot Starters. These starters enable auto-configuration of multiple STDIO and/or SSE connections to MCP servers. See thestarter-default-client andstarter-webflux-client projects for examples.
Use thestarter-default-client to connect to the weatherstarter-stdio-server:
Follow the
starter-default-clientreadme instructions to build amcp-starter-default-client-0.0.1-SNAPSHOT.jarclient application.Run the client using the configuration file:
java -Dspring.ai.mcp.client.stdio.connections.server1.command=java \ -Dspring.ai.mcp.client.stdio.connections.server1.args=-jar,/Users/christiantzolov/Dev/projects/spring-ai-examples/model-context-protocol/weather/starter-stdio-server/target/my-mcp-stdio-server-0.0.1-SNAPSHOT.jar \ -Dai.user.input='What is the weather in NY?' \ -Dlogging.pattern.console= \ -jar mcp-starter-default-client-0.0.1-SNAPSHOT.jarTo integrate with Claude Desktop, add the following configuration to your Claude Desktop settings:
{"mcpServers": {"spring-ai-mcp-weather": {"command":"java","args": ["-Dspring.ai.mcp.server.stdio=true","-Dspring.main.web-application-type=none","-Dlogging.pattern.console=","-jar","/absolute/path/to/my-mcp-stdio-server-0.0.1-SNAPSHOT.jar" ] } }}Replace/absolute/path/to/ with the actual path to your built jar file.
All properties are prefixed withspring.ai.mcp.server:
# Required STDIO Configurationspring.main.web-application-type=nonespring.main.banner-mode=offlogging.pattern.console=# Server Configurationspring.ai.mcp.server.enabled=truespring.ai.mcp.server.name=my-weather-serverspring.ai.mcp.server.version=0.0.1# SYNC or ASYNCspring.ai.mcp.server.type=SYNCspring.ai.mcp.server.resource-change-notification=truespring.ai.mcp.server.tool-change-notification=truespring.ai.mcp.server.prompt-change-notification=true# Optional file logginglogging.file.name=my-mcp-stdio-server.log
STDIO Mode Requirements
- Disable web application type (
spring.main.web-application-type=none) - Disable Spring banner (
spring.main.banner-mode=off) - Clear console logging pattern (
logging.pattern.console=)
- Disable web application type (
Server Type
SYNC(default): UsesMcpSyncServerfor straightforward request-response patternsASYNC: UsesMcpAsyncServerfor non-blocking operations with Project Reactor support
About
Spring AI MCP Weather STDIO Server - A Spring Boot starter project demonstrating how to build a Model Context Protocol (MCP) server that provides weather-related tools
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.