Support C# union return values in tools, prompts, and resources
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 55/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- csharp
- Ambito
- backend-api-design
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- C#
- Stelle
- 4.6k
- Fork
- 849
- Merge medio
- 1g 16h
- PR unite (30g)
- 24
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di modelcontextprotocol/csharp-sdk
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
modelcontextprotocol/csharp-sdk#2031 ·
I maintainer di solito rispondono entro 1 giorno
-
Static resources whose URIs differ only in the fragment or user info collapse into oneForse già presa @halter73 l’ha presa 9 giorni fa. ApertaP2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xxForse già presa @halter73 l’ha presa 9 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
modelcontextprotocol/csharp-sdk#1966 · 5 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Reserved URI-template expansion throws for supplementary Unicode charactersForse già presa @tarekgh l’ha presa 2 giorni fa. ApertaP3
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
modelcontextprotocol/csharp-sdk#1913 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
ClaimsPrincipal parameter injection sample requestForse già presa @utsavjain2809 l’ha presa 48 giorni fa. Apertahelp wanted P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
modelcontextprotocol/csharp-sdk#1899 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di modelcontextprotocol/csharp-sdk
Issue simili
-
account bug effort:S P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
nightscout/nocturne#3745 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
jsboige/CoursIA#27768 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
martin-stromberg/FinanceManager#562 ·
I maintainer di solito rispondono entro 1 giorno
-
:eyeglasses: status: needs review needs-area-label
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
gitextensions/gitextensions#13469 ·
I maintainer di solito rispondono entro 2 giorni