From f800f6e1d9b69eff2436647e275b29979012947d Mon Sep 17 00:00:00 2001 From: Ashu Date: Thu, 23 Jul 2026 19:52:24 +0530 Subject: [PATCH] fix: return connector tool errors as tool results instead of throwing Thrown tool errors abort the agent run and the model never sees the guidance written for it (e.g. "run openwiki_list_mcp_tools first and use an exact discovered name"). Wrap every connector tool func so errors come back as a `Tool error: ` result the model can self-correct from. Closes langchain-ai/openwiki#427 --- src/connectors/tools.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/connectors/tools.ts b/src/connectors/tools.ts index 07d3a4cc..02ab5bde 100644 --- a/src/connectors/tools.ts +++ b/src/connectors/tools.ts @@ -198,7 +198,30 @@ export function createOpenWikiConnectorTools(): StructuredToolInterface[] { ), ), }), - ]; + ].map(wrapToolErrors); +} + +/** + * Wraps a connector tool's func so thrown errors are returned to the model as + * a `Tool error: ` result instead of propagating. Thrown tool errors + * abort the whole agent run before the model can read the (often + * model-directed) error message and self-correct — e.g. retrying + * openwiki_call_mcp_tool with an exact discovered tool name. + */ +function wrapToolErrors< + T extends { func: (...args: any[]) => Promise }, +>(tool: T): T { + const originalFunc = tool.func; + tool.func = (async (...args: any[]) => { + try { + return await originalFunc(...args); + } catch (error) { + return `Tool error: ${ + error instanceof Error ? error.message : String(error) + }`; + } + }) as T["func"]; + return tool; } async function listConnectors() {