-
Notifications
You must be signed in to change notification settings - Fork 334
mcp: validate tool names #640
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
+168
−0
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| ) | ||
|
|
@@ -101,3 +102,38 @@ func applySchema(data json.RawMessage, resolved *jsonschema.Resolved) (json.RawM | |
| } | ||
| return data, nil | ||
| } | ||
|
|
||
| // validateToolName checks whether name is a valid tool name, reporting a | ||
| // non-nil error if not. | ||
| func validateToolName(name string) error { | ||
| if name == "" { | ||
| return fmt.Errorf("tool name cannot be empty") | ||
| } | ||
| if len(name) > 128 { | ||
| return fmt.Errorf("tool name exceeds maximum length of 128 characters (current: %d)", len(name)) | ||
| } | ||
| // For consistency with other SDKs, report characters in the order the appear | ||
findleyr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // in the name. | ||
| var invalidChars []string | ||
| seen := make(map[rune]bool) | ||
| for _, r := range name { | ||
| if !validToolNameRune(r) { | ||
| if !seen[r] { | ||
| invalidChars = append(invalidChars, fmt.Sprintf("%q", string(r))) | ||
|
Contributor
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. Use maps.Keys(seen) at the end.
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. Can't: need to report characters in the order they occur. Want to be consistent with other SDKs. |
||
| seen[r] = true | ||
| } | ||
| } | ||
| } | ||
| if len(invalidChars) > 0 { | ||
| return fmt.Errorf("tool name contains invalid characters: %s", strings.Join(invalidChars, ", ")) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // validToolNameRune reports whether r is valid within tool names. | ||
| func validToolNameRune(r rune) bool { | ||
| return (r >= 'a' && r <= 'z') || | ||
| (r >= 'A' && r <= 'Z') || | ||
| (r >= '0' && r <= '9') || | ||
| r == '_' || r == '-' || r == '.' | ||
| } | ||
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
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.
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.
feel free to ignore: should we include the tool name here to make it easier for debugging? i can also see this being a problem since it is too long. i could see this error message being annoying if the user could have multiple tool names and they don't know which one is too long
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.
The tool name is included at the call site.
An arguable style choice, but I mildly prefer having validators only describe the specific error, because this then leave it up to the caller for how to present that error. For example the caller could do "Adding tool %q: %v"