From 044ce9342f746b5fa30d3356193055f6bdfd6ab5 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Fri, 3 Apr 2026 04:35:24 +0000
Subject: [PATCH 1/3] Initial plan
From d53360de747093b675bca8e9d63b78a1b868e77a Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Fri, 3 Apr 2026 05:05:48 +0000
Subject: [PATCH 2/3] Fix McpTask double-wrapping: add McpTask case in
InvokeAsync switch and bypass ExecuteToolAsTaskAsync for McpTask-returning
tools
Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/0b57604e-d75a-4879-a7ed-139c8e03ec6b
Co-authored-by: jeffhandley <1031940+jeffhandley@users.noreply.github.com>
---
.../Server/AIFunctionMcpServerTool.cs | 24 +++++
.../Server/DelegatingMcpServerTool.cs | 3 +
.../Server/McpServerImpl.cs | 7 ++
.../Server/McpServerTool.cs | 4 +
.../Server/ToolTaskSupportTests.cs | 101 +++++++++++++++++-
5 files changed, 138 insertions(+), 1 deletion(-)
diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs
index 700d9d26d..a6e1719f2 100644
--- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs
+++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs
@@ -244,11 +244,30 @@ private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider
_structuredOutputRequiresWrapping = structuredOutputRequiresWrapping;
_metadata = metadata;
+
+ // Detect if the tool's underlying method returns McpTask (directly or wrapped in Task<>/ValueTask<>).
+ if (function.UnderlyingMethod is { } method)
+ {
+ Type returnType = method.ReturnType;
+ if (returnType.IsGenericType)
+ {
+ Type gt = returnType.GetGenericTypeDefinition();
+ if (gt == typeof(Task<>) || gt == typeof(ValueTask<>))
+ {
+ returnType = returnType.GetGenericArguments()[0];
+ }
+ }
+
+ ReturnsMcpTask = returnType == typeof(McpTask);
+ }
}
///
public override Tool ProtocolTool { get; }
+ ///
+ internal override bool ReturnsMcpTask { get; }
+
///
public override IReadOnlyList