ramu11 commented on code in PR #2094:
URL:
https://github.com/apache/camel-spring-boot/pull/2094#discussion_r4577163785
##########
components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java:
##########
@@ -108,6 +136,280 @@ public void toolRemoved(String toolName) {
}
}
+ @Override
+ public void resourceAdded(McpServerResource resource) {
+ if (server.listResources().stream().anyMatch(existing ->
existing.uri().equals(resource.uri()))) {
+ throw new McpServerRefusedException(
+ "An MCP resource with URI '" + resource.uri() + "' is
already registered");
+ }
+
+ McpSchema.Resource mcpResource = buildMcpResource(resource);
+
+ McpServerFeatures.SyncResourceSpecification spec =
+ new McpServerFeatures.SyncResourceSpecification(
+ mcpResource,
+ (exchange, request) -> {
+ McpResourceReadResult result =
resource.handler().read();
+
+ if (result.isError()) {
+ throw
McpError.builder(McpSchema.ErrorCodes.INTERNAL_ERROR)
+ .message(result.errorMessage())
+ .build();
+ }
+
+ McpSchema.ResourceContents contents;
+
+ if (result.blob() != null) {
+ contents = new McpSchema.BlobResourceContents(
+ resource.uri(),
+ resource.mimeType(),
+
Base64.getEncoder().encodeToString(result.blob()));
+ } else {
+ contents = new McpSchema.TextResourceContents(
+ resource.uri(),
+ resource.mimeType(),
+ result.text());
+ }
+
+ return new
McpSchema.ReadResourceResult(List.of(contents));
+ });
+
+ server.addResource(spec);
+ LOG.debug("MCP resource added: {}", resource.uri());
+ }
+
+ @Override
+ public void resourceRemoved(String resourceUri) {
+ try {
+ server.removeResource(resourceUri);
+ LOG.debug("MCP resource removed: {}", resourceUri);
+ } catch (Exception e) {
+ LOG.debug("Failed to remove MCP resource {}: {}", resourceUri,
e.getMessage());
+ }
+ }
+
+ @Override
+ public boolean supportsResources() {
+ McpSchema.ServerCapabilities capabilities =
server.getServerCapabilities();
+ return capabilities != null && capabilities.resources() != null;
+ }
+
+ @Override
+ public void resourceTemplateAdded(McpServerResourceTemplate
resourceTemplate) {
+ if (server.listResourceTemplates().stream()
+ .anyMatch(existing ->
existing.uriTemplate().equals(resourceTemplate.uriTemplate()))) {
Review Comment:
Thanks for pointing this out. Fixed by checking each incoming resource
template against all templates already registered with the Spring AI server,
rather than checking only for identical template strings.
The implementation uses the same underlying
`AiResourceUriTemplate.overlaps()` matching logic as Camel's `McpServerBridge`.
Overlapping templates are rejected with `McpServerRefusedException`. It also
fails closed if an existing Spring-defined template cannot be parsed safely.
Added a regression test covering the exact scenario from the review:
`camel:///customers/{id}` already registered by Spring and
`camel:///customers/{customerId}` being added by a Camel route. The test
verifies that registration is refused and the SDK server is not modified.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]