@@ -25,6 +25,7 @@ def run_mcp_server(
2525 dependencies : list [str ] | None = None ,
2626 return_mode : Literal ['json' , 'xml' ] = 'xml' ,
2727 deps_log_handler : LogHandler | None = None ,
28+ allow_networking : bool = True ,
2829) -> int :
2930 """Install dependencies then run the mcp-run-python server.
3031
@@ -34,9 +35,15 @@ def run_mcp_server(
3435 dependencies: The dependencies to install.
3536 return_mode: The mode to return tool results in.
3637 deps_log_handler: Optional function to receive logs emitted while installing dependencies.
38+ allow_networking: Whether to allow networking when running provided python code.
3739 """
3840 with prepare_deno_env (
39- mode , dependencies = dependencies , http_port = http_port , return_mode = return_mode , deps_log_handler = deps_log_handler
41+ mode ,
42+ dependencies = dependencies ,
43+ http_port = http_port ,
44+ return_mode = return_mode ,
45+ deps_log_handler = deps_log_handler ,
46+ allow_networking = allow_networking ,
4047 ) as env :
4148 if mode == 'streamable_http' :
4249 logger .info ('Running mcp-run-python via %s on port %d...' , mode , http_port )
@@ -66,6 +73,7 @@ def prepare_deno_env(
6673 dependencies : list [str ] | None = None ,
6774 return_mode : Literal ['json' , 'xml' ] = 'xml' ,
6875 deps_log_handler : LogHandler | None = None ,
76+ allow_networking : bool = True ,
6977) -> Iterator [DenoEnv ]:
7078 """Prepare the deno environment for running the mcp-run-python server with Deno.
7179
@@ -79,6 +87,8 @@ def prepare_deno_env(
7987 dependencies: The dependencies to install.
8088 return_mode: The mode to return tool results in.
8189 deps_log_handler: Optional function to receive logs emitted while installing dependencies.
90+ allow_networking: Whether the prepared DenoEnv should allow networking when running code.
91+ Note that we always allow networking during environment initialization to install dependencies.
8292
8393 Returns:
8494 Yields the deno environment details.
@@ -105,7 +115,13 @@ def prepare_deno_env(
105115 if p .returncode != 0 :
106116 raise RuntimeError (f'`deno run ...` returned a non-zero exit code { p .returncode } : { "" .join (stdout )} ' )
107117
108- args = _deno_run_args (mode , http_port = http_port , dependencies = dependencies , return_mode = return_mode )
118+ args = _deno_run_args (
119+ mode ,
120+ http_port = http_port ,
121+ dependencies = dependencies ,
122+ return_mode = return_mode ,
123+ allow_networking = allow_networking ,
124+ )
109125 yield DenoEnv (cwd , args )
110126
111127 finally :
@@ -120,6 +136,7 @@ async def async_prepare_deno_env(
120136 dependencies : list [str ] | None = None ,
121137 return_mode : Literal ['json' , 'xml' ] = 'xml' ,
122138 deps_log_handler : LogHandler | None = None ,
139+ allow_networking : bool = True ,
123140) -> AsyncIterator [DenoEnv ]:
124141 """Async variant of `prepare_deno_env`."""
125142 ct = await _asyncify (
@@ -129,6 +146,7 @@ async def async_prepare_deno_env(
129146 dependencies = dependencies ,
130147 return_mode = return_mode ,
131148 deps_log_handler = deps_log_handler ,
149+ allow_networking = allow_networking ,
132150 )
133151 try :
134152 yield await _asyncify (ct .__enter__ )
@@ -157,10 +175,12 @@ def _deno_run_args(
157175 http_port : int | None = None ,
158176 dependencies : list [str ] | None = None ,
159177 return_mode : Literal ['json' , 'xml' ] = 'xml' ,
178+ allow_networking : bool = True ,
160179) -> list [str ]:
161- args = [
162- 'run' ,
163- '--allow-net' ,
180+ args = ['run' ]
181+ if allow_networking :
182+ args += ['--allow-net' ]
183+ args += [
164184 '--allow-read=./node_modules' ,
165185 '--node-modules-dir=auto' ,
166186 'src/main.ts' ,
0 commit comments