# Mastra

[Mastra](https://mastra.ai) agents take tools from MCP servers through `@mastra/mcp`. Point its `MCPClient` at kosa8 and the agent gets tools that create sandboxes, run commands and read and write files in microVMs. Mastra itself stays in your Node.js process.

**How this page was checked**

The local gateway with `@mastra/mcp` 2.4.1 was tested in the [TypeScript recipes](https://docs.kosa8.com/guides/js-recipes/#mcp) (listing the tools and calling one). The Agent and the kosa8 Cloud configuration below follow the published types of `@mastra/mcp` 2.11.0 and `@mastra/core` 1.86.0, and were not run with a model.

## Install

```
$ curl -fsSL https://kosa8.com/install | sh
$ kosa8 daemon start
$ npm install @mastra/core @mastra/mcp
```

## The local gateway

```
import { Agent } from '@mastra/core/agent';
import { MCPClient } from '@mastra/mcp';

const mcp = new MCPClient({
  servers: {
    kosa8: { command: 'kosa8', args: ['mcp', 'serve'], env: { HOME: process.env.HOME, PATH: process.env.PATH } },
  },
});

// Names are <server>_<tool>: keep only the sandbox tools.
const all = await mcp.listTools();
const tools = Object.fromEntries(Object.entries(all).filter(([name]) => name.startsWith('kosa8_sandbox_')));

const coder = new Agent({
  id: 'coder',
  name: 'Coder',
  instructions: 'Run every command in a kosa8 sandbox. Create one with no network, and delete it when you are done.',
  model: 'openai/gpt-5',
  tools,
});

try {
  const res = await coder.generate('Create a sandbox, write fizzbuzz.py in it, run it for 1..15 and show the output.');
  console.log(res.text);
} finally {
  await mcp.disconnect();
}
```

- A sandbox the agent creates through `kosa8 mcp serve` has no network unless it asks for specific hosts.

- It can share a folder from your computer only below a directory you allowed: `args: ['mcp', 'serve', '--allow-mount', '/Users/you/work']`, read-only unless the path ends in `:rw`.

- `kosa8 mcp tools` lists every tool the gateway exposes. Without the filter the agent also gets container, image and snapshot tools.

- Clean up with `kosa8 sandbox ls` and `kosa8 sandbox rm NAME`.

## kosa8 Cloud's MCP server

```
const mcp = new MCPClient({
  servers: {
    kosa8: {
      url: new URL('https://api.kosa8.com/mcp'),
      requestInit: { headers: { Authorization: `Bearer ${process.env.KOSA8_API_KEY}` } },
    },
  },
});
```

The server speaks Streamable HTTP. In Mastra its tools are named `kosa8_sandbox_create`, `kosa8_sandbox_exec`, `kosa8_sandbox_exec_status`, `kosa8_file_read`, `kosa8_file_write`, `kosa8_file_list`, `kosa8_sandbox_delete` and so on. A command longer than 90 s should use `sandbox_exec` with `background: true` and then `sandbox_exec_status`. New sandboxes follow your organization's [egress policy](https://docs.kosa8.com/guides/cloud-egress/) and see nothing from your computer. Give the agent its own API key so you can revoke it alone; see [MCP server](https://docs.kosa8.com/guides/cloud-mcp/).

## Choosing

|  | Local gateway | kosa8 Cloud |
|---|---|---|
| Server | `command: 'kosa8'`, `args: ['mcp', 'serve']` | `url: https://api.kosa8.com/mcp` |
| Auth | none: stdio, as your user | Bearer `KOSA8_API_KEY` |
| Sandboxes run | on this computer | on kosa8's hosts |
| Network of a new sandbox | none unless the agent names hosts | the organization's egress policy |
| Your folders | only below `--allow-mount` | none |

## What it does not cover

- Mastra, its memory, its other tools and its model calls run in your process, outside the VM.

- Tools from other MCP servers you add run wherever those servers run.

- kosa8 does not detect or stop prompt injection; it limits what an injected instruction can reach.

The overview with steps and FAQs is on kosa8.com: [Run Mastra agents in a microVM sandbox](https://kosa8.com/docs/integrations/mastra/).

---
Source: https://docs.kosa8.com/guides/mastra/
