(server: string, uri: string, content: { contents: unknown })
| 524 | } |
| 525 | |
| 526 | function formatMcpResourceContent(server: string, uri: string, content: { contents: unknown }) { |
| 527 | const items = (Array.isArray(content.contents) ? content.contents : [content.contents]).filter(isRecord) |
| 528 | const text: string[] = [] |
| 529 | const attachments: Omit<SessionV1.FilePart, "id" | "sessionID" | "messageID">[] = [] |
| 530 | |
| 531 | for (const item of items) { |
| 532 | const itemUri = typeof item.uri === "string" ? item.uri : uri |
| 533 | const mime = typeof item.mimeType === "string" ? item.mimeType : "application/octet-stream" |
| 534 | if (typeof item.text === "string") { |
| 535 | text.push(`Resource: ${itemUri}\nMIME: ${mime}\n${item.text}`) |
| 536 | continue |
| 537 | } |
| 538 | if (typeof item.blob === "string") { |
| 539 | const size = base64Size(item.blob) |
| 540 | if (!SUPPORTED_MCP_RESOURCE_ATTACHMENT_MIMES.has(mime)) { |
| 541 | text.push( |
| 542 | `[Binary MCP resource omitted: ${itemUri} (${mime}, ${formatBytes(size)}) is not a supported attachment type]`, |
| 543 | ) |
| 544 | continue |
| 545 | } |
| 546 | if (size > MAX_MCP_RESOURCE_BLOB_BYTES) { |
| 547 | text.push( |
| 548 | `[Binary MCP resource omitted: ${itemUri} (${mime}, ${formatBytes(size)}) exceeds ${formatBytes(MAX_MCP_RESOURCE_BLOB_BYTES)}]`, |
| 549 | ) |
| 550 | continue |
| 551 | } |
| 552 | text.push(`[Binary MCP resource attached: ${itemUri} (${mime})]`) |
| 553 | attachments.push({ |
| 554 | type: "file", |
| 555 | mime, |
| 556 | url: `data:${mime};base64,${item.blob}`, |
| 557 | filename: itemUri, |
| 558 | }) |
| 559 | continue |
| 560 | } |
| 561 | text.push(`[MCP resource content without text or blob: ${itemUri}]`) |
| 562 | } |
| 563 | |
| 564 | return { |
| 565 | contents: items.length, |
| 566 | attachments, |
| 567 | text: text.join("\n\n") || `MCP resource ${uri} from ${server} returned no contents.`, |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | function base64Size(value: string) { |
| 572 | const trimmed = value.replace(/\s/g, "") |
no test coverage detected