Back to Agentic

TypeScript Hono OpenAPI

docs/publishing/guides/ts-openapi-hono.mdx

8.4.43.1 KB
Original Source

Hono is a popular open source TypeScript framework for building HTTP servers, and @hono/zod-openapi is an excellent solution for creating an auto-generated OpenAPI spec from your Hono routes.

1. Install dependencies

<Info> **Prerequisite**: Please install [Node.js](https://nodejs.org) before proceeding. </Info> <CodeGroup>
bash
npm add hono @hono/node-server @hono/zod-openapi zod
bash
pnpm add hono @hono/node-server @hono/zod-openapi zod
bash
bun add hono @hono/node-server @hono/zod-openapi zod
bash
yarn add hono @hono/node-server @hono/zod-openapi zod
</CodeGroup>

2. Create a Hono OpenAPI Node.js server

ts
import { serve } from '@hono/node-server'
import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi'
import { logger } from 'hono/logger'

const app = new OpenAPIHono()
app.use(logger())

const echoRoute = createRoute({
  description: 'Echoes the request body',
  // The OpenAPI `operationId` will be used as the tool name in Agentic
  operationId: 'echo',
  method: 'post',
  path: '/echo',
  request: {
    body: {
      content: {
        'application/json': {
          schema: z.object({}).passthrough()
        }
      }
    }
  },
  responses: {
    200: {
      description: 'Echoed request body',
      content: {
        'application/json': {
          schema: z.object({}).passthrough()
        }
      }
    }
  }
})

return app.openapi(echoRoute, async (c) => {
  return c.json(c.req.valid('json'))
})

app.doc31('/docs', {
  openapi: '3.1.0',
  info: {
    title: 'Example',
    description: 'Example description',
    version: '0.0.1'
  }
})

serve({ fetch: app.fetch, port: 8787 })

Note that the auto-generated OpenAPI spec will be available at /docs in this example.

<Tip> Hono is really flexible, so if you'd rather deploy your server to Cloudflare Workers instead of using Node.js (or any other platform), just follow [Hono's docs](https://hono.dev/docs/getting-started/basic). </Tip>

3. Deploy your OpenAPI server remotely

Deploy your server publicly or use a tool like ngrok to expose it to the internet.

<Warning> Tools like `ngrok` expose your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks. </Warning>

We recommend deploying your server to a cloud provider like Cloudflare Workers, Vercel (for instance, using the Hono API template), Render, Porter, or Fly.io. Or one of the big boys AWS, GCP, or Azure.

4. Deploy your origin OpenAPI service to Agentic

Now that you have a publicly available MCP server, you can follow the existing OpenAPI server guide to deploy it to Agentic.