Skip to content

fix(mcp): propagate local_key and session_key through bridge headers - #799

Closed
henkedk wants to merge 1 commit into
nextlevelbuilder:mainfrom
henkedk:fix/bridge-topic-routing-context
Closed

henkedk wants to merge 1 commit into
nextlevelbuilder:mainfrom
henkedk:fix/bridge-topic-routing-context

Conversation

@henkedk

@henkedk henkedk commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #798

Problem

MCP bridge (Claude CLI subprocess) did not propagate local_key or session_key to tool calls. Tasks created from forum topics had empty routing metadata — announce messages landed in #general instead of the originating topic.

Root Cause

BridgeContext only carried channel, chatID, and peerKind. The local_key (composite key with topic suffix) and session_key were available in RunRequest but never wired through the propagation chain:
RunRequest.LocalKey → opts → BridgeContext → headers → middleware → ctx → tool

Changes

  • Add OptLocalKey constant, pass req.LocalKey through provider opts
  • Add LocalKey to BridgeContext, send as X-Local-Key header
  • Send X-Session-Key header from existing session key
  • Extract both in bridgeContextMiddleware, inject via WithToolLocalKey/WithToolSessionKey
  • Include both in HMAC signature (variadic extra ...string) with backward-compat fallback
  • Use reg.ExecuteWithContext in bridge_server.go to pass routing context to tools

Files Changed

  • internal/providers/claude_cli.go — OptLocalKey constant
  • internal/agent/loop_pipeline_callbacks.go — pass local_key to opts
  • internal/providers/claude_cli_mcp.go — BridgeContext, headers, HMAC
  • internal/providers/claude_cli_session.go — extract from opts
  • internal/gateway/server.go — middleware extraction + context injection
  • internal/mcp/bridge_server.go — ExecuteWithContext

The MCP bridge (Claude CLI subprocess) was not propagating local_key or
session_key to tool calls. When agents running on Claude CLI created
team tasks from forum topics, the routing metadata was empty — causing
announce messages to land in #general instead of the originating topic.

Root cause: BridgeContext only carried channel, chatID, and peerKind.
The local_key (composite key with topic suffix) and session_key were
available in RunRequest but never wired through the CLI provider →
MCP config headers → bridge middleware → tool context chain.

Changes:
- Add OptLocalKey constant and pass req.LocalKey through provider opts
- Add LocalKey field to BridgeContext struct
- Send X-Local-Key and X-Session-Key as bridge headers
- Extract both in bridgeContextMiddleware, inject via WithToolLocalKey
  and WithToolSessionKey
- Include localKey and sessionKey in HMAC signature (variadic extra
  fields) with backward-compatible fallback for pre-existing sessions
- Use reg.ExecuteWithContext in bridge_server.go to pass routing
  context from the HTTP request context to tool execution

Fixes forum topic routing for all tools called through the MCP bridge.
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.

1 participant