Skip to content

Commit 748141e

Browse files
authored
Merge pull request #73 from decocms/fix/sse-bun-serve
fix bun serve
2 parents bdea2cb + d7b3def commit 748141e

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
"version": "1.0.0",
342342
"devDependencies": {
343343
"@decocms/runtime": "0.25.1",
344+
"@types/bun": "^1.2.14",
344345
"vite": "7.2.0",
345346
"zod": "^3.24.3",
346347
},

mcp-studio/server/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { type DefaultEnv, withRuntime } from "@decocms/runtime";
9+
import { serve } from "@decocms/mcps-shared/serve";
910
import {
1011
type Env as DecoEnv,
1112
Scopes,
@@ -65,4 +66,4 @@ const runtime = withRuntime<Env, typeof StateSchema>({
6566
],
6667
});
6768

68-
export default runtime;
69+
serve(runtime.fetch);

meta-ads/server/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
import { readFileSync } from "fs";
1414
import { type DefaultEnv, withRuntime } from "@decocms/runtime";
15+
import { serve } from "@decocms/mcps-shared/serve";
1516

1617
import { tools } from "./tools/index.ts";
1718
import { META_API_VERSION, META_ADS_SCOPES, META_APP_ID } from "./constants.ts";
@@ -216,4 +217,4 @@ const runtime = withRuntime<Env>({
216217
tools,
217218
});
218219

219-
export default runtime;
220+
serve(runtime.fetch);

openrouter/server/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* with built-in fallback mechanisms, cost optimization, and provider routing.
99
*/
1010
import { type DefaultEnv, withRuntime } from "@decocms/runtime";
11+
import { serve } from "@decocms/mcps-shared/serve";
1112

1213
import { tools } from "./tools/index.ts";
1314

@@ -60,4 +61,4 @@ const runtime = withRuntime<Env>({
6061
tools,
6162
});
6263

63-
export default runtime;
64+
serve(runtime.fetch);

registry/server/main.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { type DefaultEnv, withRuntime } from "@decocms/runtime";
8+
import { serve } from "@decocms/mcps-shared/serve";
89
import {
910
type Env as DecoEnv,
1011
StateSchema as BaseStateSchema,
@@ -81,13 +82,9 @@ const runtime = withRuntime<Env, typeof StateSchema>({
8182
},
8283
});
8384

84-
const server = {
85-
fetch: (req: Request) => {
86-
if (new URL(req.url).pathname === "/_healthcheck") {
87-
return new Response("OK", { status: 200 });
88-
}
89-
return runtime.fetch(req, { ...process.env } as Env, {});
90-
},
91-
};
92-
93-
export default server;
85+
serve((req: Request) => {
86+
if (new URL(req.url).pathname === "/_healthcheck") {
87+
return new Response("OK", { status: 200 });
88+
}
89+
return runtime.fetch(req, { ...process.env } as Env, {});
90+
});

shared/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
"./video-generators": "./video-generators/index.ts",
1717
"./audio-transcribers": "./audio-transcribers/index.ts",
1818
"./storage": "./storage/index.ts",
19-
"./search-ai": "./search-ai/index.ts"
19+
"./search-ai": "./search-ai/index.ts",
20+
"./serve": "./serve.ts"
2021
},
2122
"devDependencies": {
23+
"@types/bun": "^1.2.14",
2224
"vite": "7.2.0",
2325
"@decocms/runtime": "0.25.1",
2426
"zod": "^3.24.3"

shared/serve.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference types="bun" />
2+
3+
/**
4+
* Shared Bun.serve utility for MCP servers
5+
*
6+
* This utility provides a consistent way to serve MCP applications
7+
* with the correct configuration for SSE endpoints and K8s compatibility.
8+
*/
9+
10+
// Using 'any' for additional args to support both simple fetch handlers
11+
// and runtime.fetch which expects (req, env, ctx)
12+
// biome-ignore lint/suspicious/noExplicitAny: Required for compatibility with runtime.fetch signature
13+
type Fetcher = (req: Request, ...args: any[]) => Response | Promise<Response>;
14+
15+
/**
16+
* Starts a Bun server with the provided fetch handler.
17+
*
18+
* Configures the server with:
19+
* - idleTimeout: 0 (required for SSE endpoints like notifications)
20+
* - hostname: 0.0.0.0 (required for K8s)
21+
* - port: process.env.PORT or 8001
22+
* - development mode based on NODE_ENV
23+
*
24+
* @param fetcher - The fetch handler function
25+
*/
26+
export function serve(fetcher: Fetcher) {
27+
Bun.serve({
28+
// This was necessary because MCP has SSE endpoints (like notification) that disconnects after 10 seconds (default bun idle timeout)
29+
idleTimeout: 0,
30+
port: process.env.PORT || 8001,
31+
hostname: "0.0.0.0", // Listen on all network interfaces (required for K8s)
32+
fetch: fetcher,
33+
development: process.env.NODE_ENV !== "production",
34+
});
35+
}

0 commit comments

Comments
 (0)