Mastra.getToolById()
The .getToolById() method first searches the Mastra-level registry for a tool with a matching intrinsic id. If no intrinsic ID matches, it treats the value as a registration key.
Usage exampleDirect link to Usage example
This example uses weather as the registration key and weather-tool as the tool's intrinsic ID.
src/mastra/index.ts
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')
ParametersDirect link to Parameters
id:
TTools[TToolName]['id']
The intrinsic tool ID to find. When no tool has that ID, Mastra uses the value as a registration key.