Skip to main content
Glama
README.md
# NetsuGate

[![CI](https://github.com/NetsumaInfo/NetsuGate/actions/workflows/ci.yml/badge.svg)](https://github.com/NetsumaInfo/NetsuGate/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/github/license/NetsumaInfo/NetsuGate)](LICENSE)

NetsuGate is a code mode for MCP servers: instead of calling an app's tools one by one, an AI agent writes a short sandboxed script that calls them, and reads only the result.

The agent gets two tools. `netsu_api` looks up what it can call, and `netsu_code` runs a script:

```js
const { files } = await nr.list_files({ limit: 1000 });
const low = files.filter((f) => f.name.startsWith("interview") && f.rating === 2);
for (const { id } of low) await nr.set_rating({ id, rating: 4 });
return low.length;
```

That is one tool call that returns one number. With the tools alone, the agent reads the list, then makes 40 calls. In the [benchmark](#benchmark), this task cost 0.026 to 0.028 $ in code mode and 0.081 $ with the tools.

> [!NOTE]
> Early version. It is not on npm: clone it, or copy its files as `sync.js` does for [NetsuRush](https://github.com/NetsumaInfo/NetsuRush), [NetsuBoard](https://github.com/NetsumaInfo/NetsuBoard), [NetsuIcon](https://github.com/NetsumaInfo/NetsuIcon), [NetsuFont](https://github.com/NetsumaInfo/NetsuFont) and [NetsuFlow Review](https://github.com/NetsumaInfo/NetsuFlow-Review).

## When not to use it

- **One call is enough.** Looking up the API first costs more than it saves. The small question of the benchmark cost 0.013 $ in code mode and 0.008 $ with the tools.
- **The model writes poor JavaScript.** Keep the tools for it. The published benchmark covers one model only.
- **The agent must look at each picture before it goes on.** A script cannot see. It can attach up to 8 images for the model to look at afterwards.
- **Each action needs its own approval in the client.** See the warning under [what a script can do](#what-a-script-can-and-cannot-do).

## Install

It needs Node.js 22 or later.

```bash
git clone https://github.com/NetsumaInfo/NetsuGate.git
cd NetsuGate
npm install
```

## Put it in front of an MCP server

The gateway is an MCP server over stdio. It connects to another MCP server, over HTTP or stdio, and offers its tools in code mode. The other server does not change.

1. Copy `gateway/apps.example.js` to `gateway/apps.local.js`, which git ignores, and describe your server there. To keep that file elsewhere, set `NETSUGATE_APPS` to its path.
2. Link the gateway to your agent. The last word is the name you gave the server in that file. For Claude Code, from this folder:

```bash
claude mcp add my-app-code -- node "$PWD/gateway/gateway.js" over-http
```

The gateway lists its two tools even while the app is closed, and connects again after the app restarts. If a connection breaks while a call is running, it does not repeat the call: it tells the agent to check first. A token is read by `connect` each time the gateway connects.

## Add it to your own server

`src/` is CommonJS with no build step. Give `createCodeMode` the MCP definitions of your tools and a function that runs one:

```js
const { createCodeMode } = require('./src');

const mode = createCodeMode({
  app: 'My app',
  listTools: () => definitions,
  callTool: async (name, args, { script }) => run(name, args),
  guide: () => 'What an agent must know to use My app.',
});
```

`callTool` gives an object. `{ ok: false, error }` is a failure, and throws in the script. `script` is the same for every call of one script. NetsuBoard uses it to undo all the edits of a script at once.

Then answer the MCP requests:

| Request | Answer |
|---|---|
| `initialize` | `mode.instructions` as the instructions |
| `tools/list` | `{ tools: await mode.listTools() }` |
| `tools/call` | `await mode.callTool(name, args, requestId)` |
| `notifications/cancelled` | `mode.cancelRequest(requestId)` |

Create one mode per agent, because it keeps what that agent's scripts store. The other options are described in `src/index.js`.

The guide comes whole with the first `netsu_api` answer instead of in the server's instructions, which some clients cut.

For a TypeScript server built on Node's `http` and the MCP SDK, `http/codeMode.ts` is an endpoint you can copy. Put the files of `src/` and `http/package.json` in a `code` folder next to the one that holds it. It keeps one mode per MCP session. The same session id still works after a restart, with an empty store. **It checks nothing about who is calling**: put it behind the same `Host`, `Origin` and key checks as your MCP endpoint, including any check that names an exact path.

## What a script can and cannot do

> [!WARNING]
> Your client asks about `netsu_code` as a whole. Allowing it allows every tool behind it, deletions included. NetsuGate does not ask for confirmation, so any check your app needs must be in `callTool`, or in the server behind the gateway.

A script is the body of an async function.

- It calls `nr.<tool>({...})`, or `nr.<tool>.<action>({...})` when a tool has an `action` parameter with a list of values. A `netsu_` prefix can be left out.
- Unknown tools, actions and top-level parameters are refused before they reach the app, and so are missing required ones. A refused parameter comes with the right signature. Values and nested fields are left for the app to check.
- `store` keeps data from one script to the next. `nr.show(result)` attaches the images of a result. `console.log` adds short notes to the answer.
- If a script fails, the answer lists the calls that went through. Calls waiting their turn are dropped. Those already sent are not undone.
- A result over 40,000 characters is never cut. It goes into `store.last`, unless the store would grow past 5 million characters.
- After 40 seconds, the answer gives a `runId` to follow or cancel the script. By default it can run for 10 minutes (an hour through the gateway).

Scripts run in [QuickJS](https://bellard.org/quickjs/), compiled to WebAssembly by [quickjs-emscripten](https://github.com/justjake/quickjs-emscripten). They have no files, network, timers, modules or Node API. QuickJS gets 64 MB of memory. Each script has its own worker thread, stopped when the script computes for more than 3 seconds between two tool calls, with one more second of grace. A script also cannot choose a file to attach: it gets an id for a result's images, never their paths.

This limits what a script can do to your machine, not what your tools can do. A tool that reads files can read them for a script too.

## Benchmark

`bench/` runs Claude Code on a made-up library of 320 files, with the tools and then in code mode. Claude Code loads the tools on demand, as it does by default. The results of the two tasks that change files are checked in the files themselves. For the question, the answer is checked too.

These are the costs reported by Claude Code 2.1.332 with Sonnet 5.5, with two runs of each. All 12 runs were right.

| Task | Tools | Code mode |
|---|---|---|
| Sort 240 files into 6 folders | 0.128 to 0.207 $ | 0.027 to 0.053 $ |
| Change 40 files, one call each | 0.081 $ | 0.026 to 0.028 $ |
| Answer a small question | 0.008 $ | 0.013 $ |

Two runs show an order of magnitude, not an average. Tokens, calls, times and limits are in [`bench/results.md`](bench/results.md). To run it again, you need Claude Code, signed in. It uses your quota:

```bash
node bench/run.js --runs 2
```

## Development

```bash
npm run check      # types of src/
npm test
npm run sync       # copies the code into the apps in the folders next to this one
```

`node sync.js --check` shows which copies are out of date without changing them. `http/` is checked and tested in the apps that use it.

See [CONTRIBUTING.md](CONTRIBUTING.md) before opening a pull request, and [AGENTS.md](AGENTS.md) for the structure and rules. Report a way out of the sandbox privately, as described in [SECURITY.md](SECURITY.md).

## License

[MIT](LICENSE). QuickJS and quickjs-emscripten are also under MIT. A copy in another repository follows that repository's license: for example, the copies in NetsuRush and NetsuBoard are under AGPL-3.0-only.