Skip to main content

服务器路由

🌐 Server Routes

当你调用 server.init() 时,服务器适配器会注册这些路由。如果已配置,所有路由都会加上 prefix 选项作为前缀。

🌐 Server adapters register these routes when you call server.init(). All routes are prefixed with the prefix option if configured.

代理
Direct link to 代理

🌐 Agents

方法路径描述
GET/api/agents列出所有代理
GET/api/agents/:agentId根据ID获取代理
POST/api/agents/:agentId/generate生成代理响应
POST/api/agents/:agentId/stream流式传输代理响应
GET/api/agents/:agentId/tools列出代理工具
POST/api/agents/:agentId/tools/:toolId/execute执行代理工具

生成请求正文
Direct link to 生成请求正文

🌐 Generate request body

{
messages: CoreMessage[] | string; // Required
instructions?: string; // System instructions
system?: string; // System prompt
context?: CoreMessage[]; // Additional context
memory?: { key: string } | boolean; // Memory config
resourceId?: string; // Resource identifier
threadId?: string; // Thread identifier
runId?: string; // Run identifier
maxSteps?: number; // Max tool steps
activeTools?: string[]; // Tools to enable
toolChoice?: ToolChoice; // Tool selection mode
requestContext?: Record<string, unknown>; // Request context
output?: ZodSchema; // Structured output schema
}

生成回应
Direct link to 生成回应

🌐 Generate response

{
text: string;
toolCalls?: ToolCall[];
finishReason: string;
usage?: {
promptTokens: number;
completionTokens: number;
};
}

工作流程
Direct link to 工作流程

🌐 Workflows

方法路径描述
GET/api/workflows列出所有工作流
GET/api/workflows/:workflowId通过 ID 获取工作流
POST/api/workflows/:workflowId/create-run创建新的工作流运行
POST/api/workflows/:workflowId/start-async启动工作流并等待结果
POST/api/workflows/:workflowId/stream流式执行工作流
POST/api/workflows/:workflowId/resume恢复挂起的工作流
POST/api/workflows/:workflowId/resume-async异步恢复
GET/api/workflows/:workflowId/runs列出工作流运行
GET/api/workflows/:workflowId/runs/:runId获取特定运行

创建运行请求主体
Direct link to 创建运行请求主体

🌐 Create run request body

{
resourceId?: string; // Associate run with a resource (e.g., user ID)
disableScorers?: boolean; // Disable scorers for this run
}

启动异步请求体
Direct link to 启动异步请求体

🌐 Start-async request body

{
resourceId?: string; // Associate run with a resource (e.g., user ID)
inputData?: unknown;
initialState?: unknown;
requestContext?: Record<string, unknown>;
tracingOptions?: {
spanName?: string;
attributes?: Record<string, unknown>;
};
}

流工作流请求体
Direct link to 流工作流请求体

🌐 Stream workflow request body

{
resourceId?: string; // Associate run with a resource (e.g., user ID)
inputData?: unknown;
initialState?: unknown;
requestContext?: Record<string, unknown>;
closeOnSuspend?: boolean;
}

恢复请求正文
Direct link to 恢复请求正文

🌐 Resume request body

{
step?: string | string[];
resumeData?: unknown;
requestContext?: Record<string, unknown>;
}

工具
Direct link to 工具

🌐 Tools

方法路径描述
GET/api/tools列出所有工具
GET/api/tools/:toolId通过ID获取工具
POST/api/tools/:toolId/execute执行工具

执行工具请求主体
Direct link to 执行工具请求主体

🌐 Execute tool request body

{
data: unknown; // Tool input data
requestContext?: Record<string, unknown>;
}

内存
Direct link to 内存

🌐 Memory

方法路径描述
GET/api/memory/threads列出线程
GET/api/memory/threads/:threadId获取线程
POST/api/memory/threads创建线程
DELETE/api/memory/threads/:threadId删除线程
POST/api/memory/threads/:threadId/clone克隆线程
GET/api/memory/threads/:threadId/messages获取线程消息
POST/api/memory/threads/:threadId/messages添加消息

创建线程请求主体
Direct link to 创建线程请求主体

🌐 Create thread request body

{
resourceId: string;
title?: string;
metadata?: Record<string, unknown>;
}

克隆线程请求正文
Direct link to 克隆线程请求正文

🌐 Clone thread request body

{
newThreadId?: string; // Custom ID for cloned thread
resourceId?: string; // Override resource ID
title?: string; // Custom title for clone
metadata?: Record<string, unknown>; // Additional metadata
options?: {
messageLimit?: number; // Max messages to clone
messageFilter?: {
startDate?: Date; // Clone messages after this date
endDate?: Date; // Clone messages before this date
messageIds?: string[]; // Clone specific messages
};
};
}

克隆线程响应
Direct link to 克隆线程响应

🌐 Clone thread response

{
thread: {
id: string;
resourceId: string;
title: string;
createdAt: Date;
updatedAt: Date;
metadata: {
clone: {
sourceThreadId: string;
clonedAt: Date;
lastMessageId?: string;
};
// ... other metadata
};
};
clonedMessages: MastraDBMessage[];
}

向量
Direct link to 向量

🌐 Vectors

方法路径描述
POST/api/vectors/:vectorName/upsert插入或更新向量
POST/api/vectors/:vectorName/query查询向量
POST/api/vectors/:vectorName/delete删除向量

Upsert 请求体
Direct link to Upsert 请求体

🌐 Upsert request body

{
vectors: Array<{
id: string;
values: number[];
metadata?: Record<string, unknown>;
}>;
}

查询请求正文
Direct link to 查询请求正文

🌐 Query request body

{
vector: number[];
topK?: number;
filter?: Record<string, unknown>;
includeMetadata?: boolean;
}

MCP
Direct link to MCP

方法路径描述
GET/api/mcp/servers列出 MCP 服务器
GET/api/mcp/servers/:serverId/tools列出服务器工具
POST/api/mcp/:serverIdMCP HTTP 传输
GET/api/mcp/:serverId/sseMCP SSE 传输

日志
Direct link to 日志

🌐 Logs

方法路径描述
GET/api/logs列出日志
GET/api/logs/:runId按运行 ID 获取日志

查询参数
Direct link to 查询参数

🌐 Query parameters

{
page?: number;
perPage?: number;
transportId?: string;
}

遥测
Direct link to 遥测

🌐 Telemetry

方法路径描述
GET/api/telemetry/traces列出跟踪
GET/api/telemetry/traces/:traceId获取跟踪
GET/api/telemetry/traces/:traceId/spans获取跟踪跨度

常用查询参数
Direct link to 常用查询参数

🌐 Common query parameters

分页
Direct link to 分页

🌐 Pagination

大多数列表端点支持:

🌐 Most list endpoints support:

{
page?: number; // Page number (0-indexed)
perPage?: number; // Items per page (default: 10)
}

过滤
Direct link to 过滤

🌐 Filtering

工作流运行支持:

🌐 Workflow runs support:

{
fromDate?: string; // ISO date string
toDate?: string; // ISO date string
status?: string; // Run status filter
resourceId?: string; // Filter by resource
}

错误响应
Direct link to 错误响应

🌐 Error responses

所有路由都以这种格式返回错误:

🌐 All routes return errors in this format:

{
error: string; // Error message
details?: unknown; // Additional details
}

常见状态码:

🌐 Common status codes:

代码含义
400错误请求 - 参数无效
401未授权 - 缺少/无效的认证
403禁止访问 - 权限不足
404未找到 - 资源不存在
500服务器内部错误

🌐 Related