McpContentBlock class
MCPツールのレスポンスコンテンツブロック用の抽象ベースクラス。
Azure Functionsライブラリはinstanceof McpContentBlockを用いてコンテンツブロックと単純なユーザー値を区別するため、組み込みのサブクラス(McpTextContent、McpImageContent、McpAudioContent、McpResourceLinkContent、McpResourceContent)のインスタンス、またはこのクラスを拡張するカスタムサブクラスのみがコンテンツブロックとして扱われます。
{ type: 'text', text: '...' } のようなプレーンオブジェクトリテラルはコンテンツブロックとして扱われず、代わりに JSON テキストとしてシリアライズされます。
カスタムコンテンツブロックタイプでの拡張
もしMCP仕様で新しいブロックタイプが追加されたり、シナリオでカスタムブロックが必要になったりした場合、ライブラリの変更なしで独自のサブクラスを出荷できます。 コンバーターは instanceof McpContentBlock だけをチェックし、 JSON.stringify(block)を呼び出すと、 toJSON() を呼び出してワイヤペイロードを生成します。
例:仮想の VideoContent
import { McpContentBlock } from '@azure/functions';
export interface VideoContentInit {
data: string | Buffer | ArrayBuffer;
mimeType: string; // e.g. 'video/mp4'
durationMs?: number; // optional field from a hypothetical spec
}
export class VideoContent extends McpContentBlock {
readonly type = 'video' as const;
readonly data: string | Buffer | ArrayBuffer;
readonly mimeType: string;
readonly durationMs?: number;
constructor(init: VideoContentInit) {
super();
this.data = init.data;
this.mimeType = init.mimeType;
this.durationMs = init.durationMs;
}
toJSON(): Record<string, unknown> {
const out: Record<string, unknown> = {
type: this.type,
data: toBase64(this.data),
mimeType: this.mimeType,
};
if (this.durationMs !== undefined) out.durationMs = this.durationMs;
return out;
}
}
function toBase64(data: string | Buffer | ArrayBuffer): string {
if (typeof data === 'string') return data;
if (Buffer.isBuffer(data)) return data.toString('base64');
return Buffer.from(new Uint8Array(data)).toString('base64');
}
ツールハンドラーからのカスタムブロックの使用
// Single block
handler: async () => new VideoContent({ data: buf, mimeType: 'video/mp4' })
// Mixed with built-ins + structured content
handler: async () => new McpToolResponse({
content: [
new McpTextContent('Detected 3 scenes'),
new VideoContent({ data: buf, mimeType: 'video/mp4' }),
],
structuredContent: { scenes: 3, confidence: 0.92 },
})
図書館があなたにしてくれること
-
instanceof McpContentBlockサブクラスは内蔵ブロックと同じように扱います。 - シングルブロックの返りは
type文字列を外側の結果に伝播し、配列はmulti_content_resultのように回り込みます。 -
structuredContentハンドリング、フォールバックテキスト合成、およびnullish パススルーはすべて変更なしで適用されます。
自分のサブクラスで必ず身につけなければならないこと
-
toJSON()type仕様で要求されている正確なワイヤー形状を返品する必要があります。 - バイナリペイロードは
toJSON()でbase64エンコードされるべきです(上記のtoBase64ヘルパー参照)。 - プレーンオブジェクトリテラルは認識 されません — インスタンスを構築する必要があります。
プロパティ
| type |
メソッド
| toJSON() |
プロパティの詳細
type
type: string
プロパティ値
string
メソッドの詳細
toJSON()
function toJSON(): Record<string, unknown>
戻り値
Record<string, unknown>