Mastra 推出实验工具 Mock:无需调用真实 API 即可运行智能体实验
Introducing Experiment Tool Mocks | Mastra Blog
Mastra 发布 Experiment Tool Mocks,让智能体实验通过数据集项声明的 mock 输入输出替代真实工具调用结果,无需触碰支付、邮件等真实 API。该功能目前支持 agent 目标,workflow 工具 mock 后续跟进;项目中的 agent、环境变量和工具配置无需改动。运行实验需配置存储适配器,可将 Turso 环境变量加入 LibSQLStore,部署到 Mastra 平台时会自动分配 Turso 数据库,并支持用 unmockedToolPolicy: 'deny' 阻止未声明的工具调用。
正文
当前显示原文,中文全文译文尚未就绪。
You can now run agent experiments without hitting live APIs using tool mocks. Each dataset item can declare mock inputs and outputs that experiments use in place of real tool call results.
Tools are often used to interact with APIs. Some functionality can be destructive or even permanent — "delete user", "refund payment", "send email". Using tool mocks, you can assert the expected results without invoking the tool's execute function and hitting the real API.
Before tool mocks, testing agents that touched payment or messaging APIs meant running experiments in test environments or managing env var swaps. With tool mocks, nothing in your project needs to change — the agent, env vars, and tool config stay exactly as they would when running in production.
Starting today, tool mocks are supported for agent targets, with workflow tool mocks to follow.
Running experiments requires a storage adapter to persist datasets and experiment results.
Add Turso environment variables to your LibSQLStore config. When you deploy to the Mastra platform, a Turso database will be automatically provisioned for you:
Setup
The billingAgent is configured with two tools — getOrder and refundPayment:
Each tool has a defined inputSchema and outputSchema. Tool mocks must use the same data shape (see below):
Create datasets and run experiments using the TS API or Studio.
Each dataset item declares a groundTruth for scoring and a toolMocks array. Each mock requires a toolName with args and output matching the defined tool's inputSchema and outputSchema:
Target a dataset by its id then attach .startExperiment(). Configure the experiment using targetType, targetId, and scorers.
Set unmockedToolPolicy: 'deny' to block undeclared tools. This prevents live tool calls when mocks aren't declared:
For more information and full configuration options, see:
Tool mocks; Block undeclared tools; startExperiment; addItem reference