-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: improve tool() parameter parsing to handle undefined values #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: improve tool() parameter parsing to handle undefined values #990
Conversation
Rewrites parsing to find callback first, then parse args sequentially. Handles edge cases where optional params may be undefined. Fixes: tool(name, undefined, schema, undefined, callback)
@dsp-ant, if you think this PR is worthwhile, I can try to resolve the conflict. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working in this! We are moving towards the registerTool
methods in this SDK, and tool will be deprecated soon (and thank you, you reminded we need to add deprecation notice! )
Resolved conflicts: - src/server/mcp.ts: Integrated callback-first parsing logic with new registerTool method - src/server/mcp.test.ts: Applied prettier formatting and preserved three new test cases for undefined parameter handling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
- Update client.callTool() calls to use correct API signature - Add type assertions for callResult.content to fix TypeScript strict mode errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
@ihrpr Thanks for the kind introduction to the API change. I’ve resolved the conflict. |
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.