> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Server Mastra 通过 `src/mastra/` 目录下的 `server.ts` 文件配置其 HTTP [server](https://mastra.zisheng.pro/docs/server/mastra-server)。该 server 将 Agent、Workflow 和其他已注册组件公开为 REST 端点;文件默认导出的结构与传给 [`new Mastra()`](https://mastra.zisheng.pro/reference/core/mastra-class) 的 `ServerConfig` 相同。 本页介绍基于文件的约定。有关 server 功能、中间件、自定义路由、生成的 API 文档和部署行为,请参阅 [Server 概览](https://mastra.zisheng.pro/docs/server/mastra-server)。 ## 快速开始 创建 `src/mastra/server.ts` 并默认导出一个 `ServerConfig` 对象: ```typescript import type { ServerConfig } from '@mastra/core/server' export default { port: 4111, host: '0.0.0.0', } satisfies ServerConfig ``` ## 常见配置 `server.ts` 可以配置与代码注册相同的 server 选项: | 需求 | 使用 | | ---------------- | --------------------------------------- | | 绑定到部署平台地址 | `host` 和 `port` | | 限制浏览器访问 | `cors` | | 添加 webhook 或健康检查 | `apiRoutes` | | 启用生成的 API 文档 | `build.openAPIDocs` 和 `build.swaggerUI` | 有关示例,请参阅 [Mastra server](https://mastra.zisheng.pro/docs/server/mastra-server)、[中间件](https://mastra.zisheng.pro/docs/server/middleware)和[自定义 API 路由](https://mastra.zisheng.pro/docs/server/custom-api-routes)。 ## 与代码配置的优先级 代码中注册的 server 配置优先于 `server.ts`。应用基于文件的 server 配置时,会保留启动期间添加的路由,例如 channel webhook。