Content
# Springboot + MCP + JUnit Template Project
<hr/>
> Quickly build an MCP project using Springboot, with support for JUnit unit testing.
>
> Supported modes: **STDIO** and **SSE**
> **Tutorial Videos:**
> * First release: Building a Springboot+MCP+JUnit project from scratch 【with CherryStudio】 [BV1qUXkYRE6C](https://www.bilibili.com/video/BV1qUXkYRE6C)
> * First release: Springboot+MCP(SSE)+JUnit from setup to deployment [BV1rSXWYGEVz](https://www.bilibili.com/video/BV1rSXWYGEVz)
<hr/>
### Development Environment
- Development IDE: IDEA
- Java version: JAVA17 (Spring officially requires Java 17+)
- Maven version: 3.8.1 (Using a version that is too high may cause errors when importing the project, please choose an appropriate version)
### Reference Documentation
1. [MCP Official Website - Protocol](https://spec.modelcontextprotocol.io) (Must-read document)
2. [MCP Official Website - Coding Guidelines](https://modelcontextprotocol.io/quickstart/server)
3. [Spring Official Website](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs.html)
### Client Testing Scheme
#### 1. For STDIO Client Configuration:
```text
Command:
java
Arguments:
-jar
-Dfile.encoding=UTF-8
-Dspring.ai.mcp.server.stdio=true
AbsolutePath/**/xxx.jar
Environments:
env1=xxx
env2=yyy
```
The command is `java` (if the local environment is not 17+, please switch);
The parameter `file.encoding` specifies that System.in and System.out are encoded in UTF-8 to avoid encoding issues;
The parameter `spring.ai.mcp.server.stdio` indicates that the service is enabled in STDIO mode;
Environment configuration writes the settings into the current system environment for the current program to access.
<br/>
#### 2. For SSE Client Configuration:
```text
Fill in the SSE address as follows:
http://host_address:port/sse
```
<hr/>
### Exploration of Additional Optional Features
#### 1. Modify Message Endpoint
Refer to the configuration file: McpServerProperties
```yaml
spring.ai.mcp.server.sseMessageEndpoint=/mcp/message
```
#### 2. Modify SSE Endpoint
The original SSE endpoint is at `/sse`, and since the configuration file does not provide a way to configure the SSE endpoint, it is necessary to re-inject **ServerMcpTransport** to replace the original one.
Note that both of the following Bean configurations need to be added because the auto-injection file **MpcWebMvcServerAutoConfiguration** has a condition for the entire registered injection as
`@ConditionalOnMissingBean(ServerMcpTransport.class)`, which prevents the injection of both beans.
```java
private final String SSE_ENDPOINT = "/sse";
@Bean
@ConditionalOnMissingBean
public WebMvcSseServerTransport webMvcSseServerTransport(ObjectMapper objectMapper,
McpServerProperties serverProperties) {
return new WebMvcSseServerTransport(objectMapper, serverProperties.getSseMessageEndpoint(), SSE_ENDPOINT);
}
@Bean
public RouterFunction<ServerResponse> mvcMcpRouterFunction(WebMvcSseServerTransport transport) {
return transport.getRouterFunction();
}
```
#### 3. One Service with Multiple Endpoints?
You need to re-inject multiple **McpSyncServer** instances, see the class **MpcServerAutoConfiguration**.
Since multiple endpoints may involve many aspects, those interested can research it themselves; I just provide this guidance!
#### 4. SSE Authorization?
In the MCP official protocol document updated on March 26, 2025, the authentication scheme was revised,
looking forward to updates from Spring.
<br/>
<hr/>
Bye.ヾ(•ω•`)o