mastra-ai/mastra · error

Invalid input for tool "${toolId}"

Error message

Invalid input for tool "${toolId}"

What it means

After the dispatcher re-enters the real Mastra tool pipeline, a validation-failure result (isValidationError) from the tool's inputSchema check is converted into a thrown error. It means the arguments the model-authored code passed to external_<tool> did not match the tool's input schema.

Source

Thrown at packages/core/src/tools/code-mode/code-mode.ts:116

          );
        }
      }

      // Each external_* call re-enters the real Mastra tool pipeline (validation,
      // request-context checks, tracing) on the host, with the outer tool's context.
      const dispatch: CodeModeToolDispatcher = async (toolId, args) => {
        const tool = toolsById.get(toolId);
        if (!tool?.execute) {
          throw new Error(`Tool "${toolId}" is not available in Code Mode`);
        }
        const result = await tool.execute(args, {
          mastra: ctx?.mastra,
          requestContext: ctx?.requestContext,
          abortSignal: ctx?.abortSignal,
          workspace: ctx?.workspace,
        });
        if (isValidationError(result)) {
          throw new Error(result.message ?? `Invalid input for tool "${toolId}"`);
        }
        return result;
      };

      // The TypeScript program is written to a .ts module by the transport;
      // the sandbox's node strips the type annotations natively at import.
      return ctx.observe.span(`code-mode:${id}`, () =>
        transport.run({
          sandbox,
          program: code,
          toolIds,
          dispatch,
          timeout,
          abortSignal: ctx?.abortSignal,
          onExternalCall: (tool, args) => ctx.observe.log('info', 'code-mode external call', { tool, args }),
          onExternalResult: (tool, durationMs, error) =>
            ctx.observe.log(error ? 'error' : 'info', 'code-mode external result', { tool, durationMs }),
        }),

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Inspect result.message (the thrown message when non-empty) to see the schema violation and fix the generated call's arguments.
  2. Improve tool descriptions and input schema descriptions so the model emits correct argument shapes.
  3. Validate/parse arguments in generated code before calling external_* helpers.
  4. Check for recent changes to the tool's inputSchema that the model prompts/stubs no longer reflect.

Example fix

// before (generated code)
await external_weather('london');
// after
await external_weather({ location: 'london' });
Defensive patterns

Strategy: try-catch

Try / catch

try {
  result = await codeModeTool.execute(args, ctx);
} catch (e) {
  if (String(e.message).startsWith('Invalid input for tool')) {
    // feed e.message back to the model to correct the generated arguments
  } else throw e;
}

Prevention

When it happens

Trigger: Generated TypeScript calls external_<toolId> with arguments that fail the tool's Zod/inputSchema validation, e.g. missing required fields, wrong types, or wrong shapes.

Common situations: The LLM writes code with guessed argument shapes; schema drift after editing a tool's inputSchema; passing stringified JSON instead of an object; optional vs required field confusion.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/7499c0d0bb558070. Report an issue: GitHub.