Mastra.getToolById()
.getToolById() 方法首先在 Mastra 級 registry 中搜尋 id 與指定值相符的 Tool。如果沒有 Tool 的 ID 相符,則將該值視為註冊鍵。
使用範例「使用範例」的直接連結
此範例使用 weather 作為註冊鍵,並使用 weather-tool 作為 Tool 的本身的 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')
參數「參數」的直接連結
id:
TTools[TToolName]['id']
要尋找的 Tool 本身的 ID。如果沒有 Tool 具有該 ID,Mastra 會將此值用作註冊鍵。