( path: readonly string[], args: readonly unknown[], role?: string, )
| 61 | * parseable and a role containing a quote cannot break out of the call. |
| 62 | */ |
| 63 | export function toolCallCode( |
| 64 | path: readonly string[], |
| 65 | args: readonly unknown[], |
| 66 | role?: string, |
| 67 | ): string { |
| 68 | if (path.length === 0) throw new Error("Invalid tool path."); |
| 69 | const parts = path.map((part) => { |
| 70 | if (typeof part !== "string" || !TOOL_PATH_SEGMENT.test(part)) { |
| 71 | throw new Error("Invalid tool path."); |
| 72 | } |
| 73 | return part; |
| 74 | }); |
| 75 | if (role !== undefined && (typeof role !== "string" || role.length === 0)) { |
| 76 | throw new Error("Invalid tool role."); |
| 77 | } |
| 78 | const head = parts[0]; |
| 79 | if (head === undefined) throw new Error("Invalid tool path."); |
| 80 | const rest = parts.slice(1); |
| 81 | const tag = role === undefined ? "" : `(${JSON.stringify(role)})`; |
| 82 | const target = `${formatToolPathSegment(head)}${tag}${rest.map(formatToolPathSegment).join("")}`; |
| 83 | return `return await tools${target}(${JSON.stringify(args[0] ?? {})})`; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Calls one tool through the MCP Apps bridge, resolving any shell-owned |
no test coverage detected