MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / MCPToolExecutor

Class MCPToolExecutor

src/app/service/agent/core/mcp_tool_executor.ts:6–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5// MCP 工具执行器,将 ToolExecutor 接口桥接到 MCPClient.callTool
6export 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected