> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Workflow\.tool() `.tool()` 方法會加入 Tool 作為宣告式步驟。Tool 本身的輸入及輸出 schema 會套用於此,因此上一個步驟的輸出必須符合 Tool 的輸入 schema。如兩者不符,請使用 `.map()` 轉換資料。 與使用 `createStep()` 包裝 Tool 不同,`.tool()` 會在 Workflow graph 中記錄宣告式項目,令 Workflow 可移植:同一個 graph 可序列化並保存為 [dynamic Workflow](https://mastra.zisheng.pro/zh-HK/docs/workflows/dynamic-workflows)。 ## 使用範例 ```typescript workflow.tool(testTool).commit(); ``` ## 參數 **toolOrId** (`Tool | string`): Tool 實例,或在 Mastra 實例上註冊的 Tool ID。傳入 ID 時,系統會在執行階段從 registry 解析 Tool。 **options** (`{ retries?: number, scorers?: DynamicArgument, metadata?: StepMetadata }`): Tool 步驟的步驟層級重試次數、scorer 及 metadata。 **stepOptions** (`{ id?: string }`): 步驟在 Workflow 內呼叫位置的 ID。預設為 Tool 的 ID。當同一 Tool 在一個 Workflow 中出現多次時,請設定此項。 ## 傳回值 **workflow** (`Workflow`): 用於串連方法的 Workflow 實例 ## 以 ID 引用 Tool 傳入字串即可引用已註冊的 Tool,毋須匯入。執行 Workflow 時,Tool 必須已在 Mastra 實例上註冊: ```typescript workflow.tool("lookup-customer", { retries: 2 }).commit(); ``` ## 保存 Tool 步驟 使用 `.tool()` 建立的 Workflow 會序列化成與 [dynamic Workflow](https://mastra.zisheng.pro/zh-HK/docs/workflows/dynamic-workflows) 所用相同的宣告式項目。透過儲存空間來回保存時,只會保留 `retries` 及 `metadata`。儲存 Workflow 時,如 `scorers` 選項的值為 function,便會擲回錯誤。 ## 相關內容 - [Agent 及 Tool](https://mastra.zisheng.pro/zh-HK/docs/workflows/agents-and-tools) - [Dynamic Workflow](https://mastra.zisheng.pro/zh-HK/docs/workflows/dynamic-workflows) - [Workflow.agent()](https://mastra.zisheng.pro/zh-HK/reference/workflows/workflow-methods/agent)