(params: any)
| 166 | const parameters = extractParametersFromSchema(tool); |
| 167 | |
| 168 | const handler = async (params: any): Promise<any> => { |
| 169 | try { |
| 170 | const result = await tool.execute(params); |
| 171 | // Ensure the result is a string or stringify it, as required by many LLMs. |
| 172 | // This might need adjustment depending on how different LLMs handle tool results. |
| 173 | return typeof result === "string" ? result : JSON.stringify(result); |
| 174 | } catch (error) { |
| 175 | console.error( |
| 176 | `Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`, |
| 177 | error, |
| 178 | ); |
| 179 | // Re-throw or format the error for the LLM |
| 180 | throw new Error( |
| 181 | `Execution failed for MCP tool '${toolName}': ${ |
| 182 | error instanceof Error ? error.message : String(error) |
| 183 | }`, { cause: error }, |
| 184 | ); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | actions.push({ |
| 189 | name: toolName, |
searching dependent graphs…