File tree Expand file tree Collapse file tree 7 files changed +52
-14
lines changed
Expand file tree Collapse file tree 7 files changed +52
-14
lines changed Original file line number Diff line number Diff line change 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 },
Original file line number Diff line number Diff line change 66 */
77
88import { type DefaultEnv , withRuntime } from "@decocms/runtime" ;
9+ import { serve } from "@decocms/mcps-shared/serve" ;
910import {
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 ) ;
Original file line number Diff line number Diff line change 1212 */
1313import { readFileSync } from "fs" ;
1414import { type DefaultEnv , withRuntime } from "@decocms/runtime" ;
15+ import { serve } from "@decocms/mcps-shared/serve" ;
1516
1617import { tools } from "./tools/index.ts" ;
1718import { 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 ) ;
Original file line number Diff line number Diff line change 88 * with built-in fallback mechanisms, cost optimization, and provider routing.
99 */
1010import { type DefaultEnv , withRuntime } from "@decocms/runtime" ;
11+ import { serve } from "@decocms/mcps-shared/serve" ;
1112
1213import { 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 ) ;
Original file line number Diff line number Diff line change 55 */
66
77import { type DefaultEnv , withRuntime } from "@decocms/runtime" ;
8+ import { serve } from "@decocms/mcps-shared/serve" ;
89import {
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+ } ) ;
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments