@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
61 lines (42 loc) • 1.82 kB
Markdown
# 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
```typescript
await agent.getMetadata()
```
## Parameters
**options** (`{ requestContext?: RequestContext }`): Optional configuration object containing request context. (Default: `{}`)
**options.requestContext** (`RequestContext`): Request Context passed to dynamic metadata functions. Ignored when metadata is static.
## 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
Static metadata:
```typescript
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.4',
metadata: { type: 'support' },
})
const metadata = supportAgent.getMetadata()
```
Dynamic metadata resolved from the request context:
```typescript
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.4',
metadata: ({ requestContext }) => ({
type: 'support',
tenant: requestContext.get('tenant'),
}),
})
const metadata = await supportAgent.getMetadata({ requestContext })
```
## Related
- [Agents overview](https://mastra.ai/docs/agents/overview)
- [Request Context](https://mastra.ai/docs/server/request-context)