手動安裝
使用本指南逐步手動建立獨立的 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:將範例天氣 Tool 替換為會呼叫 API 或服務的實際 Tool。
- 加入記憶:保存對話記錄和使用者專屬的情境資訊。
- 設定儲存空間:為記憶、Workflow、可觀測性及其他執行階段狀態加入持久性儲存 adapter。
- 建置並部署:建置 Mastra 伺服器,並部署至託管平台。