Add mcp-stateless module: migrate an MCP server from the 2025 stateful protocol to 2026-07-28 on Spring AI 2.1.0

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4tJPXfq5UyiLSFr49
This commit is contained in:
Ankur
2026-11-22 02:18:48 +05:30
co-authored by Claude Sonnet 5.5
parent 3aa20a1dce
commit 26e0a3875a
34 changed files with 1922 additions and 0 deletions
@@ -0,0 +1,71 @@
package com.ankurm.mcpstateless;
import java.util.LinkedHashMap;
import java.util.Map;
import io.modelcontextprotocol.modern.McpSchema.CallToolOutcome;
import io.modelcontextprotocol.modern.McpSchema.CallToolResult;
import io.modelcontextprotocol.modern.McpSchema.ElicitFormRequest;
import io.modelcontextprotocol.modern.McpSchema.ElicitResult;
import io.modelcontextprotocol.modern.McpSchema.InputRequiredResult;
import io.modelcontextprotocol.modern.McpSchema.TextContent;
import org.springframework.ai.mcp.annotation.McpTool;
import org.springframework.ai.mcp.annotation.McpToolParam;
import org.springframework.ai.mcp.annotation.context.McpModernRequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
/**
* The 2026-07-28 replacement for "ask the user in the middle of a tool call". The tool does not call
* back into the client. It returns {@code InputRequiredResult}, the client answers, and the client
* retries the SAME call with the answer attached. Nothing is parked on the server between the two
* requests, which is why any instance can take the retry.
*
* <p>Modern profile only: a legacy server has no way to express this tool.
*/
@Component
@Profile("modern")
public class CancelOrderTool {
private final String instance;
public CancelOrderTool(@Value("${instance.name:solo}") String instance) {
this.instance = instance;
}
/** A LinkedHashMap, not Map.of: Map.of iterates in a different order in every JVM, which makes the wire output unstable. */
private static Map<String, Object> schema() {
Map<String, Object> ok = new LinkedHashMap<>();
ok.put("type", "boolean");
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("ok", ok);
Map<String, Object> schema = new LinkedHashMap<>();
schema.put("type", "object");
schema.put("properties", properties);
return schema;
}
@McpTool(name = "cancel_order", description = "Cancels an order after the user confirms",
annotations = @McpTool.McpAnnotations(readOnlyHint = false, destructiveHint = true,
idempotentHint = true, openWorldHint = false))
public CallToolOutcome cancelOrder(McpModernRequestContext ctx,
@McpToolParam(description = "Order id such as A-101") String orderId) {
ElicitResult confirmation = ctx.inputResponse("confirm", ElicitResult.class);
if (confirmation == null) {
// First request: no answer yet. Ask, and seal the order id into requestState.
return InputRequiredResult.builder()
.elicit("confirm", ElicitFormRequest.builder("Cancel order " + orderId + "?", schema()).build())
.requestState("orderId=" + orderId)
.build();
}
boolean confirmed = confirmation.action() == ElicitResult.Action.ACCEPT
&& Boolean.TRUE.equals(confirmation.content().get("ok"));
String text = confirmed
? "order " + orderId + " cancelled (answered by instance " + instance + ")"
: "order " + orderId + " kept (answered by instance " + instance + ")";
return CallToolResult.builder().addContent(TextContent.builder(text).build()).build();
}
}
@@ -0,0 +1,35 @@
package com.ankurm.mcpstateless;
import java.util.Map;
import org.springframework.ai.mcp.annotation.McpTool;
import org.springframework.ai.mcp.annotation.McpToolParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* The same {@code @McpTool} bean serves both protocol generations. The tool reports which
* instance answered, so a load-balancer test can see the requests spreading out.
*/
@Component
public class OrderTools {
private static final Map<String, String> ORDERS = Map.of(
"A-100", "PACKED",
"A-101", "SHIPPED",
"A-102", "DELIVERED");
private final String instance;
public OrderTools(@Value("${instance.name:solo}") String instance) {
this.instance = instance;
}
@McpTool(name = "order_status", description = "Looks up the status of an order by its id",
annotations = @McpTool.McpAnnotations(readOnlyHint = true, destructiveHint = false,
idempotentHint = true, openWorldHint = false))
public String orderStatus(@McpToolParam(description = "Order id such as A-101") String orderId) {
String status = ORDERS.getOrDefault(orderId, "UNKNOWN");
return "order " + orderId + " is " + status + " (answered by instance " + instance + ")";
}
}
@@ -0,0 +1,16 @@
package com.ankurm.mcpstateless;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* One order-lookup MCP server that can run as a 2025-era stateful server ({@code legacy} profile)
* or as a 2026-07-28 stateless server ({@code modern} profile). The tools are identical in both.
*/
@SpringBootApplication
public class StatelessApp {
public static void main(String[] args) {
SpringApplication.run(StatelessApp.class, args);
}
}
@@ -0,0 +1,6 @@
# The 2025-era server: initialize handshake, Mcp-Session-Id, one session per client held in server memory.
spring:
ai:
mcp:
server:
protocol: STREAMABLE
@@ -0,0 +1,13 @@
# The 2026-07-28 server: no handshake, no session, every POST stands alone.
spring:
ai:
mcp:
server:
protocol: MODERN
modern:
# Tell clients how long they may cache server/discover and the *list results. Safe here because
# nothing in them depends on who is asking. Anything per-caller must stay PRIVATE.
discover-ttl: 5m
discover-cache-scope: PUBLIC
list-ttl: 5m
list-cache-scope: PUBLIC
@@ -0,0 +1,15 @@
spring:
main:
banner-mode: off
jmx:
enabled: false
ai:
mcp:
server:
name: orders
version: 1.0.0
instructions: Order lookup server used to compare the 2025 and 2026-07-28 MCP protocol generations.
logging:
level:
root: warn
com.ankurm: info