> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Mastra.getToolById() `.getToolById()` 方法首先在 Mastra 級 registry 中搜尋 `id` 與指定值相符的 Tool。如果沒有 Tool 的 ID 相符,則將該值視為註冊鍵。 ## 使用範例 此範例使用 `weather` 作為註冊鍵,並使用 `weather-tool` 作為 Tool 的本身的 ID。 ```typescript import { Mastra } from '@mastra/core/mastra' import { createTool } from '@mastra/core/tools' import { z } from 'zod' const weatherTool = createTool({ id: 'weather-tool', description: 'Fetches weather for a location', inputSchema: z.object({ location: z.string(), }), outputSchema: z.object({ weather: z.string(), }), execute: async ({ location }) => ({ weather: `Weather for ${location}`, }), }) export const mastra = new Mastra({ tools: { weather: weatherTool, }, }) const toolById = mastra.getToolById('weather-tool') ``` ## 參數 **id** (`TTools[TToolName]['id']`): 要尋找的 Tool 本身的 ID。如果沒有 Tool 具有該 ID,Mastra 會將此值用作註冊鍵。 ## 相關內容 - [在 Agent 之間共享 Tool](https://mastra.zisheng.pro/zh-TW/docs/agents/using-tools) - [Mastra 類別](https://mastra.zisheng.pro/zh-TW/reference/core/mastra-class) - [Mastra.getTool()](https://mastra.zisheng.pro/zh-TW/reference/core/getTool) - [Mastra.listTools()](https://mastra.zisheng.pro/zh-TW/reference/core/listTools)