{"record":{"id":"a524a800bbae9770","repo":"mastra-ai/mastra","slug":"list-available-tools-requires-a-mastra-context","errorCode":null,"errorMessage":"list-available-tools requires a Mastra context.","messagePattern":"list-available-tools requires a Mastra context\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/tools/workflows/list-available-tools.ts","lineNumber":30,"sourceCode":"const WORKFLOW_BUILDER_NOISE_TOOLS = new Set<string>(WORKFLOW_BUILDER_NOISE_TOOL_IDS);\n\nexport const listAvailableToolsTool = createTool({\n  id: 'list-available-tools',\n  description:\n    'Returns the tools currently registered on the Mastra instance. The tool ids returned here are the only valid values you can put in `{ type: \"tool\", toolId }` graph entries. Each row includes `inputSchema` and `outputSchema` as JSON Schema — read them to know what fields the tool accepts and emits; never invent field names.',\n  inputSchema: z.object({}),\n  outputSchema: z.object({\n    tools: z.array(\n      z.object({\n        id: z.string(),\n        description: z.string().optional(),\n        inputSchema: z.any().optional(),\n        outputSchema: z.any().optional(),\n      }),\n    ),\n  }),\n  execute: async (_input, { mastra }) => {\n    if (!mastra) throw new Error('list-available-tools requires a Mastra context.');\n    const all = (mastra as { listTools?: () => Record<string, unknown> }).listTools?.() ?? {};\n    return {\n      tools: Object.entries(all)\n        .filter(([id]) => !WORKFLOW_BUILDER_NOISE_TOOLS.has(id))\n        .map(([id, t]) => {\n          const tool = t as { description?: string; inputSchema?: unknown; outputSchema?: unknown } | undefined;\n          return {\n            id,\n            description: tool?.description,\n            inputSchema: extractJsonSchema(tool?.inputSchema, 'input'),\n            outputSchema: extractJsonSchema(tool?.outputSchema, 'output'),\n          };\n        }),\n    };\n  },\n});\n","sourceCodeStart":12,"sourceCodeEnd":47,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/tools/workflows/list-available-tools.ts#L12-L47","documentation":"list-available-tools enumerates tools from the Mastra registry via `mastra.listTools()`. The execute function throws this Error when the injected runtime context has no `mastra` instance, because there is no registry to read tools from. It is a fail-fast guard against executing the tool outside a Mastra-managed runtime.","triggerScenarios":"Invoking list-available-tools' execute with an executionContext lacking `mastra` — manual `tool.execute()` calls, custom harnesses that don't inject Mastra, or test stubs omitting the runtime context.","commonSituations":"Custom workflow runners/bots invoking SDK tools directly without a Mastra server; test code with partial context mocks; calling the tool in an environment where the Mastra instance was never constructed or registered.","solutions":["Execute the tool within a Mastra runtime (agent, server, or Studio) so `mastra` is injected.","In custom code, supply `{ mastra }` in the executionContext when calling execute.","In tests, build a minimal `new Mastra({ ... })` and include it in the context.","Confirm the tool is registered with/derived from a Mastra instance rather than instantiated standalone."],"exampleFix":"// before\nconst res = await listAvailableTools.execute({}, { requestContext });\n// after\nconst mastra = new Mastra({ tools: { myTool } });\nconst res = await listAvailableTools.execute({}, { mastra, requestContext });","handlingStrategy":"type-guard","validationCode":"if (!executionContext?.mastra) {\n  throw new Error('list-available-tools requires a Mastra context before execution.');\n}","typeGuard":"function hasMastra(ctx: unknown): ctx is { mastra: { listTools: () => Record<string, unknown> } } {\n  return (\n    typeof ctx === 'object' &&\n    ctx !== null &&\n    'mastra' in ctx &&\n    typeof (ctx as any).mastra?.listTools === 'function'\n  );\n}","tryCatchPattern":"try {\n  const { tools } = await listAvailableTools.execute(input, ctx);\n} catch (err) {\n  if (err instanceof Error && /requires a Mastra context/.test(err.message)) {\n    // fall back to constructing/parsing a Mastra instance and retry once\n  } else throw err;\n}","preventionTips":["Run the tool inside a Mastra-managed runtime (agent/server/Studio).","Include `mastra` in any hand-rolled executionContext passed to execute.","Use a context factory helper in tests so `mastra` is never omitted.","Type your custom runner's context to require `mastra: Mastra` so TypeScript catches omissions."],"tags":["mastra","configuration","runtime-context","tools"],"backgroundTag":"missing-mastra-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}