Croway commented on code in PR #2094:
URL:
https://github.com/apache/camel-spring-boot/pull/2094#discussion_r4578037557
##########
components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java:
##########
@@ -108,6 +142,314 @@ 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 =
McpSchema.BlobResourceContents.builder(
+ resource.uri(),
+
Base64.getEncoder().encodeToString(result.blob()))
+ .mimeType(resource.mimeType())
+ .build();
+ } else {
+ contents =
McpSchema.TextResourceContents.builder(resource.uri(), result.text())
+ .mimeType(resource.mimeType())
+ .build();
+ }
+
+ return
McpSchema.ReadResourceResult.builder(List.of(contents)).build();
+ });
+
+ 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) {
+ List<McpSchema.ResourceTemplate> existingTemplates =
server.listResourceTemplates();
+ if (existingTemplates.stream()
+ .anyMatch(existing ->
existing.uriTemplate().equals(resourceTemplate.uriTemplate()))) {
+ throw new McpServerRefusedException(
+ "An MCP resource template '" +
resourceTemplate.uriTemplate() + "' is already registered");
+ }
+
+ AiResourceUriTemplate candidate;
+ try {
+ candidate = new
AiResourceUriTemplate(resourceTemplate.uriTemplate());
+ } catch (IllegalArgumentException e) {
+ throw new McpServerRefusedException(
+ "Invalid MCP resource template '" +
resourceTemplate.uriTemplate() + "': " + e.getMessage(), e);
+ }
+
+ for (McpSchema.ResourceTemplate existing : existingTemplates) {
+ AiResourceUriTemplate existingMatcher;
+ try {
+ existingMatcher = new
AiResourceUriTemplate(existing.uriTemplate());
+ } catch (IllegalArgumentException e) {
+ // Fail closed: Camel cannot prove that an unsupported Spring
template does not overlap.
Review Comment:
**A single Spring-defined template that Camel can't parse blocks every Camel
resource template.** The overlap check fails closed whenever an existing
template is rejected by `AiResourceUriTemplate`. That parser intentionally
accepts path-segment variables only, so it rejects the style Spring AI
documents for `@McpResource`, where the variable follows the scheme directly:
`document://{id}`, `async-data://{id}` and `static://{path}` in the Spring AI
2.0.2 `mcp-annotations-examples.adoc` all fail with "Resource uriTemplate must
contain a path". Templates with query expressions such as `{?q}` are rejected
too.
Example: an app defines `@McpResource(uri = "document://{id}")` and a Camel
route defines
`ai-resource:customer?uriTemplate=camel:///customers/{customerId}`. The two can
never match the same URI because the schemes differ, but the Camel template is
refused. So is every other Camel resource template in the app, as long as that
one Spring template is registered.
Suggestion: only fail closed when the unparseable template has the same
scheme as the candidate (case-insensitive). Templates with a different scheme
can't overlap, so log a WARN and continue. A test with a Spring
`document://{id}` template next to a Camel `camel:///...` template would cover
it.
_Claude Code (Opus 5.5) on behalf of Federico Mariani_
--
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]