@@ -20,27 +20,28 @@ const VERSION = '0.0.13'
2020export async function main ( ) {
2121 const { args } = Deno
2222 const flags = parseArgs ( Deno . args , {
23- string : [ 'deps' , 'return-mode' , 'port' ] ,
23+ string : [ 'deps' , 'return-mode' , 'port' , 'index-urls' ] ,
2424 default : { port : '3001' , 'return-mode' : 'xml' } ,
2525 } )
2626 const deps = flags . deps ?. split ( ',' ) ?? [ ]
27+ const indexUrls = flags [ 'index-urls' ] ?. split ( ',' ) ?? [ ]
2728 if ( args . length >= 1 ) {
2829 if ( args [ 0 ] === 'stdio' ) {
29- await runStdio ( deps , flags [ 'return-mode' ] )
30+ await runStdio ( deps , indexUrls , flags [ 'return-mode' ] )
3031 return
3132 } else if ( args [ 0 ] === 'streamable_http' ) {
3233 const port = parseInt ( flags . port )
33- runStreamableHttp ( port , deps , flags [ 'return-mode' ] , false )
34+ runStreamableHttp ( port , deps , indexUrls , flags [ 'return-mode' ] , false )
3435 return
3536 } else if ( args [ 0 ] === 'streamable_http_stateless' ) {
3637 const port = parseInt ( flags . port )
37- runStreamableHttp ( port , deps , flags [ 'return-mode' ] , true )
38+ runStreamableHttp ( port , deps , indexUrls , flags [ 'return-mode' ] , true )
3839 return
3940 } else if ( args [ 0 ] === 'example' ) {
40- await example ( deps )
41+ await example ( deps , indexUrls )
4142 return
4243 } else if ( args [ 0 ] === 'noop' ) {
43- await installDeps ( deps )
44+ await installDeps ( deps , indexUrls )
4445 return
4546 }
4647 }
@@ -51,17 +52,18 @@ Invalid arguments: ${args.join(' ')}
5152Usage: deno ... deno/main.ts [stdio|streamable_http|streamable_http_stateless|example|noop]
5253
5354options:
54- --port <port> Port to run the HTTP server on (default: 3001)
55- --deps <deps> Comma separated list of dependencies to install
56- --return-mode <xml/json> Return mode for output data (default: xml)` ,
55+ --port <port> Port to run the HTTP server on (default: 3001)
56+ --deps <deps> Comma separated list of dependencies to install
57+ --index-urls <urls> Comma separated list of package index URLs (tried in order before PyPI)
58+ --return-mode <xml/json> Return mode for output data (default: xml)` ,
5759 )
5860 Deno . exit ( 1 )
5961}
6062
6163/*
6264 * Create an MCP server with the `run_python_code` tool registered.
6365 */
64- function createServer ( deps : string [ ] , returnMode : string ) : McpServer {
66+ function createServer ( deps : string [ ] , indexUrls : string [ ] , returnMode : string ) : McpServer {
6567 const runCode = new RunCode ( )
6668 const server = new McpServer (
6769 {
@@ -106,6 +108,7 @@ The code will be executed with Python 3.13.
106108 const logPromises : Promise < void > [ ] = [ ]
107109 const result = await runCode . run (
108110 deps ,
111+ indexUrls ,
109112 ( level , data ) => {
110113 if ( LogLevels . indexOf ( level ) >= LogLevels . indexOf ( setLogLevel ) ) {
111114 logPromises . push ( server . server . sendLoggingMessage ( { level, data } ) )
@@ -171,14 +174,14 @@ function httpSetJsonResponse(res: http.ServerResponse, status: number, text: str
171174/*
172175 * Run the MCP server using the Streamable HTTP transport
173176 */
174- function runStreamableHttp ( port : number , deps : string [ ] , returnMode : string , stateless : boolean ) : void {
175- const server = ( stateless ? createStatelessHttpServer : createStatefulHttpServer ) ( deps , returnMode )
177+ function runStreamableHttp ( port : number , deps : string [ ] , indexUrls : string [ ] , returnMode : string , stateless : boolean ) : void {
178+ const server = ( stateless ? createStatelessHttpServer : createStatefulHttpServer ) ( deps , indexUrls , returnMode )
176179 server . listen ( port , ( ) => {
177180 console . log ( `Listening on port ${ port } ` )
178181 } )
179182}
180183
181- function createStatelessHttpServer ( deps : string [ ] , returnMode : string ) : http . Server {
184+ function createStatelessHttpServer ( deps : string [ ] , indexUrls : string [ ] , returnMode : string ) : http . Server {
182185 return http . createServer ( async ( req , res ) => {
183186 const url = httpGetUrl ( req )
184187
@@ -188,7 +191,7 @@ function createStatelessHttpServer(deps: string[], returnMode: string): http.Ser
188191 }
189192
190193 try {
191- const mcpServer = createServer ( deps , returnMode )
194+ const mcpServer = createServer ( deps , indexUrls , returnMode )
192195 const transport : StreamableHTTPServerTransport = new StreamableHTTPServerTransport ( {
193196 sessionIdGenerator : undefined ,
194197 } )
@@ -211,10 +214,10 @@ function createStatelessHttpServer(deps: string[], returnMode: string): http.Ser
211214 } )
212215}
213216
214- function createStatefulHttpServer ( deps : string [ ] , returnMode : string ) : http . Server {
217+ function createStatefulHttpServer ( deps : string [ ] , indexUrls : string [ ] , returnMode : string ) : http . Server {
215218 // Stateful mode with session management
216219 // https://github.com/modelcontextprotocol/typescript-sdk?tab=readme-ov-file#with-session-management
217- const mcpServer = createServer ( deps , returnMode )
220+ const mcpServer = createServer ( deps , indexUrls , returnMode )
218221 const transports : { [ sessionId : string ] : StreamableHTTPServerTransport } = { }
219222
220223 return http . createServer ( async ( req , res ) => {
@@ -293,19 +296,20 @@ function createStatefulHttpServer(deps: string[], returnMode: string): http.Serv
293296/*
294297 * Run the MCP server using the Stdio transport.
295298 */
296- async function runStdio ( deps : string [ ] , returnMode : string ) {
297- const mcpServer = createServer ( deps , returnMode )
299+ async function runStdio ( deps : string [ ] , indexUrls : string [ ] , returnMode : string ) {
300+ const mcpServer = createServer ( deps , indexUrls , returnMode )
298301 const transport = new StdioServerTransport ( )
299302 await mcpServer . connect ( transport )
300303}
301304
302305/*
303306 * Run pyodide to download and install dependencies.
304307 */
305- async function installDeps ( deps : string [ ] ) {
308+ async function installDeps ( deps : string [ ] , indexUrls : string [ ] ) {
306309 const runCode = new RunCode ( )
307310 const result = await runCode . run (
308311 deps ,
312+ indexUrls ,
309313 ( level , data ) => console . error ( `${ level } |${ data } ` ) ,
310314 )
311315 if ( result . status !== 'success' ) {
@@ -317,7 +321,7 @@ async function installDeps(deps: string[]) {
317321/*
318322 * Run a short example script that requires numpy.
319323 */
320- async function example ( deps : string [ ] ) {
324+ async function example ( deps : string [ ] , indexUrls : string [ ] ) {
321325 console . error (
322326 `Running example script for MCP Run Python version ${ VERSION } ...` ,
323327 )
330334 const runCode = new RunCode ( )
331335 const result = await runCode . run (
332336 deps ,
337+ indexUrls ,
333338 // use warn to avoid recursion since console.log is patched in runCode
334339 ( level , data ) => console . warn ( `${ level } : ${ data } ` ) ,
335340 { name : 'example.py' , content : code } ,
0 commit comments