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

オープン 初心者向け
#2,033 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@atailakh がすでに取り組んでいます。

2026年11月22日 から。

  • #2034 @atailakh による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
api

調査の方向性

issue のスニペットで再現する: 単独の ImageContentBlock と、SamplingMessage 内の同じブロックをシリアル化し、data フィールドを比較する。原因は SingleItemOrListConverter<T>.Write にあり、これは options.GetTypeInfo(typeof(object)) を解決し、ContentBlock.Converter ではなく派生した既定のコントラクトを選択する。SamplingMessage.Content と CreateMessageResult.Content に対して宣言されたコンバーターを尊重するようにする。完了とは、両方が iVBORw0KGgo= を出力し、sampling 経由の画像ブロックと音声ブロックに対する回帰テストがあることを意味する。

索引モデルが issue の本文から書いたものです。

説明

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).

主要言語
C#
スター
4.7k
フォーク
863
平均マージ
15時間 29分
マージ済み PR(30日)
8

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/csharp-sdk のほかの issue

modelcontextprotocol/csharp-sdk の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。