Skip to content

Type --tool-arg values by the tool's inputSchema (#3075) - #3087

Merged
cliffhall merged 19 commits into
v2/mainfrom
v2/feat/3075-schema-typed-tool-args
Nov 30, 2026
Merged

cliffhall merged 19 commits into
v2/mainfrom
v2/feat/3075-schema-typed-tool-args

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #3075

What changed

--tool-arg used to JSON-parse every value as soon as it came off the command line, before the CLI had connected. So zip=10001 was sent as the number 10001, and a tool whose schema declares zip a string rejected it. That applied to the one-shot CLI, to mcpdo's --tool-arg, and to mcpdo's key:=value positionals. Values now stay as raw text until tools/call (or --load) has looked the tool up, which both paths already do for tool_not_found. Each value is then typed from the tool's inputSchema, so there is no extra round trip.

The shared helper is core/cli/utils/tool-arg-types.ts:

Declared property type Result
"string" The text exactly as typed. It never reaches the JSON parser, so 1.10, 9007199254740993 and 1e400 stay verbatim.
number / integer / boolean / object / array The client's existing JSON parse. A result of the wrong type is a usage error (exit 1, code: "usage") that names the key and never the value.
Undeclared key, type array, anyOf/oneOf, no type The current behaviour: JSON-parse when the value parses, otherwise send the text.

--tool-args-json and an inline JSON positional are still sent verbatim.

Design notes

  • The JSON path is still each client's own parser. runMethod and the load engine take a parseToolArgValue hook (RunMethodOptions / LoadRunExtras). The CLI passes a wrapper around its existing parseKeyValuePair, and the mcpdo daemon passes its existing key:=value parse. Edits to those functions, such as CLI and mcpdo silently round integers beyond 2^53 in --tool-arg, --tool-args-json, --metadata, --tool-metadata #3068's rejection of integers beyond 2^53, therefore apply wherever JSON parsing still happens. The string path never calls them.
  • A whole JSON string literal is still unquoted on the string path. zip='"10001"' sends "10001" and message="" sends "". The issue's literal spec says "verbatim", but key='"…"' was the documented way to force a string, and existing tests cover it (message="null", message=""). Sending the quotes literally would break every script that used the workaround. Only text that is one complete JSON string is unquoted. Everything else is kept as typed.
  • mcpdo :=. It gets the same schema typing as --tool-arg. It is mcpdo's only positional key/value syntax, and it has never meant "this is JSON" in the httpie sense: message:=hello has always sent a string, and nothing like httpie's = exists beside it. So it is the general-purpose syntax, and the general-purpose rule applies. Explicit JSON is still available as an inline object, '{"zip":10001}'. events/poll / events/stream positionals have no schema and are unchanged.
  • --load types the flag arguments the same way after its own tool lookup. One exception: a value that contains a load placeholder (page={{rand:1-50}}) skips the type check, because the template engine makes the number per call.
  • Timing of value errors. A bad value (count=abc, n=1e400) is now reported after the connection and the tool lookup rather than at argument parsing, because the type isn't known before then. A malformed pair (novalue, =x, k=) still fails before connecting.

Tests

  • clients/cli/__tests__/tool-arg-types.test.ts: unit coverage of the helper (each type, unions, mismatches, unquoting, parser hand-off, the placeholder skip, __proto__).
  • clients/cli/__tests__/tools.test.ts: end to end against a typed tool. zip=10001 and version=1.10 arrive as strings, count=3 as an integer and a union-typed key as a number, and count=many exits 1 naming the key.
  • clients/mcpdo/__tests__/daemon-connections.test.ts: the daemon types toolArgText (a string stays a string, numbers are parsed, a mismatch is refused). parse-tool-args.test.ts is updated for the raw-text split.
  • npm run local:gate is green.

Docs: the CLI README, the mcpdo README, skills/mcpdo/SKILL.md and docs/cli-smoke-testing.md.

🤖 Generated with Claude Code

