Skip to content

feat: added @hono/mcp package #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 17 additions & 61 deletions examples/mcp/hono-mcp/netlify/functions/hono-mcp-server.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,38 @@
import { Hono } from "hono";
import { handle } from 'hono/netlify'
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { toFetchResponse, toReqRes } from "fetch-to-node";
import { StreamableHTTPTransport } from "@hono/mcp"
import { setupMCPServer } from "../mcp-server";
import { HTTPException } from "hono/http-exception"

const app = new Hono();

app.post("/mcp", async (c) => {
// Convert the Request object into a Node.js Request object
const { req, res } = toReqRes(c.req.raw);

app.all("/mcp", async (c) => {
const server = setupMCPServer();

try {
const transport: StreamableHTTPServerTransport =
new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
});

await server.connect(transport);

await transport.handleRequest(req, res, await c.req.json());
const transport = new StreamableHTTPTransport();
await server.connect(transport);
return transport.handleRequest(c);
});

res.on("close", () => {
console.log("Request closed");
transport.close();
server.close();
});
app.onError((err, c) => {
console.log(err.message)

return toFetchResponse(res);
} catch (e) {
console.error(e);
return c.json(
{
jsonrpc: "2.0",
error: {
code: -32603,
message: "Internal server error",
},
id: null,
},
{ status: 500 }
);
if (err instanceof HTTPException && err.res) {
return err.res
}
});

app.get("/mcp", async (c) => {
// For the stateless server, GET requests are used to initialize
// SSE connections which are stateful. Therefore, we don't need
// to handle GET requests but we can signal to the client this error.
console.log("Received GET MCP request");
return c.json(
{
jsonrpc: "2.0",
jsonrpc: '2.0',
error: {
code: -32000,
message: "Method not allowed.",
code: -32603,
message: 'Internal server error',
},
id: null,
},
{ status: 405 }
);
});

app.delete("/mcp", async (c) => {
console.log("Received DELETE MCP request");
return c.json(
{
jsonrpc: "2.0",
error: {
code: -32000,
message: "Method not allowed.",
},
id: null,
},
{ status: 405 }
);
});
500
)
})

export default handle(app);

Expand Down
77 changes: 68 additions & 9 deletions examples/mcp/hono-mcp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/mcp/hono-mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"type": "module",
"license": "ISC",
"dependencies": {
"@hono/mcp": "^0.1.0",
"@modelcontextprotocol/sdk": "^1.10.2",
"fetch-to-node": "^2.0.0",
"hono": "^4.7.7",
"zod": "^3.24.3"
}
}
}