Back to Agentic

TypeScript Hono MCP

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

8.4.42.9 KB
Original Source

Hono is a popular open source TypeScript framework for building HTTP servers, and @hono/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 add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
bash
pnpm add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
bash
bun add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
bash
yarn add hono @hono/node-server @hono/mcp @modelcontextprotocol/sdk zod
</CodeGroup>

2. Create a Hono MCP Node.js 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 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 MCP server to Agentic

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