`--tool-arg` (CLI and mcpdo) and mcpdo's `key:=value` positionals used to
JSON-parse every value at parse time, so `zip=10001` reached a string-typed
property as a number and was rejected. The values now stay raw text until
`tools/call` / `--load` has looked the tool up, and a shared core helper
(core/cli/utils/tool-arg-types.ts) types them by the declared property type:
string keeps the text (a whole JSON string literal is still unquoted),
number/integer/boolean/object/array go through the client's existing JSON
parse and must match, and undeclared or union-typed keys keep the old parse.
--tool-args-json and inline JSON stay verbatim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Nov 30, 2026
@cliffhall
cliffhall requested a balanced review from Copilot November 30, 2026 06:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The compatibility exception mishandles whitespace-padded JSON string literals, causing existing scripts to pass spaces and literal quotes instead of the intended string value.

1 open issue
What changed in this PR

This PR addresses #3075 by typing command-line tool arguments from each tool’s inputSchema, using shared core logic across the one-shot CLI and mcpdo.

Changes:

  • Preserve raw argument text until tool lookup, keeping numeric-looking string fields intact.
  • Apply schema-based parsing to tool calls and load runs while leaving explicit JSON arguments unchanged.
  • Add regression coverage and update CLI documentation.
File Description
skills/​mcpdo/​SKILL.md Documents schema-typed tool arguments.
docs/​cli-smoke-testing.md Updates argument behavior and testing guidance.
core/​cli/​utils/​tool-arg-types.ts Adds shared argument collection and coercion.
core/​cli/​handlers/​run-method.ts Types arguments before tool invocation.
core/​cli/​handlers/​method-types.ts Adds raw tool-argument text to method inputs.
core/​cli/​handlers/​load-test.ts Applies schema typing to load arguments.
core/​cli/​handlers/​load-template.ts Exposes load-placeholder detection.
clients/​mcpdo/​src/​daemon/​server.ts Supplies mcpdo’s parser to shared handlers.
clients/​mcpdo/​src/​connection/​parse-tool-args.ts Separates raw positionals from explicit JSON.
clients/​mcpdo/​src/​connection/​mcp.ts Passes raw argument text to the daemon.
clients/​mcpdo/​README.md Explains typing rules and JSON alternatives.
clients/​mcpdo/​__tests__/​parse-tool-args.test.ts Tests raw-text splitting and parser behavior.
clients/​mcpdo/​__tests__/​daemon-connections.test.ts Tests schema typing through daemon requests.
clients/​cli/​src/​cli.ts Defers value parsing until tool lookup.
clients/​cli/​README.md Documents schema typing and unchanged event parsing.
clients/​cli/​__tests__/​tools.test.ts Adds end-to-end typed-argument coverage.
clients/​cli/​__tests__/​tool-arg-types.test.ts Tests shared coercion and compatibility behavior.

Review effort: Balanced

Comment thread core/cli/utils/tool-arg-types.ts Resolved
cliffhall and others added 2 commits November 30, 2026 01:13
…3068)

`JSON.parse` turns every number into a float64, so an integer past
±2^53 in --tool-arg, --tool-args-json, --metadata, --tool-metadata or
mcpdo's key:=value / inline-JSON positionals reached the server rounded,
with exit 0. A shared `parseJsonExact` (core/cli/utils/exact-json.ts)
uses the reviver's `context.source` to detect an integer literal that
does not survive the conversion, and both clients reject it as a usage
error naming the key but never the value, as they already do for 1e400.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
…3075)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

JSON-decoded tool arguments can bypass raw-text validation, allowing a reversed load-placeholder range to be evaluated instead of rejected with a usage error.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Validate decoded load-argument templates before evaluation

core/​cli/​handlers/​load-test.ts:2237

JSON decoding here can reveal an invalid load placeholder that both frontends' raw-text validation misses: --tool-arg 'message="call-{{rand:9\u002d1}}"' becomes call-{{rand:9-1}} after coercion. The load template then evaluates the reversed range instead of rejecting it with a usage error. Import and call assertLoadArgsTemplate(flagArgs, "--tool-arg") before loadArgsTemplate, and cover a JSON-escaped placeholder in a regression test.

1 resolved since last review

Review effort: Balanced

