Skip to main content

Mastra.listStoredAgents()

.listStoredAgents() 方法从存储中检索分页的代理配置列表。默认情况下,它返回可执行的 Agent 实例,但也可以返回原始存储数据。

🌐 The .listStoredAgents() method retrieves a paginated list of agent configurations from storage. By default, it returns executable Agent instances, but can also return raw storage data.

使用示例
Direct link to 使用示例

🌐 Usage example

// Get Agent instances from storage
const { agents, total, hasMore } = await mastra.listStoredAgents();

for (const agent of agents) {
console.log(agent.id, agent.name);
// Each agent is ready to use
// const response = await agent.generate("Hello");
}
// Get paginated results with raw storage data
const result = await mastra.listStoredAgents({
page: 0,
perPage: 10,
raw: true,
});

console.log(`Showing ${result.agents.length} of ${result.total} agents`);
console.log(`Has more: ${result.hasMore}`);

for (const config of result.agents) {
console.log(config.id, config.name, config.createdAt);
}

参数
Direct link to 参数

🌐 Parameters

args?:

object
Optional configuration object for pagination and output format.

参数选项
Direct link to 参数选项

🌐 Args Options

page?:

number
= 0
Zero-indexed page number for pagination.

perPage?:

number | false
= 100
Number of items per page. Set to `false` to fetch all records without pagination.

raw?:

boolean
= false
When `true`, returns raw `StorageAgentType` objects instead of `Agent` instances.

返回
Direct link to 返回

🌐 Returns

agents:

Agent[] | StorageAgentType[]
Array of `Agent` instances by default, or `StorageAgentType` objects when `raw: true`.

total:

number
Total number of stored agents across all pages.

page:

number
Current page number (zero-indexed).

perPage:

number | false
Number of items per page, or `false` if fetching all.

hasMore:

boolean
Whether there are more pages available.

原始分辨率
Direct link to 原始分辨率

🌐 Primitive Resolution

在创建 Agent 实例时(默认行为),每个存储的代理配置都会根据已注册的原语进行解析:

🌐 When creating Agent instances (default behavior), each stored agent's configuration is resolved against registered primitives:

  • 工具:已从 Mastra 配置中注册的 tools 解析
  • 工作流程:已从 Mastra 配置中注册的 workflows 解析
  • 子代理:已从 Mastra 配置中注册的 agents 解析
  • 内存:已从 Mastra 配置中注册的 memory 解析
  • 得分者:已从 Mastra 配置中注册的 scorers 解析

如果未找到引用的原语,会记录一个警告,但代理仍然会被创建。

🌐 If a referenced primitive is not found, a warning is logged but the agent is still created.

示例:遍历所有存储的代理
Direct link to 示例:遍历所有存储的代理

🌐 Example: Iterating Through All Stored Agents

async function getAllStoredAgents(mastra: Mastra) {
const allAgents: Agent[] = [];
let page = 0;
let hasMore = true;

while (hasMore) {
const result = await mastra.listStoredAgents({ page, perPage: 50 });
allAgents.push(...result.agents);
hasMore = result.hasMore;
page++;
}

return allAgents;
}

🌐 Related