feat: add type coercion for tool arguments#1739
Open
martingarramon wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
feat: add type coercion for tool arguments#1739martingarramon wants to merge 2 commits intomodelcontextprotocol:mainfrom
martingarramon wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
LLM models frequently send string values for non-string tool parameters (e.g. "42" instead of 42, "true" instead of true). This adds a coerceToolArgs() utility that applies safe, conservative type coercions based on the JSON Schema before schema validation runs. Coercion rules follow the AJV coercion table: - string → number/integer: Number() only if finite and non-empty - string → boolean: exact "true"/"false" only - number/boolean → string: String() - Nested objects: recursive coercion Fixes modelcontextprotocol#1361 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: ea60e9b The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LLM models frequently send string values for non-string tool parameters — e.g.
"42"instead of42,"true"instead oftrue. This causes schema validation failures that tool authors have to work around individually (as in #1111 withz.coerce.number()forexpires_in).Solution
Adds a
coerceToolArgs()utility inpackages/corethat applies safe, conservative type coercions based on the tool's JSON Schema beforevalidateStandardSchema()runs. This works regardless of which schema library (Zod, Valibot, ArkType) the tool author chose.Coercion rules follow the AJV coercion table:
string → number/integer:Number()only ifNumber.isFinite()and non-emptystring → boolean: exact"true"/"false"only (no truthy coercion)number/boolean → string:String()properties: recursive coerciononeOf,anyOf: skipped (too ambiguous to coerce safely)Hook point:
validateToolInput()inmcp.ts, one line before the existingvalidateStandardSchemacall.Testing
12 test cases in
test/integration/test/issues/test1361.tool-arg-type-coercion.test.ts:"yes"), empty strings all correctly rejectedAll existing tests pass.
pnpm build:all && pnpm lint:all && pnpm test:allclean (only pre-existing Cloudflare Workers env test skipped).Fixes #1361