fix(mcp): resolve MCP endpoint sessions by their storage ID

resolveAskSession stores the session owner as
MCPEndpointPrincipal(...).StorageID() -> "mcp_endpoint:<tenant>:<endpoint>",
but SessionOwnerIDFromContext has no PrincipalMCPEndpoint case and falls
back to the raw user id set by the MCP auth middleware ("mcp-<endpoint>"),
so every ask call failed with
"failed to create user message: session not found".

Handle PrincipalMCPEndpoint the same way as PrincipalEmbedSession.
main
linus-liu-web 2026-09-22 01:38:24 +08:00 committed by lyingbug
parent e55e342387
commit 7feac9c570
1 changed files with 8 additions and 1 deletions

View File

@ -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-<endpointID>"), which never matches the
// stored owner ("mcp_endpoint:<tenantID>:<endpointID>"), 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 {