> Discover all available pages from the documentation index: https://mastra.zisheng.pro/fr/llms.txt # Intégrer Mastra à votre projet Hono Dans ce guide, vous allez créer avec Mastra et Hono un Agent d’IA capable d’appeler des Tools. Grâce à l’[adaptateur de serveur Hono](https://mastra.zisheng.pro/fr/reference/server/hono-adapter), vous pouvez exposer vos Agents sous forme de points de terminaison HTTP sans écrire vous-même le routage ni exécuter un serveur Mastra distinct. ## Avant de commencer - Vous aurez besoin d’une clé API provenant d’un [Provider de modèles](https://mastra.zisheng.pro/fr/models) pris en charge. Si vous n’avez pas de préférence, utilisez [OpenAI](https://mastra.zisheng.pro/fr/models/providers/openai). - Installez Node.js `v22.13.0` ou une version ultérieure ## Créer une application Hono (facultatif) Si vous disposez déjà d’une application Hono, passez à l’étape suivante. Exécutez la commande suivante pour [créer une application Hono](https://hono.dev/docs/getting-started/basic) : **npm**: ```bash npm create hono@latest mastra-hono -- --template nodejs --install --pm npm ``` **pnpm**: ```bash pnpm create hono mastra-hono --template nodejs --install --pm npm ``` **Yarn**: ```bash yarn create hono mastra-hono --template nodejs --install --pm npm ``` **Bun**: ```bash bunx create-hono mastra-hono --template nodejs --install --pm npm ``` Cette commande crée un projet nommé `mastra-hono`, mais vous pouvez choisir le nom de votre choix. ## Initialiser Mastra Accédez au répertoire de votre projet Hono : ```bash cd mastra-hono ``` Exécutez [`mastra init`](https://mastra.zisheng.pro/fr/reference/cli/mastra). Lorsque vous y êtes invité, choisissez un Provider (par exemple OpenAI) et saisissez votre clé : **npm**: ```bash npx mastra@latest init ``` **pnpm**: ```bash pnpm dlx mastra@latest init ``` **Yarn**: ```bash yarn dlx mastra@latest init ``` **Bun**: ```bash bun x mastra@latest init ``` Cette commande crée un dossier `src/mastra` contenant un exemple d’Agent météo et les fichiers suivants : - `index.ts` — configuration de Mastra, y compris la mémoire - `tools/weather-tool.ts` — Tool qui récupère la météo d’un lieu donné - `agents/weather-agent.ts` — Agent météo doté d’une instruction qui utilise le Tool Vous transmettrez ensuite le fichier `src/mastra/index.ts` à l’adaptateur de serveur Hono. ## Ajouter l’adaptateur de serveur Installez le package de l’adaptateur de serveur Hono : **npm**: ```bash npm install @mastra/hono@latest ``` **pnpm**: ```bash pnpm add @mastra/hono@latest ``` **Yarn**: ```bash yarn add @mastra/hono@latest ``` **Bun**: ```bash bun add @mastra/hono@latest ``` Ouvrez le fichier d’entrée Hono `src/index.ts` et ajoutez les imports ainsi que le code d’initialisation nécessaires : ```typescript import { serve } from '@hono/node-server' import { Hono } from 'hono' import { type HonoBindings, type HonoVariables, MastraServer } from '@mastra/hono' import { mastra } from './mastra/index.js' const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>() const server = new MastraServer({ app, mastra }) await server.init() app.get('/', c => { return c.text('Hello Hono!') }) serve( { fetch: app.fetch, port: 3000, }, info => { console.log(`Server is running on http://localhost:${info.port}`) }, ) ``` Le ⁠`MastraServer` est initialisé avec l’application Hono `app` existante et l’instance ⁠`mastra` racine. L’appel à `⁠init()` enregistre le middleware Mastra et expose tous les points de terminaison Mastra disponibles. ## Tester votre Agent Par défaut, les points de terminaison Mastra sont ajoutés sous le sous-chemin `/api` et utilisent les ID de vos Agents et Workflows. L’Agent `weather-agent` créé par défaut avec `mastra init` est disponible à l’adresse `/api/agents/weather-agent`. Démarrez votre serveur Hono : **npm**: ```bash npm run dev ``` **pnpm**: ```bash pnpm run dev ``` **Yarn**: ```bash yarn dev ``` **Bun**: ```bash bun run dev ``` Dans un autre terminal, utilisez `curl` pour interroger l’Agent météo : ```bash curl -X POST http://localhost:3000/api/agents/weather-agent/generate -H "Content-Type: application/json" -d "{\"messages\":[{\"role\":\"user\",\"content\":\"What is the weather like in Seoul?\"}]}" ``` ## Étapes suivantes Félicitations, vous avez créé votre Agent Mastra avec Hono ! 🎉 Vous pouvez maintenant enrichir le projet avec vos propres Tools et votre propre logique : - En savoir plus sur les [Agents](https://mastra.zisheng.pro/fr/docs/agents/overview) - Donnez à votre Agent ses propres [Tools](https://mastra.zisheng.pro/fr/docs/agents/using-tools) - Ajoutez une [mémoire](https://mastra.zisheng.pro/fr/docs/memory/overview) semblable à celle des humains à votre Agent Lorsque vous êtes prêt, découvrez plus en détail l’intégration de Mastra avec Hono : - [Adaptateurs de serveur](https://mastra.zisheng.pro/fr/docs/server/server-adapters)