Mastra.getToolById()
.getToolById() 方法會先在 Mastra 層級登錄中搜尋內在 id 相符的 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 會將此值用作登錄鍵。