@mastra/core
Version:
48 lines (36 loc) • 1.43 kB
Markdown
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
# 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 example
This example uses `weather` as the registration key and `weather-tool` as the tool's intrinsic 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')
```
## 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.
## Related
- [Share tools across agents](https://mastra.ai/docs/agents/using-tools)
- [Mastra class](https://mastra.ai/reference/core/mastra-class)
- [Mastra.getTool()](https://mastra.ai/reference/core/getTool)
- [Mastra.listTools()](https://mastra.ai/reference/core/listTools)