> Discover all available pages from the documentation index: https://mastra.zisheng.pro/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/docs/agents/using-tools) - [Mastra 类](https://mastra.zisheng.pro/reference/core/mastra-class) - [Mastra.getTool()](https://mastra.zisheng.pro/reference/core/getTool) - [Mastra.listTools()](https://mastra.zisheng.pro/reference/core/listTools)