From 7feac9c57081a6f60a698df369b745380f75066d Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Tue, 22 Sep 2026 01:38:24 +0800 Subject: [PATCH] fix(mcp): resolve MCP endpoint sessions by their storage ID resolveAskSession stores the session owner as MCPEndpointPrincipal(...).StorageID() -> "mcp_endpoint::", but SessionOwnerIDFromContext has no PrincipalMCPEndpoint case and falls back to the raw user id set by the MCP auth middleware ("mcp-"), so every ask call failed with "failed to create user message: session not found". Handle PrincipalMCPEndpoint the same way as PrincipalEmbedSession. --- internal/types/principal.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/types/principal.go b/internal/types/principal.go index 263e19ea..ca06274e 100644 --- a/internal/types/principal.go +++ b/internal/types/principal.go @@ -185,10 +185,17 @@ func MCPOAuthPrincipalFromContext(ctx context.Context) Principal { // caller. API external users and embed chat sessions use principal-derived IDs; // tenant API keys are isolated per key id; MCP OAuth token storage uses // MCPOAuthPrincipalFromContext (visitor-level for embed). +// +// PrincipalMCPEndpoint must resolve to StorageID() too: the MCP `ask` tool +// creates its session with `MCPEndpointPrincipal(...).StorageID()` and then +// looks it up through this function. Without the case below the lookup falls +// through to the raw user id ("mcp-"), which never matches the +// stored owner ("mcp_endpoint::"), so every ask call +// failed with "failed to create user message: session not found". func SessionOwnerIDFromContext(ctx context.Context) string { if p, ok := PrincipalFromContext(ctx); ok { switch p.Type { - case PrincipalAPIExternalUser, PrincipalEmbedSession: + case PrincipalAPIExternalUser, PrincipalEmbedSession, PrincipalMCPEndpoint: return p.StorageID() case PrincipalAPITenant: if scope, ok := TenantAPIKeyScopeFromContext(ctx); ok && scope.KeyID > 0 {