McpContentBlock class
Abstracte basisklasse voor MCP-toolresponsinhoudsblokken.
De Azure Functions-bibliotheek onderscheidt inhoudsblokken van gewone gebruikerswaarden met behulp van instanceof McpContentBlock, zodat alleen instanties van de ingebouwde subklassen (McpTextContent, McpImageContent, McpAudioContent, McpResourceLinkContent, McpResourceContent) — of een aangepaste subklasse die deze klasse uitbreidt — als inhoudsblokken worden behandeld.
Gewone objectliterals zoals { type: 'text', text: '...' } worden niet als inhoudsblokken behandeld en worden in plaats daarvan als JSON-tekst geserialiseerd.
Uitbreiden met aangepaste inhoudsbloktypes
Als de MCP-specificatie een nieuw bloktype toevoegt — of als je scenario een aangepast bloktype nodig heeft — kun je je eigen subklasse verzenden zonder enige bibliotheekwijziging. De converter controleert instanceof McpContentBlock en belt JSON.stringify(block)alleen , waarna je toJSON() wordt ingeschakeld om de draadlading te produceren.
Voorbeeld: een hypothetisch toevoegsel 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');
}
Een aangepast blok gebruiken vanuit een gereedschapshandler
// 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 },
})
Wat de bibliotheek voor je doet
-
instanceof McpContentBlockbehandelt je subklasse identiek als ingebouwde blokken. - Single-block returns verspreiden uw
typestring naar het buitenste resultaat; arrays worden omwikkeld alsmulti_content_result. -
structuredContentHandling, fallback-text synthese en nullish passthrough zijn allemaal ongewijzigd van toepassing.
Wat je goed moet doen in je subklasse
-
toJSON()moet exact de draadvorm teruggeven die de specificatie vereist voor uwtype. - Binaire payloads moeten base64-gecodeerd zijn in
toJSON()(zie detoBase64helper hierboven). - Gewone objectliteralen worden niet herkend — u moet een instantie construeren.
Eigenschappen
| type |
Methoden
| toJSON() |
Eigenschapdetails
type
type: string
Waarde van eigenschap
string
Methodedetails
toJSON()
function toJSON(): Record<string, unknown>
Retouren
Record<string, unknown>