* Processes the raw response from the MCP tool to generate a clean, * human-readable string for display in the CLI. It summarizes non-text * content and presents text directly. * * @param rawResponse The raw Part[] array from the GenAI SDK. * @returns A formatted string representing the tool's
(rawResponse: Part[])
| 309 | * @returns A formatted string representing the tool's output. |
| 310 | */ |
| 311 | function getStringifiedResultForDisplay(rawResponse: Part[]): string { |
| 312 | const mcpContent = rawResponse?.[0]?.functionResponse?.response?.[ |
| 313 | 'content' |
| 314 | ] as McpContentBlock[]; |
| 315 | |
| 316 | if (!Array.isArray(mcpContent)) { |
| 317 | return '```json\n' + JSON.stringify(rawResponse, null, 2) + '\n```'; |
| 318 | } |
| 319 | |
| 320 | const displayParts = mcpContent.map((block: McpContentBlock): string => { |
| 321 | switch (block.type) { |
| 322 | case 'text': |
| 323 | return block.text; |
| 324 | case 'image': |
| 325 | return `[Image: ${block.mimeType}]`; |
| 326 | case 'audio': |
| 327 | return `[Audio: ${block.mimeType}]`; |
| 328 | case 'resource_link': |
| 329 | return `[Link to ${block.title || block.name}: ${block.uri}]`; |
| 330 | case 'resource': |
| 331 | if (block.resource?.text) { |
| 332 | return block.resource.text; |
| 333 | } |
| 334 | return `[Embedded Resource: ${ |
| 335 | block.resource?.mimeType || 'unknown type' |
| 336 | }]`; |
| 337 | default: |
| 338 | return `[Unknown content type: ${(block as { type: string }).type}]`; |
| 339 | } |
| 340 | }); |
| 341 | |
| 342 | return displayParts.join('\n'); |
| 343 | } |
| 344 | |
| 345 | /** Visible for testing */ |
| 346 | export function generateValidName(name: string) { |