Skip to content

Commit 75b5306

Browse files
committed
Return a handler from find in modern tool and prompt repositories
The tool and prompt repositories had separate find and call/get methods, so every tools/call and prompts/get looked the primitive up twice. That means two round trips for repositories backed by a database or remote definitions. find now returns McpAsyncTool/McpSyncTool or McpAsyncPrompt/McpSyncPrompt, which carry the definition and its handler. The call/get methods are removed from the repositories. ToolsFeature and PromptsFeature validate against the definition and invoke the handler they found. Closes #1268 Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
1 parent 54e8b61 commit 75b5306

17 files changed

Lines changed: 289 additions & 172 deletions

File tree

‎conformance-tests/server-servlet-modern/src/main/java/io/modelcontextprotocol/conformance/server/modern/ModernConformanceServlet.java‎

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
import io.modelcontextprotocol.modern.server.feature.CompletionsFeature;
5151
import io.modelcontextprotocol.modern.server.feature.McpChangeBroadcaster;
5252
import io.modelcontextprotocol.modern.server.feature.McpChangeFeed;
53+
import io.modelcontextprotocol.modern.server.feature.McpSyncPrompt;
5354
import io.modelcontextprotocol.modern.server.feature.McpSyncPromptRepository;
5455
import io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository;
56+
import io.modelcontextprotocol.modern.server.feature.McpSyncTool;
5557
import io.modelcontextprotocol.modern.server.feature.McpSyncToolRepository;
5658
import io.modelcontextprotocol.modern.server.feature.PromptsFeature;
5759
import io.modelcontextprotocol.modern.server.feature.PromptsPage;
@@ -254,12 +256,9 @@ private static Handler<CallToolRequest, CallToolOutcome> stream(StreamingTool to
254256
return (ctx, request) -> McpSyncResponse.streaming(notifier -> tool.run(ctx, request, notifier));
255257
}
256258