cliffhall and others added 2 commits November 30, 2026 02:10
`JSON.stringify` writes a double's shortest round-trip form, so an
exactly representable literal like 1000000000000000128 still reached
the wire as 1000000000000000100. Compare the source digits against the
integer the serialized token denotes, exponent notation included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 2. This finding had no inline thread (it was a "previously missed issue"), so I'm answering it here.

  • Validate decoded load-argument templates before evaluation (core/cli/handlers/load-test.ts): fixed in 94be728. This PR introduced the regression. The clients' pre-connect placeholder check now sees raw text, and a JSON escape such as message="call-{{rand:9\\u002d1}}" only spells a reversed range once it is decoded. runToolLoad now runs assertLoadArgsTemplate on the typed flag arguments before compiling them. The regression test in load-test.test.ts asserts exit 1, load_args_invalid, and that no tools/call was sent.

Round 1 recap: the whitespace-padded JSON string literal was fixed in c74517c, and the reply is in its thread.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Placeholder detection checks only raw input, causing valid escaped numeric templates to be rejected before template expansion.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Accept escaped numeric placeholders in integer arguments

core/​cli/​utils/​tool-arg-types.ts:165

For an integer property, --load --tool-arg 'page="{{rand:1\u002d50}}"' now fails with a usage error even though it is a valid numeric template. The parser decodes the value to {{rand:1-50}}, but the placeholder exemption checks only the raw text, misses the escaped hyphen, and rejects the string before loadArgsTemplate can expand it. Check the decoded string for placeholders too, and add a regression case for a valid escaped numeric placeholder.

Review effort: Balanced

cliffhall and others added 3 commits November 30, 2026 02:51
The reviver walk recurses where a plain JSON.parse does not, so very
deep but valid JSON now throws RangeError, and the key=value parsers'
catch-all fallback would have sent it as a string. A shared
`parseJsonOrString` takes the fallback on SyntaxError only, turns an
inexact integer into the usage error, and propagates anything else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 3. This one was also a "previously missed issue" with no thread, so I'm answering it here.

  • Accept escaped numeric placeholders in integer arguments (core/cli/utils/tool-arg-types.ts): fixed in 6dcee64. Like round 2, this came from deferring the parse. The placeholder exemption for --load is now checked against the decoded string as well as the raw text, so page="{{rand:1\\u002d50}}" for an integer property reaches the template engine again. A non-placeholder string is still refused. Unit cases for both are in tool-arg-types.test.ts.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the schema-aware tool-argument parsing change.

No open issues 🎉

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed. Round 4 came back clean (no inline comments, no suppressed or previously missed findings), so per the pr-flow exit rule I'm stopping on the first clean round. Over the four rounds, three findings were raised and all three were fixed (c74517c, 94be728, 6dcee64). None were declined.

cliffhall and others added 5 commits November 30, 2026 03:35
…3080 rollup)

Both PRs edit the --tool-arg row; the resolution keeps #3069's `key=` sends `""` and #3068's integer-precision sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
…-trip' into v2/feat/3075-schema-typed-tool-args
… into v2/feat/3075-schema-typed-tool-args

Signed-off-by: cliffhall <cliff@futurescale.com>

# Conflicts:
#	clients/cli/README.md
cliffhall and others added 3 commits November 30, 2026 03:35
…hod-flags' into v2/feat/3075-schema-typed-tool-args
…rs' into v2/feat/3075-schema-typed-tool-args
Rollup for tracker #3080: #3075 conflicted textually with every sibling in
clients/cli/src/cli.ts and with #3068 in mcpdo's parsers, so this branch
carries the resolution and merges last.

- Imports: keep #3075's tool-arg-types imports beside #3068's exact-json
  ones and #3074's suggest-flag import.
- mcpdo: #3075 moved --tool-arg / key:=value parsing into autoParseValue;
  it now passes the key so #3068's integer-precision guard applies on that
  path (parseToolArgValue included), with a regression test.
- collectToolArgText accepted no empty value, which would have undone
  #3069 for --tool-arg once both merged; k= is now the empty string, which
  a string or undeclared property receives as "" and a typed non-string
  property refuses as a usage error (tests for both).
- CLI README --tool-arg row: #3075's schema typing plus #3069's k= and
  #3068's integer-precision sentences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall
cliffhall merged commit 089facb into v2/main Nov 30, 2026
11 checks passed
@cliffhall
cliffhall deleted the v2/feat/3075-schema-typed-tool-args branch November 30, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: coerce --tool-arg values using the tool's inputSchema (zip=10001 is sent as a number to a string field)

2 participants