跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Hono adapter

The @mastra/hono package provides a server adapter for running Mastra with Hono. For general adapter concepts (constructor options, initialization flow, etc.), see Server Adapters.

Installation
Installation的直接链接

Install the Hono adapter and Hono framework:

npm install @mastra/hono@latest hono

Usage example
Usage example的直接链接

server.ts
import { Hono } from 'hono'
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono'
import { mastra } from './mastra'

const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>()
const server = new MastraServer({ app, mastra })

await server.init()

export default app

init() doesn't start Mastra's background workers. To run scheduled workflows, agent schedules, or event listeners, call await mastra.startWorkers() after the server starts listening. See Start background workers.

Constructor parameters
Constructor parameters的直接链接

app:

Hono
Hono app instance

mastra:

Mastra
Mastra instance

prefix?:

string
= '/api'
Route path prefix (e.g., /api/v2)

openapiPath?:

string
= ''
Path to serve OpenAPI spec (e.g., /openapi.json)

bodyLimitOptions?:

{ maxSize: number, onError: (err) => unknown }
Request body size limits

streamOptions?:

{ redact?: boolean }
= { redact: true }
Stream redaction config. When true, redacts sensitive data from streams.

customRouteAuthConfig?:

Map<string, boolean>
Per-route auth overrides. Keys are METHOD:PATH (e.g., GET:/api/health). Value false makes route public, true requires auth.

tools?:

Record<string, Tool>
Available tools for the server

taskStore?:

InMemoryTaskStore
Task store for A2A (Agent-to-Agent) operations

mcpOptions?:

MCPOptions
MCP transport options, such as setRequestAuth to control what tools see as context.mcp.extra.authInfo.

Adding custom routes
Adding custom routes的直接链接

Add routes directly to the Hono app:

server.ts
import { Hono } from 'hono'
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono'

const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>()
const server = new MastraServer({ app, mastra })

// Before init - runs before Mastra middleware
app.get('/early-health', c => c.json({ status: 'ok' }))

await server.init()

// After init - has access to Mastra context
app.get('/custom', c => {
const mastraInstance = c.get('mastra')
return c.json({ agents: Object.keys(mastraInstance.listAgents()) })
})
提示

Routes added before init() run without Mastra context. Add routes after init() to access the Mastra instance and request context.

When you want Mastra-managed auth and route metadata such as requiresAuth, prefer registerApiRoute(). For raw Hono routes mounted directly on app, use createAuthMiddleware():

server.ts
import { Hono } from 'hono'
import { createAuthMiddleware, HonoBindings, HonoVariables, MastraServer } from '@mastra/hono'
import { mastra } from './mastra'

const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>()
const server = new MastraServer({ app, mastra })

await server.init()

app.get('/custom/protected', createAuthMiddleware({ mastra }), c => {
const user = c.get('requestContext').get('user')
return c.json({ user })
})

app.get('/custom/public', createAuthMiddleware({ mastra, requiresAuth: false }), c => {
return c.json({ ok: true })
})

Accessing context
Accessing context的直接链接

In Hono middleware and route handlers, access Mastra context via c.get():

app.get('/custom', async c => {
const mastra = c.get('mastra')
const requestContext = c.get('requestContext')
const abortSignal = c.get('abortSignal')

const agent = mastra.getAgent('myAgent')
return c.json({ agent: agent.name })
})

Available context keys:

KeyDescription
mastraMastra instance
requestContextRequest context map
abortSignalRequest cancellation signal
registeredToolsTools registered with the adapter
taskStoreTask store for A2A operations
customRouteAuthConfigPer-route auth overrides
userAuthenticated user (if auth configured)

Adding middleware
Adding middleware的直接链接

Add Hono middleware with app.use() before init() to run it on every request. Mastra context isn't available at that point:

server.ts
import { Hono } from 'hono'
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono'

const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>()

// Runs on every request, before Mastra context exists
app.use('*', async (c, next) => {
console.log(`${c.req.method} ${c.req.url}`)
await next()
})

const server = new MastraServer({ app, mastra })
await server.init()

Middleware added after init() never runs for Mastra's routes. Hono dispatches handlers in registration order, so middleware registered after the routes only applies to routes added later.

To run middleware with Mastra context on Mastra's routes, use server.middleware in the Mastra config. The adapter registers it during init(), after the context step and before any route. These handlers are skipped for routes declared public with requiresAuth: false, so they can't block endpoints such as the Studio sign-in routes:

src/mastra/index.ts
import { Mastra } from '@mastra/core'

export const mastra = new Mastra({
server: {
middleware: [
async (c, next) => {
c.get('requestContext').set('locale', c.req.header('accept-language') ?? 'en')
await next()
},
],
},
})

Manual initialization
Manual initialization的直接链接

For custom middleware ordering, call each method separately instead of init(). See manual initialization for details.

Examples
Examples的直接链接