-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Gracefully handle numeric parameters passed as strings #2130
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,23 @@ func isAcceptedError(err error) bool { | |
| return errors.As(err, &acceptedError) | ||
| } | ||
|
|
||
| // toFloat64 attempts to convert a value to float64, handling both numeric and | ||
| // string representations. Some MCP clients send numeric values as strings. | ||
| func toFloat64(val any) (float64, bool) { | ||
| switch v := val.(type) { | ||
| case float64: | ||
| return v, true | ||
| case string: | ||
| f, err := strconv.ParseFloat(v, 64) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| return f, true | ||
| default: | ||
| return 0, false | ||
| } | ||
| } | ||
|
|
||
| // RequiredParam is a helper function that can be used to fetch a requested parameter from the request. | ||
| // It does the following checks: | ||
| // 1. Checks if the parameter is present in the request. | ||
|
|
@@ -68,32 +85,51 @@ func RequiredParam[T comparable](args map[string]any, p string) (T, error) { | |
| // RequiredInt is a helper function that can be used to fetch a requested parameter from the request. | ||
| // It does the following checks: | ||
| // 1. Checks if the parameter is present in the request. | ||
| // 2. Checks if the parameter is of the expected type. | ||
| // 2. Checks if the parameter is of the expected type (float64 or numeric string). | ||
| // 3. Checks if the parameter is not empty, i.e: non-zero value | ||
| func RequiredInt(args map[string]any, p string) (int, error) { | ||
| v, err := RequiredParam[float64](args, p) | ||
| if err != nil { | ||
| return 0, err | ||
| v, ok := args[p] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the most important change is here and in OptionalIntParam |
||
| if !ok { | ||
| return 0, fmt.Errorf("missing required parameter: %s", p) | ||
| } | ||
|
|
||
| f, ok := toFloat64(v) | ||
| if !ok { | ||
| return 0, fmt.Errorf("parameter %s is not a valid number, is %T", p, v) | ||
| } | ||
|
|
||
| if f == 0 { | ||
| return 0, fmt.Errorf("missing required parameter: %s", p) | ||
| } | ||
| return int(v), nil | ||
|
|
||
| return int(f), nil | ||
almaleksia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // RequiredBigInt is a helper function that can be used to fetch a requested parameter from the request. | ||
| // It does the following checks: | ||
| // 1. Checks if the parameter is present in the request. | ||
| // 2. Checks if the parameter is of the expected type (float64). | ||
| // 2. Checks if the parameter is of the expected type (float64 or numeric string). | ||
| // 3. Checks if the parameter is not empty, i.e: non-zero value. | ||
| // 4. Validates that the float64 value can be safely converted to int64 without truncation. | ||
| func RequiredBigInt(args map[string]any, p string) (int64, error) { | ||
| v, err := RequiredParam[float64](args, p) | ||
| if err != nil { | ||
| return 0, err | ||
| val, ok := args[p] | ||
| if !ok { | ||
| return 0, fmt.Errorf("missing required parameter: %s", p) | ||
| } | ||
|
|
||
| f, ok := toFloat64(val) | ||
| if !ok { | ||
| return 0, fmt.Errorf("parameter %s is not a valid number, is %T", p, val) | ||
| } | ||
|
|
||
| if f == 0 { | ||
| return 0, fmt.Errorf("missing required parameter: %s", p) | ||
| } | ||
|
|
||
| result := int64(v) | ||
| result := int64(f) | ||
| // Check if converting back produces the same value to avoid silent truncation | ||
| if float64(result) != v { | ||
| return 0, fmt.Errorf("parameter %s value %f is too large to fit in int64", p, v) | ||
| if float64(result) != f { | ||
| return 0, fmt.Errorf("parameter %s value %f is too large to fit in int64", p, f) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
@@ -121,13 +157,19 @@ func OptionalParam[T any](args map[string]any, p string) (T, error) { | |
| // OptionalIntParam is a helper function that can be used to fetch a requested parameter from the request. | ||
| // It does the following checks: | ||
| // 1. Checks if the parameter is present in the request, if not, it returns its zero-value | ||
| // 2. If it is present, it checks if the parameter is of the expected type and returns it | ||
| // 2. If it is present, it checks if the parameter is of the expected type (float64 or numeric string) and returns it | ||
| func OptionalIntParam(args map[string]any, p string) (int, error) { | ||
| v, err := OptionalParam[float64](args, p) | ||
| if err != nil { | ||
| return 0, err | ||
| val, ok := args[p] | ||
| if !ok { | ||
| return 0, nil | ||
| } | ||
| return int(v), nil | ||
|
|
||
| f, ok := toFloat64(val) | ||
| if !ok { | ||
| return 0, fmt.Errorf("parameter %s is not a valid number, is %T", p, val) | ||
| } | ||
|
|
||
| return int(f), nil | ||
| } | ||
|
|
||
| // OptionalIntParamWithDefault is a helper function that can be used to fetch a requested parameter from the request | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.