-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: fetch transport #1209
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
Merged
+1,220
−870
Merged
feat: fetch transport #1209
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3014283
feat: web standards based transport
mattzcarey 24e96ed
rename to webstandards
mattzcarey f5bbe02
fix: optional stuff
mattzcarey d3e796a
Merge branch 'main' into feat-fetch-transport
mattzcarey e3e5af3
data in error response
mattzcarey 4dfa889
close the stream on error
mattzcarey 3e2862e
Merge branch 'main' into feat-fetch-transport
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Example MCP server using Hono with WebStandardStreamableHTTPServerTransport | ||
| * | ||
| * This example demonstrates using the Web Standard transport directly with Hono, | ||
| * which works on any runtime: Node.js, Cloudflare Workers, Deno, Bun, etc. | ||
| * | ||
| * Run with: npx tsx src/examples/server/honoWebStandardStreamableHttp.ts | ||
| */ | ||
|
|
||
| import { Hono } from 'hono'; | ||
| import { cors } from 'hono/cors'; | ||
| import { serve } from '@hono/node-server'; | ||
| import * as z from 'zod/v4'; | ||
| import { McpServer } from '../../server/mcp.js'; | ||
| import { WebStandardStreamableHTTPServerTransport } from '../../server/webStandardStreamableHttp.js'; | ||
| import { CallToolResult } from '../../types.js'; | ||
|
|
||
| // Create the MCP server | ||
| const server = new McpServer({ | ||
| name: 'hono-webstandard-mcp-server', | ||
| version: '1.0.0' | ||
| }); | ||
|
|
||
| // Register a simple greeting tool | ||
| server.registerTool( | ||
| 'greet', | ||
| { | ||
| title: 'Greeting Tool', | ||
| description: 'A simple greeting tool', | ||
| inputSchema: { name: z.string().describe('Name to greet') } | ||
| }, | ||
| async ({ name }): Promise<CallToolResult> => { | ||
| return { | ||
| content: [{ type: 'text', text: `Hello, ${name}! (from Hono + WebStandard transport)` }] | ||
| }; | ||
| } | ||
| ); | ||
|
|
||
| // Create a stateless transport (no options = no session management) | ||
| const transport = new WebStandardStreamableHTTPServerTransport(); | ||
|
|
||
| // Create the Hono app | ||
| const app = new Hono(); | ||
|
|
||
| // Enable CORS for all origins | ||
| app.use( | ||
| '*', | ||
| cors({ | ||
| origin: '*', | ||
| allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'], | ||
| allowHeaders: ['Content-Type', 'mcp-session-id', 'Last-Event-ID', 'mcp-protocol-version'], | ||
| exposeHeaders: ['mcp-session-id', 'mcp-protocol-version'] | ||
| }) | ||
| ); | ||
|
|
||
| // Health check endpoint | ||
| app.get('/health', c => c.json({ status: 'ok' })); | ||
|
|
||
| // MCP endpoint | ||
| app.all('/mcp', c => transport.handleRequest(c.req.raw)); | ||
|
|
||
| // Start the server | ||
| const PORT = process.env.MCP_PORT ? parseInt(process.env.MCP_PORT, 10) : 3000; | ||
|
|
||
| server.connect(transport).then(() => { | ||
| console.log(`Starting Hono MCP server on port ${PORT}`); | ||
| console.log(`Health check: http://localhost:${PORT}/health`); | ||
| console.log(`MCP endpoint: http://localhost:${PORT}/mcp`); | ||
|
|
||
| serve({ | ||
| fetch: app.fetch, | ||
| port: PORT | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked into this library a bit https://github.com/honojs/hono
While it doesn't have a huge number of stars (~580), it's actually not a very big package - ~1.5k lines of code, mostly to convert between Node.js and WebStandards requests. 3.5M DLs on
npm(cf. ~10M for TypeScript SDK). The maintainer is also the creator ofhonoand the package has had a very regular release cadence.Compared to our other runtime dependencies it is a bit fresher / less mature. It's in the same ballpark as our
eventsource(SSE) andeventsource-parser(SHTTP) dependencies roughly in terms of adoption, but those don't have an org likehonobehind them, so arguably this new dependency has more backing than other ones we already depend on.So in all I think this is fine to achieve our goal of decoupling from Express. What's worth discussing is whether we ship this in v1 or wait for v2 with this for risk management in case this causes unexpected regressions.
@mattzcarey @KKonstantinov have either of you potentially used this in production before?