Repository navigation
feat(mcp): put the shell tool's exit code in a third text block - #268
ferdingler wants to merge 2 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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 3returned["out\n", "err\n", "exit code: 3"]andmetadata.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 forls /nonexistentit correctly reported "exit code 2, failed". - Every way
exec_shellreturns after running a command, including a timeout, gives three blocks. The one early return is the missing-commanderror atsrc/mcp.rs:213, which gives a single block withisError. 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-133checks 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)
metadataisn't an MCP result field; the spec reserves_meta. Strands Python copiescall_tool_result.metatoresult["metadata"](harness-sdk strands-py/src/strands/tools/mcp/mcp_client.py:1462-1463) and never readsmetadata. Emitting"_meta": {"exit_code": N}would make Strands users seetool_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: Nto 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
isErrorunset, so Strands reportsstatus: "success"for a failed command. That's defensible, sincegrepandtestexit non-zero normally, and it's existing behavior. A short note in theexec_shellcomment could stop someone from "fixing" it later.
Reading order: src/mcp.rs:242-249, then :131, then tests/mcp_integration.rs:119-135.
| { | ||
| "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.", |
There was a problem hiding this comment.
🟡 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):
| "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.", |
There was a problem hiding this comment.
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.
|
Thanks. On the non-blocking points:
|
Description
The MCP server's
shelltool 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.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.