-
Notifications
You must be signed in to change notification settings - Fork 441
Adding an overload of McpClientExtensions::CallToolAsync taking JsonElement tool argument #641
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
Open
anuchandy
wants to merge
5
commits into
modelcontextprotocol:main
Choose a base branch
from
anuchandy:Dictionary_string_JsonElement_call_tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcd2be5
Dictionary_string_JsonElement overload for CallToolAsync
anuchandy 5820adb
Adding tests for CallToolAsync taking Dictionary of JsonElement
anuchandy abd8104
updating the CallToolAsync to take JsonElement
anuchandy f7fd3ec
review feedback: add validation check for non-object JsonElement
anuchandy c0d54f8
Merge branch 'main' into Dictionary_string_JsonElement_call_tool
anuchandy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,56 @@ public async Task CallTool_Stdio_EchoServer(string clientId) | |
Assert.Equal("Echo: Hello MCP!", textContent.Text); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetClients))] | ||
public async Task CallTool_Stdio_EchoServer_WithJsonElementArguments(string clientId) | ||
{ | ||
// arrange | ||
JsonElement arguments = JsonDocument.Parse(""" | ||
{ | ||
"message": "Hello MCP with JsonElement!" | ||
} | ||
""").RootElement; | ||
|
||
// act | ||
await using var client = await _fixture.CreateClientAsync(clientId); | ||
var result = await client.CallToolAsync( | ||
"echo", | ||
arguments, | ||
cancellationToken: TestContext.Current.CancellationToken | ||
); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.Null(result.IsError); | ||
var textContent = Assert.Single(result.Content.OfType<TextContentBlock>()); | ||
Assert.Equal("Echo: Hello MCP with JsonElement!", textContent.Text); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetClients))] | ||
public async Task CallTool_Stdio_EchoServer_WithJsonElementArguments_ThrowsForNonObject(string clientId) | ||
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. For completeness, you could try turning this into a theory that accepts all types of invalid JSON. |
||
{ | ||
// arrange - JsonElement representing a string, not an object | ||
JsonElement stringArguments = JsonDocument.Parse(""" | ||
"Hello MCP!" | ||
""").RootElement; | ||
|
||
// act & assert | ||
await using var client = await _fixture.CreateClientAsync(clientId); | ||
var exception = await Assert.ThrowsAsync<ArgumentException>(async () => | ||
await client.CallToolAsync( | ||
"echo", | ||
stringArguments, | ||
cancellationToken: TestContext.Current.CancellationToken | ||
) | ||
); | ||
|
||
Assert.Contains("arguments parameter must represent a JSON object", exception.Message); | ||
Assert.Contains("String", exception.Message); | ||
Assert.Equal("arguments", exception.ParamName); | ||
} | ||
|
||
[Fact] | ||
public async Task CallTool_Stdio_EchoSessionId_ReturnsEmpty() | ||
{ | ||
|
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.
This will throw an exception if the JsonElement isn't representing an object. Perhaps it makes sense to add a check at the start of the method so that a bespoke error message can be raised?
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.
Great point, Just updated the pr with the check as suggested.
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.
What if it's a no-parameter tool? Would
JsonValueKind.Undefined
be an acceptable value then, or must it be a property-less JsonElement object? I would thinkdefault(JsonElement)
would an intuitive choice for someone who is passing no arguments, and that's undefined afair?Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, good point. Maybe we should make the parameter nullable for that reason, needing to create a
null
JsonElement or empty object seems somewhat overbearing.