Skip to content

feat(policy): record an MCP tool's output and read http tool replies - #416

Merged
akujane merged 6 commits into
strands-agents:mainfrom
akujane:feat/mcp-tool-output
Nov 12, 2026
Merged

akujane merged 6 commits into
strands-agents:mainfrom
akujane:feat/mcp-tool-output

Conversation

@akujane

@akujane akujane commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

Follow-up to #391.

Problem

#391 records how a tool call ended (output.reply), but not what the tool answered. A rule like
"reset a password only after verify_identity said verified: true" can't be written. And over
http Box never read the reply, so every http call recorded indeterminate and a precondition on
an http server refused every call.

Solution

  • Tool output. A tool's ::response carries output.structured: the reply's
    structuredContent, keeping only the fields its outputSchema declares, for a result only. An
    optional field that doesn't match its type is left out. Like inputs, outputs have no size cap of
    their own; the stdio frame and the gateway's response limit bound both. Example:

    forbid (principal, action == servicenow::Action::"reset_password", resource)
    unless temporal {
      formerly within 1h (
        servicenow::Action::"verify_identity"::response{
          input.user_name: context.input.user_name,
          output.reply: "result",
          output.structured.verified: true }
      )
    };
    
  • http replies. The gateway copies a tools/call reply after redaction and hands it to a new
    ReplyObserver. The policy adapter records the tool's response once both the reply and the
    request outcome are known, and only after mcp:call::response recorded. It works for streamed
    replies too. The gateway asks the server for an uncompressed tool reply, and a reply id matches
    by value (1.0 is 1).

  • Not an answer yet. A reply that only accepts an MCP task, or has a resultType other than
    complete (such as input_required), records indeterminate. The task case was raised by
    @kevin-orellana on fix(policy): record a finished MCP tool call under the tool's own action #391.

  • Unreadable output schemas. An outputSchema Cedar can't read drops only that tool's
    output.structured, instead of the server's whole catalog.

Limits:

  • A server that holds its stream open after the reply delays the tool's record until the stream
    ends. This fails closed.
  • All reply copies held at once share 64 MiB, so concurrent tool calls can't make the gateway hold
    a full copy per connection. A reply over it records indeterminate and still reaches the agent.

On the topic of Frozen surfaces

