( task: Task, params: UseMcpToolParams, pushToolResult: (content: string) => void, )
| 108 | } |
| 109 | |
| 110 | private async validateParams( |
| 111 | task: Task, |
| 112 | params: UseMcpToolParams, |
| 113 | pushToolResult: (content: string) => void, |
| 114 | ): Promise<ValidationResult> { |
| 115 | if (!params.server_name) { |
| 116 | task.consecutiveMistakeCount++ |
| 117 | task.recordToolError("use_mcp_tool") |
| 118 | pushToolResult(await task.sayAndCreateMissingParamError("use_mcp_tool", "server_name")) |
| 119 | return { isValid: false } |
| 120 | } |
| 121 | |
| 122 | if (!params.tool_name) { |
| 123 | task.consecutiveMistakeCount++ |
| 124 | task.recordToolError("use_mcp_tool") |
| 125 | pushToolResult(await task.sayAndCreateMissingParamError("use_mcp_tool", "tool_name")) |
| 126 | return { isValid: false } |
| 127 | } |
| 128 | |
| 129 | // Some LLMs emit arguments as JSON-encoded strings rather than objects. |
| 130 | // Parse them early so the type check below sees the unwrapped object. |
| 131 | if (typeof params.arguments === "string") { |
| 132 | try { |
| 133 | params.arguments = JSON.parse(params.arguments) |
| 134 | } catch {} |
| 135 | } |
| 136 | |
| 137 | let parsedArguments: Record<string, unknown> | undefined |
| 138 | if (params.arguments !== undefined) { |
| 139 | if (typeof params.arguments !== "object" || params.arguments === null || Array.isArray(params.arguments)) { |
| 140 | task.consecutiveMistakeCount++ |
| 141 | task.recordToolError("use_mcp_tool") |
| 142 | await task.say("error", t("mcp:errors.invalidJsonArgument", { toolName: params.tool_name })) |
| 143 | task.didToolFailInCurrentTurn = true |
| 144 | pushToolResult( |
| 145 | formatResponse.toolError( |
| 146 | formatResponse.invalidMcpToolArgumentError(params.server_name, params.tool_name), |
| 147 | ), |
| 148 | ) |
| 149 | return { isValid: false } |
| 150 | } |
| 151 | parsedArguments = params.arguments |
| 152 | } |
| 153 | |
| 154 | return { |
| 155 | isValid: true, |
| 156 | serverName: params.server_name, |
| 157 | toolName: params.tool_name, |
| 158 | parsedArguments, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private async validateToolExists( |
| 163 | task: Task, |
no test coverage detected