Hono 프로젝트에 Mastra 통합
이 가이드에서는 Mastra와 Hono를 사용하여 Tool 호출 AI Agent를 구축합니다. 사용하여Hono server adapter, 라우팅을 직접 작성하거나 별도의 Mastra 서버를 실행하지 않고도 Agent를 HTTP 엔드포인트로 노출할 수 있습니다.
시작하기 전에시작하기 전에에 대한 직접 링크
- 지원되는 Model Provider의 API 키가 필요합니다. 선호하는 Provider가 없다면 OpenAI를 사용하세요.
- Node.js
v22.13.0이상 설치
새 Hono 앱 만들기(선택 사항)새 Hono 앱 만들기(선택 사항)에 대한 직접 링크
이미 Hono 앱이 있다면 다음 단계로 건너뛰세요.
다음 명령을 실행하여 새 Hono 앱을 생성합니다.
- npm
- pnpm
- Yarn
- Bun
npm create hono@latest mastra-hono -- --template nodejs --install --pm npm
pnpm create hono mastra-hono --template nodejs --install --pm npm
yarn create hono mastra-hono --template nodejs --install --pm npm
bunx create-hono mastra-hono --template nodejs --install --pm npm
이 명령은 mastra-hono라는 프로젝트를 생성하지만 원하는 이름으로 바꿀 수 있습니다.
마스트라 초기화마스트라 초기화에 대한 직접 링크
Hono 프로젝트 디렉터리로 이동합니다.
cd mastra-hono
mastra init을 실행합니다. 메시지가 표시되면 Provider(예: OpenAI)를 선택하고 키를 입력합니다.
- npm
- pnpm
- Yarn
- Bun
npx mastra@latest init
pnpm dlx mastra@latest init
yarn dlx mastra@latest init
bun x mastra@latest init
그러면 예제 날씨 Agent와 다음 파일을 포함하는 src/mastra 폴더가 생성됩니다.
index.ts- Memory를 포함한 마스트라 구성tools/weather-tool.ts- 특정 위치의 날씨를 가져오는 Toolagents/weather-agent.ts- Tool을 사용하는 Prompt가 있는 기상 요원
나중에 src/mastra/index.ts 파일을 Hono 서버 어댑터에 전달합니다.
서버 어댑터 추가서버 어댑터 추가에 대한 직접 링크
Hono 서버 어댑터 패키지를 설치합니다.
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/hono@latest
pnpm add @mastra/hono@latest
yarn add @mastra/hono@latest
bun add @mastra/hono@latest
src/index.ts에서 Hono 진입점 파일을 열고 필요한 import 및 초기화 코드를 추가합니다.
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}`)
},
)
MastraServer는 기존 Hono app과 루트 mastra 인스턴스로 초기화됩니다. init()을 호출하면 Mastra 미들웨어가 등록되고 사용 가능한 모든 Mastra 엔드포인트가 노출됩니다.
Agent 테스트Agent 테스트에 대한 직접 링크
기본적으로 Mastra 엔드포인트는 /api 하위 경로에 위치하며 Agent/Workflow ID를 사용합니다. mastra init으로 생성된 기본 weather-agent는 /api/agents/weather-agent에서 사용할 수 있습니다.
Hono 서버를 시작합니다:
- npm
- pnpm
- Yarn
- Bun
npm run dev
pnpm run dev
yarn dev
bun run dev
별도의 터미널 창에서 curl을 사용하여 날씨 Agent에게 질문합니다.
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?\"}]}"
다음 단계다음 단계에 대한 직접 링크
Hono를 사용하여 Mastra Agent를 구축한 것을 축하합니다! 🎉
여기에서 자신만의 Tool와 논리를 사용하여 프로젝트를 확장할 수 있습니다.