-
Notifications
You must be signed in to change notification settings - Fork 817
Register List<object> JSON metadata for Native AOT array tool arguments #1872
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
girishkvs
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
girishkvs:dev/girishkvs/native-aot-list-arguments
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.
Open
Changes from all commits
Commits
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
132 changes: 132 additions & 0 deletions
132
tests/ModelContextProtocol.AotCompatibility.TestApp/OllamaProviderArrayRegression.cs
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,132 @@ | ||
| using System.Net; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.AI; | ||
| using ModelContextProtocol.Client; | ||
|
|
||
| internal sealed class OllamaProviderArrayRegression | ||
| { | ||
| public async Task RunAsync(McpClientTool tool) | ||
| { | ||
| foreach (bool streaming in new[] { false, true }) | ||
| { | ||
| await VerifyAsync(tool, streaming, """["one","two"]""", ["one", "two"]); | ||
| await VerifyAsync(tool, streaming, "[]", []); | ||
| } | ||
| } | ||
|
|
||
| private async Task VerifyAsync(McpClientTool tool, bool streaming, string itemsJson, string[] expectedItems) | ||
| { | ||
| using var handler = new ToolCallResponseHandler(itemsJson, streaming); | ||
| using var http = new HttpClient(handler); | ||
| using var provider = new Ollama.OllamaClient( | ||
| httpClient: http, | ||
| baseUri: new Uri("http://127.0.0.1:11434"), | ||
| disposeHttpClient: false); | ||
| using var client = new ObservingFunctionClient(provider) | ||
| { | ||
| MaximumConsecutiveErrorsPerRequest = 0, | ||
| }; | ||
| var messages = new[] { new ChatMessage(ChatRole.User, "Join the supplied items.") }; | ||
| var options = new ChatOptions { ModelId = "mcp-array-regression", Tools = [tool] }; | ||
| using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
| var expectedResult = $"Joined: {string.Join(',', expectedItems)}"; | ||
| using var json = JsonDocument.Parse(itemsJson); | ||
| var controlResult = await tool.InvokeAsync(new() { ["items"] = json.RootElement.Clone() }, timeout.Token); | ||
| if (controlResult is not TextContent controlText || | ||
| controlText.Text != expectedResult) | ||
| { | ||
| throw new InvalidOperationException("The already-supported JsonElement array control failed."); | ||
| } | ||
|
|
||
| if (streaming) | ||
| { | ||
| await foreach (var _ in client.GetStreamingResponseAsync(messages, options, timeout.Token)) | ||
| { | ||
| } | ||
| } | ||
| else | ||
| { | ||
| await client.GetResponseAsync(messages, options, timeout.Token); | ||
| } | ||
|
|
||
| if (client.InvocationCount != 1 || | ||
| handler.RequestCount != 1 || | ||
| client.Items is null || | ||
| !client.Items.SequenceEqual(expectedItems) || | ||
| client.Result != expectedResult) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Ollama array regression failed: streaming={streaming}, expectedItems={expectedItems.Length}, " + | ||
| $"invocations={client.InvocationCount}, requests={handler.RequestCount}, result={client.Result}."); | ||
| } | ||
|
|
||
| Console.WriteLine($"Ollama provider array passed: streaming={streaming}, count={expectedItems.Length}."); | ||
| } | ||
|
|
||
| private sealed class ObservingFunctionClient(IChatClient provider) : FunctionInvokingChatClient(provider) | ||
| { | ||
| public int InvocationCount { get; private set; } | ||
| public string[]? Items { get; private set; } | ||
| public string? Result { get; private set; } | ||
|
|
||
| protected override async ValueTask<object?> InvokeFunctionAsync( | ||
| FunctionInvocationContext context, CancellationToken cancellationToken) | ||
| { | ||
| InvocationCount++; | ||
| if (!context.Arguments.TryGetValue("items", out var value) || | ||
| value is not List<object> items || | ||
| items.Any(item => item is not string) || | ||
| !ReferenceEquals(value, context.CallContent.Arguments?["items"])) | ||
| { | ||
| throw new InvalidOperationException("The real provider did not supply an unchanged List<object> argument."); | ||
| } | ||
|
|
||
| Items = items.Cast<string>().ToArray(); | ||
| try | ||
| { | ||
| var result = await base.InvokeFunctionAsync(context, cancellationToken); | ||
| Result = (result as TextContent)?.Text; | ||
| return result; | ||
| } | ||
| finally | ||
| { | ||
| // The regression ends at MCP invocation, not the provider's later tool-result formatting. | ||
| context.Terminate = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private sealed class ToolCallResponseHandler(string itemsJson, bool streaming) : HttpMessageHandler | ||
| { | ||
| public int RequestCount { get; private set; } | ||
|
|
||
| protected override async Task<HttpResponseMessage> SendAsync( | ||
| HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| if (request.Content is null || | ||
| request.Method != HttpMethod.Post || | ||
| request.RequestUri?.AbsolutePath != "/api/chat" || | ||
| ++RequestCount != 1) | ||
| { | ||
| throw new InvalidOperationException("Unexpected provider request."); | ||
| } | ||
|
|
||
| using var document = JsonDocument.Parse(await request.Content.ReadAsStringAsync(cancellationToken)); | ||
| if (document.RootElement.GetProperty("model").GetString() != "mcp-array-regression" || | ||
| document.RootElement.GetProperty("stream").GetBoolean() != streaming) | ||
| { | ||
| throw new InvalidOperationException("The provider request did not match the scenario."); | ||
| } | ||
|
|
||
| var response = """ | ||
| {"model":"mcp-array-regression","created_at":"2026-08-28T16:00:00Z","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"Join","arguments":{"items":__ITEMS__}}}]},"done":true,"done_reason":"stop","eval_count":1,"prompt_eval_count":1} | ||
| """.Replace("__ITEMS__", itemsJson, StringComparison.Ordinal); | ||
| return new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent( | ||
| response + "\n", Encoding.UTF8, streaming ? "application/x-ndjson" : "application/json"), | ||
| }; | ||
| } | ||
| } | ||
| } |
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.
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.
Do we know which MEAI provider produces
List<object>here?FunctionInvokingChatClientforwards the provider's arguments unchanged, and MEAI's OpenAI adapter parses JSON arrays asJsonElement, which we already handle. This test constructsList<object>directly, so it doesn't establish that this is a general MEAI array gap. Could we identify the provider and cover its path?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.
Found
tryAGI/Ollama:ConvertJsonValueinOllamaClient.ChatClient.csuses.ToList()for JSON arrays. The same reporter filed tryAGI/Ollama#215 forread_multiple_files. Added real-provider AOT coverage throughFunctionInvokingChatClientand MCP; removing the registration fails the new test, and Release/Debug pass with it.— girishkvs's Copilot 🤖