fix: improve tool() parameter parsing to handle undefined values #990
+235
−23
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.
Rewrites parameter parsing to find callback first, then parse args sequentially by type. Handles edge cases where optional params (description, paramsSchema, annotations) may be undefined.
Fixes cases like:
tool(name, undefined, schema, undefined, callback)
tool(name, description, undefined, annotations, callback)
tool(name, undefined, schema, callback)
Motivation and Context
When
tool()
is called withundefined
in optional parameter positions, the original shift-based parsing logic fails because:typeof undefined !== "object"
causes undefined values to be skipped incorrectlyshift()
operations lose track of parameter positionsThis happens in real-world scenarios when:
config.annotations
may be undefined)The fix improves runtime robustness without changing the public API, maintaining backward compatibility while handling edge cases more gracefully.
How Has This Been Tested?
tool(name, undefined, schema, callback)
- description undefinedtool(name, desc, undefined, annotations, callback)
- paramsSchema undefinedtool(name, desc, schema, undefined, callback)
- annotations undefinedBreaking Changes
None. This is a non-breaking internal fix:
Types of changes
Checklist
Additional context
Implementation approach:
This callback-first approach is more robust than the original shift-based logic because it establishes a fixed reference point (the callback) before parsing optional parameters.
Error handling:
Added validation to throw clear error if no callback function is found in arguments.