Metadata-Version: 2.5
Name: adk-kosa8
Version: 0.1.0
Summary: kosa8 Cloud microVM sandboxes for Google's Agent Development Kit: a code executor and an MCP toolset
Project-URL: Homepage, https://kosa8.com
Project-URL: Documentation, https://docs.kosa8.com/guides/google-adk/
Project-URL: Repository, https://github.com/kosa8ai/adk-kosa8
Project-URL: Issues, https://github.com/kosa8ai/adk-kosa8/issues
Author: kosa8
License: MIT
License-File: LICENSE
Keywords: adk,agents,code-executor,google-adk,kosa8,mcp,microvm,sandbox
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <4.0,>=3.11
Requires-Dist: google-adk[mcp]<3,>=2.0
Requires-Dist: kosa8>=0.11
Description-Content-Type: text/markdown

# adk-kosa8

Run the code a [Google Agent Development Kit (ADK)](https://adk.dev) agent writes in a [kosa8 Cloud](https://kosa8.com) sandbox instead of on your machine.

- **`Kosa8CodeExecutor`** is an ADK code executor (`BaseCodeExecutor`). Give it to an `LlmAgent` as `code_executor=`, and each Python block the model writes runs in a kosa8 sandbox. You get stdout, stderr, the exit code and output files (saved as ADK artifacts).
- **`kosa8_mcp_toolset()`** gives an agent kosa8's sandbox tools (`run`, `sandbox_create`, `sandbox_exec`, `file_read`, `file_write`, `sandbox_delete` and more) from kosa8's remote MCP server, through ADK's `McpToolset`.

## Install

`adk-kosa8` is not on PyPI yet. Until it is, install it from GitHub:

```bash
pip install "adk-kosa8 @ git+https://github.com/kosa8ai/adk-kosa8"
```

Once it is published, this becomes `pip install adk-kosa8`.

It needs Python 3.11 or newer and installs `google-adk[mcp]` 2.x and the `kosa8` SDK.

Set `KOSA8_API_KEY` to a kosa8 Cloud API key (create one at [app.kosa8.com](https://app.kosa8.com)), or run `kosa8 login` once. The Starter plan is free with no card and includes $10 of one-time kosa8 Cloud usage. Paid plans start at $29 a month ([pricing](https://kosa8.com/pricing/)).

## Quick start: a code executor

```python
from google.adk.agents import LlmAgent
from adk_kosa8 import Kosa8CodeExecutor

root_agent = LlmAgent(
    name="python_coder",
    model="gemini-flash-latest",
    instruction="Solve problems by writing Python in ```python blocks. Answer from the output.",
    code_executor=Kosa8CodeExecutor(),
)
```

Run it with `adk web`, `adk run` or a `Runner` as usual. By default each ADK session gets one sandbox (the `python` template). The sandbox is created on the session's first code block and reused for later blocks, so files the code writes are still there next time. Call `close()` on the executor when you are done to delete its sandboxes. Each sandbox also has a one-hour lifetime (`ttl_s`), after which kosa8 deletes it anyway.

### Options

| Option | Default | |
|---|---|---|
| `api_key` | `KOSA8_API_KEY`, then `~/.kosa8/cloud.json` | kosa8 Cloud API key |
| `template` | `"python"` | kosa8 template (needs `python3`) |
| `size` | template's | vCPU x GiB: `"0.25x0.5"` … `"16x32"` |
| `region` | chosen by kosa8 | e.g. `"us-ash1"` |
| `allow_hosts` | org policy | only these hosts (`[]`: no network) |
| `egress` | org policy | a full kosa8 egress policy (`internet`, `allow`, `deny`, `presets`) |
| `sandbox_scope` | `"session"` | `"session"`, `"executor"` (one shared sandbox) or `"call"` (a new sandbox per code block, deleted after) |
| `stateful` | `False` | keep Python variables between blocks (see below) |
| `timeout_seconds` | 300 (80 when stateful) | the time limit for one code block |
| `ttl_s` | 3600 | lifetime of each sandbox (`None`: none) |
| `sleep_after_s` | kosa8's | idle seconds before a sandbox sleeps (it wakes on the next block) |
| `env`, `labels` | none | environment variables for the code, and kosa8 labels |
| `input_dir`, `output_dir` | `/workspace/inputs`, `/workspace/outputs` | where input files go, and where output files are collected |
| `client` | made from `api_key` | a `kosa8.Kosa8Cloud` to share |

### What comes back

- `stdout`, `stderr` and `exit_code`. An exception in the code is a traceback in `stderr` and exit code 1. ADK shows these to the model, and its `error_retry_attempts` applies.
- A block that runs past `timeout_seconds` is stopped in the sandbox. It returns exit code 124, any output so far, and `Code execution timed out after N seconds.` in `stderr`.
- Every file the code writes under `output_dir` (also in `$ADK_OUTPUT_DIR`) is returned as an output file, and ADK saves it as an artifact. Input files from `CodeExecutorContext.add_input_files()` are written to `input_dir` (`$ADK_INPUT_DIR`). Both folders are emptied before each block. At most 100 files and 32 MiB are returned per block.
- Problems with kosa8 itself raise `kosa8` exceptions rather than returning output to the model: a bad key (`Kosa8AuthError`), no plan or credit (`Kosa8PaymentRequiredError`, with `upgrade_url`) or a rate limit (`Kosa8RateLimitError`). If a session's sandbox was deleted, for example by its `ttl_s`, the executor starts a new one and runs the block again.

### Stateful execution

```python
Kosa8CodeExecutor(stateful=True)
```

With `stateful=True`, variables, imports and functions stay from one code block to the next, as in a notebook. Each block runs in [kosa8's code interpreter](https://docs.kosa8.com/guides/cloud-code-interpreter/) in a persistent sandbox, with one context per ADK session. The value of a block's last expression is added to `stdout`, and charts it draws come back as PNG output files. A cell takes at most 80 seconds. When the time limit stops a cell, the context keeps what the cell had set up to that point.

### Lock the network to one host

```python
Kosa8CodeExecutor(allow_hosts=["pypi.org"])
```

The sandbox then reaches `pypi.org` and nothing else. A request to another host fails at DNS, and an address the code already knows gets nowhere. kosa8 enforces the policy on the host, outside the sandbox, and nothing inside the sandbox can change it. Entries can be exact names, `*.domain` wildcards, IPv4 addresses or CIDRs. With no policy set, a sandbox gets your organization's policy. That is kosa8's default allow-list (package registries, GitHub, Hugging Face and the main AI APIs) unless your organization changed it. See [egress controls](https://docs.kosa8.com/guides/cloud-egress/).

## kosa8 tools over MCP

```python
from google.adk.agents import LlmAgent
from adk_kosa8 import SANDBOX_TOOLS, kosa8_mcp_toolset

root_agent = LlmAgent(
    name="kosa8_tools",
    model="gemini-flash-latest",
    instruction="Run code with the kosa8 sandbox tools. Delete every sandbox you create.",
    tools=[kosa8_mcp_toolset(tool_filter=SANDBOX_TOOLS)],
)
```

`kosa8_mcp_toolset(api_key=None, tool_filter=None, ...)` connects to `https://api.kosa8.com/mcp` over Streamable HTTP, with the key as a Bearer token. `SANDBOX_TOOLS` is `run`, `templates_list`, `sandbox_create`, `sandbox_get`, `sandbox_list`, `sandbox_exec`, `sandbox_exec_status`, `file_read`, `file_write`, `file_list` and `sandbox_delete`. Leave `tool_filter` out to get every tool the server lists, including GitHub, previews, snapshots and usage. Other keyword arguments go to `McpToolset`, for example `require_confirmation=True` to ask the user before each tool call.

Use the code executor when the model writes code for you to run. Use the toolset when the model should manage sandboxes itself: start one, run shell commands, read and write files, and delete it.

## Examples

[`examples/`](examples) holds three agents for `adk web examples`, plus a runner:

- `python_coder`: writes Python and runs it with `Kosa8CodeExecutor`.
- `egress_locked`: the sandbox can reach `pypi.org` only, so a request to `example.com` is blocked.
- `mcp_tools`: kosa8's MCP sandbox tools.

```bash
export KOSA8_API_KEY=k8c_live_...  GOOGLE_API_KEY=...   # or ADK_MODEL=anthropic/claude-haiku-4-5 with ANTHROPIC_API_KEY
python examples/run.py egress_locked "Fetch https://pypi.org/simple/ and https://example.com and report the status of each."
python examples/executor_only.py     # the executor on its own, no model needed
```

## Security notes

- **The code runs in a sandbox, not on your machine.** Each kosa8 Cloud sandbox is a microVM with its own Linux kernel on a kosa8 host. One exception: when kosa8's hosts are full, a plain sandbox may run on kosa8's overflow capacity (Cloudflare Containers, same API), which is not a kosa8 microVM. Persistent sandbox (`stateful=True`), egress-policy (`allow_hosts`, `egress`) and named-region (`region=`) sandboxes always wait for a kosa8 host. See [kosa8 Cloud](https://docs.kosa8.com/guides/cloud/).
- **Limit what the code can reach.** Model-written code can be steered by what the model reads. kosa8 limits what that code can do, through the sandbox, its egress policy and its lifetime. kosa8 does not stop prompt injection itself. Use `allow_hosts` for code that needs no general internet access.
- **Keep keys out of the sandbox.** Do not put API keys in `env`. Store them as [kosa8 secrets](https://docs.kosa8.com/guides/cloud-secrets/): the sandbox then holds a placeholder, and kosa8 adds the real key on the way out, only to the named host.
- **Clean up.** Call `close()` (or use `with Kosa8CodeExecutor(...) as ex:`) to delete sandboxes. The `ttl_s` lifetime is the backstop if a process dies first.
- **Compliance.** kosa8's SOC 2 Type II audit is in progress, with the report expected in January 2027. kosa8 has no SOC 2 report or external pen test yet. See [kosa8.com/trust](https://kosa8.com/trust/).

## Development

```bash
uv venv && uv pip install -e . pytest pytest-asyncio ruff mypy
pytest                       # unit tests against a local fake kosa8 API (no network)
ruff check . && ruff format --check . && mypy
KOSA8_API_KEY=k8c_live_... pytest -m kosa8_live    # the live tier, against kosa8 Cloud
```

The live tier creates sandboxes (each labelled and given a 15-minute lifetime as a backstop), covers create, running code, files across calls, error passthrough, timeouts, input and output files, stateful variables, an egress allow-list, a whole ADK run with a scripted model, and the MCP tool list. Its last test checks that none of its sandboxes are left.

## License

MIT
