diff --git a/components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java b/components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java index d22358a7049..c2ab5e1176d 100644 --- a/components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java +++ b/components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngine.java @@ -305,15 +305,9 @@ public void resourceTemplateRemoved(String uriTemplate) { return; } + // The MCP Java SDK sends notifications/resources/list_changed itself when the server advertises it server.removeResourceTemplate(uriTemplate); - McpSchema.ServerCapabilities capabilities = server.capabilities(); - if (capabilities != null && capabilities.resources() != null - && Boolean.TRUE.equals(capabilities.resources().listChanged())) { - // MCP Java SDK 2.1.0 does not notify on template removal; notify only when advertised. - server.notifyResourcesListChanged(); - } - LOG.debug("MCP resource template removed: {}", uriTemplate); } catch (Exception e) { LOG.debug( diff --git a/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngineTest.java b/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngineTest.java index cf52df4c2c9..20adef652a3 100644 --- a/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngineTest.java +++ b/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringAiMcpServerEngineTest.java @@ -130,31 +130,22 @@ void translatesInvalidToolSchemaIntoServerRefusal() { } @Test - void notifiesTemplateRemovalOnlyWhenAdvertisedAndActuallyRemoved() { + void removesOnlyARegisteredTemplateAndLeavesTheNotificationToTheSdk() { String uriTemplate = "camel:///customers/{id}"; McpSchema.ResourceTemplate template = McpSchema.ResourceTemplate .builder(uriTemplate, "customers") .build(); - McpSyncServer notifyingServer = mock(McpSyncServer.class); - when(notifyingServer.listResourceTemplates()).thenReturn(List.of(template)); - when(notifyingServer.getServerCapabilities()).thenReturn( + // The MCP Java SDK sends resources/list_changed itself when it removes a template + McpSyncServer registeredServer = mock(McpSyncServer.class); + when(registeredServer.listResourceTemplates()).thenReturn(List.of(template)); + when(registeredServer.getServerCapabilities()).thenReturn( new McpSchema.ServerCapabilities( null, null, null, null, new McpSchema.ServerCapabilities.ResourceCapabilities(false, true), null)); - new SpringAiMcpServerEngine(notifyingServer).resourceTemplateRemoved(uriTemplate); - verify(notifyingServer).removeResourceTemplate(uriTemplate); - verify(notifyingServer).notifyResourcesListChanged(); - - McpSyncServer nonNotifyingServer = mock(McpSyncServer.class); - when(nonNotifyingServer.listResourceTemplates()).thenReturn(List.of(template)); - when(nonNotifyingServer.getServerCapabilities()).thenReturn( - new McpSchema.ServerCapabilities( - null, null, null, null, - new McpSchema.ServerCapabilities.ResourceCapabilities(false, false), null)); - new SpringAiMcpServerEngine(nonNotifyingServer).resourceTemplateRemoved(uriTemplate); - verify(nonNotifyingServer).removeResourceTemplate(uriTemplate); - verify(nonNotifyingServer, never()).notifyResourcesListChanged(); + new SpringAiMcpServerEngine(registeredServer).resourceTemplateRemoved(uriTemplate); + verify(registeredServer).removeResourceTemplate(uriTemplate); + verify(registeredServer, never()).notifyResourcesListChanged(); McpSyncServer absentServer = mock(McpSyncServer.class); when(absentServer.listResourceTemplates()).thenReturn(List.of()); diff --git a/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringBootMcpServerTest.java b/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringBootMcpServerTest.java index 769810022c0..b2b3e31f428 100644 --- a/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringBootMcpServerTest.java +++ b/components-starter/camel-mcp-server-starter/src/test/java/org/apache/camel/springboot/mcp/server/SpringBootMcpServerTest.java @@ -833,7 +833,7 @@ void testResourceAndPromptChangeNotificationsAreDelivered() throws Exception { await().atMost(10, TimeUnit.SECONDS) .until(() -> resourceNotificationCount.get() >= 1); - // Template removal has an explicit resources/list_changed notification. + // Template removal sends one resources/list_changed notification. camelContext.getRouteController().stopRoute("customer-template-route"); await().atMost(10, TimeUnit.SECONDS) @@ -843,6 +843,10 @@ void testResourceAndPromptChangeNotificationsAreDelivered() throws Exception { await().atMost(10, TimeUnit.SECONDS) .until(() -> promptNotificationCount.get() >= 1); + + // The notifications of a session arrive in order, so a second notification + // for the template removal would be here before the prompt one. + assertThat(resourceNotificationCount).hasValue(2); } finally { notificationClient.closeGracefully(); }