Skip to content

fix: return connector tool errors as tool results instead of throwing - #451

Open
Ashu (ashudhanda) wants to merge 1 commit into
langchain-ai:mainfrom
ashudhanda:fix-connector-tool-errors
Open

fix: return connector tool errors as tool results instead of throwing#451
Ashu (ashudhanda) wants to merge 1 commit into
langchain-ai:mainfrom
ashudhanda:fix-connector-tool-errors

Conversation

@ashudhanda

Copy link
Copy Markdown
Contributor

Summary

Closes #427

Every func in createOpenWikiConnectorTools() currently lets errors propagate. A thrown tool error aborts the whole agent run, so the model never sees the error messages that were written for it — e.g. the openwiki_call_mcp_tool guidance to run openwiki_list_mcp_tools first and retry with an exact discovered tool name. A single hallucinated tool name or one expired connector token kills the entire wiki-build instead of letting the agent self-correct or continue with other connectors.

Change

Added a small wrapToolErrors helper in src/connectors/tools.ts and applied it to all seven connector tools via .map(wrapToolErrors) on the returned array, following the approach suggested (and verified with a local patch) in the issue:

function wrapToolErrors<
  T extends { func: (...args: any[]) => Promise<unknown> },
>(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;
}

Behavior

  • Tool failures now return Tool error: <message> as the tool result, matching how LangChain's own ToolNode reports errors, so the agent can retry with corrected input or move on
  • Bad tool name → model retries after discovery; expired token → model skips that connector and continues; the run completes with partial results instead of crashing
  • No behavior change on the success path; internal helper throws (input validation, MCP-backed checks) are preserved and simply surface through the wrapper

Kept the scope minimal per the issue; making ingestAllConnectors collect per-connector failures individually could be a follow-up.

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: <message>` result the model can
self-correct from.

Closes langchain-ai#427
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connector tools throw instead of returning errors as tool results, so the model can't self-correct (e.g. wrong MCP tool name)

1 participant