|
| 1 | +# mitos Go SDK |
| 2 | + |
| 3 | +A thin, dependency-free (standard-library only) Go client for the standalone and |
| 4 | +hosted mitos sandbox-server REST API. It mirrors the direct-mode surface of the |
| 5 | +Python SDK (`sdk/python/mitos/direct.py`), the TypeScript SDK |
| 6 | +(`sdk/typescript/src/server.ts`), the Ruby SDK (`sdk/ruby`), the Rust SDK |
| 7 | +(`sdk/rust`), and the Java SDK (`sdk/java`): create a template, fork a sandbox, |
| 8 | +run `exec`, list, and terminate. |
| 9 | + |
| 10 | +The SDK uses only the Go standard library (`net/http`, `encoding/json`, |
| 11 | +`crypto/rand`, `os`), so there are no third-party dependencies. It lives in its |
| 12 | +own nested Go module, so importing it does NOT pull the rest of the |
| 13 | +`mitos.run/mitos` repository (the controller, forkd, and their dependencies) |
| 14 | +into your build. |
| 15 | + |
| 16 | +## Scope |
| 17 | + |
| 18 | +This SDK covers DIRECT mode only: the standalone `cmd/sandbox-server` and the |
| 19 | +hosted control plane at `https://mitos.run`. The Kubernetes / cluster mode (the |
| 20 | +controller, forkd, and the SandboxTemplate / SandboxPool / SandboxClaim / |
| 21 | +SandboxFork CRDs) is served by the Python and TypeScript SDKs and is NOT part of |
| 22 | +this module. |
| 23 | + |
| 24 | +## Install |
| 25 | + |
| 26 | +```bash |
| 27 | +go get github.com/mitos-run/mitos/sdk/go |
| 28 | +``` |
| 29 | + |
| 30 | +Import it as the `mitos` package: |
| 31 | + |
| 32 | +```go |
| 33 | +import mitos "github.com/mitos-run/mitos/sdk/go" |
| 34 | +``` |
| 35 | + |
| 36 | +A branded `mitos.run/go` vanity import path is a documented follow-up: it needs a |
| 37 | +`go-import` meta tag served from `mitos.run` (which already hosts the project |
| 38 | +site). Until that lands, the GitHub path above resolves directly. |
| 39 | + |
| 40 | +## Quickstart (hosted) |
| 41 | + |
| 42 | +The base URL defaults to the hosted endpoint `https://mitos.run`. Set your API |
| 43 | +key in the environment; it is sent as `Authorization: Bearer <key>` and is never |
| 44 | +logged or placed in an error message. |
| 45 | + |
| 46 | +```bash |
| 47 | +export MITOS_API_KEY="sk-..." |
| 48 | +``` |
| 49 | + |
| 50 | +```go |
| 51 | +package main |
| 52 | + |
| 53 | +import ( |
| 54 | + "context" |
| 55 | + "fmt" |
| 56 | + "log" |
| 57 | + |
| 58 | + mitos "github.com/mitos-run/mitos/sdk/go" |
| 59 | +) |
| 60 | + |
| 61 | +func main() { |
| 62 | + ctx := context.Background() |
| 63 | + srv := mitos.NewSandboxServer() // base URL + token resolved from the env |
| 64 | + |
| 65 | + if _, err := srv.CreateTemplate(ctx, "python"); err != nil { |
| 66 | + log.Fatal(err) |
| 67 | + } |
| 68 | + sb, err := srv.Fork(ctx, "python", "") // empty id -> generated sandbox-<hex> |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + defer sb.Terminate(ctx) |
| 73 | + |
| 74 | + res, err := sb.Exec(ctx, "echo hi") |
| 75 | + if err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + fmt.Println(res.Stdout) |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Self-hosted or local standalone users opt out of the hosted default by setting |
| 83 | +`MITOS_BASE_URL` (for example `http://localhost:8080`) or passing |
| 84 | +`mitos.WithBaseURL(...)`. The standalone server is tokenless and ignores the |
| 85 | +bearer header; the hosted front door verifies it. |
| 86 | + |
| 87 | +## Surface |
| 88 | + |
| 89 | +| Method | HTTP | Returns | Notes | |
| 90 | +| --- | --- | --- | --- | |
| 91 | +| `NewSandboxServer(opts ...Option)` | - | `*SandboxServer` | Functional options: `WithBaseURL`, `WithAPIKey`, `WithHTTPClient`. | |
| 92 | +| `(*SandboxServer).CreateTemplate(ctx, id, opts...)` | `POST /v1/templates` | `*Template` | Sends a fresh `Idempotency-Key`. `WithInitWaitSeconds`, `WithTemplateIdempotencyKey`. | |
| 93 | +| `(*SandboxServer).ListTemplates(ctx)` | `GET /v1/templates` | `[]Template` | | |
| 94 | +| `(*SandboxServer).Fork(ctx, template, id, opts...)` | `POST /v1/fork` | `*Sandbox` | Generates a `sandbox-<hex>` id when `id` is empty; validates against the id allowlist (typed error before any request). Sends a fresh `Idempotency-Key`. `WithForkIdempotencyKey`. | |
| 95 | +| `(*SandboxServer).ListSandboxes(ctx)` | `GET /v1/sandboxes` | `[]ServerSandbox` | | |
| 96 | +| `(*Sandbox).Exec(ctx, command, opts...)` | `POST /v1/exec` | `*ExecResult` | Needs a Ready sandbox. `WithExecTimeout`. | |
| 97 | +| `(*Sandbox).Terminate(ctx)` | `DELETE /v1/sandboxes/{id}` | `error` | | |
| 98 | + |
| 99 | +Every call takes a `context.Context` for cancellation and deadlines. |
| 100 | + |
| 101 | +Value types: |
| 102 | + |
| 103 | +- `Template`: `ID`, `Ready`, `CreatedAt`, `CreationTimeMs`. |
| 104 | +- `ServerSandbox`: `ID`, `TemplateID`, `Endpoint`, `CreatedAt`, `ForkTimeMs`. |
| 105 | +- `ExecResult`: `ExitCode`, `Stdout`, `Stderr`, `ExecTimeMs`. |
| 106 | +- `Sandbox`: `ID`, `Template`, `Endpoint`, `ForkTimeMs`. |
| 107 | + |
| 108 | +## Auth and base-URL precedence |
| 109 | + |
| 110 | +Resolved once when you call `NewSandboxServer`: |
| 111 | + |
| 112 | +- Base URL: `WithBaseURL(...)`, else `MITOS_BASE_URL`, else `https://mitos.run`. |
| 113 | +- Bearer token: `WithAPIKey(...)`, else `MITOS_API_KEY`, else the `mitos auth |
| 114 | + login` credential file (the `token` field of |
| 115 | + `~/.config/mitos/credentials.json`, honoring `MITOS_CONFIG_DIR`), else |
| 116 | + tokenless. |
| 117 | + |
| 118 | +A missing, unreadable, or non-JSON credential file is NOT an error: it just |
| 119 | +yields no token so the SDK stays usable tokenless. The path rule mirrors |
| 120 | +`internal/credfile`, the single source of truth shared with the CLI. The token |
| 121 | +VALUE is never logged and is redacted from any error body. |
| 122 | + |
| 123 | +## Errors |
| 124 | + |
| 125 | +Every non-2xx response returns a `*mitos.Error`, which parses the server |
| 126 | +envelope `{error:{code, message, cause, remediation}}`. Branch on the code with |
| 127 | +`errors.Is`, never on the message text: |
| 128 | + |
| 129 | +```go |
| 130 | +res, err := sb.Exec(ctx, "false") |
| 131 | +if err != nil { |
| 132 | + var e *mitos.Error |
| 133 | + if errors.As(err, &e) { |
| 134 | + fmt.Println(e.Code) // e.g. "not_found" |
| 135 | + fmt.Println(e.Status) // e.g. 404 |
| 136 | + fmt.Println(e.Remediation) // actionable hint |
| 137 | + } |
| 138 | + // or test a specific code directly: |
| 139 | + if errors.Is(err, &mitos.Error{Code: "not_found"}) { |
| 140 | + // ... |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +The configured API key is redacted from any error body before it becomes the |
| 146 | +error cause, so a token a hostile or misconfigured server reflects back never |
| 147 | +surfaces in `err.Error()`. |
| 148 | + |
| 149 | +## The sandbox id allowlist |
| 150 | + |
| 151 | +Sandbox ids must match `^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$` (the same allowlist the |
| 152 | +Go daemon and the other SDKs enforce). `Fork` validates the id (the explicit one |
| 153 | +or the generated `sandbox-<hex>`) and returns a typed `invalid_sandbox_id` error |
| 154 | +BEFORE sending any request. `ValidSandboxID(id)` exposes the check. |
| 155 | + |
| 156 | +## Tests |
| 157 | + |
| 158 | +The tests use only the standard library (`net/http/httptest`, `testing`). They |
| 159 | +spin up a stub server reproducing the sandbox-server wire shapes and assert the |
| 160 | +SDK round-trips them, including the `Idempotency-Key` header, the credential-file |
| 161 | +auth fallback, and token redaction in errors. |
| 162 | + |
| 163 | +```bash |
| 164 | +cd sdk/go |
| 165 | +go test ./... -count=1 |
| 166 | +``` |
| 167 | + |
| 168 | +## Deferred |
| 169 | + |
| 170 | +Not yet implemented in the Go SDK (covered by the Python / TypeScript SDKs): |
| 171 | + |
| 172 | +- Kubernetes / cluster mode (controller, forkd, CRDs). |
| 173 | +- The files API (`/v1/files/*`). |
| 174 | +- Interactive PTY (`/v1/pty`). |
| 175 | +- `run_code`: the server exposes a streaming-only route |
| 176 | + (`POST /v1/run_code/stream`); a synchronous Go wrapper is deferred until a |
| 177 | + non-streaming contract exists. |
| 178 | +- Per-sandbox network posture, `set_timeout`, `pause` / `resume`, and |
| 179 | + `get_host(port)` preview URLs. |
| 180 | +- The branded `mitos.run/go` vanity import path (needs a `go-import` meta tag on |
| 181 | + `mitos.run`). |
0 commit comments