Skip to content

Refuse integers a double would round in the CLI's and mcpdo's JSON-typed flags - #3088

Merged
cliffhall merged 6 commits into
v2/mainfrom
v2/fix/3068-reject-unsafe-integers
Nov 30, 2026
Merged

cliffhall merged 6 commits into
v2/mainfrom
v2/fix/3068-reject-unsafe-integers

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #3068

What

The one-shot CLI and mcpdo parse --tool-arg, --tool-args-json, --metadata, --tool-metadata and mcpdo's key:=value / inline-JSON positionals with JSON.parse, which turns every number into a float64. An integer past ±2^53 (a Snowflake ID, an int64 key) reached the server rounded (12345678901234567890 → 12345678901234567000), with exit 0.

They now refuse it as a usage error, the same position they already take for 1e400:

$ mcp-inspector --cli … --tool-arg id=12345678901234567890
Invalid value for "id": the integer is too large to send exactly as a JSON number (beyond ±2^53 it would be silently rounded); quote it to send it as a string.

$ mcp-inspector --cli … --tool-args-json '{"id":12345678901234567890}'
Invalid --tool-args-json: the integer at "id" is too large to send exactly …

The message names the key, never the value, because these values can carry credentials and the text lands in stderr and CI logs.

How

  • One shared helper, parseJsonExact in core/cli/utils/exact-json.ts (the Node-only surface both clients share). It is JSON.parse with a reviver that reads context.source. JSON.parse source text access ships on every Node in the engines floor (22.23+). A token written as a plain integer literal has to convert back to exactly the same integer (BigInt(source) === BigInt(value)). If it doesn't, the helper throws ImpreciseJsonIntegerError, which carries the reviver key.
  • Only integer literals are checked. A fraction or exponent (0.1, 1e21) asks for a double and is left alone. An overflow to Infinity is left to the existing finite-number checks. 2^53 itself and other exactly representable large integers (18446744073709551616) still pass.
  • Callers: CLI parseKeyValuePair and the --tool-args-json parse site; mcpdo parseKeyValue, and in parse-tool-args.ts the positional key:=value, inline JSON and --tool-args-json paths. Each one tells the new error apart from a SyntaxError, so the "not JSON, send the raw string" fallback still applies only to malformed text.
  • Interaction with siblings: the guard is part of the JSON-parse step itself, so CLI: coerce --tool-arg values using the tool's inputSchema (zip=10001 is sent as a number to a string field) #3075's schema coercion (a type: "string" property keeps the raw text) composes. A big integer for a string field never reaches the parse. CLI --tool-args-json silently sends 1e400 as null (round-trip guard missing; mcpdo rejects it) #3067's Infinity guard on --tool-args-json is untouched.

Tests

  • clients/cli/__tests__/exact-json.test.ts covers the helper on its own: exact and inexact literals, nested keys and array indices, non-integer literals, overflow, malformed JSON, and that the message never holds the value.
  • CLI end to end (metadata.test.ts, programmatic-ergonomics.test.ts): --metadata, --tool-metadata, --tool-arg and --tool-args-json are refused without a tools/call being sent, and 9007199254740991 is still sent as a number.
  • mcpdo (parse-tool-args.test.ts, mcp-coverage.test.ts): positional, inline JSON (tool and event), --tool-args-json and --metadata.
  • README rows for the affected flags in clients/cli/README.md and clients/mcpdo/README.md are updated.

npm run local:gate passes.

🤖 Generated with Claude Code

…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>

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

Integer validation allows literals such as 1000000000000000128 to serialize as a different numeric value, silently sending incorrect IDs to servers that parse integers exactly.

1 open issue
What changed in this PR

This PR addresses #3068 by adding a shared integer-precision guard for JSON-typed inputs in the one-shot CLI and mcpdo.

Changes:

  • Rejects integer literals that lose precision when converted to JavaScript numbers.
  • Integrates the guard into argument and metadata parsing, with errors that omit values.
  • Adds regression coverage and documents the rejection behavior and string workaround.
File Description
core/​cli/​utils/​exact-json.ts Adds the shared parser and precision error.
clients/​mcpdo/​src/​connection/​parse-tool-args.ts Guards positional and JSON-object arguments.
clients/​mcpdo/​src/​connection/​mcp.ts Guards JSON-parsed key/value inputs.
clients/​mcpdo/​README.md Documents rejection and string quoting.
clients/​mcpdo/​__tests__/​parse-tool-args.test.ts Tests argument rejection and value-safe errors.
clients/​mcpdo/​__tests__/​mcp-coverage.test.ts Tests oversized metadata integer rejection.
clients/​cli/​src/​cli.ts Integrates precision checks into CLI parsing.
clients/​cli/​README.md Updates argument and metadata flag documentation.
clients/​cli/​__tests__/​programmatic-ergonomics.test.ts Tests --tool-args-json rejection.
clients/​cli/​__tests__/​metadata.test.ts Tests rejection before tool calls and exact-number acceptance.
clients/​cli/​__tests__/​exact-json.test.ts Tests shared parser behavior and error messages.

Review effort: Balanced

Comment thread core/cli/utils/exact-json.ts Resolved
`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>

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

The catch blocks can silently convert valid JSON into a raw string when the new reviver throws a RangeError; after handling ImpreciseJsonIntegerError, fallback must be limited to SyntaxError and other failures propagated.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Restrict raw JSON fallback to syntax errors

clients/​cli/​src/​cli.ts:1106

The new reviver can throw RangeError while traversing deeply nested, valid JSON, but this catch falls back to parsedValue = val, silently sending the JSON text as a string. After handling ImpreciseJsonIntegerError, restrict the raw-string fallback to SyntaxError and propagate other failures. Apply the same guard in clients/mcpdo/src/connection/mcp.ts:2046 and clients/mcpdo/src/connection/parse-tool-args.ts:116.

1 resolved since last review

Review effort: Balanced

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>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review, rounds 1–2

  • Round 1, high: an exactly representable literal like 1000000000000000128 still reached the wire changed, because JSON.stringify writes the shortest round-trip form. Fixed in 78b9c26, which compares against the integer the serialized token denotes, exponent form included, and adds a wire-token regression. Answered inline.
  • Round 2, the "previously missed" item, which has no thread to reply in: the reviver walk recurses, so very deep valid JSON throws RangeError, and the key=value catch-all would have sent it as a raw string. Fixed in 78e9538. A shared parseJsonOrString in core/cli/utils/exact-json.ts now does the string fallback on SyntaxError only, raises the usage error for an inexact integer, and propagates anything else. It is used by the CLI parseKeyValuePair, mcpdo parseKeyValue and mcpdo's positional key:=value. The other three sites (--tool-args-json, inline JSON, mcpdo --tool-args-json) already turn any other failure into a "not valid JSON" error rather than a fallback. Covered by a 200k-deep nesting test.

npm run local:gate passes on 78e9538. Requesting another round.

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 block the shared JSON integer-validation changes.

No open issues 🎉

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review closed. Round 3 (on 78e9538) was clean: no inline comments, no headline finding, nothing suppressed. Three rounds in all, both findings fixed.

cliffhall and others added 2 commits November 30, 2026 03:05
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
…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>
@cliffhall
cliffhall merged commit 5069870 into v2/main Nov 30, 2026
11 checks passed
@cliffhall
cliffhall deleted the v2/fix/3068-reject-unsafe-integers branch November 30, 2026 09:28
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 and mcpdo silently round integers beyond 2^53 in --tool-arg, --tool-args-json, --metadata, --tool-metadata

2 participants