| 10 | @pytest.mark.asyncio |
| 11 | @pytest.mark.mcp |
| 12 | async def test_code_execution(): |
| 13 | async with Client(MCP_URL) as client: |
| 14 | |
| 15 | async def call_tool(tool_name: str, **kwargs) -> Dict[str, Any]: |
| 16 | res = await client.call_tool(tool_name, kwargs or {}) |
| 17 | return getattr(res, "data", None) or {} |
| 18 | |
| 19 | ex = await call_tool( |
| 20 | "execute_python_code", |
| 21 | code='print("hi-from-code")\n', |
| 22 | timeout=15, |
| 23 | parent="/tmp/code", |
| 24 | filename="hello.py", |
| 25 | save_to_file=True, |
| 26 | ) |
| 27 | assert "error" not in ex, f"execute_python_code failed: {ex}" |
| 28 | assert "hi-from-code" in ex.get( |
| 29 | "stdout", "" |
| 30 | ), f"execute_python_code stdout mismatch: {ex}" |
| 31 | file_path = ex.get("file_path") |
| 32 | assert file_path, f"execute_python_code missing file_path: {ex}" |
| 33 | |
| 34 | # execute_python_script expects a session-relative path (e.g. /tmp/code/hello.py), |
| 35 | # not the absolute session-internal path returned by execute_python_code. |
| 36 | ex2 = await call_tool( |
| 37 | "execute_python_script", |
| 38 | script_path="/tmp/code/hello.py", |
| 39 | args=["--flag"], |
| 40 | timeout=15, |
| 41 | cwd_path="/tmp/code", |
| 42 | ) |
| 43 | assert "error" not in ex2, f"execute_python_script failed: {ex2}" |
| 44 | assert "hi-from-code" in ex2.get( |
| 45 | "stdout", "" |
| 46 | ), f"execute_python_script stdout mismatch: {ex2}" |