Skip to main content

Build Your MCP Server with SpringBoot

1. Background

In traditional human-machine interaction, AI's information sources mainly rely on large-scale corpora fed during pre-training, or limited network access. Therefore, if you want AI to analyze your own website content, or combine all your answers on Zhihu for in-depth analysis, traditional AI cannot accomplish this.

MCP Server solves this problem. Its essence is to connect more information sources to AI, essentially expanding AI's "perception range". Through MCP Server, you can let AI access data you provide in real-time, such as web pages, databases, document interfaces, etc., for more contextual analysis and responses.

Of course, the cost of this capability is: you need to integrate and maintain these data sources you want AI to access yourself.

So how do we do it? The following content will use Java SpringBoot to implement a simple case: building an MCP weather server that lets Claude Desktop know real-time weather.

2. Building Spring AI MCP Server with Spring AI

For Java developers, SpringBoot is a very familiar framework, so here we'll directly download the already built Spring AI MCP Weather STDIO Server project for explanation.

Code location: starter-stdio-server, and you can find more examples in the repository: spring-ai-examples

2.1 Project Explanation: pom.xml

From pom.xml we can clearly see it directly uses spring-ai-starter-mcp-server and spring-web dependencies, with no additional dependencies.

  • spring-ai-starter-mcp-server is an official Spring AI dependency for building custom MCP Servers.
  • spring-web is more familiar - it's a core module in Spring Framework that provides basic functionality for building web applications.
pom.xml
    <dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>

2.2 Project Explanation: application.properties

application.properties
spring.main.web-application-type=none

# Note: You must disable banner and console logging for STDIO transport to work properly!!!

spring.main.banner-mode=off
logging.pattern.console=

spring.ai.mcp.server.name=my-weather-server
spring.ai.mcp.server.version=0.0.1

logging.file.name=./model-context-protocol/weather/starter-stdio-server/target/mcp-weather-stdio-server.log

2.3 Implementation

The project structure shows only two classes: McpServerApplication.java and WeatherService.java

The WeatherService.java has two methods decorated with @Tool annotation. This annotation's main purpose is to mark a method as a tool in Spring AI.

The method parameters decorated with @ToolParam annotation add description information to help AI understand parameter meanings.

This Service's function is: when AI calls it, request the api.weather.gov interface to get weather information, then output to the user.

It provides two AI-callable interfaces:

  1. getWeatherForecastByLocation(double latitude, double longitude) — Get weather forecast for a location
  2. getAlerts(String state) — Get weather alerts for a US state

2.4 Using the Project

When you want to start this project, you need Java 17+ and Maven 3.6+.

First, package it as a JAR: run mvn package in the project root directory, and you'll see mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar in the target directory.

Then you need Claude for Desktop installed locally. If not, download it here: https://claude.ai/download

After installation, open with a text editor:

~/Library/Application Support/Claude/claude_desktop_config.json

Create the file if it doesn't exist.

Then write the following content, remember to modify /ABSOLUTE/PATH/TO/PARENT/FOLDER/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar to your JAR's absolute path:

{
"mcpServers": {
"spring-ai-mcp-weather": {
"command": "java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-jar",
"/ABSOLUTE/PATH/TO/PARENT/FOLDER/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar"
]
}
}
}

Finally, restart your Claude, and you can even ask AI directly: How many MCP servers are you connected to?

Then ask about Los Angeles weather, and it will actually call your JAR:

Done! Go extend your MCP Server.

3. References

Model Context Protocol | For Server Developers