diwang01/spring-mcp-server
3.2
If you are the rightful owner of spring-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
A modern Model Context Protocol (MCP) server implementation using Spring Framework and Java 21.
Tools
2
Resources
0
Prompts
0
Spring MCP Server
A modern Model Context Protocol (MCP) server implementation using Spring Framework and Java 21.
Features
- ✨ Java 21: Leverages latest Java features (records, sealed classes, pattern matching, switch expressions)
- 🚀 Spring Boot 3.3+: Modern Spring Framework with full dependency injection
- 🔧 MCP Protocol: Implements tools, resources, and prompts
- 🧪 Well-Tested: Comprehensive unit tests with JUnit 5
- 📦 Maven Build: Easy dependency management and building
Architecture
Clean Code Principles Applied
- Records for Immutability: All DTOs use Java records for concise, immutable data structures
- Sealed Classes: Type-safe hierarchies with compile-time exhaustiveness
- Functional Programming: Optional, Stream API, and functional error handling
- SOLID Principles: Single responsibility, dependency injection, interface segregation
- Modern Switch Expressions: Pattern matching for cleaner conditional logic
Project Structure
src/main/java/com/example/mcp/
├── McpServerApplication.java # Main application entry point
├── config/
│ └── McpServerConfig.java # MCP server configuration
├── tool/
│ ├── EchoTool.java # Example tool: echo messages
│ └── DateTimeTool.java # Example tool: date/time operations
├── resource/
│ └── SystemInfoResource.java # Example resource: system information
└── prompt/
└── TemplatePromptProvider.java # Example prompts: code review templates
Components
Tools
- EchoTool: Demonstrates string manipulation with optional parameters
- DateTimeTool: Shows timezone handling and date formatting with error handling
Resources
- SystemInfoResource: Provides JVM and system runtime information using sealed classes
Prompts
- TemplatePromptProvider: Code review and explanation templates with functional composition
Building
mvn clean package
Running
mvn spring-boot:run
Testing
mvn test
Java 21 Features Used
Records
public record EchoInput(String message, String prefix) {}
Sealed Classes
public sealed interface SystemInfo permits JavaInfo, RuntimeInfo, MemoryInfo {}
Pattern Matching
return switch (info) {
case JavaInfo java -> formatJava(java);
case RuntimeInfo rt -> formatRuntime(rt);
case MemoryInfo mem -> formatMemory(mem);
};
Text Blocks
String template = """
Please review the following code:
{{code}}
""";
Dependencies
- Spring Boot 3.3.5
- Spring AI MCP (Milestone)
- Java 21
- JUnit 5
- AssertJ
Best Practices
- Null Safety: Using
Optionalfor nullable values - Immutability: Records and
List.copyOf()for defensive copying - Error Handling: Functional error handling without exceptions where possible
- Thread Safety: Immutable data structures by default
- Dependency Injection: Constructor injection with Spring
- Testing: Parameterized tests for comprehensive coverage
License
MIT License