Image/audio content in SamplingMessage and CreateMessageResult is base64-encoded twice

Đang mở Phù hợp với người mới
#2,033 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

@atailakh đang làm issue này rồi.

Từ ngày 22/11/2026.

  • #2034 của @atailakh — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
68/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
api

Hướng nghiên cứu

Tái hiện bằng đoạn mã trong issue: serialize một ImageContentBlock độc lập và cùng khối đó bên trong một SamplingMessage, sau đó so sánh trường data. Nguyên nhân nằm ở SingleItemOrListConverter<T>.Write, đoạn này phân giải options.GetTypeInfo(typeof(object)) và chọn contract mặc định dẫn xuất thay vì ContentBlock.Converter; hãy làm cho nó tuân theo converter đã khai báo cho SamplingMessage.Content và CreateMessageResult.Content. Hoàn tất nghĩa là cả hai đều phát ra iVBORw0KGgo=, với một test hồi quy cho khối hình ảnh và âm thanh thông qua sampling.

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

Mô tả

Describe the bug

Image (and audio) content in sampling messages is base64-encoded twice on the wire.

SingleItemOrListConverter<T>.Write serializes each item with options.GetTypeInfo(typeof(object)). For a ContentBlock list this resolves the derived type's default contract (e.g. ImageContentBlock) instead of ContentBlock.Converter. The default contract serializes ReadOnlyMemory<byte> Data (which already holds base64 UTF-8 bytes) as base64 again.

Affected: SamplingMessage.Content and CreateMessageResult.Content. In practice, McpServer.SampleAsync / AsSamplingChatClient() with a DataContent image sends an image the client cannot use: after decoding, the client gets the base64 text instead of the image bytes, and model providers reject it (e.g. OpenAI-compatible APIs return 400 invalid image data).

To Reproduce

byte[] png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
var block = ImageContentBlock.FromBytes(png, "image/png");

JsonSerializer.Serialize<ContentBlock>(block, McpJsonUtilities.DefaultOptions);
// "data":"iVBORw0KGgo="          <- correct

JsonSerializer.Serialize(new SamplingMessage { Role = Role.User, Content = [block] }, McpJsonUtilities.DefaultOptions);
// "data":"aVZCT1J3MEtHZ289"      <- base64 of the base64

Expected behavior

Content blocks inside SamplingMessage / CreateMessageResult serialize the same way as a standalone ContentBlock (data encoded once).

Version

ModelContextProtocol.Core 2.4.0 (also reproduces on main).

Ngôn ngữ chính
C#
Star
4.6k
Fork
855
Merge trung bình
15 giờ 29 phút
Pull request đã merge (30 ngày)
8

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.