@@ -18,12 +18,13 @@ const VERSION = '0.0.13'
1818export async function main ( ) {
1919 // Parse global flags once, then branch on subcommand
2020 const flags = parseArgs ( Deno . args , {
21- string : [ 'port' ] ,
22- default : { port : '3001' , mount : false } ,
21+ string : [ 'port' , 'mount' ] ,
22+ default : { port : '3001' } ,
2323 } )
2424 const mode = ( flags . _ [ 0 ] as string | undefined ) ?? ''
2525 const port = parseInt ( flags . port as string )
26- const mount = flags . mount as string | boolean
26+ const rawMount = flags . mount as string | undefined
27+ const mount : string | boolean = rawMount === undefined ? false : rawMount === '' ? true : rawMount
2728
2829 if ( mode === 'stdio' ) {
2930 await runStdio ( mount )
@@ -32,7 +33,7 @@ export async function main() {
3233 } else if ( mode === 'sse' ) {
3334 runSse ( port , mount )
3435 } else if ( mode === 'warmup' ) {
35- await warmup ( mount )
36+ await warmup ( )
3637 } else {
3738 console . error (
3839 `\
@@ -58,8 +59,11 @@ function createServer(rootDir: string | null, mount: string | boolean): McpServe
5859 version : VERSION ,
5960 } ,
6061 {
61- instructions : 'Call the "run_python_code" tool with the Python code to run.' ,
62+ instructions : 'Call the "run_python_code" tool with the Python code to run.' +
63+ ( rootDir != null ? ` Persistent storage is mounted at: "${ rootDir } ".` : '' ) ,
6264 capabilities : {
65+ resources : { } ,
66+ tools : { } ,
6367 logging : { } ,
6468 } ,
6569 } ,
@@ -128,6 +132,36 @@ function httpSetJsonResponse(
128132 res . end ( )
129133}
130134
135+ function addDirCleanupCallback ( server : http . Server | StdioServerTransport , dir : string ) {
136+ let cleaned = false
137+ const cleanup = ( ) => {
138+ if ( cleaned ) return
139+ cleaned = true
140+ try {
141+ Deno . removeSync ( dir , { recursive : true } )
142+ } catch {
143+ // ignore
144+ }
145+ }
146+ if ( server instanceof http . Server ) {
147+ server . on ( 'close' , cleanup )
148+ } else {
149+ server . onclose = cleanup
150+ }
151+ const handleSig = ( ) => {
152+ try {
153+ server . close ( ( ) => { } )
154+ } catch {
155+ // ignore
156+ }
157+ cleanup ( )
158+ Deno . exit ( )
159+ }
160+ Deno . addSignalListener ( 'SIGINT' , handleSig )
161+ Deno . addSignalListener ( 'SIGTERM' , handleSig )
162+ addEventListener ( 'unload' , cleanup )
163+ }
164+
131165/*
132166 * Run the MCP server using the Streamable HTTP transport
133167 */
@@ -215,16 +249,14 @@ function runStreamableHttp(port: number, mount: string | boolean) {
215249 }
216250 } )
217251
218- // Cleanup root dir on server close
252+ // Cleanup root dir on server close and on process signals
219253 if ( rootDir != null ) {
220- server . on ( 'close' , ( ) => {
221- Deno . removeSync ( rootDir , { recursive : true } )
222- } )
254+ addDirCleanupCallback ( server , rootDir )
223255 }
224256
225257 server . listen ( port , ( ) => {
226258 console . log (
227- `Running MCP Run Python version ${ VERSION } with Streamable HTTP transport on port ${ port } ` ,
259+ `Running MCP Run Python version ${ VERSION } with SSE transport on port ${ port } . ` ,
228260 )
229261 } )
230262}
@@ -275,16 +307,14 @@ function runSse(port: number, mount: string | boolean) {
275307 }
276308 } )
277309
278- // Cleanup root dir on server close
310+ // Cleanup root dir on server close and on process signals
279311 if ( rootDir != null ) {
280- server . on ( 'close' , ( ) => {
281- Deno . removeSync ( rootDir , { recursive : true } )
282- } )
312+ addDirCleanupCallback ( server , rootDir )
283313 }
284314
285315 server . listen ( port , ( ) => {
286316 console . log (
287- `Running MCP Run Python version ${ VERSION } with SSE transport on port ${ port } ` ,
317+ `Running MCP Run Python version ${ VERSION } with SSE transport on port ${ port } . ` ,
288318 )
289319 } )
290320}
@@ -297,11 +327,9 @@ async function runStdio(mount: string | boolean) {
297327 const mcpServer = createServer ( rootDir , mount )
298328 const transport = new StdioServerTransport ( )
299329
300- // Cleanup root dir on server close
330+ // Cleanup root dir on server close and on process signals
301331 if ( rootDir != null ) {
302- transport . onclose = ( ) => {
303- Deno . removeSync ( rootDir , { recursive : true } )
304- }
332+ addDirCleanupCallback ( transport , rootDir )
305333 }
306334
307335 await mcpServer . connect ( transport )
@@ -310,10 +338,9 @@ async function runStdio(mount: string | boolean) {
310338/*
311339 * Run pyodide to download packages which can otherwise interrupt the server
312340 */
313- async function warmup ( mount ?: string | boolean ) {
341+ async function warmup ( ) {
314342 console . error (
315- `Running warmup script for MCP Run Python version ${ VERSION } ...` +
316- ( mount ? ` (mount: ${ typeof mount === 'string' ? mount : 'enabled' } )` : '' ) ,
343+ `Running warmup script for MCP Run Python version ${ VERSION } ...` ,
317344 )
318345 const code = `
319346import numpy
0 commit comments