-
Notifications
You must be signed in to change notification settings - Fork 715
refactor: make CallToolRequest.Arguments more flexible (Breaking Change) #287
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
13 commits
Select commit
Hold shift + click to select a range
a5ac0b3
refactor: make CallToolRequest.Arguments more flexible
ezynda3 27e3a49
refactor: move tests to tools_test.go
ezynda3 57b579d
feat: add BindArguments and helper functions for CallToolRequest
ezynda3 a591489
test: add tests for BindArguments and helper functions
ezynda3 c9d3248
docs: update README with helper function examples
ezynda3 5879189
update tests
ezynda3 0dd5858
feat: add more slice helper functions
ezynda3 984b77e
Update mcp/tools.go
ezynda3 db2fa84
Update mcp/tools.go
ezynda3 41f3ab2
fix tests
ezynda3 a8474c7
implement `NewTypedToolHandlerFunc`
ezynda3 ac5c2ef
Merge branch 'main' into update-arguments-parsing
ezynda3 70e6056
fix
ezynda3 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
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCallToolRequestWithMapArguments(t *testing.T) { | ||
| // Create a request with map arguments | ||
| req := CallToolRequest{} | ||
| req.Params.Name = "test-tool" | ||
| req.Params.Arguments = map[string]any{ | ||
| "key1": "value1", | ||
| "key2": 123, | ||
| } | ||
|
|
||
| // Test GetArguments | ||
| args := req.GetArguments() | ||
| assert.Equal(t, "value1", args["key1"]) | ||
| assert.Equal(t, 123, args["key2"]) | ||
|
|
||
| // Test GetRawArguments | ||
| rawArgs := req.GetRawArguments() | ||
| mapArgs, ok := rawArgs.(map[string]any) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, "value1", mapArgs["key1"]) | ||
| assert.Equal(t, 123, mapArgs["key2"]) | ||
| } | ||
|
|
||
| func TestCallToolRequestWithNonMapArguments(t *testing.T) { | ||
| // Create a request with non-map arguments | ||
| req := CallToolRequest{} | ||
| req.Params.Name = "test-tool" | ||
| req.Params.Arguments = "string-argument" | ||
|
|
||
| // Test GetArguments (should return empty map) | ||
| args := req.GetArguments() | ||
| assert.Empty(t, args) | ||
|
|
||
| // Test GetRawArguments | ||
| rawArgs := req.GetRawArguments() | ||
| strArg, ok := rawArgs.(string) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, "string-argument", strArg) | ||
| } | ||
|
|
||
| func TestCallToolRequestWithStructArguments(t *testing.T) { | ||
| // Create a custom struct | ||
| type CustomArgs struct { | ||
| Field1 string `json:"field1"` | ||
| Field2 int `json:"field2"` | ||
| } | ||
|
|
||
| // Create a request with struct arguments | ||
| req := CallToolRequest{} | ||
| req.Params.Name = "test-tool" | ||
| req.Params.Arguments = CustomArgs{ | ||
| Field1: "test", | ||
| Field2: 42, | ||
| } | ||
|
|
||
| // Test GetArguments (should return empty map) | ||
| args := req.GetArguments() | ||
| assert.Empty(t, args) | ||
|
|
||
| // Test GetRawArguments | ||
| rawArgs := req.GetRawArguments() | ||
| structArg, ok := rawArgs.(CustomArgs) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, "test", structArg.Field1) | ||
| assert.Equal(t, 42, structArg.Field2) | ||
| } | ||
|
|
||
| func TestCallToolRequestJSONMarshalUnmarshal(t *testing.T) { | ||
| // Create a request with map arguments | ||
| req := CallToolRequest{} | ||
| req.Params.Name = "test-tool" | ||
| req.Params.Arguments = map[string]any{ | ||
| "key1": "value1", | ||
| "key2": 123, | ||
| } | ||
|
|
||
| // Marshal to JSON | ||
| data, err := json.Marshal(req) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Unmarshal from JSON | ||
| var unmarshaledReq CallToolRequest | ||
| err = json.Unmarshal(data, &unmarshaledReq) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Check if arguments are correctly unmarshaled | ||
| args := unmarshaledReq.GetArguments() | ||
| assert.Equal(t, "value1", args["key1"]) | ||
| assert.Equal(t, float64(123), args["key2"]) // JSON numbers are unmarshaled as float64 | ||
| } |
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
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.