| 4 | |
| 5 | // MCP 工具执行器,将 ToolExecutor 接口桥接到 MCPClient.callTool |
| 6 | export class MCPToolExecutor implements ToolExecutor { |
| 7 | constructor( |
| 8 | private client: MCPClient, |
| 9 | private toolName: string |
| 10 | ) {} |
| 11 | |
| 12 | async execute(args: Record<string, unknown>): Promise<unknown> { |
| 13 | const result = await this.client.callTool(this.toolName, args); |
| 14 | |
| 15 | // 检测 MCP 返回的 content 数组是否包含 image 类型 |
| 16 | if (Array.isArray(result)) { |
| 17 | const textParts: string[] = []; |
| 18 | const attachments: ToolResultWithAttachments["attachments"] = []; |
| 19 | |
| 20 | for (const item of result) { |
| 21 | if (item.type === "text" && item.text) { |
| 22 | textParts.push(item.text); |
| 23 | } else if (item.type === "image" && item.data) { |
| 24 | attachments.push({ |
| 25 | type: "image", |
| 26 | name: "image." + (item.mimeType?.split("/")[1] || "png"), |
| 27 | mimeType: item.mimeType || "image/png", |
| 28 | data: `data:${item.mimeType || "image/png"};base64,${item.data}`, |
| 29 | }); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if (attachments.length > 0) { |
| 34 | return { |
| 35 | content: textParts.join("\n") || "Tool completed.", |
| 36 | attachments, |
| 37 | } as ToolResultWithAttachments; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return result; |
| 42 | } |
| 43 | } |
nothing calls this directly
no outgoing calls
no test coverage detected