|
| 1 | +# minimax-code-sdk |
| 2 | + |
| 3 | +Python SDK for driving the local [MiniMax Code](https://agent.minimax.io) agent (`mcode`). |
| 4 | + |
| 5 | +It mirrors the TypeScript SDK [`@minimax-ai/code-sdk`](../code-sdk): every Turn runs |
| 6 | +`mcode exec --output-format=stream-json` and continues the Session with `--session`. |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +- Python 3.10+ |
| 11 | +- The `mcode` CLI (0.6.3 or newer): `npm install -g @minimax-ai/code` |
| 12 | + |
| 13 | +The CLI is found from `MCodeOptions(bin=...)`, then the `MCODE_BIN` environment variable, |
| 14 | +then `PATH`. |
| 15 | + |
| 16 | +## Quick start |
| 17 | + |
| 18 | +```python |
| 19 | +import asyncio |
| 20 | +from minimax_code import MCode, ThreadOptions |
| 21 | + |
| 22 | + |
| 23 | +async def main() -> None: |
| 24 | + client = MCode() |
| 25 | + thread = client.start_thread(ThreadOptions(cwd=".", permission="smart")) |
| 26 | + |
| 27 | + turn = await thread.run("Summarize this repository in one sentence.") |
| 28 | + print(turn.final_response, turn.usage) |
| 29 | + |
| 30 | + # Same Session, next Turn. |
| 31 | + turn = await thread.run("Now list its top-level directories.") |
| 32 | + |
| 33 | + # Persist thread.id and continue later. |
| 34 | + later = client.resume_thread(thread.id) |
| 35 | + print((await later.run("What did I ask first?")).final_response) |
| 36 | + |
| 37 | + |
| 38 | +asyncio.run(main()) |
| 39 | +``` |
| 40 | + |
| 41 | +### Streaming |
| 42 | + |
| 43 | +```python |
| 44 | +streamed = await thread.run_streamed("Fix the failing test.") |
| 45 | +async for event in streamed: |
| 46 | + if event["type"] == "item.updated" and event["item"]["type"] == "agent_message": |
| 47 | + print(event["item"].get("contentDelta", ""), end="") |
| 48 | + elif event["type"] == "turn.failed": |
| 49 | + print("failed:", event.get("error")) |
| 50 | +``` |
| 51 | + |
| 52 | +Events are the CLI's stream-json events, passed through unchanged (unknown types included). |
| 53 | +`turn.failed` is an ordinary event; iteration raises only for process or protocol failures. |
| 54 | + |
| 55 | +### Cancellation |
| 56 | + |
| 57 | +- `streamed.cancel()` sends SIGINT; the stream ends with the CLI's final events. |
| 58 | +- Leaving the `async for` early, or cancelling the task running `thread.run()`, stops the CLI |
| 59 | + (SIGINT, then SIGKILL after a grace period). |
| 60 | +- `RunOptions(timeout_ms=...)` is enforced by the CLI. |
| 61 | + |
| 62 | +### Blocking API |
| 63 | + |
| 64 | +```python |
| 65 | +from minimax_code import SyncMCode |
| 66 | + |
| 67 | +with SyncMCode() as client: |
| 68 | + thread = client.start_thread() |
| 69 | + print(thread.run("hello").final_response) |
| 70 | + for event in thread.run_streamed("again"): |
| 71 | + print(event["type"]) |
| 72 | +``` |
| 73 | + |
| 74 | +## Options |
| 75 | + |
| 76 | +| Object | Fields | |
| 77 | +| --- | --- | |
| 78 | +| `MCodeOptions` | `bin`, `data_dir` (`MINIMAX_DATA_DIR`), `env` (replaces `os.environ`), `config_path` | |
| 79 | +| `ThreadOptions` | `cwd`, `model` (`provider/model`), `effort`, `permission` (`smart`/`full`/`off`), `mode`, `prompt_mode`, `system_prompt`, `append_system_prompt` | |
| 80 | +| `RunOptions` | `files`, `timeout_ms`, `max_steps` | |
| 81 | + |
| 82 | +## Errors |
| 83 | + |
| 84 | +All errors subclass `MCodeError` (with `exit_code`, `stderr`, `session_id`, `result`, `items`): |
| 85 | +`MCodeNotFoundError`, `MCodeVersionError`, `MCodeInvocationError`, `MCodeRunError` |
| 86 | +(`status`, `category`), `MCodeAbortError`, `MCodeProtocolError`, `ThreadBusyError`. |
| 87 | + |
| 88 | +## Limitations |
| 89 | + |
| 90 | +Each Turn is a separate process, so interactive permission prompts (`permission="ask"`) and |
| 91 | +custom in-process tools are not supported yet. A long-lived ACP transport is planned. |
| 92 | + |
| 93 | +## Development |
| 94 | + |
| 95 | +```bash |
| 96 | +uv sync |
| 97 | +uv run pytest |
| 98 | +uv run ruff check . && uv run ruff format --check . && uv run mypy |
| 99 | +``` |
| 100 | + |
| 101 | +End-to-end tests against a real CLI and model are opt-in: |
| 102 | +`MCODE_SDK_E2E=1 MCODE_BIN=/path/to/mcode uv run pytest -m e2e`. |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +MIT |
0 commit comments