McpContentBlock class
فئة أساسية مجردة لكتل محتوى استجابة أداة MCP.
تميز مكتبة دالات Azure بين كتل المحتوى وقيم المستخدم العادية باستخدام instanceof McpContentBlock، لذا سيتم التعامل فقط مع نسخ الفئات الفرعية المدمجة (McpTextContent، McpImageContent، McpAudioContent، McpResourceLinkContentMcpResourceContent، ) — أو الفئة الفرعية المخصصة التي توسع هذه الفئة — ككتل محتوى.
الأشكال الحرفية للكائنات العادية مثل { 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المعالجة، وتركيب النص الاحتياطي، والتمرير الفارغ كلها تنطبق دون تغيير.
ما يجب أن تحصل عليه بشكل صحيح في فئتك الفرعية
-
toJSON()يجب أن تعيد شكل السلك الدقيق الذي تتطلبه المواصفات لtype. - يجب ترميز الحمولات الثنائية بنظام base64 (
toJSON()انظر المساعدtoBase64أعلاه). - لا يتم التعرف على الحروف البسيطة — يجب عليك إنشاء مثيل.
الخصائص
| type |
الأساليب
| toJSON() |
تفاصيل الخاصية
type
type: string
قيمة الخاصية
string
تفاصيل الأسلوب
toJSON()
function toJSON(): Record<string, unknown>
المرتجعات
Record<string, unknown>