Skip to content

feat(mcp): support attachment-backed broker queries for CallMcpWithQuery - #1309

Merged
jhrozek merged 4 commits into
mainfrom
plan/callmcpwithquery-broker-support
Sep 9, 2026
Merged

jhrozek merged 4 commits into
mainfrom
plan/callmcpwithquery-broker-support

Conversation

@jhrozek

@jhrozek jhrozek commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Routes CallMcpWithQuery through the current session's existing MCP broker attachment for broker-backed targets, instead of only the direct/global MCP manager — fixes #1304: a broker-only session (no direct MCP configured) never got this tool registered at all, and even in a mixed session a broker-routed target bypassed the attachment's authorization/audit path entirely.
  • The query wrapper reuses the attachment's own frozen route + existing authorization lifecycle (grant/deny/cancel, live and restarted-host continuation) — no second connection to the broker-owned upstream, and the raw upstream response never crosses the attachment boundary; only the bounded jq projection does.
  • Direct-manager behavior for non-broker targets is unchanged.

Process note

This branch's acceptance plan (docs/acceptance/callmcpwithquery-broker-support.md) records that the directing user explicitly waived the normal /to-acceptance-plan → /plan-orchestrate spine for this implementation, and has now explicitly requested this PR. No merge is claimed — human review and merge remain required.

Verification

  • Offline: full acceptance-plan test list passes with -race (attachment-bound projection, authorization continuation across a live and a restarted host, isolation/foreign-session rejection, at-most-once/no-raw-result invariant, post-delivery failure bounding, broker-only factory registration, direct-manager compatibility, ADR-0234 authority-unchanged proof). task lint, task test (incl. standalone module checks), task docs, task api:check, task site:build all green.
  • Live, end-to-end (real kind cluster + Keycloak + the connector-gateway.stacklok.dev fixture backend, mecak8s in broker-only mode):
    • Confirmed CallMcpWithQuery is now registered where it previously was not (broker-only session, no direct MCP manager).
    • Confirmed it routes through the session's real broker attachment and its ordinary lazy per-tool authorization — same grant machinery and continuation path as any other protected tool call, no bypass.
    • Confirmed a direct call to the same tool legitimately exceeds mecatl's 25000-byte output cap, and confirmed CallMcpWithQuery with a genuinely narrowing jq filter (keys) successfully returns the bounded projection where the direct call cannot.

Known non-blocking follow-up

attachmentQueryTool.Execute currently collapses every failure from the underlying tool call (a pre-transport replay-ledger rejection, a genuine transport failure, and an oversized-output rejection after a successful call) into one generic message ("target may have succeeded; automatic replay refused..."). Live-testing showed this is misleading specifically in the oversized-output case: the direct-call path already gives actionable guidance ("narrow your filter"), and the query wrapper should too. The underlying behavior is correct (nothing is silently truncated or replayed) — this is a message-quality issue, tracked in the acceptance plan's "Deferred decisions and known risks", not a blocker for this PR.

Test plan

  • go test ./internal/adapter/mcpbroker/... ./internal/adapter/mcp/... ./internal/app/... -run 'CallMcpWithQueryBrokerSupport|Invariant_call_mcp_with_query|ADR_0234' -race
  • task lint && task test
  • go run ./cmd/mecademo
  • task docs
  • Live verification against a real broker-only mecak8s deployment (see above)

🤖 Generated with Claude Code

@jhrozek
jhrozek force-pushed the plan/callmcpwithquery-broker-support branch from 7c4d860 to 275e03b Compare September 9, 2026 20:26
jhrozek and others added 4 commits September 9, 2026 22:56
Scope the fix to registering CallMcpWithQuery for broker-backed sessions
and filtering results through the existing attachment and authorization
path. Preserve direct-manager behavior and exclude engine lifecycle,
permission, and persistence redesigns.

Co-authored-by: mecatl <[email protected]>
Why: CallMcpWithQuery depended on the direct MCP manager, so broker-only sessions lacked the existing result-narrowing tool. Broker support must preserve session attachment authority and keep successful raw responses out of model history, events, and audit records.

