kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
103 wiersze
3.1 KiB
Markdown
103 wiersze
3.1 KiB
Markdown
---
|
|
title: TypeScript Hono MCP
|
|
description: This guide will show you how to publish an MCP server to Agentic using TypeScript and Hono's MCP support.
|
|
---
|
|
|
|
[Hono](https://hono.dev) is a popular open source TypeScript framework for building HTTP servers, and [`@hono/mcp`](https://github.com/honojs/middleware/tree/main/packages/mcp) is an excellent solution for exposing MCP servers.
|
|
|
|
## 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/mcp @modelcontextprotocol/sdk zod
|
|
```
|
|
|
|
```bash pnpm
|
|
pnpm add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
|
|
```
|
|
|
|
```bash bun
|
|
bun add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
|
|
```
|
|
|
|
```bash yarn
|
|
yarn add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
## 2. Create a Hono MCP Node.js server
|
|
|
|
```ts server.ts
|
|
import { StreamableHTTPTransport } from '@hono/mcp'
|
|
import { serve } from '@hono/node-server'
|
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
import { Hono } from 'hono'
|
|
import { logger } from 'hono/logger'
|
|
import { z } from 'zod'
|
|
|
|
const app = new Hono()
|
|
app.use(logger())
|
|
|
|
const mcpServer = new McpServer({
|
|
name: 'Example',
|
|
version: '0.0.1'
|
|
})
|
|
|
|
mcpServer.registerTool(
|
|
'add',
|
|
{
|
|
description: 'Add two numbers',
|
|
inputSchema: z.object({
|
|
a: z.number(),
|
|
b: z.number()
|
|
})
|
|
},
|
|
async (args) => {
|
|
return {
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
content: String(args.a + args.b)
|
|
}
|
|
]
|
|
}
|
|
}
|
|
)
|
|
|
|
app.all('/mcp', async (c) => {
|
|
const transport = new StreamableHTTPTransport()
|
|
await mcpServer.connect(transport)
|
|
return transport.handleRequest(c)
|
|
})
|
|
|
|
serve({ fetch: app.fetch, port: 8787 })
|
|
```
|
|
|
|
<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 MCP 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 MCP server to Agentic
|
|
|
|
Now that you have a publicly available MCP server, you can follow the [existing MCP server guide](/publishing/guides/existing-mcp-server) to deploy it to Agentic.
|