| 49 | } |
| 50 | |
| 51 | function extractResourceText(result: unknown): string { |
| 52 | const contents = (result as { contents?: unknown }).contents; |
| 53 | if (!Array.isArray(contents)) { |
| 54 | throw new Error('MCP resource snapshot result did not include a contents array.'); |
| 55 | } |
| 56 | |
| 57 | const contentBlocks: string[] = []; |
| 58 | for (const entry of contents) { |
| 59 | if (!entry || typeof entry !== 'object') { |
| 60 | throw new Error('MCP resource snapshot result contained an invalid content block.'); |
| 61 | } |
| 62 | |
| 63 | const typedEntry = entry as { text?: unknown }; |
| 64 | if (typeof typedEntry.text !== 'string') { |
| 65 | throw new Error('MCP resource snapshot result contained a non-text content block.'); |
| 66 | } |
| 67 | |
| 68 | contentBlocks.push(typedEntry.text); |
| 69 | } |
| 70 | |
| 71 | return `${contentBlocks.join('\n')}\n`; |
| 72 | } |
| 73 | |
| 74 | export async function invokeResource(resourceId: string): Promise<ResourceSnapshotResult> { |
| 75 | const manifest = resolveResourceManifest(resourceId); |