> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Agent.getMetadata() `.getMetadata()` 方法會擷取為 Agent 設定的中繼資料;如設定值為函式,則會先解析。你可以使用中繼資料在用戶端分類 Agent,毋須將該資料編碼至 Agent ID 或名稱中。 ## 使用範例 ```typescript await agent.getMetadata() ``` ## 參數 **options** (`{ requestContext?: RequestContext }`): 包含請求情境的選用設定物件。 (Default: `{}`) **options.requestContext** (`RequestContext`): 傳送至動態中繼資料函式的 Request Context。中繼資料為靜態時會忽略此值。 ## 傳回值 **metadata** (`Record | Promise>`): 為 Agent 設定的中繼資料;如未設定中繼資料,則為 undefined。如中繼資料定義為函式,可以直接傳回,亦可以是解析後傳回中繼資料的 promise。 ## 進階使用範例 靜態中繼資料: ```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.6-sol', metadata: { type: 'support' }, }) const metadata = supportAgent.getMetadata() ``` 從請求情境解析的動態中繼資料: ```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.6-sol', metadata: ({ requestContext }) => ({ type: 'support', tenant: requestContext.get('tenant'), }), }) const metadata = await supportAgent.getMetadata({ requestContext }) ``` ## 相關內容 - [Agent 概覽](https://mastra.zisheng.pro/zh-HK/docs/agents/overview) - [Request Context](https://mastra.zisheng.pro/zh-HK/docs/server/request-context)