spring-mcp-server

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

  1. Records for Immutability: All DTOs use Java records for concise, immutable data structures
  2. Sealed Classes: Type-safe hierarchies with compile-time exhaustiveness
  3. Functional Programming: Optional, Stream API, and functional error handling
  4. SOLID Principles: Single responsibility, dependency injection, interface segregation
  5. 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

  1. Null Safety: Using Optional for nullable values
  2. Immutability: Records and List.copyOf() for defensive copying
  3. Error Handling: Functional error handling without exceptions where possible
  4. Thread Safety: Immutable data structures by default
  5. Dependency Injection: Constructor injection with Spring
  6. Testing: Parameterized tests for comprehensive coverage

License

MIT License