Skip to content

Commit 0f65c6c

Browse files
committed
fix: update sync tool/resource/prompt handlers to use McpSyncServerExchange
This commit changes the parameter type in sync handler interfaces from McpAsyncServerExchange to McpSyncServerExchange for better API consistency. It also adds proper wrapping of async exchanges in sync exchanges when bridging between async and sync contexts in the adapter methods. Signed-off-by: Christian Tzolov <[email protected]>
1 parent 69c1038 commit 0f65c6c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public SyncSpecification capabilities(McpSchema.ServerCapabilities serverCapabil
668668
* @throws IllegalArgumentException if tool or handler is null
669669
*/
670670
public SyncSpecification tool(McpSchema.Tool tool,
671-
BiFunction<McpAsyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> handler) {
671+
BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> handler) {
672672
Assert.notNull(tool, "Tool must not be null");
673673
Assert.notNull(handler, "Handler must not be null");
674674

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ static AsyncToolSpecification fromSync(SyncToolSpecification tool) {
220220
return null;
221221
}
222222
return new AsyncToolSpecification(tool.tool(),
223-
(exchange, map) -> Mono.fromCallable(() -> tool.call().apply(exchange, map))
223+
(exchange, map) -> Mono
224+
.fromCallable(() -> tool.call().apply(new McpSyncServerExchange(exchange), map))
224225
.subscribeOn(Schedulers.boundedElastic()));
225226
}
226227
}
@@ -259,7 +260,8 @@ static AsyncResourceSpecification fromSync(SyncResourceSpecification resource) {
259260
return null;
260261
}
261262
return new AsyncResourceSpecification(resource.resource(),
262-
(exchange, req) -> Mono.fromCallable(() -> resource.readHandler().apply(exchange, req))
263+
(exchange, req) -> Mono
264+
.fromCallable(() -> resource.readHandler().apply(new McpSyncServerExchange(exchange), req))
263265
.subscribeOn(Schedulers.boundedElastic()));
264266
}
265267
}
@@ -301,7 +303,8 @@ static AsyncPromptSpecification fromSync(SyncPromptSpecification prompt) {
301303
return null;
302304
}
303305
return new AsyncPromptSpecification(prompt.prompt(),
304-
(exchange, req) -> Mono.fromCallable(() -> prompt.promptHandler().apply(exchange, req))
306+
(exchange, req) -> Mono
307+
.fromCallable(() -> prompt.promptHandler().apply(new McpSyncServerExchange(exchange), req))
305308
.subscribeOn(Schedulers.boundedElastic()));
306309
}
307310
}
@@ -340,7 +343,7 @@ static AsyncPromptSpecification fromSync(SyncPromptSpecification prompt) {
340343
* returning results
341344
*/
342345
public record SyncToolSpecification(McpSchema.Tool tool,
343-
BiFunction<McpAsyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> call) {
346+
BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> call) {
344347
}
345348

346349
/**
@@ -369,7 +372,7 @@ public record SyncToolSpecification(McpSchema.Tool tool,
369372
* @param readHandler The function that handles resource read requests
370373
*/
371374
public record SyncResourceSpecification(McpSchema.Resource resource,
372-
BiFunction<McpAsyncServerExchange, McpSchema.ReadResourceRequest, McpSchema.ReadResourceResult> readHandler) {
375+
BiFunction<McpSyncServerExchange, McpSchema.ReadResourceRequest, McpSchema.ReadResourceResult> readHandler) {
373376
}
374377

375378
/**
@@ -401,7 +404,7 @@ public record SyncResourceSpecification(McpSchema.Resource resource,
401404
* formatted templates
402405
*/
403406
public record SyncPromptSpecification(McpSchema.Prompt prompt,
404-
BiFunction<McpAsyncServerExchange, McpSchema.GetPromptRequest, McpSchema.GetPromptResult> promptHandler) {
407+
BiFunction<McpSyncServerExchange, McpSchema.GetPromptRequest, McpSchema.GetPromptResult> promptHandler) {
405408
}
406409

407410
// ---------------------------------------

0 commit comments

Comments
 (0)