|
48 | 48 | import reactor.core.publisher.Flux; |
49 | 49 | import reactor.core.publisher.Mono; |
50 | 50 |
|
| 51 | +import static io.modelcontextprotocol.spec.McpError.RESOURCE_NOT_FOUND; |
| 52 | + |
51 | 53 | /** |
52 | 54 | * The Model Context Protocol (MCP) server implementation that provides asynchronous |
53 | 55 | * communication using Project Reactor's Mono and Flux types. |
@@ -638,24 +640,23 @@ private List<McpSchema.ResourceTemplate> getResourceTemplates() { |
638 | 640 | } |
639 | 641 |
|
640 | 642 | private McpRequestHandler<McpSchema.ReadResourceResult> resourcesReadRequestHandler() { |
641 | | - return (exchange, params) -> { |
642 | | - McpSchema.ReadResourceRequest resourceRequest = jsonMapper.convertValue(params, |
643 | | - new TypeRef<McpSchema.ReadResourceRequest>() { |
644 | | - }); |
| 643 | + return (ex, params) -> { |
| 644 | + McpSchema.ReadResourceRequest resourceRequest = jsonMapper.convertValue(params, new TypeRef<>() { |
| 645 | + }); |
645 | 646 | var resourceUri = resourceRequest.uri(); |
646 | | - |
647 | | - McpServerFeatures.AsyncResourceSpecification specification = this.resources.values() |
648 | | - .stream() |
649 | | - .filter(resourceSpecification -> this.uriTemplateManagerFactory |
650 | | - .create(resourceSpecification.resource().uri()) |
651 | | - .matches(resourceUri)) |
652 | | - .findFirst() |
653 | | - .orElseThrow(() -> new McpError("Resource not found: " + resourceUri)); |
654 | | - |
655 | | - return Mono.defer(() -> specification.readHandler().apply(exchange, resourceRequest)); |
| 647 | + return asyncResourceSpecification(resourceUri) |
| 648 | + .map(spec -> Mono.defer(() -> spec.readHandler().apply(ex, resourceRequest))) |
| 649 | + .orElseGet(() -> Mono.error(RESOURCE_NOT_FOUND.apply(resourceUri))); |
656 | 650 | }; |
657 | 651 | } |
658 | 652 |
|
| 653 | + private Optional<McpServerFeatures.AsyncResourceSpecification> asyncResourceSpecification(String uri) { |
| 654 | + return resources.values() |
| 655 | + .stream() |
| 656 | + .filter(spec -> uriTemplateManagerFactory.create(spec.resource().uri()).matches(uri)) |
| 657 | + .findFirst(); |
| 658 | + } |
| 659 | + |
659 | 660 | // --------------------------------------- |
660 | 661 | // Prompt Management |
661 | 662 | // --------------------------------------- |
@@ -846,7 +847,7 @@ private McpRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHan |
846 | 847 | if (type.equals("ref/resource") && request.ref() instanceof McpSchema.ResourceReference resourceReference) { |
847 | 848 | McpServerFeatures.AsyncResourceSpecification resourceSpec = this.resources.get(resourceReference.uri()); |
848 | 849 | if (resourceSpec == null) { |
849 | | - return Mono.error(new McpError("Resource not found: " + resourceReference.uri())); |
| 850 | + return Mono.error(RESOURCE_NOT_FOUND.apply(resourceReference.uri())); |
850 | 851 | } |
851 | 852 | if (!uriTemplateManagerFactory.create(resourceSpec.resource().uri()) |
852 | 853 | .getVariableNames() |
|
0 commit comments