Aller au contenu principal

Mastra.getToolById()

La méthode .getToolById() recherche d’abord dans le registre au niveau de Mastra un Tool dont l’id intrinsèque correspond. Si aucun ID intrinsèque ne correspond, elle traite la valeur comme une clé d’enregistrement.

Exemple d’utilisation
Lien direct vers Exemple d’utilisation

Cet exemple utilise weather comme clé d’enregistrement et weather-tool comme ID intrinsèque du Tool.

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')

Paramètres
Lien direct vers Paramètres

id:

TTools[TToolName]['id']
ID intrinsèque du Tool à rechercher. Lorsqu’aucun Tool n’a cet ID, Mastra utilise la valeur comme clé d’enregistrement.