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

110 wiersze
3.3 KiB
Markdown

---
title: TypeScript Hono OpenAPI
description: This guide will show you how to publish an OpenAPI service to Agentic using TypeScript and Hono's OpenAPI support.
---
[Hono](https://hono.dev) is a popular open source TypeScript framework for building HTTP servers, and [`@hono/zod-openapi`](https://hono.dev/examples/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
npm add hono @hono/node-server @hono/zod-openapi zod
```
```bash pnpm
pnpm add hono @hono/node-server @hono/zod-openapi zod
```
```bash bun
bun add hono @hono/node-server @hono/zod-openapi zod
```
```bash yarn
yarn add hono @hono/node-server @hono/zod-openapi zod
```
</CodeGroup>
## 2. Create a Hono OpenAPI Node.js server
```ts 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](https://ngrok.com) 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](https://workers.cloudflare.com), [Vercel](https://vercel.com/guides/hosting-backend-apis) (for instance, using the [Hono](https://vercel.com/templates/hono/hono-on-vercel) API template), [Render](https://render.com/docs/deploy-fastapi), [Porter](https://docs.porter.run/guides/nodejs/deploy-nodejs), or [Fly.io](https://fly.io/docs/python/frameworks/fastapi/). Or one of the big boys [AWS](https://aws.amazon.com), [GCP](https://cloud.google.com), or [Azure](https://azure.microsoft.com).
## 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](/publishing/guides/existing-openapi-service) to deploy it to Agentic.