From 0951c53827d9e1b37016ddfba87b602bc167b9b8 Mon Sep 17 00:00:00 2001 From: Yaojia Wang Date: Sun, 23 Nov 2025 15:39:13 +0100 Subject: [PATCH] fix(backend): Fix ApiKeyId lookup in PendingChangeService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PendingChangeService was looking for 'ApiKeyId' in HttpContext.Items, but McpApiKeyAuthenticationHandler sets 'McpApiKeyId'. Updated the lookup to check both keys for backward compatibility. Changes: - Modified ApiKeyId retrieval to check 'McpApiKeyId' first, then fall back to 'ApiKeyId' - Prevents McpUnauthorizedException: API Key not found in request context Fixes compatibility between McpApiKeyAuthenticationHandler and PendingChangeService. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../Services/PendingChangeService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/colaflow-api/src/Modules/Mcp/ColaFlow.Modules.Mcp.Application/Services/PendingChangeService.cs b/colaflow-api/src/Modules/Mcp/ColaFlow.Modules.Mcp.Application/Services/PendingChangeService.cs index cb7da5f..90a850a 100644 --- a/colaflow-api/src/Modules/Mcp/ColaFlow.Modules.Mcp.Application/Services/PendingChangeService.cs +++ b/colaflow-api/src/Modules/Mcp/ColaFlow.Modules.Mcp.Application/Services/PendingChangeService.cs @@ -34,7 +34,9 @@ public class PendingChangeService( var tenantId = _tenantContext.GetCurrentTenantId(); // Get API Key ID from HttpContext (set by MCP authentication middleware) - var apiKeyIdNullable = _httpContextAccessor.HttpContext?.Items["ApiKeyId"] as Guid?; + // Check both "McpApiKeyId" (from McpApiKeyAuthenticationHandler) and "ApiKeyId" (from legacy middleware) + var apiKeyIdNullable = _httpContextAccessor.HttpContext?.Items["McpApiKeyId"] as Guid? + ?? _httpContextAccessor.HttpContext?.Items["ApiKeyId"] as Guid?; if (!apiKeyIdNullable.HasValue) { throw new McpUnauthorizedException("API Key not found in request context");