手动安装
使用本指南逐步手动构建独立的 Mastra 服务器。大多数情况下,按照快速入门指南操作会更快,该指南使用 create-mastra 命令实现相同结果。对于现有项目,也可以使用 mastra init。
阅读 https://mastra.ai/guides/getting-started/manual-install.md,然后帮助我创建 Mastra 项目。询问我想给项目取什么名称,并将其用作文件夹名称。
操作说明操作说明的直接链接
如果不想使用自动 CLI Tool,可以按照以下指南自行设置项目。
创建新项目并切换目录:
mkdir my-first-agent && cd my-first-agent生成新的
package.json文件:- npm
- pnpm
- Yarn
- Bun
npm initpnpm inityarn initbun init安装以下依赖项:
- npm
- pnpm
- Yarn
- Bun
npm install -D typescript @types/node mastra@latestnpm install @mastra/core@latest zod@^4pnpm add -D typescript @types/node mastra@latestpnpm add @mastra/core@latest zod@^4yarn add --dev typescript @types/node mastra@latestyarn add @mastra/core@latest zod@^4bun add --dev typescript @types/node mastra@latestbun add @mastra/core@latest zod@^4将
dev和build脚本添加到package.json文件:package.json{"scripts": {"dev": "mastra dev","build": "mastra build"}}创建
tsconfig.json文件:touch tsconfig.json添加以下配置:
tsconfig.json{"compilerOptions": {"target": "ES2022","module": "ES2022","moduleResolution": "bundler","esModuleInterop": true,"forceConsistentCasingInFileNames": true,"strict": true,"skipLibCheck": true,"noEmit": true,"outDir": "dist"},"include": ["src/**/*"]}信息Mastra 需要现代的
module和moduleResolution设置。使用CommonJS或node会导致解析错误。创建
.env文件:touch .env添加 API 密钥:
.envGOOGLE_API_KEY=<your-api-key>备注本指南使用 Google Gemini,但你可以使用任何受支持的模型 Provider,包括 OpenAI、Anthropic 等。
创建
weather-tool.ts文件:mkdir -p src/mastra/tools && touch src/mastra/tools/weather-tool.ts添加以下代码:
src/mastra/tools/weather-tool.tsimport { createTool } from '@mastra/core/tools'import { z } from 'zod'export const weatherTool = createTool({id: 'get-weather',description: 'Get current weather for a location',inputSchema: z.object({location: z.string().describe('City name'),}),outputSchema: z.object({output: z.string(),}),execute: async ({ location }) => {return {output: `The weather in ${location} is sunny`,}},})信息此处缩短并简化了
weatherTool示例。完整天气 Tool 请参阅为 Agent 提供 Tool。创建
weather-agent.ts文件:mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts添加以下代码:
src/mastra/agents/weather-agent.tsimport { Agent } from '@mastra/core/agent'import { weatherTool } from '../tools/weather-tool.ts'export const weatherAgent = new Agent({id: 'weather-agent',name: 'Weather Agent',instructions: `You are a helpful weather assistant that provides accurate weather information.Your primary function is to help users get weather details for specific locations. When responding:- Always ask for a location if none is provided- If the location name isn't in English, please translate it- If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York")- Include relevant details like humidity, wind conditions, and precipitation- Keep responses concise but informativeUse the weatherTool to fetch current weather data.`,model: 'google/gemini-2.5-pro',tools: { weatherTool },})创建 Mastra 入口并注册 Agent:
touch src/mastra/index.ts添加以下代码:
src/mastra/index.tsimport { Mastra } from '@mastra/core'import { weatherAgent } from './agents/weather-agent.ts'export const mastra = new Mastra({agents: { weatherAgent },})现在可以启动 Studio 并测试 Agent。
- npm
- pnpm
- Yarn
- Bun
npm run devpnpm run devyarn devbun run dev
后续步骤后续步骤的直接链接
- 查看项目结构:了解
src/mastra/文件如何映射到 Agent、Tool、Workflow、存储和配置。 - 在 Studio 中测试 Agent:打开本地 Studio UI 并运行天气 Agent。
- 在 Agent 中使用 Tool:将示例天气 Tool 替换为调用 API 或服务的真实 Tool。
- 添加 memory:持久化对话历史和用户特定上下文。
- 配置存储:为 memory、Workflow、可观测性及其他运行时状态添加持久化存储 adapter。
- 构建和部署:构建 Mastra 服务器并部署到托管平台。