跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Workflow.tool()

The .tool() method adds a tool as a declarative step. The tool's own input and output schemas apply, so the previous step's output must satisfy the tool's input schema. Use .map() to transform the data if they don't match.

Unlike wrapping a tool with createStep(), .tool() records a declarative entry in the workflow graph. This makes the workflow portable: the same graph can be serialized and persisted as a dynamic workflow.

Usage example
Usage example的直接链接

workflow.tool(testTool).commit()

Parameters
Parameters的直接链接

toolOrId:

Tool | string
A tool instance, or the ID of a tool registered on the Mastra instance. When passing an ID, the tool is resolved from the registry at execution time.

options?:

{ retries?: number, scorers?: DynamicArgument<MastraScorers>, metadata?: StepMetadata }
Step-level retry count, scorers, and metadata for the tool step.

stepOptions?:

{ id?: string }
The step's call-site ID within the workflow. Defaults to the tool's ID. Set this when the same tool appears more than once in one workflow.

Returns
Returns的直接链接

workflow:

Workflow
The workflow instance for method chaining

Referencing a tool by ID
Referencing a tool by ID的直接链接

Pass a string to reference a registered tool without importing it. The tool must be registered on the Mastra instance when the workflow runs:

workflow.tool('lookup-customer', { retries: 2 }).commit()

Persisting tool steps
Persisting tool steps的直接链接

Workflows built with .tool() serialize to the same declarative entries that dynamic workflows use. Only retries and metadata round-trip through storage. A function-valued scorers option throws an error when the workflow is stored.