Support C# union return values in tools, prompts, and resources

Đang mở
#2,015 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

P3

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.

Ngôn ngữ chính
C#
Star
4.6k
Fork
849
Merge trung bình
1 ngày 16 giờ
Pull request đã merge (30 ngày)
24

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/csharp-sdk

Tất cả issue của modelcontextprotocol/csharp-sdk

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.