Aller au contenu principal

Installation manuelle

Utilisez ce guide pour créer manuellement, étape par étape, un serveur Mastra autonome. Dans la plupart des cas, il est plus rapide de suivre le guide de démarrage rapide, qui obtient le même résultat avec la commande create-mastra. Pour les projets existants, vous pouvez également utiliser mastra init.

Instructions
Lien direct vers Instructions

Si vous préférez ne pas utiliser notre outil CLI automatique, vous pouvez configurer votre projet vous-même en suivant le guide ci-dessous.

  1. Créez un nouveau projet et accédez à son répertoire :

    mkdir my-first-agent && cd my-first-agent

    Générez un nouveau fichier package.json :

    npm init

    Installez les dépendances suivantes :

    npm install -D typescript @types/node mastra@latest
    npm install @mastra/core@latest zod@^4

    Ajoutez les scripts dev et build à votre fichier package.json :

    package.json
    {
    "scripts": {
    "dev": "mastra dev",
    "build": "mastra build"
    }
    }
  2. Créez un fichier tsconfig.json :

    touch tsconfig.json

    Ajoutez la configuration suivante :

    tsconfig.json
    {
    "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "noEmit": true,
    "outDir": "dist"
    },
    "include": ["src/**/*"]
    }
    info

    Mastra requiert des paramètres module et moduleResolution modernes. L’utilisation de CommonJS ou de node provoquera des erreurs de résolution.

  3. Créez un fichier .env :

    touch .env

    Ajoutez votre clé API :

    .env
    GOOGLE_API_KEY=<your-api-key>
    remarque

    Ce guide utilise Google Gemini, mais vous pouvez choisir tout fournisseur de modèles pris en charge, notamment OpenAI, Anthropic et d’autres.

  4. Créez un fichier weather-tool.ts :

    mkdir -p src/mastra/tools && touch src/mastra/tools/weather-tool.ts

    Ajoutez le code suivant :

    src/mastra/tools/weather-tool.ts
    import { 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`,
    }
    },
    })
    info

    Nous avons raccourci et simplifié ici l’exemple weatherTool. Vous pouvez consulter le Tool météo complet dans Donner un Tool à un Agent.

  5. Créez un fichier weather-agent.ts :

    mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts

    Ajoutez le code suivant :

    src/mastra/agents/weather-agent.ts
    import { 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 informative

    Use the weatherTool to fetch current weather data.
    `,
    model: 'google/gemini-2.5-pro',
    tools: { weatherTool },
    })
  6. Créez le point d’entrée Mastra et enregistrez votre Agent :

    touch src/mastra/index.ts

    Ajoutez le code suivant :

    src/mastra/index.ts
    import { Mastra } from '@mastra/core'
    import { weatherAgent } from './agents/weather-agent.ts'

    export const mastra = new Mastra({
    agents: { weatherAgent },
    })
  7. Vous pouvez maintenant lancer Studio et tester votre Agent.

    npm run dev

Étapes suivantes
Lien direct vers Étapes suivantes