Skip to content

Commit 5133786

Browse files
authored
Return a handler from find in modern server repositories (#1282)
Tool and prompt repositories had separate `find` and `call`/`get` methods, so every call looked the primitive up twice. That's two round trips for repositories backed by a database or other external storage. `find` now returns the definition together with its handler: - `McpAsync/SyncToolRepository.find` -> `McpAsync/SyncTool`; `call` is removed - `McpAsync/SyncPromptRepository.find` -> `McpAsync/SyncPrompt`; `get` is removed - `McpAsync/SyncResourceRepository.find(uri)` -> `McpAsync/SyncResource`, replacing `read` The features validate against the definition they found, then call its handler. `ResourcesFeature` now answers an unknown URI itself (`-32602` with `{"uri": ...}`). Closes #1268 Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
1 parent 54e8b61 commit 5133786

25 files changed

Lines changed: 439 additions & 211 deletions

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

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import io.modelcontextprotocol.modern.McpSchema.Prompt;
3232
import io.modelcontextprotocol.modern.McpSchema.PromptArgument;
3333
import io.modelcontextprotocol.modern.McpSchema.PromptMessage;
34-
import io.modelcontextprotocol.modern.McpSchema.ReadResourceOutcome;
35-
import io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest;
3634
import io.modelcontextprotocol.modern.McpSchema.ReadResourceResult;
3735
import io.modelcontextprotocol.modern.McpSchema.Resource;
3836
import io.modelcontextprotocol.modern.McpSchema.Result;
@@ -50,8 +48,11 @@
5048
import io.modelcontextprotocol.modern.server.feature.CompletionsFeature;
5149
import io.modelcontextprotocol.modern.server.feature.McpChangeBroadcaster;
5250
import io.modelcontextprotocol.modern.server.feature.McpChangeFeed;
51+
import io.modelcontextprotocol.modern.server.feature.McpSyncPrompt;
5352
import io.modelcontextprotocol.modern.server.feature.McpSyncPromptRepository;
53+
import io.modelcontextprotocol.modern.server.feature.McpSyncResource;
5454
import io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository;
55+
import io.modelcontextprotocol.modern.server.feature.McpSyncTool;
5556
import io.modelcontextprotocol.modern.server.feature.McpSyncToolRepository;
5657
import io.modelcontextprotocol.modern.server.feature.PromptsFeature;
5758
import io.modelcontextprotocol.modern.server.feature.PromptsPage;
@@ -254,12 +255,9 @@ private static Handler<CallToolRequest, CallToolOutcome> stream(StreamingTool to
254255
return (ctx, request) -> McpSyncResponse.streaming(notifier -> tool.run(ctx, request, notifier));
255256
}
256257

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

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

264262
ConformanceTools(McpChangeBroadcaster changes) {
265263
add("test_simple_text", "Returns simple text content for testing",
@@ -465,27 +463,17 @@ private void add(String name, String description, Handler<CallToolRequest, CallT
465463
}
466464

467465
private void add(Tool tool, Handler<CallToolRequest, CallToolOutcome> handler) {
468-
this.tools.put(tool.name(), new ToolEntry(tool, handler));
466+
this.tools.put(tool.name(), McpSyncTool.of(tool, handler::handle));
469467
}
470468

471469
@Override
472470
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;
471+
return ToolsPage.of(this.tools.values().stream().map(McpSyncTool::definition).toList());
480472
}
481473

482474
@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);
475+
public McpSyncTool find(McpRequestContext ctx, String name) {
476+
return this.tools.get(name);
489477
}
490478

491479
private static void sleep(long millis) {
@@ -530,20 +518,20 @@ public ResourceTemplatesPage listTemplates(McpRequestContext ctx, String cursor)
530518
}
531519

532520
@Override
533-
public McpSyncResponse<ReadResourceOutcome> read(McpRequestContext ctx, ReadResourceRequest request) {
534-
String uri = request.uri();
535-
ReadResourceResult result = switch (uri) {
521+
public McpSyncResource find(McpRequestContext ctx, String uri) {
522+
ReadResourceResult result = read(uri);
523+
return result != null ? McpSyncResource.of((c, request) -> McpSyncResponse.result(result)) : null;
524+
}
525+
526+
private static ReadResourceResult read(String uri) {
527+
return switch (uri) {
536528
case "test://static-text" -> textResource(uri, "This is the content of the static text resource.");
537529
case "test://watched-resource" -> textResource(uri, "This is a watched resource content.");
538530
case "test://static-binary" -> ReadResourceResult
539531
.builder(List.of(BlobResourceContents.builder(uri, RED_PIXEL_PNG).mimeType("image/png").build()))
540532
.build();
541533
default -> templateResource(uri);
542534
};
543-
if (result == null) {
544-
throw McpException.invalidParams("Unknown resource: " + uri, Map.of("uri", uri));
545-
}
546-
return McpSyncResponse.result(result);
547535
}
548536

549537
@Override
@@ -571,12 +559,9 @@ private static ReadResourceResult templateResource(String uri) {
571559

572560
}
573561

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

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

581566
ConformancePrompts() {
582567
add(Prompt.builder("test_simple_prompt").description("A simple prompt for testing").build(),
@@ -637,7 +622,7 @@ private static final class ConformancePrompts implements McpSyncPromptRepository
637622
}
638623

639624
private void add(Prompt prompt, Handler<GetPromptRequest, GetPromptOutcome> handler) {
640-
this.prompts.put(prompt.name(), new PromptEntry(prompt, handler));
625+
this.prompts.put(prompt.name(), McpSyncPrompt.of(prompt, handler::handle));
641626
}
642627

643628
private static GetPromptResult messages(PromptMessage... messages) {
@@ -646,22 +631,12 @@ private static GetPromptResult messages(PromptMessage... messages) {
646631

647632
@Override
648633
public PromptsPage list(McpRequestContext ctx, String cursor) {
649-
return PromptsPage.of(this.prompts.values().stream().map(PromptEntry::prompt).toList());
634+
return PromptsPage.of(this.prompts.values().stream().map(McpSyncPrompt::definition).toList());
650635
}
651636

652637
@Override
653-
public Prompt find(McpRequestContext ctx, String name) {
654-
PromptEntry entry = this.prompts.get(name);
655-
return entry != null ? entry.prompt() : null;
656-
}
657-
658-
@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);
638+
public McpSyncPrompt find(McpRequestContext ctx, String name) {
639+
return this.prompts.get(name);
665640
}
666641

667642
}
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.ReadResourceOutcome;
8+
import io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest;
9+
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
10+
import io.modelcontextprotocol.modern.server.McpRequestContext;
11+
import io.modelcontextprotocol.util.Assert;
12+
import reactor.core.publisher.Mono;
13+
14+
/**
15+
* A readable resource: the handler that answers {@code resources/read} for it.
16+
*
17+
* @author Dariusz Jędrzejczyk
18+
*/
19+
public final class McpAsyncResource {
20+
21+
private final Handler handler;
22+
23+
private McpAsyncResource(Handler handler) {
24+
Assert.notNull(handler, "handler must not be null");
25+
this.handler = handler;
26+
}
27+
28+
public static McpAsyncResource of(Handler handler) {
29+
return new McpAsyncResource(handler);
30+
}
31+
32+
public Handler handler() {
33+
return this.handler;
34+
}
35+
36+
/** Answers a {@code resources/read} for this resource. */
37+
@FunctionalInterface
38+
public interface Handler {
39+
40+
Mono<McpAsyncResponse<ReadResourceOutcome>> read(McpRequestContext ctx, ReadResourceRequest request);
41+
42+
}
43+
44+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import java.util.List;
88

99
import io.modelcontextprotocol.modern.McpException;
10-
import io.modelcontextprotocol.modern.McpSchema.ReadResourceOutcome;
11-
import io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest;
1210
import io.modelcontextprotocol.modern.server.McpRequestContext;
13-
import io.modelcontextprotocol.modern.server.McpAsyncResponse;
1411
import reactor.core.publisher.Mono;
1512

1613
/**
@@ -32,11 +29,10 @@ default Mono<ResourceTemplatesPage> listTemplates(McpRequestContext ctx, String
3229
}
3330

3431
/**
35-
* Answer a {@code resources/read}. An unknown resource is an
36-
* {@link McpException#invalidParams(String, Object) invalid-params error} carrying
37-
* {@code {"uri": ...}} as data.
32+
* The resource at this URI, whether listed or matching a template, or empty if there
33+
* is none.
3834
*/
39-
Mono<McpAsyncResponse<ReadResourceOutcome>> read(McpRequestContext ctx, ReadResourceRequest request);
35+
Mono<McpAsyncResource> find(McpRequestContext ctx, String uri);
4036

4137
/**
4238
* Whether {@code resources/updated} is reported for individual resources, advertised
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
}

0 commit comments

Comments
 (0)