> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Mastra.getToolById() `.getToolById()` 方法會先在 Mastra 層級登錄中搜尋內在 `id` 相符的 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-HK/docs/agents/using-tools) - [Mastra 類別](https://mastra.zisheng.pro/zh-HK/reference/core/mastra-class) - [Mastra.getTool()](https://mastra.zisheng.pro/zh-HK/reference/core/getTool) - [Mastra.listTools()](https://mastra.zisheng.pro/zh-HK/reference/core/listTools)