Support C# union return values in tools, prompts, and resources
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 55/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- csharp
- Bereich
- backend-api-design
Rechercherichtung
Read the result switches in AIFunctionMcpServerTool.cs, AIFunctionMcpServerPrompt.cs, and AIFunctionMcpServerResource.cs, then trace how their current cases handle direct return values. Check how compiler generated unions expose their active case and account for older target frameworks and Native AOT. Done means union cases behave like their directly returned values across tools, prompts, and resources while preserving output schemas and older protocol compatibility.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
When a tool, prompt, or resource method returns a C# union, its active case is not handled like the same value returned directly. The SDK's result conversion sees the boxed union rather than the active value.
Reproduction
Run this with the .NET 11 SDK, targeting net11.0 with LangVersion set to preview:
using System;
using System.IO;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
await using var server = McpServer.Create(
new StreamServerTransport(Stream.Null, Stream.Null),
new McpServerOptions());
var tool = McpServerTool.Create(
() => (TextOrImage)"hello",
new() { Name = "example" });
var request = new RequestContext<CallToolRequestParams>(
server,
new JsonRpcRequest { Method = "tools/call" },
new CallToolRequestParams { Name = "example" });
var result = await tool.InvokeAsync(request);
Console.WriteLine(((TextContentBlock)result.Content[0]).Text);
public union TextOrImage(string, ImageContentBlock);
The text contains "hello", including the quote characters. Returning "hello" directly produces hello without those characters. An ImageContentBlock case is similarly serialized into a text block instead of being returned as image content. A CallToolResult case is serialized into text rather than passed through, so properties such as IsError are not propagated as result properties.
Prompts and resources have the same underlying issue, but throw instead of falling back to JSON serialization. For example, a prompt returning the string case of union TextOrPrompt(string, PromptMessage) throws InvalidOperationException: Unknown result type 'TextOrPrompt' returned from prompt function. A resource returning the string case of union TextOrResource(string, ResourceContents) throws InvalidOperationException: Unsupported result type 'TextOrResource' returned from resource function.
Expected behavior
Handle a union's active case as the corresponding directly returned value for tools, prompts, and resources. Preserve the existing handling of ordinary return values, structured content and output schemas, including compatibility with older protocol versions.
Investigation
The result switches in AIFunctionMcpServerTool, AIFunctionMcpServerPrompt, and AIFunctionMcpServerResource match the boxed union rather than its active case. Compiler-generated unions implement System.Runtime.CompilerServices.IUnion on .NET 11; the SDK also targets older frameworks, so compatibility and Native AOT need consideration.
Reproduced against the SDK at commit 64cfd66b147553d8cf953f9e2a374aae96014968, using its net10.0 Core build in a .NET 11 application.
[!NOTE]
This issue was drafted with GitHub Copilot.
- Vorherrschende Sprache
- C#
- Sterne
- 4.6k
- Forks
- 849
- Ø Merge
- 1 T. 16 Std.
- Gemergte PRs (30 T.)
- 24
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/csharp-sdk
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
modelcontextprotocol/csharp-sdk#2031 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Static resources whose URIs differ only in the fragment or user info collapse into oneEvtl. vergeben @halter73 hat das vor 9 Tagen übernommen. OffenP2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xxEvtl. vergeben @halter73 hat das vor 9 Tagen übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
modelcontextprotocol/csharp-sdk#1966 · 5 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Reserved URI-template expansion throws for supplementary Unicode charactersEvtl. vergeben @tarekgh hat das vor 2 Tagen übernommen. OffenP3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
modelcontextprotocol/csharp-sdk#1913 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
ClaimsPrincipal parameter injection sample requestEvtl. vergeben @utsavjain2809 hat das vor 48 Tagen übernommen. Offenhelp wanted P3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
modelcontextprotocol/csharp-sdk#1899 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/csharp-sdk
Ähnliche Issues
-
account bug effort:S P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
nightscout/nocturne#3745 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
grate-devs/grate#969 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
jsboige/CoursIA#27768 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 63/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
martin-stromberg/FinanceManager#562 ·
Maintainer antworten meist innerhalb von 1 Tag