Skip to content

feat(mcp): put the shell tool's exit code in a third text block - #268

Open
ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:feat/mcp-exit-code-text
Open

ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:feat/mcp-exit-code-text

Conversation

@ferdingler

Copy link
Copy Markdown
Collaborator

Description

The MCP server's shell tool only reports the exit code in the result metadata. Plenty of MCP clients never show metadata to the model, so an agent using one of them can't tell whether its command failed unless the command happened to print something.

Now the result has a third text block that just says exit code: N, after the stdout and stderr blocks. I put it in its own block instead of adding a line to stderr so the stdout and stderr blocks stay exactly what the command wrote. Anything that reads the first two blocks by position keeps working, and stderr doesn't turn non-empty on every failed command. The metadata field stays, and the tool description now describes the three blocks. Only the MCP server changes; the Python and Node bindings don't use it.

I ran a failing command against the server by hand and got stdout, stderr and the exit code as three blocks, with the metadata unchanged. The MCP tests now check the new block on success, on failure and on a timeout, and the full Rust check passes.

Related Issues

Part of #207

Documentation PR

None. The tool description is the documentation for this.

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

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
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

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

@ferdingler
ferdingler requested a review from a team as a code owner November 13, 2026 05:08
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. I found no correctness issues. The exit code now reaches the model on every path that runs a command, and the first two blocks are unchanged. There's one optional suggestion inline: write the tool description for the model rather than in client JSON paths.

This changes what the model sees, and the repo has no API-review label. design is the closest one if you want a maintainer to look at the shape.

@ferdingler, you asked for this review.

✅ What I verified, plus questions and appendix

Verified at a6045d7

  • cargo test --test mcp_integration: 41 passed, 0 failed.
  • I ran the server by hand with JSON-RPC: echo out; echo err >&2; exit 3 returned ["out\n", "err\n", "exit code: 3"] and metadata.exit_code: 3.
  • I ran it end to end: Strands MCPClient → PR binary → Bedrock Claude Sonnet 4.5. The model got the three blocks in order, empty blocks included, and for ls /nonexistent it correctly reported "exit code 2, failed".
  • Every way exec_shell returns after running a command, including a timeout, gives three blocks. The one early return is the missing-command error at src/mcp.rs:213, which gives a single block with isError. That's correct, because nothing ran.
  • In-repo consumer: mcp_result_to_lua (src/builtins/lua.rs:1214-1228) returns a string only when there is exactly one block. Otherwise it returns a table. This tool already returned two blocks, so it was already a table; it now has 3 entries, and [1] and [2] are unchanged.
  • No test asserted a length of 2. The new test at tests/mcp_integration.rs:130-133 checks all three blocks.
  • Docs: README, AGENTS.md, and the Python and TS bindings don't describe the block layout. The tool description is the only place it's documented, and this PR updates it.

Passes run: correctness review, plus API and model-facing review, because the tool description and result shape changed. I skipped the adversarial pass. The diff adds one format! of an integer, with no parsing, concurrency, or security surface.

Questions (non-blocking)

  • metadata isn't an MCP result field; the spec reserves _meta. Strands Python copies call_tool_result.meta to result["metadata"] (harness-sdk strands-py/src/strands/tools/mcp/mcp_client.py:1462-1463) and never reads metadata. Emitting "_meta": {"exit_code": N} would make Strands users see tool_result["metadata"]["exit_code"], the name you already use. Worth doing here or as a follow-up? This is pre-existing, so I didn't file it. Whether and when to do it is your call.
  • A client that joins every text block into one string will now append exit code: N to every result. That's the point of the change, but it's worth a line in the release notes as a result-shape change.

Appendix (non-blocking, 1)

  • ⚪ A non-zero exit still leaves isError unset, so Strands reports status: "success" for a failed command. That's defensible, since grep and test exit non-zero normally, and it's existing behavior. A short note in the exec_shell comment could stop someone from "fixing" it later.

Reading order: src/mcp.rs:242-249, then :131, then tests/mcp_integration.rs:119-135.

Comment thread src/mcp.rs Outdated
{
"name": "shell",
"description": "Runs a command in the strands-shell virtual shell. Returns two text content blocks: content[0].text is stdout, content[1].text is stderr (both always present, empty string when a stream produced nothing). The exit code is in metadata.exit_code.",
"description": "Runs a command in the strands-shell virtual shell. Returns three text content blocks: content[0].text is stdout, content[1].text is stderr (both always present, empty string when a stream produced nothing), and content[2].text is the exit code as `exit code: N`. The exit code is also in metadata.exit_code.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 The description refers to JSON paths and a field the model never sees. Clients such as Strands, the OpenAI Agents SDK and langchain-mcp-adapters pass the model three unlabeled text parts in order, and none of them show metadata to the model. So content[0].text and metadata.exit_code mean nothing to it and cost tokens on every tools/list.

Suggestion (optional):

Suggested change
"description": "Runs a command in the strands-shell virtual shell. Returns three text content blocks: content[0].text is stdout, content[1].text is stderr (both always present, empty string when a stream produced nothing), and content[2].text is the exit code as `exit code: N`. The exit code is also in metadata.exit_code.",
"description": "Runs a command in the strands-shell virtual shell. Returns three text blocks, in order: stdout, stderr, then `exit code: N`. stdout and stderr are exactly what the command wrote; an empty block means that stream produced nothing.",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in efd0780. The description now names the three blocks in order without JSON paths or the metadata field. metadata.exit_code is still emitted for clients that read it; it's just not advertised to the model.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks. On the non-blocking points:

  • metadata vs _meta: good catch, but it's the existing field name and moving it is a separate result-shape change. I'm leaving it out of this PR and will raise it as its own follow-up.
  • Clients that join all blocks: yes, they'll now see exit code: N at the end. That's the intent; I'll note it as a result-shape change when this ships.
  • isError on non-zero exit: agreed it should stay unset, since grep/test exit non-zero normally. I didn't add a comment for it here, to keep this PR to the one change.

@ferdingler ferdingler mentioned this pull request Nov 13, 2026
10 tasks done

This branch has not been deployed

No deployments
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