mcpField
S
Stateless Hono MCP Server

Stateless Hono MCP Server

本地服务
github.com/mhart开发工具10279 周前更新

关于 Stateless Hono MCP Server

一个基于 Hono 的示例 MCP 服务,使用 Streamable HTTP 传输,改编自官方 Express 示例。它以无状态方式运行,可部署到 Cloudflare Workers 或其他 Hono 支持的运行环境,适合作为参考实现。

主要功能

  1. 01基于 Hono 框架的 MCP 服务
  2. 02使用 Streamable HTTP 传输
  3. 03无状态运行模式
  4. 04借助 fetch-to-node 完成请求转换
  5. 05可部署到 Cloudflare Workers

适用场景

  1. 01学习如何在 Hono 中搭建 MCP 服务
  2. 02将 MCP 服务部署到 Cloudflare Workers 进行测试
  3. 03使用示例客户端验证 MCP 服务的工具与提示词

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

无状态 Hono MCP 服务

部署到 Cloudflare

一个使用 Streamable HTTP 的 Hono MCP 服务示例,基于官方 Express 示例, 使用 fetch-to-node 进行转换,可部署到 Cloudflare Workers(以及其他任何可以运行 Hono 的地方)。

与 Express 示例相比,实际改动只有:

- import express, { Request, Response } from 'express';
+ import { Hono } from 'hono';
+ import { toFetchResponse, toReqRes } from 'fetch-to-node';

// ...

- const app = express();
- app.use(express.json());
+ const app = new Hono();

- app.post('/mcp', async (req: Request, res: Response) => {
+ app.post('/mcp', async (c) => {
+   const { req, res } = toReqRes(c.req.raw);

    const server = getServer();
    try {
      const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({
        sessionIdGenerator: undefined,
      });
      await server.connect(transport);
-     await transport.handleRequest(req, res, req.body);
+     await transport.handleRequest(req, res, await c.req.json());
      res.on('close', () => {
        console.log('Request closed');
        transport.close();
        server.close();
      });
+     return toFetchResponse(res);
    } catch (error) {

使用示例 MCP 客户端进行测试

在一个终端中:

npm start

在另一个终端中:

node node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.js

这会尝试连接到运行在端口 3000 上的 MCP 服务。你可以使用 connect <url>/mcp 连接到其他主机或端口。

然后,你可以运行 list-prompts 或 list-tools 等命令,验证 MCP 服务是否正常运行。

部署

npm run deploy

以下是在部署到 Cloudflare Workers 的 Hono MCP 服务上进行的一次示例会话:

> connect https://mcp-hono-stateless.michael.workers.dev/mcp
Connecting to https://mcp-hono-stateless.michael.workers.dev/mcp...
Transport created with session ID: undefined
Connected to MCP server

> list-tools
Available tools:
  - start-notification-stream: Starts sending periodic notifications for testing resumability

> list-prompts
Available prompts:
  - greeting-template: A simple greeting prompt template

> call-tool start-notification-stream
Calling tool 'start-notification-stream' with args: {}

Notification #1: info - Periodic notification #1 at 2025-04-22T16:20:50.178Z
>
Notification #2: info - Periodic notification #2 at 2025-04-22T16:20:50.278Z
>
Notification #3: info - Periodic notification #3 at 2025-04-22T16:20:50.378Z
>
Notification #4: info - Periodic notification #4 at 2025-04-22T16:20:50.478Z
>
Notification #5: info - Periodic notification #5 at 2025-04-22T16:20:50.578Z
>
Notification #6: info - Periodic notification #6 at 2025-04-22T16:20:50.678Z
>
Notification #7: info - Periodic notification #7 at 2025-04-22T16:20:50.778Z
>
Notification #8: info - Periodic notification #8 at 2025-04-22T16:20:50.878Z
>
Notification #9: info - Periodic notification #9 at 2025-04-22T16:20:50.978Z
>
Notification #10: info - Periodic notification #10 at 2025-04-22T16:20:51.078Z
> Tool result:
  Started sending periodic notifications every 100ms

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。