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 会将此值用作注册键。