createTool()
createTool() 函數用於定義 Mastra Agent 可執行的自訂 Tool。Tool 可讓 Agent 與外部系統互動或進行計算,從而擴展其功能。Tool 亦可存取特定資料。
使用範例使用範例 的直接連結
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Get the current weather for a location',
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute: async ({ location }) => {
return {
location,
temperatureCelsius: 21,
conditions: 'sunny',
}
},
})
第一個 execute 參數是經 inputSchema 驗證的值。請直接在函數簽名中解構 schema 欄位,如 { location } 所示。可選的第二個參數包含執行 context。
參數參數 的直接連結
id:
description:
inputSchema?:
execute 函數預期輸入參數的 Standard JSON Schema。outputSchema?:
execute 函數預期輸出結構的 Standard JSON Schema。strict?:
toModelOutput?:
execute 輸出傳回模型前進行轉換。使用此函數可向模型傳回 text、json 或 content 形式的輸出(包括圖片/檔案等多模態部分),同時在應用程式碼中保留完整的原始輸出。transform?:
input、inputDelta、output、error、approval、suspend 及 resume 等階段設定 display 和 transcript 轉換。suspendSchema?:
suspend() 的 payload 結構的 Standard JSON Schema。Tool 暫停執行時,此 payload 會傳回 client。resumeSchema?:
resumeData 預期結構的 Standard JSON Schema。啟用 autoResumeSuspendedTools 時,Agent 會用它從使用者訊息擷取資料。requireApproval?:
tool-call-approval chunk,並暫停直至獲批准或被拒絕。mcp?:
annotations(例如 title、readOnlyHint、destructiveHint、idempotentHint、openWorldHint 等 Tool 行為提示)及 _meta(原樣傳遞至 MCP client 的任意 metadata)。requestContextSchema?:
providerOptions?:
anthropic 或 openai;value 則是該 Provider 專屬的設定物件。inputExamples?:
background?:
execute?:
execute,但對於在其他地方執行或調整的 Tool 定義,此類型允許省略該函數。它接收兩個參數:根據 inputSchema 驗證的輸入資料(第一個參數),以及包含 requestContext、abortSignal 和其他執行 metadata 的執行 context 物件(第二個參數)。input:
context?:
requestContext?:
abortSignal?:
agent?:
workflow?:
mcp?:
observe:
span 會直接執行函數,而 log 則不會執行任何操作。onInputStart?:
(options: ToolCallOptions) => void | PromiseLike<void>。onInputDelta?:
({ inputTextDelta, ...options }: { inputTextDelta: string } & ToolCallOptions) => void | PromiseLike<void>。onInputAvailable?:
({ input, ...options }: { input: TSchemaIn } & ToolCallOptions) => void | PromiseLike<void>。onOutput?:
({ output, toolName, ...options }: { output: TSchemaOut; toolName: string } & Omit<ToolCallOptions, 'messages'>) => void | PromiseLike<void>。原始碼類型中會出現 mastra 和 mcpMetadata 等由 runtime 填入的欄位,但這些欄位由 Mastra 或 MCP adapter 設定。一般使用 createTool() 時毋須設定。
傳回值傳回值 的直接連結
createTool() 函數會傳回 Tool 物件。
Tool:
定義 schema定義 schema 的直接連結
你可以使用任何支援 Standard JSON Schema 的程式庫,定義 Tool 的 inputSchema 和 outputSchema。這包括 Zod、Valibot 和 ArkType 等程式庫。
- Zod
- Valibot
- ArkType
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Fetches weather for a location',
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute: async ({ location }) => {
return { location, temperatureCelsius: 21, conditions: 'sunny' }
},
})
import { createTool } from '@mastra/core/tools'
import * as v from 'valibot'
import { toStandardJsonSchema } from '@valibot/to-json-schema'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Fetches weather for a location',
inputSchema: toStandardJsonSchema(
v.object({
location: v.string(),
}),
),
outputSchema: toStandardJsonSchema(
v.object({
location: v.string(),
temperatureCelsius: v.number(),
conditions: v.string(),
}),
),
execute: async ({ location }) => {
return { location, temperatureCelsius: 21, conditions: 'sunny' }
},
})
import { createTool } from '@mastra/core/tools'
import { type } from 'arktype'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Fetches weather for a location',
inputSchema: type({
location: 'string',
}),
outputSchema: type({
location: 'string',
temperatureCelsius: 'number',
conditions: 'string',
}),
execute: async ({ location }) => {
return { location, temperatureCelsius: 21, conditions: 'sunny' }
},
})
嚴格 Tool 輸入範例嚴格 Tool 輸入範例 的直接連結
如要 Mastra 要求支援的模型 Provider 產生與 Tool schema 完全相符的引數,請設定 strict: true。
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Get the current weather for a location',
strict: true,
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute: async ({ location }) => {
return {
location,
temperatureCelsius: 21,
conditions: 'sunny',
}
},
})
Mastra 會將 strict: true 傳遞至支援嚴格 Tool 呼叫的模型 adapter。對於不支援嚴格 Tool 呼叫的 adapter,Mastra 會忽略此選項。
toModelOutput 範例example-with-tomodeloutput 的直接連結
如果 Tool 應向應用程式傳回豐富的內部資料,但模型只應接收簡化值或多模態內容,請使用 toModelOutput。
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Get the current weather for a location',
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
radarImageUrl: z.string().url(),
}),
execute: async ({ location }) => ({
location,
temperatureCelsius: 21,
conditions: 'sunny',
radarImageUrl: 'https://example.com/radar/seattle.png',
}),
toModelOutput: output => {
return {
type: 'content',
value: [
{
type: 'text',
text: `${output.location}: ${output.temperatureCelsius}°C and ${output.conditions}`,
},
{ type: 'image-url', url: output.radarImageUrl },
],
}
},
})
Tool 仍會向應用程式傳回完整的 execute 結果,而模型會接收經轉換的 toModelOutput 值。
toModelOutput 可傳回:
type: 'text'type: 'json'type: 'content'with parts liketext,image-url,image-data,file-url,file-data,file-id,image-file-id, orcustom
transform 範例example-with-transform 的直接連結
如果 Tool 應為 runtime 行為保留原始輸入或輸出,但顯示 stream 或 transcript 訊息應接收更精簡或安全的結構,請使用 transform。
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const customerTool = createTool({
id: 'lookup-customer',
description: 'Looks up a customer',
inputSchema: z.object({
customerId: z.string(),
internalPath: z.string(),
}),
outputSchema: z.object({
displayName: z.string(),
apiKey: z.string(),
debugScore: z.number(),
}),
execute: async () => {
return {
displayName: 'Acme',
apiKey: 'secret-value',
debugScore: 0.97,
}
},
transform: {
display: {
input: ({ input }) => ({ customerId: input?.customerId }),
output: ({ output }) => ({ displayName: output?.displayName }),
error: () => ({ message: 'Customer lookup failed' }),
},
transcript: {
input: ({ input }) => ({ customerId: input?.customerId }),
output: ({ output }) => ({ displayName: output?.displayName }),
error: () => ({ message: 'Customer lookup failed' }),
},
},
})
Tool 仍會接收原始 inputSchema 值並傳回原始 execute 結果。Mastra 會將 display 轉換套用至串流 UI payload,並將 transcript 轉換套用至使用者可見的 transcript 訊息。
MCP annotation 範例MCP annotation 範例 的直接連結
透過 MCP (Model Context Protocol) 公開 Tool 時,你可以加入 annotation 以說明 Tool 行為,並自訂 client 顯示 Tool 的方式。這些 MCP 專屬屬性歸入 mcp 屬性:
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Get the current weather for a location',
inputSchema: z.object({
location: z.string().describe('City name or coordinates'),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
// MCP-specific properties
mcp: {
// Annotations for client behavior hints
annotations: {
title: 'Weather Lookup', // Human-readable display name
readOnlyHint: true, // Tool doesn't modify environment
destructiveHint: false, // Tool doesn't perform destructive updates
idempotentHint: true, // Same args = same result
openWorldHint: true, // Interacts with external API
},
// Custom metadata for client-specific functionality
_meta: {
version: '1.0.0',
category: 'weather',
},
},
execute: async ({ location }) => {
return {
location,
temperatureCelsius: 21,
conditions: 'sunny',
}
},
})
Tool 生命週期 hookTool 生命週期 hook 的直接連結
Tool 支援生命週期 hook,讓你監察 Tool 執行的不同階段並作出回應。這些 hook 尤其適合用於 logging、analytics、驗證,以及串流期間的即時更新。
以下範例示範已設定所有生命週期 hook 的 Tool:
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Get the current weather for a location',
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute: async ({ location }) => {
return {
location,
temperatureCelsius: 21,
conditions: 'sunny',
}
},
onInputStart: ({ toolCallId }) => {
console.log(`Tool call ${toolCallId} input started`)
},
onInputDelta: ({ inputTextDelta, toolCallId }) => {
console.log(`Tool call ${toolCallId} received input chunk: ${inputTextDelta}`)
},
onInputAvailable: ({ input, toolCallId }) => {
console.log(`Tool call ${toolCallId} received location: ${input.location}`)
},
onOutput: ({ output, toolCallId, toolName }) => {
console.log(`Tool ${toolName} call ${toolCallId} returned conditions: ${output.conditions}`)
},
})
可用的 hook可用的 hook 的直接連結
onInputStartoninputstart 的直接連結
在 Tool 呼叫的輸入串流開始時、接收任何輸入資料前呼叫。
export const tool = createTool({
id: 'example-tool',
description: 'Example tool with hooks',
onInputStart: ({ toolCallId, messages, abortSignal }) => {
console.log(`Tool ${toolCallId} input streaming started`)
},
})
onInputDeltaoninputdelta 的直接連結
輸入文字串流傳入時,會針對每個增量 chunk 呼叫。適合用於顯示即時進度或解析部分 JSON。
export const tool = createTool({
id: 'example-tool',
description: 'Example tool with hooks',
onInputDelta: ({ inputTextDelta, toolCallId, messages, abortSignal }) => {
console.log(`Received input chunk: ${inputTextDelta}`)
},
})
onInputAvailableoninputavailable 的直接連結
完整 Tool 輸入可用,並已根據 inputSchema 完成解析及驗證時呼叫。
export const tool = createTool({
id: 'example-tool',
description: 'Example tool with hooks',
inputSchema: z.object({
location: z.string(),
}),
onInputAvailable: ({ input, toolCallId, messages, abortSignal }) => {
console.log(`Tool received complete input:`, input)
// input is fully typed based on inputSchema
},
})
onOutputonoutput 的直接連結
Tool 成功執行並傳回輸出後呼叫。適合用於記錄結果、觸發後續動作或 analytics。
export const tool = createTool({
id: 'example-tool',
description: 'Example tool with hooks',
outputSchema: z.object({
result: z.string(),
}),
execute: async input => {
return { result: 'Success' }
},
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
console.log(`${toolName} execution completed:`, output)
// output is fully typed based on outputSchema
},
})
Hook 執行次序Hook 執行次序 的直接連結
一般串流 Tool 呼叫會按以下次序觸發 hook:
- onInputStart: 輸入串流開始
- onInputDelta: 在 chunk 傳入時呼叫多次
- onInputAvailable: 完整輸入已完成解析及驗證
- Tool 的 execute 函數執行
- onOutput: Tool 已成功完成
Hook 參數Hook 參數 的直接連結
Hook callback 會接收以下由原始碼定義的參數結構:
onInputStart: 接收ToolCallOptions,包括toolCallId、messages及abortSignal等欄位。onInputDelta: 接收{ inputTextDelta: string } & ToolCallOptions。onInputAvailable: 接收{ input: TSchemaIn } & ToolCallOptions,其中input的類型來自inputSchema。onOutput: 接收{ output: TSchemaOut; toolName: string } & Omit<ToolCallOptions, 'messages'>,其中output的類型來自outputSchema。此 hook 不會接收messages。
錯誤處理錯誤處理 的直接連結
Hook 錯誤會被自動捕捉並記錄,但不會阻止 Tool 繼續執行。如果 hook 拋出錯誤,系統會將其記錄至 console,但不會令 Tool 呼叫失敗。
MCP Tool annotationMCP Tool annotation 的直接連結
透過 Model Context Protocol (MCP) 公開 Tool 時,你可以提供說明 Tool 行為的 annotation。這些 annotation 可協助 OpenAI Apps SDK 等 MCP client 了解如何呈現及處理你的 Tool。
MCP 專屬屬性歸入 mcp 屬性,其中包括 annotations 和 _meta:
mcp: {
annotations: { /* behavior hints */ },
_meta: { /* custom metadata */ },
}
ToolAnnotations 屬性toolannotations-properties 的直接連結
title?:
readOnlyHint?:
destructiveHint?:
idempotentHint?:
openWorldHint?:
這些 annotation 遵循 MCP 規範,並會在透過 MCP 列出 Tool 時原樣傳遞。