Skip to main content

项目结构

🌐 Project Structure

你使用 create mastra 命令创建的新 Mastra 项目附带了一套预定义的文件和文件夹,帮助你快速入门。

🌐 Your new Mastra project, created with the create mastra command, comes with a predefined set of files and folders to help you get started.

Mastra 是一个框架,但它对你如何组织或放置文件没有固定的意见。CLI 提供了一个合理的默认结构,适用于大多数项目,但你可以根据自己的工作流程或团队规范进行调整。如果你愿意,甚至可以把整个项目构建在一个文件中!无论你选择什么结构,都要保持一致,以确保代码保持可维护性和易于导航。

🌐 Mastra is a framework, but it's unopinionated about how you organize or colocate your files. The CLI provides a sensible default structure that works well for most projects, but you're free to adapt it to your workflow or team conventions. You could even build your entire project in a single file if you wanted! Whatever structure you choose, keep it consistent to ensure your code stays maintainable and easy to navigate.

默认项目结构
Direct link to 默认项目结构

🌐 Default project structure

使用 create mastra 命令创建的项目看起来像这样:

🌐 A project created with the create mastra command looks like this:

src/
├── mastra/
│ ├── agents/
│ │ └── weather-agent.ts
│ ├── tools/
│ │ └── weather-tool.ts
│ ├── workflows/
│ │ └── weather-workflow.ts
│ ├── scorers/
│ │ └── weather-scorer.ts
│ └── index.ts
├── .env.example
├── package.json
└── tsconfig.json
tip

使用预定义的文件作为模板。复制并调整它们,以快速创建你自己的代理、工具、工作流程等。

文件夹
Direct link to 文件夹

🌐 Folders

文件夹可以组织你代理的资源,例如代理、工具和工作流。

🌐 Folders organize your agent's resources, like agents, tools, and workflows.

FolderDescription
src/mastraEntry point for all Mastra-related code and configuration.
src/mastra/agentsDefine and configure your agents - their behavior, goals, and tools.
src/mastra/workflowsDefine multi-step workflows that orchestrate agents and tools together.
src/mastra/toolsCreate reusable tools that your agents can call
src/mastra/mcp(Optional) Implement custom MCP servers to share your tools with external agents
src/mastra/scorers(Optional) Define scorers for evaluating agent performance over time
src/mastra/public(Optional) Contents are copied into the .build/output directory during the build process, making them available for serving at runtime

顶层文件
Direct link to 顶层文件

🌐 Top-level files

顶层文件定义了你的 Mastra 项目如何配置、构建以及与其环境连接。

🌐 Top-level files define how your Mastra project is configured, built, and connected to its environment.

文件描述
src/mastra/index.ts配置和初始化 Mastra 的中心入口点。
.env.example环境变量模板 - 复制并重命名为 .env 以添加你的秘密 模型提供商 密钥。
package.json定义项目元数据、依赖以及可用的 npm 脚本。
tsconfig.json配置 TypeScript 选项,如路径别名、编译器设置和构建输出。

下一步
Direct link to 下一步

🌐 Next steps