How: Register a session-bound query wrapper for eligible broker targets. Derive the same normalized native call for authorization and execution, reuse the attachment's frozen route and authorization gates, and apply bounded jq projection inside the concrete broker transport before returning. Validate jq and cancellation before invocation, and never retry a possibly delivered query. Keep the direct-manager path and tool schema unchanged.

Add offline coverage for real Service registration, bounded projection, cancellation, attachment isolation, live and restarted authorization continuation, and direct-manager compatibility. Update architecture, public documentation, and acceptance evidence. Verified with task lint, task test, task docs, task api:check, task site:build, and the offline demo.

Fixes #1304

Co-authored-by: mecatl <[email protected]>
…ation

Update the callmcpwithquery-broker-support plan status now that the directing
user has explicitly requested a PR: link tracking issue #1304, record the live
kind/Keycloak/connector-gateway end-to-end verification (broker-only
registration, attachment-routed lazy authorization, oversized-output handling),
and note the known non-blocking error-message-flattening follow-up.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ht ADRs

ADR 0319 was independently claimed by two other open PRs (#1238, #1278).
Renumber this ADR to 0323, the next free number after main's highest
(0322), to avoid a duplicate-number collision on merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jhrozek
jhrozek force-pushed the plan/callmcpwithquery-broker-support branch from 275e03b to f62d79c Compare September 9, 2026 20:56
@jhrozek
jhrozek merged commit 8996592 into main Sep 9, 2026
56 of 58 checks passed
@jhrozek
jhrozek deleted the plan/callmcpwithquery-broker-support branch September 9, 2026 21:26
jhrozek added a commit that referenced this pull request Sep 9, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jhrozek added a commit that referenced this pull request Sep 10, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jhrozek added a commit that referenced this pull request Sep 10, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jhrozek added a commit that referenced this pull request Sep 10, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jbeda added a commit that referenced this pull request Sep 11, 2026
* commit 'cc6b16afbf7feec3e16451fdc590a3265fbf0c0b':
  docs: amend Shell diagnostic ownership (#1330)
  fix(mecatui): refresh status context on session continuation (#1328)
  fix(jsonlstore): preserve session saves with legacy lineage (#1327)
  test(sessiondebug): eliminate handle tampering flake (#1323)
  fix(agent): authorize structured subagent results (#1317)
  feat(mcp): support attachment-backed broker queries for CallMcpWithQuery (#1309)
  docs(agents): document full-gate timeout guidance (#1316)
  fix(server): let the stale-session sweep re-acquire a lost lease (#1302)
  feat(clientauth): headless Linux credential storage (#1291)
  chore(mcpbroker): trace protected call latency (#1314)

# Conflicts:
#	docs/acceptance/canonical-shell-command-tool.md
jhrozek added a commit that referenced this pull request Sep 11, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jhrozek added a commit that referenced this pull request Sep 11, 2026
…on rebuild

Rebasing onto the newly-merged attachment-backed CallMcpWithQuery support
(#1309) surfaced two problems in the rebased tree:

- RefreshGrantedAuthorizationCatalogue's cyclomatic complexity exceeded the
  lint gate once merged with the surrounding code; split it into
  resolveGrantedBundle/buildRefreshedCatalogue/publishRefreshedCatalogue
  helpers with no behavior change.
- continueGrantedAuthorizationLocked rebuilds the session engine from the
  attachment's exact catalogue tools after a granted authorization, which
  dropped the attachment-bound CallMcpWithQuery meta-tool that brokerTools
  normally adds alongside the catalogue. A parked CallMcpWithQuery call then
  resumed into a catalogue that no longer had it registered. Factor the
  addition into a shared withAttachmentQueryTool helper used by both
  brokerTools and the granted-authorization continuation path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
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.

CallMcpWithQuery bypasses the MCP broker attachment for broker-routed backends

1 participant