McpContentBlock class
MCP 工具响应内容块的抽象基类。
Azure Functions 库使用 instanceof McpContentBlock,将内容块与纯用户值区分开来,因此只有内置子类(McpTextContent、McpResourceLinkContentMcpImageContentMcpAudioContentMcpResourceContent)的实例——或扩展该类的自定义子类——才会被视为内容块。
像这样的 { 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的完全相同线形。 - 二进制载荷应以base64编码(
toJSON()见上文辅助工具)。toBase64 - 纯对象文字 无法 识别——您必须构造一个实例。
属性
| type |
方法
| toJSON() |
属性详细信息
type
type: string
属性值
string
方法详细信息
toJSON()
function toJSON(): Record<string, unknown>
返回
Record<string, unknown>