createToolProvider
createToolProvider<
ToolProviderContext,Tools>(config):ToolProvider<Tools,ToolProviderContext>
Defined in: packages/core/src/tools/provider.ts:151
Typed authoring helper: wrap a plain function — written against a known
ToolProviderContext shape, matching whatever AgentRunInput<ToolProviderContext> your
agent uses — into a ToolProvider object, safe to assign to AgentDefinition.tools /
AgentRunInput.tools. Takes a config object (not positional args) so future additions don’t
require reordering existing call sites.
type SandboxContext = { root: string };const tools = createToolProvider<SandboxContext>({ getTools: (ctx) => ({ bash: createBashTool({ root: ctx.toolProviderContext?.root ?? "." }), }),});ToolProviderContext is the only type parameter you need to pass explicitly — Tools is
inferred from whatever getTools returns. Prefer implementing ToolProvider directly
(e.g. as a class) when the provider needs its own state.
Narrowing toolProviderContext this way is a trust boundary, not a guarantee — nothing
stops a caller from passing an AgentRunInput.toolProviderContext of a different shape at
runtime. It mirrors how the AI SDK itself types streamText’s experimental_context as
unknown for the same reason.
The framework never validates toolProviderContext on your behalf — if you want Zod
validation with defaults, parse it yourself as the first line of getTools, and pass the
same schema as contextSchema so it’s discoverable via ToolProvider.contextSchema (e.g.
for a settings UI to build a form from) without the framework ever calling it itself. The
schema’s input shape must match ToolProviderContext — a mismatched schema is a compile
error, not a silent drift:
const sandboxSchema = z.object({ root: z.string().default("/tmp") });const tools = createToolProvider<z.input<typeof sandboxSchema>>({ contextSchema: sandboxSchema, getTools: (ctx) => { const { root } = sandboxSchema.parse(ctx.toolProviderContext); return { bash: createBashTool({ root }) }; },});Type Parameters
Section titled “Type Parameters”ToolProviderContext
Section titled “ToolProviderContext”ToolProviderContext = unknown
Tools extends ToolSet = ToolSet
Parameters
Section titled “Parameters”config
Section titled “config”contextSchema?
Section titled “contextSchema?”ZodType<unknown, ToolProviderContext, $ZodTypeInternals<unknown, ToolProviderContext>>
getTools
Section titled “getTools”(ctx) => Tools | Promise<Tools>
listTools?
Section titled “listTools?”Returns
Section titled “Returns”ToolProvider<Tools, ToolProviderContext>