ramu11 commented on code in PR #9509:
URL: https://github.com/apache/camel-quarkus/pull/9509#discussion_r4704326923
##########
extensions/mcp-server/runtime/src/main/java/org/apache/camel/quarkus/component/mcp/server/QuarkusMcpServerEngine.java:
##########
@@ -86,6 +129,122 @@ public void initialize(McpServerInfo info) {
LOG.debug("quarkus-mcp-server engine initialized; serving
configuration is owned by quarkus.mcp.server.*");
}
+ /**
+ * Converts the current Quarkus request identity to the transport-neutral
Camel MCP context.
+ *
+ * The CurrentIdentityAssociation is request scoped. It is deliberately
read inside the
+ * tool callback so concurrent MCP requests cannot share or overwrite each
other's identity.
+ */
+ private McpToolCallContext currentToolCallContext() {
+ // Applications without quarkus-security do not have this association.
+ if (currentIdentityAssociation == null) {
+ return McpToolCallContext.EMPTY;
+ }
+
+ SecurityIdentity identity = currentIdentityAssociation.getIdentity();
+ if (identity == null || identity.isAnonymous()) {
+ return McpToolCallContext.EMPTY;
+ }
+
+ Principal principal = identity.getPrincipal();
+ String principalName = principal != null ? principal.getName() : null;
+ if (principalName != null && principalName.isBlank()) {
+ principalName = null;
+ }
+
+ List<String> roles = identity.getRoles().stream()
+ .filter(role -> role != null && !role.isBlank())
+ .sorted()
+ .toList();
+
+ OAuthIdentity oauth = oauthIdentity(principal);
+
+ if (principal == null && principalName == null && roles.isEmpty() &&
oauth == null) {
+ return McpToolCallContext.EMPTY;
+ }
+
+ return new McpToolCallContext(
+ principal,
+ principalName,
+ roles.isEmpty() ? null : roles,
+ oauth != null ? oauth.subject() : null,
+ oauth != null ? oauth.scopes() : null,
+ oauth != null ? oauth.claims() : null,
+ null);
+ }
+
+ /**
+ * Extracts OAuth information from a MicroProfile JWT principal without
making
+ * the MP JWT API a mandatory runtime dependency of the MCP extension.
+ */
+ private static OAuthIdentity oauthIdentity(Principal principal) {
+ if (principal == null) {
+ return null;
+ }
+
+ try {
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ if (classLoader == null) {
+ classLoader = principal.getClass().getClassLoader();
+ }
+
+ Class<?> jwtType = Class.forName(
+ "org.eclipse.microprofile.jwt.JsonWebToken", false,
classLoader);
+
+ if (!jwtType.isInstance(principal)) {
+ return null;
+ }
+
+ String subject = (String)
jwtType.getMethod("getSubject").invoke(principal);
+ Object claimNames =
jwtType.getMethod("getClaimNames").invoke(principal);
+
+ Map<String, Object> claims = new LinkedHashMap<>();
+ if (claimNames instanceof Collection<?> names) {
+ var getClaim = jwtType.getMethod("getClaim", String.class);
+ for (Object name : names) {
+ if (name instanceof String claimName) {
+ claims.put(claimName, getClaim.invoke(principal,
claimName));
+ }
+ }
+ }
+
+ List<String> scopes = oauthScopes(claims.get("scope"));
Review Comment:
Addressed in commit `ed222bd3d881889857afc2ce375d7e46f326bfa7`.
Updated OAuth claim extraction in `QuarkusMcpServerEngine`:
- Reads scopes from both `scope` and `scp`.
- Normalizes JSON-P values before processing, including unwrapping JSON
string values and converting arrays into Java collections.
- Splits scope strings on whitespace and de-duplicates the resulting scopes.
- Normalizes nested JSON-P objects and arrays into Java maps and lists so
Camel routes receive ordinary Java values.
Added focused tests covering `scope`, `scp`, array-valued scopes, duplicate
scopes and nested claim normalization. Updated both the extension usage guide
and the reference documentation to describe the behavior.
The MCP integration-test build also completes successfully.
--
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]