257-
private record ToolEntry(Tool tool, Handler<CallToolRequest, CallToolOutcome> handler) {
258-
}
259-
260259
private static final class ConformanceTools implements McpSyncToolRepository {
261260

262-
private final Map<String, ToolEntry> tools = new LinkedHashMap<>();
261+
private final Map<String, McpSyncTool> tools = new LinkedHashMap<>();
263262

264263
ConformanceTools(McpChangeBroadcaster changes) {
265264
add("test_simple_text", "Returns simple text content for testing",
@@ -465,27 +464,17 @@ private void add(String name, String description, Handler<CallToolRequest, CallT
465464
}
466465

467466
private void add(Tool tool, Handler<CallToolRequest, CallToolOutcome> handler) {
468-
this.tools.put(tool.name(), new ToolEntry(tool, handler));
467+
this.tools.put(tool.name(), McpSyncTool.of(tool, handler::handle));
469468
}
470469

471470
@Override
472471
public ToolsPage list(McpRequestContext ctx, String cursor) {
473-
return ToolsPage.of(this.tools.values().stream().map(ToolEntry::tool).toList());
474-
}
475-
476-
@Override
477-
public Tool find(McpRequestContext ctx, String name) {
478-
ToolEntry entry = this.tools.get(name);
479-
return entry != null ? entry.tool() : null;
472+
return ToolsPage.of(this.tools.values().stream().map(McpSyncTool::definition).toList());
480473
}
481474

482475
@Override
483-
public McpSyncResponse<CallToolOutcome> call(McpRequestContext ctx, CallToolRequest request) {
484-
ToolEntry entry = this.tools.get(request.name());
485-
if (entry == null) {
486-
throw McpException.invalidParams("Unknown tool: " + request.name());
487-
}
488-
return entry.handler().handle(ctx, request);
476+
public McpSyncTool find(McpRequestContext ctx, String name) {
477+
return this.tools.get(name);
489478
}
490479

491480
private static void sleep(long millis) {
@@ -571,12 +560,9 @@ private static ReadResourceResult templateResource(String uri) {
571560

572561
}
573562

574-
private record PromptEntry(Prompt prompt, Handler<GetPromptRequest, GetPromptOutcome> handler) {
575-
}
576-
577563
private static final class ConformancePrompts implements McpSyncPromptRepository {
578564

579-
private final Map<String, PromptEntry> prompts = new LinkedHashMap<>();
565+
private final Map<String, McpSyncPrompt> prompts = new LinkedHashMap<>();
580566

581567
ConformancePrompts() {
582568
add(Prompt.builder("test_simple_prompt").description("A simple prompt for testing").build(),
@@ -637,7 +623,7 @@ private static final class ConformancePrompts implements McpSyncPromptRepository
637623
}
638624

639625
private void add(Prompt prompt, Handler<GetPromptRequest, GetPromptOutcome> handler) {
640-
this.prompts.put(prompt.name(), new PromptEntry(prompt, handler));
626+
this.prompts.put(prompt.name(), McpSyncPrompt.of(prompt, handler::handle));
641627
}
642628

643629
private static GetPromptResult messages(PromptMessage... messages) {
@@ -646,22 +632,12 @@ private static GetPromptResult messages(PromptMessage... messages) {
646632

647633
@Override
648634
public PromptsPage list(McpRequestContext ctx, String cursor) {
649-
return PromptsPage.of(this.prompts.values().stream().map(PromptEntry::prompt).toList());
650-
}
651-
652-
@Override
653-
public Prompt find(McpRequestContext ctx, String name) {
654-
PromptEntry entry = this.prompts.get(name);
655-
return entry != null ? entry.prompt() : null;
635+
return PromptsPage.of(this.prompts.values().stream().map(McpSyncPrompt::definition).toList());
656636
}
657637

658638
@Override
659-
public McpSyncResponse<GetPromptOutcome> get(McpRequestContext ctx, GetPromptRequest request) {
660-
PromptEntry entry = this.prompts.get(request.name());
661-
if (entry == null) {
662-
throw McpException.invalidParams("Unknown prompt: " + request.name());
663-
}
664-
return entry.handler().handle(ctx, request);
639+
public McpSyncPrompt find(McpRequestContext ctx, String name) {
640+
return this.prompts.get(name);
665641
}
666642

667643
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2026-2026 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.modern.server.feature;
6+
7+
import io.modelcontextprotocol.modern.McpSchema.GetPromptOutcome;
8+
import io.modelcontextprotocol.modern.McpSchema.GetPromptRequest;
9+
import io.modelcontextprotocol.modern.McpSchema.Prompt;
10+
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
11+
import io.modelcontextprotocol.modern.server.McpRequestContext;
12+
import io.modelcontextprotocol.util.Assert;
13+
import reactor.core.publisher.Mono;
14+
15+
/**
16+
* A prompt's definition together with the handler that renders it.
17+
*
18+
* @author Dariusz Jędrzejczyk
19+
*/
20+
public final class McpAsyncPrompt {
21+
22+
private final Prompt definition;
23+
24+
private final Handler handler;
25+
26+
private McpAsyncPrompt(Prompt definition, Handler handler) {
27+
Assert.notNull(definition, "definition must not be null");
28+
Assert.notNull(handler, "handler must not be null");
29+
this.definition = definition;
30+
this.handler = handler;
31+
}
32+
33+
public static McpAsyncPrompt of(Prompt definition, Handler handler) {
34+
return new McpAsyncPrompt(definition, handler);
35+
}
36+
37+
public Prompt definition() {
38+
return this.definition;
39+
}
40+
41+
public Handler handler() {
42+
return this.handler;
43+
}
44+
45+
/** Answers a {@code prompts/get} for this prompt. */
46+
@FunctionalInterface
47+
public interface Handler {
48+
49+
/** The arguments include every required argument of the definition. */
50+
Mono<McpAsyncResponse<GetPromptOutcome>> get(McpRequestContext ctx, GetPromptRequest request);
51+
52+
}
53+
54+
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/McpAsyncPromptRepository.java‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package io.modelcontextprotocol.modern.server.feature;
66

77
import io.modelcontextprotocol.modern.McpException;
8-
import io.modelcontextprotocol.modern.McpSchema.GetPromptOutcome;
9-
import io.modelcontextprotocol.modern.McpSchema.GetPromptRequest;
10-
import io.modelcontextprotocol.modern.McpSchema.Prompt;
118
import io.modelcontextprotocol.modern.server.McpRequestContext;
12-
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
139
import reactor.core.publisher.Mono;
1410

1511
/**
@@ -26,13 +22,6 @@ public interface McpAsyncPromptRepository {
2622
Mono<PromptsPage> list(McpRequestContext ctx, String cursor);
2723

2824
/** The prompt with this name, or empty if there is none. */
29-
Mono<Prompt> find(McpRequestContext ctx, String name);
30-
31-
/**
32-
* Answer a {@code prompts/get}. An unknown prompt is an
33-
* {@link McpException#invalidParams(String) invalid-params error}. The arguments
34-
* include every required argument of the prompt {@link #find} returned.
35-
*/
36-
Mono<McpAsyncResponse<GetPromptOutcome>> get(McpRequestContext ctx, GetPromptRequest request);
25+
Mono<McpAsyncPrompt> find(McpRequestContext ctx, String name);
3726

3827
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2026-2026 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.modern.server.feature;
6+
7+
import io.modelcontextprotocol.modern.McpSchema.CallToolOutcome;
8+
import io.modelcontextprotocol.modern.McpSchema.CallToolRequest;
9+
import io.modelcontextprotocol.modern.McpSchema.Tool;
10+
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
11+
import io.modelcontextprotocol.modern.server.McpRequestContext;
12+
import io.modelcontextprotocol.util.Assert;
13+
import reactor.core.publisher.Mono;
14+
15+
/**
16+
* A tool's definition together with the handler that answers its calls.
17+
*
18+
* @author Dariusz Jędrzejczyk
19+
*/
20+
public final class McpAsyncTool {
21+
22+
private final Tool definition;
23+
24+
private final Handler handler;
25+
26+
private McpAsyncTool(Tool definition, Handler handler) {
27+
Assert.notNull(definition, "definition must not be null");
28+
Assert.notNull(handler, "handler must not be null");
29+
this.definition = definition;
30+
this.handler = handler;
31+
}
32+
33+
public static McpAsyncTool of(Tool definition, Handler handler) {
34+
return new McpAsyncTool(definition, handler);
35+
}
36+
37+
public Tool definition() {
38+
return this.definition;
39+
}
40+
41+
public Handler handler() {
42+
return this.handler;
43+
}
44+
45+
/** Answers a {@code tools/call} for this tool. */
46+
@FunctionalInterface
47+
public interface Handler {
48+
49+
/**
50+
* The arguments conform to the definition's {@code inputSchema}, and a result's
51+
* {@code structuredContent} must conform to its {@code outputSchema}.
52+
*/
53+
Mono<McpAsyncResponse<CallToolOutcome>> call(McpRequestContext ctx, CallToolRequest request);
54+
55+
}
56+
57+
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/McpAsyncToolRepository.java‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package io.modelcontextprotocol.modern.server.feature;
66

77
import io.modelcontextprotocol.modern.McpException;
8-
import io.modelcontextprotocol.modern.McpSchema.CallToolOutcome;
9-
import io.modelcontextprotocol.modern.McpSchema.CallToolRequest;
10-
import io.modelcontextprotocol.modern.McpSchema.Tool;
118
import io.modelcontextprotocol.modern.server.McpRequestContext;
12-
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
139
import reactor.core.publisher.Mono;
1410

1511
/**
@@ -27,14 +23,6 @@ public interface McpAsyncToolRepository {
2723
Mono<ToolsPage> list(McpRequestContext ctx, String cursor);
2824

2925
/** The tool with this name, or empty if there is none. */
30-
Mono<Tool> find(McpRequestContext ctx, String name);
31-
32-
/**
33-
* Answer a {@code tools/call}. An unknown tool is an
34-
* {@link McpException#invalidParams(String) invalid-params error}. The arguments
35-
* conform to the {@code inputSchema} of the tool {@link #find} returned, and a
36-
* result's {@code structuredContent} must conform to its {@code outputSchema}.
37-
*/
38-
Mono<McpAsyncResponse<CallToolOutcome>> call(McpRequestContext ctx, CallToolRequest request);
26+
Mono<McpAsyncTool> find(McpRequestContext ctx, String name);
3927

4028
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2026-2026 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.modern.server.feature;
6+
7+
import io.modelcontextprotocol.modern.McpSchema.GetPromptOutcome;
8+
import io.modelcontextprotocol.modern.McpSchema.GetPromptRequest;
9+
import io.modelcontextprotocol.modern.McpSchema.Prompt;
10+
import io.modelcontextprotocol.modern.server.McpRequestContext;
11+
import io.modelcontextprotocol.modern.server.McpSyncResponse;
12+
import io.modelcontextprotocol.util.Assert;
13+
14+
/**
15+
* The blocking counterpart of {@link McpAsyncPrompt}.
16+
*
17+
* @author Dariusz Jędrzejczyk
18+
*/
19+
public final class McpSyncPrompt {
20+
21+
private final Prompt definition;
22+
23+
private final Handler handler;
24+
25+
private McpSyncPrompt(Prompt definition, Handler handler) {
26+
Assert.notNull(definition, "definition must not be null");
27+
Assert.notNull(handler, "handler must not be null");
28+
this.definition = definition;
29+
this.handler = handler;
30+
}
31+
32+
public static McpSyncPrompt of(Prompt definition, Handler handler) {
33+
return new McpSyncPrompt(definition, handler);
34+
}
35+
36+
public Prompt definition() {
37+
return this.definition;
38+
}
39+
40+
public Handler handler() {
41+
return this.handler;
42+
}
43+
44+
/** See {@link McpAsyncPrompt.Handler}. */
45+
@FunctionalInterface
46+
public interface Handler {
47+
48+
McpSyncResponse<GetPromptOutcome> get(McpRequestContext ctx, GetPromptRequest request);
49+
50+
}
51+
52+
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/McpSyncPromptRepository.java‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
package io.modelcontextprotocol.modern.server.feature;
66

7-
import io.modelcontextprotocol.modern.McpSchema.GetPromptOutcome;
8-
import io.modelcontextprotocol.modern.McpSchema.GetPromptRequest;
9-
import io.modelcontextprotocol.modern.McpSchema.Prompt;
107
import io.modelcontextprotocol.modern.server.McpRequestContext;
11-
import io.modelcontextprotocol.modern.server.McpSyncResponse;
128

139
/**
1410
* The blocking counterpart of {@link McpAsyncPromptRepository}.
@@ -20,8 +16,6 @@ public interface McpSyncPromptRepository {
2016
PromptsPage list(McpRequestContext ctx, String cursor);
2117

2218
/** The prompt with this name, or {@code null} if there is none. */
23-
Prompt find(McpRequestContext ctx, String name);
24-
25-
McpSyncResponse<GetPromptOutcome> get(McpRequestContext ctx, GetPromptRequest request);
19+
McpSyncPrompt find(McpRequestContext ctx, String name);
2620

2721
}

0 commit comments

Comments
 (0)