Repository navigation
fix(gateway): inject agent key in MCP bridge so session tools resolve identity - #1197
Conversation
mrgoonie
left a comment
There was a problem hiding this comment.
Summary
This PR fixes the MCP bridge session-tool identity path by loading the agent row that bridgeContextMiddleware already fetches and injecting ag.AgentKey into the tool context. The code change itself matches the session tools' current contract: those tools fail closed on empty tools.ToolAgentKeyFromCtx(ctx), while bridge requests currently only inject the agent UUID.
Risk level: Medium. The code delta is low risk, but the PR materially overlaps an existing open PR and the overlap needs maintainer coordination before merge.
Mandatory gates
- Duplicate / prior implementation: overlap found.
#1094already patchesinternal/gateway/server.goin the samebridgeContextMiddlewareblock and injectstools.WithToolAgentKey(ctx, ag.AgentKey)there. - Project standards: docs found. Checked
README.md,CLAUDE.md,AGENTS.md, and current code patterns. - Strategic necessity: clear value. This unblocks session tools over
/mcp/bridgefor provider agents using signed bridge headers.
Findings
Important
- Resolve the material overlap with
#1094before merge. This PR says#1094does not touch thebridgeContextMiddlewarepath, but the current#1094diff does: it adds agent-key recovery ininternal/gateway/server.goaround the sameGetByIDUnscopedlookup, includingctx = tools.WithToolAgentKey(ctx, ag.AgentKey). That makes the PR rationale inaccurate and leaves two open PRs carrying the same server-path fix. Please either mark this PR as the deliberate minimal successor for that slice and retarget/close the overlapping portion of#1094, or rebase/merge the intended implementation into one PR.
Suggestions
- Add a narrow regression test for
bridgeContextMiddleware: signedX-Agent-ID, fakeAgentStorereturningAgentKey, and a handler assertion thattools.ToolAgentKeyFromCtx(ctx)is populated. Existing session-tool tests cover the tool behavior, but not the bridge propagation that regressed here. - Before merge or squash, remove the AI co-author trailer from the commit metadata to align with the repo's no-AI-reference commit guidance.
Verdict: Request changes.
Posted by /ck:review-pr at 2026-06-15T03:50:40Z
… identity bridgeContextMiddleware injects the agent UUID (store.WithAgentID) but never the agent key, so session tools (sessions_list/history/send/status) that resolve the caller via tools.ToolAgentKeyFromCtx always get "" and fail with "agent context required" when invoked over /mcp/bridge (e.g. by a claude-cli provider agent). Session keys are namespaced by agent key (agent:<key>:...), not UUID, and the bridge has no RunContext fallback. Inject the key in the same block that already fetches the agent for shell-deny overrides — symmetric with store.WithShellDenyGroups, no extra DB call, no new imports. nextlevelbuilder#1094 makes the same fix at this site but bundles it into a large, stale (CONFLICTING) ACP/i18n refactor; this is the minimal extraction of that slice. It omits nextlevelbuilder#1094's companion store.WithAgentKey write, which nothing on dev reads (every session tool reads tools.ToolAgentKeyFromCtx). Add internal/gateway/bridge_context_test.go: a regression guard asserting a signed X-Agent-ID lands the agent key in tools.ToolAgentKeyFromCtx, plus a no-store negative control. Signed-off-by: Jaegeon Oh <zezaeoh@gmail.com>
0b51f7f to
36c88fd
Compare
|
Thanks for the review — all three points addressed. Pushed Important — overlap with #1094: You're right, and my original description was wrong. #1094 does patch this same
I can't retarget/close #1094 (different author). Suggested resolution: land this minimal PR and drop the Suggestion — regression test: Added Suggestion — commit trailer: Removed the AI co-author trailer; |
mrgoonie
left a comment
There was a problem hiding this comment.
Summary
This PR fixes the MCP bridge identity path by reusing the already-loaded agent row in and injecting into the tool context. The new regression test covers the signed bridge path and the no-store negative control.
Risk level: Medium. The code delta is narrow, but it touches the signed bridge identity context used by session tools. I did not find a correctness or security blocker.
Mandatory gates
- Duplicate / prior implementation: overlap found, acceptable. already contains the same server-path slice, but this PR now correctly documents itself as the minimal successor/extraction from that stale, broad PR.
- Project standards: docs found. Checked , , , and current bridge/session-tool patterns.
- Strategic necessity: clear value. This unblocks , , , and over for provider agents that currently only propagate the agent UUID.
Findings
No critical or important findings remain.
Verification
- Reviewed the full PR diff: and .
- Confirmed session tools fail closed on empty and use agent keys, not UUIDs, for session ownership checks.
- Confirmed the Claude MCP bridge signs the same context fields used by the new test.
- Ran on PR head : -> passed.
- GitHub reports no PR checks for this branch, so the approval relies on the targeted local test plus diff review.
Verdict: Approve.
Posted by /ck:review-pr at 2026-06-15T07:17:44Z
mrgoonie
left a comment
There was a problem hiding this comment.
Correction
The previous approval review submitted successfully, but shell expansion stripped the inline-code tokens from its body. Approval verdict stands; corrected review text below.
Summary
This PR fixes the MCP bridge identity path by reusing the already-loaded agent row in bridgeContextMiddleware and injecting ag.AgentKey into the tool context. The new regression test covers the signed X-Agent-ID bridge path and the no-store negative control.
Risk level: Medium. The code delta is narrow, but it touches the signed bridge identity context used by session tools. I did not find a correctness or security blocker.
Mandatory gates
- Duplicate / prior implementation: overlap found, acceptable.
#1094already contains the same server-path slice, but this PR now correctly documents itself as the minimal successor/extraction from that stale, broad PR. - Project standards: docs found. Checked
README.md,CLAUDE.md,AGENTS.md, and current bridge/session-tool patterns. - Strategic necessity: clear value. This unblocks
sessions_list,sessions_history,sessions_send, andsession_statusover/mcp/bridgefor provider agents that currently only propagate the agent UUID.
Findings
No critical or important findings remain.
Verification
- Reviewed the full PR diff:
internal/gateway/server.goandinternal/gateway/bridge_context_test.go. - Confirmed session tools fail closed on empty
tools.ToolAgentKeyFromCtx(ctx)and use agent keys, not UUIDs, for session ownership checks. - Confirmed the Claude MCP bridge signs the same context fields used by the new test.
- Ran on PR head
36c88fdd6e4cd78d3f75c9c597381e31800759ba:go test ./internal/gateway -run TestBridgeContextMiddleware -count=1-> passed. - GitHub reports no PR checks for this branch, so the approval relies on the targeted local test plus diff review.
Verdict: Approve.
Posted by /ck:review-pr at 2026-06-15T07:18:03Z
Problem
Builtin session tools (
sessions_list,sessions_history,sessions_send,session_status) always fail with"agent context required"when invoked through the MCP bridge (e.g. by aclaude-cliprovider agent). They are enabled and callable, but unusable over/mcp/bridge.Root cause
bridgeContextMiddleware(internal/gateway/server.go) injects the agent UUID viastore.WithAgentID(ctx, id)but never the agent key. The session tools resolve the caller viatools.ToolAgentKeyFromCtx(ctx)— the agent key, because session keys are namespaced by key (agent:<key>:...), not UUID — and over the bridge there is noRunContextfallback, so it returns""and every tool short-circuits:Fix
The middleware already fetches the agent (
ag) in the same block to apply shell-deny-group overrides. Inject the key right there, symmetric with the existingstore.WithShellDenyGroups(ctx, groups):No extra DB round-trip (reuses the existing
GetByIDUnscoped), no new imports (toolsis already used here forWithToolChannel/WithToolChatID).Scope vs #1094
Correction (was inaccurate in the original description): #1094 does touch this same
bridgeContextMiddlewareblock — it addsctx = tools.WithToolAgentKey(ctx, ag.AgentKey)at the sameGetByIDUnscopedsite. So the server-path fix overlaps; my earlier claim that it "does not touch this path" was wrong. Thanks to the reviewer for catching it.This PR is the minimal, isolated extraction of exactly that slice, intended as the deliberate successor for it:
BuiltinToolStoreplumbing. It is currentlyCONFLICTINGagainstdevand last updated 2026-05-04 (stale), so the load-bearing 5-line bridge fix is blocked behind unrelated work that is unlikely to land soon.internal/gateway/server.go(+5) plus a regression test, so it can merge independently and immediately unblock session tools over/mcp/bridge.store.WithAgentKey(ctx, ag.AgentKey)write: nothing ondevreads the store-level key (store.AgentKeyFromCtxhas no callers), while every session tool readstools.ToolAgentKeyFromCtx. Keeping the change to exactly what is load-bearing.I can't edit/close #1094 (different author). Suggested resolution: land this minimal PR, and drop the
internal/gateway/server.goslice from #1094 when it rebases — the rest of #1094 is independent.Testing
internal/gateway/bridge_context_test.goexercises the bridge propagation that regressed here: a signedX-Agent-ID+ a stubAgentStorereturning anAgentKey, asserting the downstream handler seestools.ToolAgentKeyFromCtx(ctx)populated, plus a negative control (no store → empty key). Mutation-checked: removing the fix line makesTestBridgeContextMiddleware_InjectsAgentKeyfail withToolAgentKeyFromCtx = "".TestSessionStatus_EmptyAgentID_Error(no key →"agent context required") and the positive cases via theagentCtx()helper. This PR makes the bridge actually supply the key those tools expect.v3.13.2+ this patch):sessions_list/sessions_historynow return the agent's sessions over the bridge instead of erroring.