@@ -28,7 +28,7 @@ import * as Option from "effect/Option";
2828import * as Cause from "effect/Cause" ;
2929
3030import { ExecutorApi } from "@executor/api" ;
31- import { createServerHandlers , runMcpStdioServer , getExecutor } from "@executor/local" ;
31+ import { startServer , runMcpStdioServer , getExecutor } from "@executor/local" ;
3232
3333// ---------------------------------------------------------------------------
3434// Constants
@@ -39,9 +39,6 @@ const { version: CLI_VERSION } = await import("../package.json");
3939const DEFAULT_PORT = 4788 ;
4040const DEFAULT_BASE_URL = `http://localhost:${ DEFAULT_PORT } ` ;
4141
42- // Embedded web UI — baked into compiled binaries via `with { type: "file" }`
43- import embeddedWebUI from "./embedded-web-ui.gen" ;
44-
4542// ---------------------------------------------------------------------------
4643// Helpers
4744// ---------------------------------------------------------------------------
@@ -57,38 +54,13 @@ const waitForShutdownSignal = () =>
5754 } ) ;
5855 } ) ;
5956
60- const appendUrlPath = ( baseUrl : string , pathname : string ) : string =>
61- new URL ( pathname , baseUrl . endsWith ( "/" ) ? baseUrl : `${ baseUrl } /` ) . toString ( ) ;
62-
63- const renderSessionSummary = ( kind : "web" | "mcp" , baseUrl : string ) : string => {
64- const displayKind = kind === "mcp" ? "MCP" : "web" ;
65- const primaryLabel = kind === "web" ? "Web" : "MCP" ;
66- const primaryUrl = kind === "web" ? baseUrl : appendUrlPath ( baseUrl , "mcp" ) ;
67- const secondaryLabel = kind === "web" ? "MCP" : "Web" ;
68- const secondaryUrl = kind === "web" ? appendUrlPath ( baseUrl , "mcp" ) : baseUrl ;
69- const guidance =
70- kind === "web"
71- ? "Keep this process running while you use the browser session."
72- : "Use this MCP URL in your client and keep this process running." ;
73-
74- return [
75- `Executor ${ displayKind } session is ready.` ,
76- `${ primaryLabel } : ${ primaryUrl } ` ,
77- `${ secondaryLabel } : ${ secondaryUrl } ` ,
78- `OpenAPI: ${ appendUrlPath ( baseUrl , "api/docs" ) } ` ,
79- "" ,
80- guidance ,
81- "Press Ctrl+C to stop." ,
82- ] . join ( "\n" ) ;
83- } ;
84-
8557// ---------------------------------------------------------------------------
8658// Background server management
8759// ---------------------------------------------------------------------------
8860
8961const isServerReachable = async ( baseUrl : string ) : Promise < boolean > => {
9062 try {
91- const res = await fetch ( `${ baseUrl } /api/docs ` , { signal : AbortSignal . timeout ( 2000 ) } ) ;
63+ const res = await fetch ( `${ baseUrl } /api/scope ` , { signal : AbortSignal . timeout ( 2000 ) } ) ;
9264 return res . ok ;
9365 } catch {
9466 return false ;
@@ -133,121 +105,27 @@ const ensureServer = (baseUrl: string) =>
133105// ---------------------------------------------------------------------------
134106
135107const makeApiClient = ( baseUrl : string ) =>
136- HttpApiClient . make ( ExecutorApi , { baseUrl } ) . pipe (
108+ HttpApiClient . make ( ExecutorApi , { baseUrl : ` ${ baseUrl } /api` } ) . pipe (
137109 Effect . provide ( FetchHttpClient . layer ) ,
138110 ) ;
139111
140112// ---------------------------------------------------------------------------
141- // Static file serving from embedded web UI
142- // ---------------------------------------------------------------------------
143-
144- const WEB_DIST_DIR = resolve ( import . meta. dirname , "../../local/dist" ) ;
145-
146- const serveStatic = async ( pathname : string ) : Promise < Response | null > => {
147- const key = pathname . replace ( / ^ \/ / , "" ) ;
148-
149- // Compiled binary: serve from embedded bunfs
150- if ( embeddedWebUI ) {
151- const match = embeddedWebUI [ key ] ?? embeddedWebUI [ "index.html" ] ?? null ;
152- if ( ! match ) return null ;
153- const file = Bun . file ( match ) ;
154- return new Response ( file , {
155- headers : { "content-type" : file . type || "application/octet-stream" } ,
156- } ) ;
157- }
158-
159- // Dev mode: serve from apps/local/dist on disk
160- const filePath = resolve ( WEB_DIST_DIR , key ) ;
161- if ( ! filePath . startsWith ( WEB_DIST_DIR ) ) return null ;
162-
163- const file = Bun . file ( filePath ) ;
164- if ( await file . exists ( ) ) {
165- return new Response ( file , {
166- headers : { "content-type" : file . type || "application/octet-stream" } ,
167- } ) ;
168- }
169-
170- // SPA fallback
171- const index = Bun . file ( resolve ( WEB_DIST_DIR , "index.html" ) ) ;
172- if ( await index . exists ( ) ) {
173- return new Response ( index , { headers : { "content-type" : "text/html" } } ) ;
174- }
175-
176- return null ;
177- } ;
178-
179- // ---------------------------------------------------------------------------
180- // Host-header allowlist — blocks DNS rebinding attacks
113+ // Foreground session
181114// ---------------------------------------------------------------------------
182115
183- const ALLOWED_HOSTS = new Set ( [
184- "localhost" ,
185- "127.0.0.1" ,
186- "[::1]" ,
187- "::1" ,
188- ] ) ;
189-
190- const isAllowedHost = ( request : Request ) : boolean => {
191- const host = request . headers . get ( "host" ) ;
192- if ( ! host ) return true ; // no host header (e.g. HTTP/1.0) — allow
193- // Strip port ("localhost:4788" → "localhost")
194- const hostname = host . replace ( / : \d + $ / , "" ) ;
195- return ALLOWED_HOSTS . has ( hostname ) ;
196- } ;
197-
198- // ---------------------------------------------------------------------------
199- // Foreground session — API + MCP + Web UI on one Bun.serve()
200- // ---------------------------------------------------------------------------
201-
202- const runForegroundSession = ( input : { kind : "web" | "mcp" ; port : number } ) =>
116+ const runForegroundSession = ( input : { port : number } ) =>
203117 Effect . gen ( function * ( ) {
204- const handlers = yield * Effect . promise ( ( ) => createServerHandlers ( ) ) ;
205-
206- // Bind to loopback only — one server per address family so both
207- // "localhost" (which may resolve to ::1 on Windows) and 127.0.0.1 work,
208- // without exposing the server on the LAN.
209- const fetch = async ( request : Request ) : Promise < Response > => {
210- if ( ! isAllowedHost ( request ) ) {
211- return new Response ( "Forbidden" , { status : 403 } ) ;
212- }
213-
214- const url = new URL ( request . url ) ;
215-
216- if ( url . pathname . startsWith ( "/mcp" ) ) {
217- return handlers . mcp . handleRequest ( request ) ;
218- }
219-
220- if ( url . pathname . startsWith ( "/api/" ) || url . pathname === "/api" ) {
221- return handlers . api . handler ( request ) ;
222- }
223-
224- const staticResponse = await serveStatic ( url . pathname ) ;
225- if ( staticResponse ) return staticResponse ;
226-
227- return new Response ( "Not Found" , { status : 404 } ) ;
228- } ;
229-
230- const serverV4 = Bun . serve ( { port : input . port , hostname : "127.0.0.1" , fetch } ) ;
231-
232- // IPv6 loopback — may fail on systems without IPv6; that's fine.
233- let serverV6 : ReturnType < typeof Bun . serve > | null = null ;
234- try {
235- serverV6 = Bun . serve ( { port : input . port , hostname : "::1" , fetch } ) ;
236- } catch {
237- // IPv6 not available — continue with IPv4 only
238- }
239-
240- const server = serverV4 ;
118+ const server = yield * Effect . promise ( ( ) => startServer ( { port : input . port } ) ) ;
241119
242120 const baseUrl = `http://localhost:${ server . port } ` ;
243- console . log ( renderSessionSummary ( input . kind , baseUrl ) ) ;
121+ console . log ( `Executor is ready.` ) ;
122+ console . log ( `Web: ${ baseUrl } ` ) ;
123+ console . log ( `MCP: ${ baseUrl } /mcp` ) ;
124+ console . log ( `OpenAPI: ${ baseUrl } /api/docs` ) ;
125+ console . log ( `\nPress Ctrl+C to stop.` ) ;
244126
245127 yield * waitForShutdownSignal ( ) ;
246-
247- serverV4 . stop ( true ) ;
248- serverV6 ?. stop ( true ) ;
249- yield * Effect . promise ( ( ) => handlers . mcp . close ( ) ) ;
250- yield * Effect . promise ( ( ) => handlers . api . dispose ( ) ) ;
128+ yield * Effect . promise ( ( ) => server . stop ( ) ) ;
251129 } ) ;
252130
253131// ---------------------------------------------------------------------------
@@ -387,7 +265,7 @@ const webCommand = Command.make(
387265 } ,
388266 ( { port, scope } ) => Effect . gen ( function * ( ) {
389267 applyScope ( scope ) ;
390- yield * runForegroundSession ( { kind : "web" , port } ) ;
268+ yield * runForegroundSession ( { port } ) ;
391269 } ) ,
392270) . pipe ( Command . withDescription ( "Start a foreground web session" ) ) ;
393271
0 commit comments