Skip to content

Commit 477d570

Browse files
committed
simplify sandbox example
1 parent 6db5c50 commit 477d570

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

examples/sandbox.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
from mcp_run_python import code_sandbox
22

33

4-
async def print_wait(level: str, message: str):
5-
await asyncio.sleep(1)
6-
print(f'[{level}] {message}')
4+
def print_wait(level: str, message: str):
5+
print(f'{level}: {message}')
6+
7+
8+
code = """
9+
import numpy
10+
a = numpy.array([1, 2, 3])
11+
print(a)
12+
a
13+
"""
714

815

916
async def main():
1017
async with code_sandbox(dependencies=['numpy'], print_handler=print_wait) as sandbox:
11-
for i in range(10):
12-
result = await sandbox.eval(f'import numpy as np\na = np.array([1, 2, {i}])\nprint(a)\na')
13-
print(result)
18+
result = await sandbox.eval(code)
19+
print(f'{result["status"].title()}:')
20+
if result['status'] == 'success':
21+
print(result['return_value'])
22+
else:
23+
print(result['error'])
1424

1525

1626
if __name__ == '__main__':

mcp_run_python/code_sandbox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
from .main import deno_args_prepare
1212

13-
JsonData: TypeAlias = 'str| bool | int | float | None | list[JsonData] | dict[str, JsonData]'
13+
JsonData: TypeAlias = 'str | bool | int | float | None | list[JsonData] | dict[str, JsonData]'
1414

1515

1616
class RunSuccess(TypedDict):
1717
status: Literal['success']
1818
output: list[str]
19-
returnValueJson: JsonData
19+
return_value: JsonData
2020

2121

2222
class RunError(TypedDict):

0 commit comments

Comments
 (0)