Spring AI - MCP Server
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
application.yaml
spring:
ai:
mcp:
server:
name: todo-mcp-server
version: 1.0.0
type: SYNC
protocol: STREAMABLE
server:
port: 8081
Todo
@Service
public class TodoTools {
private final List<String> items = new CopyOnWriteArrayList<>();
@McpTool(description = "Add an item to the todo list")
public String addTodo(
@McpToolParam(
description = "The text of the todo item to add",
required = true)
String item) {
if (item == null || item.isBlank()) {
throw new IllegalArgumentException("The todo element cannot be empty.");
}
String normalizedItem = item.trim();
items.add(normalizedItem);
return "Added: " + normalizedItem;
}
@McpTool(description = "List all todo items")
public List<String> listTodos() {
return items.isEmpty()
? List.of("The list is empty")
: List.copyOf(items);
}
}