Skip to content

fix: propagate tenant_id through MCP bridge for memory indexing - #512

Merged
viettranx merged 1 commit into
nextlevelbuilder:mainfrom
ongchuthap:fix/bridge-tenant-id-propagation
Mar 27, 2026
Merged

viettranx merged 1 commit into
nextlevelbuilder:mainfrom
ongchuthap:fix/bridge-tenant-id-propagation

Conversation

@ongchuthap

Copy link
Copy Markdown
Contributor

Summary

Memory indexing fails silently for all agents using claude_cli provider because tenant_id is never propagated through the MCP bridge chain.

Symptom:

memory interceptor: index failed after write path=MEMORY.md error="tenant_id required"
KG embeddings backfill failed error="tenant_id required"

Documents save to memory_documents (PutDocument succeeds via fail-open tenant fallback), but IndexDocument fails (fail-closed requireTenantID) → 0 chunks in memory_chunks → memory_search returns empty.

Root Cause

The MCP bridge chain has a gap in tenant_id propagation:

  1. BridgeContext struct has no TenantID field
  2. writeMCPConfigInternal() never sets X-Tenant-ID header
  3. bridgeContextMiddleware never extracts or injects tenant_id into context
  4. PutDocument uses tenantIDForInsert() — fail-open, defaults to MasterTenantID → succeeds
  5. IndexDocument uses scopeClause() → requireTenantID() — fail-closed → fails

Changes (5 files, ~35 lines)

File Change
internal/providers/claude_cli.go Add OptTenantID constant
internal/providers/claude_cli_mcp.go Add TenantID to BridgeContext, set X-Tenant-ID header, update HMAC sign/verify
internal/providers/claude_cli_session.go Include TenantID in bridgeContextFromOpts()
internal/agent/loop.go Pass store.TenantIDFromContext(ctx) to provider opts
internal/gateway/server.go Extract X-Tenant-ID header in bridgeContextMiddleware, inject via store.WithTenantID()

Backward Compatibility

VerifyBridgeContext falls back through 3 HMAC signature formats:

  1. Current: all fields including tenantID
  2. Without tenantID (pre-this-fix sessions)
  3. Without workspace or tenantID (oldest sessions)

Existing sessions continue working without re-authentication.

Test plan

  • Verify memory_search returns results after agent writes to memory via write_file("MEMORY.md")
  • Verify memory_chunks table gets populated after IndexDocument
  • Verify existing sessions (without X-Tenant-ID) still authenticate via HMAC fallback
  • Verify KG embeddings backfill succeeds on container restart

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants