Skip to content

Fix WithMeta + WithProgress causing tool invocation failure#1464

Merged
stephentoub merged 3 commits intomainfrom
copilot/fix-invocation-failure-mcpclienttool
Mar 27, 2026
Merged

Fix WithMeta + WithProgress causing tool invocation failure#1464
stephentoub merged 3 commits intomainfrom
copilot/fix-invocation-failure-mcpclienttool

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

new JsonObject(meta) reparents child nodes from the source, throwing InvalidOperationException ("The node already has a parent") when the original meta object is still live. This breaks any McpClientTool usage combining WithMeta and WithProgress.

Fix

Replace new JsonObject(meta) with (JsonObject)meta.DeepClone() in both SendRequestWithProgressAsync and SendRequestWithProgressAsTaskAsync—consistent with how RequestOptions.GetMetaForRequest() already handles this.

- JsonObject metaWithProgress = meta is not null ? new(meta) : [];
+ JsonObject metaWithProgress = meta is not null ? (JsonObject)meta.DeepClone() : [];

Tests

Six new tests covering:

  • CallAsync with progress token in meta
  • WithMeta + progress via both CallAsync and InvokeAsync (AIFunction) paths
  • Original meta not mutated across repeated calls with progress
  • All With* methods chained together with progress
  • Three-way merge of tool meta, progress token, and RequestOptions.Meta

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…nstructor

Replace `new JsonObject(meta)` with `(JsonObject)meta.DeepClone()` in both
SendRequestWithProgressAsync and SendRequestWithProgressAsTaskAsync to prevent
InvalidOperationException when JsonNode values already have a parent.

Add tests covering progress with meta, InvokeAsync path, mutation safety,
chaining all With* methods, and merging RequestOptions meta with progress.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/d4ec72e6-6d91-429a-92ae-84f8eaa41bbc
Copilot AI changed the title [WIP] Fix tool invocation failure with WithMeta and WithProgress Fix WithMeta + WithProgress causing tool invocation failure Mar 25, 2026
Copilot AI requested a review from stephentoub March 25, 2026 13:00
@stephentoub stephentoub marked this pull request as ready for review March 25, 2026 13:06
Use [Theory] with useInvokeAsync parameter and a shared helper method
to test both CallAsync and InvokeAsync paths without code duplication.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/9ab96e2e-534b-4f1d-a047-5cb7f49cd078
@stephentoub stephentoub merged commit 49bf170 into main Mar 27, 2026
8 of 10 checks passed
@stephentoub stephentoub deleted the copilot/fix-invocation-failure-mcpclienttool branch March 27, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

McpClientTool WithMeta and WithProgress cause tool invokation to fail

3 participants