This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 5fab89e402ed CAMEL-26360: Test MCP caller identity propagation (#29902)
5fab89e402ed is described below

commit 5fab89e402eda75cedc7f7e2bf7c9020b7686ff2
Author: Ramu <[email protected]>
AuthorDate: Sat Nov 14 15:10:00 2026 +0530

    CAMEL-26360: Test MCP caller identity propagation (#29902)
    
    Adds bridge-level tests for McpServerBridge.stampCallerIdentity: the
    principal object, principal name and roles of the McpToolCallContext
    reach the exchange properties, and no identity is stamped without a call
    context, with an empty one, or with a blank principal name and empty
    roles. Until now this was only covered end to end through the Camel Main
    engine.
---
 .../component/mcp/server/McpServerBridgeTest.java  | 72 ++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git 
a/components/camel-ai/camel-mcp-server-api/src/test/java/org/apache/camel/component/mcp/server/McpServerBridgeTest.java
 
b/components/camel-ai/camel-mcp-server-api/src/test/java/org/apache/camel/component/mcp/server/McpServerBridgeTest.java
index c4908e526813..9811d254ea49 100644
--- 
a/components/camel-ai/camel-mcp-server-api/src/test/java/org/apache/camel/component/mcp/server/McpServerBridgeTest.java
+++ 
b/components/camel-ai/camel-mcp-server-api/src/test/java/org/apache/camel/component/mcp/server/McpServerBridgeTest.java
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.component.mcp.server;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.builder.RouteBuilder;
@@ -31,6 +33,9 @@ import static 
org.assertj.core.api.Assertions.assertThatThrownBy;
 class McpServerBridgeTest extends CamelTestSupport {
 
     private final RecordingMcpServerEngine engine = new 
RecordingMcpServerEngine();
+    private final AtomicReference<Object> capturedPrincipal = new 
AtomicReference<>();
+    private final AtomicReference<String> capturedPrincipalName = new 
AtomicReference<>();
+    private final AtomicReference<List<String>> capturedPrincipalRoles = new 
AtomicReference<>();
     private McpServerBridge bridge;
 
     @Override
@@ -82,6 +87,20 @@ class McpServerBridgeTest extends CamelTestSupport {
                 from("ai-tool:other_tool?tags=untrusted&description=Other tag")
                         .setBody(constant("other"));
 
+                from("ai-tool:whoami?tags=crm&description=Expose the MCP 
caller identity")
+                        .routeId("whoami-route")
+                        .process(exchange -> {
+                            capturedPrincipal.set(exchange.getProperty(
+                                    
McpToolCallContext.SECURITY_PRINCIPAL_PROPERTY));
+                            capturedPrincipalName.set(exchange.getProperty(
+                                    
McpToolCallContext.SECURITY_PRINCIPAL_NAME_PROPERTY, String.class));
+                            Collection<?> roles = exchange.getProperty(
+                                    
McpToolCallContext.SECURITY_PRINCIPAL_ROLES_PROPERTY, Collection.class);
+                            capturedPrincipalRoles.set(roles == null
+                                    ? null : 
roles.stream().map(String.class::cast).toList());
+                            exchange.getMessage().setBody("identity-captured");
+                        });
+
                 from("ai-tool:getWeather?tags=crm&description=Get weather"
                      + 
"&outputSchema={\"type\":\"object\",\"properties\":{\"temperature\":{\"type\":\"number\"},\"unit\":{\"type\":\"string\"}},\"required\":[\"temperature\",\"unit\"]}")
                         .routeId("weather-route")
@@ -124,6 +143,59 @@ class McpServerBridgeTest extends CamelTestSupport {
         assertThat(result.text()).isEqualTo("customer-42");
     }
 
+    @Test
+    void testToolCallContextIsPropagatedToExchange() {
+        Object callerPrincipal = new Object();
+        McpToolCallContext callContext = new McpToolCallContext(
+                callerPrincipal, "alice", List.of("admin", "operator"));
+
+        McpToolCallResult result = 
engine.tools().get("whoami").handler().call(Map.of(), callContext);
+
+        assertThat(result.isError()).isFalse();
+        assertThat(result.text()).isEqualTo("identity-captured");
+        assertThat(capturedPrincipal.get()).isSameAs(callerPrincipal);
+        assertThat(capturedPrincipalName.get()).isEqualTo("alice");
+        assertThat(capturedPrincipalRoles.get()).containsExactly("admin", 
"operator");
+    }
+
+    @Test
+    void testNoCallContextStampsNoIdentity() {
+        McpToolCallResult result = 
engine.tools().get("whoami").handler().call(Map.of());
+
+        assertThat(result.isError()).isFalse();
+        assertThat(result.text()).isEqualTo("identity-captured");
+        assertThat(capturedPrincipal.get()).isNull();
+        assertThat(capturedPrincipalName.get()).isNull();
+        assertThat(capturedPrincipalRoles.get()).isNull();
+    }
+
+    @Test
+    void testEmptyCallContextStampsNoIdentity() {
+        McpToolCallResult result = engine.tools().get("whoami").handler()
+                .call(Map.of(), McpToolCallContext.EMPTY);
+
+        assertThat(result.isError()).isFalse();
+        assertThat(result.text()).isEqualTo("identity-captured");
+        assertThat(capturedPrincipal.get()).isNull();
+        assertThat(capturedPrincipalName.get()).isNull();
+        assertThat(capturedPrincipalRoles.get()).isNull();
+    }
+
+    @Test
+    void testBlankPrincipalNameAndEmptyRolesAreNotStamped() {
+        Object callerPrincipal = new Object();
+        McpToolCallContext callContext = new McpToolCallContext(
+                callerPrincipal, " ", List.of());
+
+        McpToolCallResult result = 
engine.tools().get("whoami").handler().call(Map.of(), callContext);
+
+        assertThat(result.isError()).isFalse();
+        assertThat(result.text()).isEqualTo("identity-captured");
+        assertThat(capturedPrincipal.get()).isSameAs(callerPrincipal);
+        assertThat(capturedPrincipalName.get()).isNull();
+        assertThat(capturedPrincipalRoles.get()).isNull();
+    }
+
     @Test
     void testCallToolMissingRequiredArgument() {
         McpToolCallResult result = 
engine.tools().get("query_db").handler().call(Map.of());

Reply via email to