Discussed why we marked "frozen interfaces" in the code with Fernando:
The changes to "frozen surfaces" here are:

  • egress-gateway: new ReplyObserver; EffectPermit::reply_observer with
    a default.
  • policy: new PolicyError::OutcomeKept, returned when the history keeps an outcome to write
    later (after fix(policy): refuse history stamped ahead of the clock #410), so callers record the tool's response behind it; new
    PolicyEngine::record_tool_reply; McpReply::of_reply returns indeterminate for a
    task reply.
  • Policy vocabulary: new output.structured; over http, output.reply now records result,
    tool_error, or protocol_error.
  • Generated tool schemas declare output.structured and <tool>Output types. A server that
    declares outputs restages once after upgrade.

Related Issues

Follow-up to #391.

Type of Change

New feature

Testing

Unit tests were added to cover the relevant sections. Not covered: recording the tool only after
mcp:call recorded, because no test can make that record fail. The box-level http test uses
plain HTTP; TLS is covered in the gateway's own tests.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment thread crates/policy/src/outcome.rs Resolved
Comment thread crates/policy/src/mcp_input.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Approve

I re-reviewed at 41a7ee4. The two findings from my 8d5d2c6 review are fixed in 67e3cdc and covered by tests (see the thread replies). The merge from main brings in only #393's shell and metadata-address changes. It applies cleanly next to this PR's docs/design/egress.md edits. I ran adapters::egress, mcp_tool_output, mcp_tool_response and the gateway body tests on this head, and they pass.

New code in 67e3cdc: checks that held
  • CopyBudget accounting: held. Bytes released equal bytes reserved in every path: going over the per-reply limit, a failed reserve, finish, abandon, and Drop after finish, which releases 0. Once the observer is taken, nothing more is reserved.
  • Running out of budget fails closed: held. A copy that goes over the shared budget is abandoned, so the tool records indeterminate. The workload still gets the whole reply, and mcp:call::response still counts. This matches the "only a precondition on output.structured is sound" premise in crates/policy/AGENTS.md.
  • same_id on large ids: held. 2^53+1 vs 2^53, u64::MAX vs u64::MAX-1, 1 vs 1.0, -1 vs a large u64, and 1 vs "1" all give the right answer. An integer too large to compare as a float now falls back to comparing the JSON values exactly, so two different ids can't match.
  • Dropping an optional field: held. A rule on the dropped field still isn't met, and a required field that doesn't conform still drops the whole output.

Both fixes are the narrow ones, with a test for each case.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

I re-reviewed head 67e3cdc against 8d5d2c6. Both earlier findings are fixed and covered by tests, and the new code in this commit raised no new findings. cargo test -p strands-box-policy --all-features and the gateway's new body test pass.

Assumptions checked in this commit that held
  • of_reply and resultType: no resultType behaves as before, "complete" gives Result, and input_required or a non-string value gives Indeterminate, which fails closed.
  • Optional-field projection: a bad optional field is dropped, a bad required field still drops the whole output, a rule on the dropped field isn't met (has is false), and the 16 KiB cap applies to the reduced value.
  • CopyBudget: each frame's reservation is released exactly once, whether the copy finishes, is abandoned, or is dropped. The per-reply limit is checked before any reservation, and a repeated finish or abandon releases 0 bytes.
  • same_id: large integer ids are compared as integers, so ids that differ above 2^53 stay distinct, and 1 still matches 1.0.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

I re-reviewed head 453cd19. That covers the main merge (41a7ee4), which my last summary missed, and the commit that drops the output size cap. Neither raised a finding. cargo test -p strands-box-policy --all-features passes, and clippy (--no-deps -D warnings) is clean on the policy crate. Ignore the line in my earlier summary that says the 16 KiB cap applies to the reduced value; that cap is gone now.

Assumptions checked that held
  • Merge 41a7ee4: docs/design/egress.md is the only file both sides changed. The PR's tools/call paragraph lands cleanly beside fix(shell): refuse translated and platform metadata addresses before transport #393's text, with no conflict markers.
  • "Like input, the transport bounds output.structured": the MCP stdio frame limit (MAX_FRAME_BYTES, 1 MiB) bounds a stdio output, and the gateway's 16 MiB response limit bounds an http one. An http input can already be 16 MiB and is recorded on both legs, so an output adds no larger exposure than today. No rule in crates/policy/AGENTS.md or the root AGENTS.md sets a size limit on history records.
  • Removing MAX_STRUCTURED_OUTPUT_BYTES: nothing references it any more, and the tests and the user docs (mcp-policy.md, actions.md) match the new behaviour.

ferdingler
ferdingler previously approved these changes Nov 12, 2026
Comment thread crates/policy/src/adapters/egress.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

I re-reviewed head 901c576. It merges three main commits (#402, #411, #410) on top of 453cd19, which I reviewed last time. The PR's own diff against main hasn't changed (32 files, +2239/−77). There are no conflict markers. The merge sends this PR's observe_tool_response through #410's record_outcome. That routing is correct, but #410 changes what a coarse-leg Err means, and the per-tool leg doesn't account for it yet (see the inline comment).

Assumptions checked that held

Comment thread crates/policy/src/adapters/egress.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Approve

I re-reviewed head 34e9284 against 901c576. The commit fixes my earlier finding that a kept outcome dropped the tool's response (6 files: exchange.rs, egress.rs, dogwood.rs, error.rs, and the policy AGENTS.md/README.md). The fix is correct. What remains is a gap in the tests for the two callers (see the inline comment).

Assumptions checked that held
  • OutcomeKept is returned only for a kept event: the event is pushed onto pending_outcomes before submit_pending maps its error. A mismatched request ID and a poisoned lock fail before that and stay Evaluation, so the tool's response is still skipped for them.
  • Order: the tool's response is queued after the kept mcp:call event, in FIFO order. When the gateway's http:request leg is kept first, both later events wait behind it.
  • The variant reaches the callers: PolicyEngine::record → Authority::record → observe_outcome passes the error through without rewrapping. The broker's Arc<PolicyEngine> sees the same variant.
  • Reply observer ending second: CoarseLeg::Recorded is set on a kept event, so settle records the tool's response. An OutcomeKept there only prints the unconfirmed warning.
  • Box crate: nothing in crates/box matches PolicyError variants exhaustively or relies on the Evaluation message text.
  • Tests: cargo test -p strands-box-policy --all-features passes at 34e9284.

@akujane
akujane merged commit a2cf46e into strands-agents:main Nov 12, 2026
19 checks passed
ferdingler added a commit that referenced this pull request Nov 13, 2026
…against the upstream Shell

The vendored Shell files that #397 changed are deleted here. #397's Shell tests
move to crates/shell/tests/main_shell_semantics.rs and the broker tests, and its
two Lua stdin tests move to the broker. The tests for #397's Shell fixes fail
until the upstream Shell fixes land.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants