跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Agent.getMetadata()

The .getMetadata() method retrieves the metadata configured for an agent, resolving it if it's a function. Use metadata to classify agents in clients without encoding that data in the agent ID or name.

Usage example
Usage example的直接链接

await agent.getMetadata()

Parameters
Parameters的直接链接

options?:

{ requestContext?: RequestContext }
= {}
Optional configuration object containing request context.
{ requestContext?: RequestContext }

requestContext?:

RequestContext
Request Context passed to dynamic metadata functions. Ignored when metadata is static.

Returns
Returns的直接链接

metadata:

Record<string, unknown> | Promise<Record<string, unknown>>
The metadata configured for the agent, or undefined if no metadata was configured. Returns either directly or as a promise that resolves to the metadata when defined as a function.

Extended usage example
Extended usage example的直接链接

Static metadata:

src/mastra/agents/support-agent.ts
import { Agent } from '@mastra/core/agent'

export const supportAgent = new Agent({
id: 'support-agent',
name: 'Support Agent',
instructions: 'You help customers with support requests.',
model: 'openai/gpt-5.6-sol',
metadata: { type: 'support' },
})

const metadata = supportAgent.getMetadata()

Dynamic metadata resolved from the request context:

src/mastra/agents/support-agent.ts
import { Agent } from '@mastra/core/agent'

export const supportAgent = new Agent({
id: 'support-agent',
name: 'Support Agent',
instructions: 'You help customers with support requests.',
model: 'openai/gpt-5.6-sol',
metadata: ({ requestContext }) => ({
type: 'support',
tenant: requestContext.get('tenant'),
}),
})

const metadata = await supportAgent.getMetadata({ requestContext })