Repository navigation
fix: propagate tenant_id through MCP bridge for memory indexing - #512
Merged
viettranx merged 1 commit intoMar 27, 2026
Merged
Conversation
Problem: Memory documents saved via write_file → MemoryInterceptor are stored in memory_documents (PutDocument succeeds via fail-open tenant fallback), but IndexDocument fails with "tenant_id required" because the MCP bridge chain never propagates tenant_id. This results in 0 memory_chunks, making memory_search return empty for all agents using claude_cli provider. Root cause: - BridgeContext struct lacks TenantID field - writeMCPConfigInternal never sets X-Tenant-ID header - bridgeContextMiddleware never extracts/injects tenant_id into context - PutDocument uses tenantIDForInsert() (fail-open, defaults to master) - IndexDocument uses scopeClause() → requireTenantID() (fail-closed) Fix: - Add OptTenantID constant and BridgeContext.TenantID field - Pass tenant_id from agent loop context → provider opts → MCP headers - Add X-Tenant-ID to bridge request headers (HMAC-signed) - Extract X-Tenant-ID in bridgeContextMiddleware → store.WithTenantID() - Backward-compatible HMAC verification with 3 fallback levels (with tenant, without tenant, without workspace+tenant) Affected: all agents using claude_cli provider type. Also fixes: "KG embeddings backfill failed: tenant_id required"
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Memory indexing fails silently for all agents using
claude_cliprovider becausetenant_idis never propagated through the MCP bridge chain.Symptom:
Documents save to
memory_documents(PutDocument succeeds via fail-open tenant fallback), butIndexDocumentfails (fail-closedrequireTenantID) → 0 chunks inmemory_chunks→memory_searchreturns empty.Root Cause
The MCP bridge chain has a gap in tenant_id propagation:
BridgeContextstruct has noTenantIDfieldwriteMCPConfigInternal()never setsX-Tenant-IDheaderbridgeContextMiddlewarenever extracts or injects tenant_id into contextPutDocumentusestenantIDForInsert()— fail-open, defaults to MasterTenantID → succeedsIndexDocumentusesscopeClause()→requireTenantID()— fail-closed → failsChanges (5 files, ~35 lines)
internal/providers/claude_cli.goOptTenantIDconstantinternal/providers/claude_cli_mcp.goTenantIDtoBridgeContext, setX-Tenant-IDheader, update HMAC sign/verifyinternal/providers/claude_cli_session.goTenantIDinbridgeContextFromOpts()internal/agent/loop.gostore.TenantIDFromContext(ctx)to provider optsinternal/gateway/server.goX-Tenant-IDheader inbridgeContextMiddleware, inject viastore.WithTenantID()Backward Compatibility
VerifyBridgeContextfalls back through 3 HMAC signature formats:Existing sessions continue working without re-authentication.
Test plan
memory_searchreturns results after agent writes to memory viawrite_file("MEMORY.md")memory_chunkstable gets populated afterIndexDocument