手動安裝
使用本指南逐步手動建立獨立的 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在
package.json文件中加入dev和build腳本: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範例經過精簡。你可以在為 Agent 提供 Tool中查看完整的天氣 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:以會呼叫 API 或服務的真實 Tool 取代範例天氣 Tool。
- 加入記憶體:保存對話記錄和使用者特定的內容脈絡。
- 設定儲存空間:為記憶體、Workflow、可觀測性及其他執行階段狀態加入持久儲存配接器。
- 建置及部署:建置 Mastra 伺服器,並將其部署至託管平台。