# Badulaque

> Two MCP tools, one of them read-only: a sandboxed terminal with CLIs built for agents. Any AI client reaches all my services and spends 66% less.

- Role: Solo project
- When: Oct 2026 – present
- Stack: NixOS, Node.js, TypeScript, React, MCP, sandbox-runtime, bubblewrap, agent-browser, CalDAV, CardDAV
- Links: [cli-memoria](https://github.com/PabloPC05/cli-memoria), [campus-usc](https://github.com/PabloPC05/campus-usc), [open-grocery-mcp](https://github.com/PabloPC05/open-grocery-mcp)
- Image: [Diagram: AI clients reach the Badulaque with their key and get two tools, a read-only one and run; each command runs in its own sandbox with that agent's native CLIs and a generic CLI for the remaining services; credentials travel as fakes and the server swaps in the real ones on the way out](https://ppardocotos.com/_astro/badulaque.en.BJNlgIUT.svg)
- URL: https://ppardocotos.com/projects/badulaque

I switch AI clients constantly (Claude Code, claude.ai, Codex, opencode…) and want all of them to reach my services through one URL and key. **Badulaque** — named after the 24-hour shop in *The Simpsons* — is that single door, on my own server.

## Premise: 594 tools do not fit in context

<figure class="grafico">
<figcaption>Fixed context the MCP server adds to every model call</figcaption>
<p class="sub">Tool definitions and instructions, in tokens, before doing anything.</p>
<div class="barras">
<span class="etq">Plain MCP<br><small>594 tools</small></span><span class="barra est" style="--v:60%" title="Plain MCP: ~170,000 tokens estimated from 742,000 characters">~170,000 (estimated)</span>
<span class="etq">Previous search<br><small>2 tools</small></span><span class="barra v1" style="--v:0.95%" title="Previous tool search: ~2,700 tokens">~2,700</span>
<span class="etq">Current terminal<br><small>2 tools</small></span><span class="barra v2" style="--v:0.99%" title="Current terminal: ~2,800 tokens estimated (~2,400 measured with a single tool, plus the read-only one)">~2,800 (estimated)</span>
</div>
<p class="nota">Plain MCP alone almost fills a 200,000-token window. Tool search lowered that floor, yet every intermediate result still bounced through the model.</p>
</figure>

## Architecture shift: one sandboxed terminal with CLIs

Instead of back-and-forth tool calls, one command chains the work inside a sandbox and returns only what matters. Across **7 real read-only tasks (×2, Claude Sonnet)**:

<div class="cifras">
<div class="cifra"><strong>−66%</strong><span>cost</span><small>50.5 → 17.0 cents</small></div>
<div class="cifra"><strong>−71%</strong><span>tool calls</span><small>35 → 10</small></div>
<div class="cifra"><strong>−92%</strong><span>accumulated context</span><small>170,300 → 14,000 chars</small></div>
<div class="cifra"><strong>−41%</strong><span>time</span><small>71.6 → 42.5 s</small></div>
</div>
<p class="pie-grafico">Terminal vs. previous tool search, summed across the 7 tasks.</p>

<figure class="grafico">
<figcaption>Cost per task, in cents</figcaption>
<p class="leyenda"><span><i class="v1"></i>Previous tool search</span><span><i class="v2"></i>Terminal</span></p>
<div class="barras">
<span class="etq">Unread mail</span><span class="pista"><span class="barra v1" style="--v:57.60%" title="Previous tool search: 14.4 cents">14.4</span><span class="barra v2" style="--v:13.20%" title="Terminal: 3.3 cents">3.3</span></span>
<span class="etq">Pending items in memory</span><span class="pista"><span class="barra v1" style="--v:35.56%" title="Previous tool search: 8.9 cents">8.9</span><span class="barra v2" style="--v:16.92%" title="Terminal: 4.2 cents">4.2</span></span>
<span class="etq">Latest PDF in a folder</span><span class="pista"><span class="barra v1" style="--v:24.24%" title="Previous tool search: 6.1 cents">6.1</span><span class="barra v2" style="--v:7.92%" title="Terminal: 2.0 cents">2.0</span></span>
<span class="etq">Calendar and reminders</span><span class="pista"><span class="barra v1" style="--v:10.72%" title="Previous tool search: 2.7 cents">2.7</span><span class="barra v2" style="--v:6.84%" title="Terminal: 1.7 cents">1.7</span></span>
<span class="etq">Last workout</span><span class="pista"><span class="barra v1" style="--v:15.44%" title="Previous tool search: 3.9 cents">3.9</span><span class="barra v2" style="--v:8.32%" title="Terminal: 2.1 cents">2.1</span></span>
<span class="etq">Open PRs on GitHub</span><span class="pista"><span class="barra v1" style="--v:18.60%" title="Previous tool search: 4.7 cents">4.7</span><span class="barra v2" style="--v:7.40%" title="Terminal: 1.9 cents">1.9</span></span>
<span class="etq">Latest emailed PDF in my files</span><span class="pista"><span class="barra v1" style="--v:39.92%" title="Previous tool search: 10.0 cents">10.0</span><span class="barra v2" style="--v:7.24%" title="Terminal: 1.8 cents">1.8</span></span>
</div>
<p class="nota">Six of the seven tasks finish in a single call. Switching to the terminal drove the cost drop; later work on guides, aliases and navigation improved reliability, not the bill.</p>
</figure>

## Second pass: compact signatures, parallel execution and lower footprint

After a follow-up audit inspired by *Code Mode* and Anthropic's code-execution patterns, I tightened the rest of the pipeline (measured live on the server before and after the change):

<div class="cifras">
<div class="cifra"><strong>−73%</strong><span>search payload</span><small>92,337 → 25,352 chars</small></div>
<div class="cifra"><strong>−58%</strong><span>backend tools</span><small>595 → 250 (6 MCPs retired)</small></div>
<div class="cifra"><strong>−48%</strong><span>3-service query</span><small>6.70 → 3.45 s (in parallel)</small></div>
<div class="cifra"><strong>−67%</strong><span>server RAM</span><small>2.29 → 0.76 GB</small></div>
</div>
<p class="pie-grafico">Apples-to-apples measurements on the same server before and after retiring 6 redundant MCPs, adding TypeScript signatures, a local Unix bridge and parallel execution.</p>

<figure class="grafico">
<figcaption>Tool-search payload size, in characters</figcaption>
<p class="leyenda"><span><i class="v1"></i>Full JSON Schema (before)</span><span><i class="v2"></i>TypeScript signatures (after)</span></p>
<div class="barras">
<span class="etq">List Overleaf (31)</span><span class="pista"><span class="barra v1" style="--v:57.67%" title="Full JSON Schema (before): 48,061 chars">48,061</span><span class="barra v2" style="--v:11.66%" title="TypeScript signatures (after): 9,718 chars">9,718</span></span>
<span class="etq">List TV tools (50)</span><span class="pista"><span class="barra v1" style="--v:25.26%" title="Full JSON Schema (before): 21,050 chars">21,050</span><span class="barra v2" style="--v:12.46%" title="TypeScript signatures (after): 10,384 chars">10,384</span></span>
<span class="etq">Search analytics (top 5)</span><span class="pista"><span class="barra v1" style="--v:14.25%" title="Full JSON Schema (before): 11,878 chars">11,878</span><span class="barra v2" style="--v:1.82%" title="TypeScript signatures (after): 1,520 chars">1,520</span></span>
<span class="etq">Search LinkedIn (top 5)</span><span class="pista"><span class="barra v1" style="--v:4.91%" title="Full JSON Schema (before): 4,093 chars">4,093</span><span class="barra v2" style="--v:1.86%" title="TypeScript signatures (after): 1,550 chars">1,550</span></span>
<span class="etq">List analytics (3)</span><span class="pista"><span class="barra v1" style="--v:4.71%" title="Full JSON Schema (before): 3,928 chars">3,928</span><span class="barra v2" style="--v:1.24%" title="TypeScript signatures (after): 1,035 chars">1,035</span></span>
<span class="etq">Search Overleaf (top 5)</span><span class="pista"><span class="barra v1" style="--v:3.99%" title="Full JSON Schema (before): 3,327 chars">3,327</span><span class="barra v2" style="--v:1.37%" title="TypeScript signatures (after): 1,145 chars">1,145</span></span>
</div>
<p class="nota">When exploring generic connectors with <code>detalle: "firma"</code>, the search proxy returns one-line TypeScript-style signatures instead of full JSON Schemas (−51% to −87% per query). Across all 6 real queries combined: ~23,100 → ~6,300 tokens.</p>
</figure>

- **No internet hairpinning**: a local HMAC-authenticated Unix socket bridge connects the `badulaque` CLI straight to the local search proxy. `badulaque servicios` inside the sandbox dropped from **780 to 470 ms** (−40%; subtracting ~423 ms of `srt` startup, the call itself went from ~350 to ~47 ms).
- **Concurrency in JavaScript and on-demand credentials**: querying calendar, reminders and unread mail in one call dropped from **6.70 s** sequentially to **5.43 s** with OAuth caching (−19%) and **3.45 s** in parallel through a JavaScript code mode (−48%), returning only the filtered summary. Dynamic credentials (such as the Legálitas session) are fetched only when a command actually touches that service (**0** calls otherwise).
- **Smaller server footprint**: retiring 6 MCP servers already covered by native CLIs (GitHub, Hevy, calendar and contacts, DNS, USC Moodle and browser) and turning off the browser by default cut the internal catalogue from **595 to 250 tools** (346 → 121 for `claude-ai`, −65%) and freed **~1.5 GB of RAM** (the gateway alone dropped from 420 to 104 MB).

## Sandboxing and credentials the agent never sees

<div class="flujo">
<div class="paso"><b>AI client</b>Sends a command to the MCP server.</div>
<span class="flecha" aria-hidden="true">→</span>
<div class="paso"><b>Gate</b>Its key identifies the agent and active services.</div>
<span class="flecha" aria-hidden="true">→</span>
<div class="paso caja"><b>Ephemeral sandbox</b><ul><li>only its CLIs and domains</li><li>only its folders</li><li>time and output caps</li></ul></div>
<span class="flecha" aria-hidden="true">→</span>
<div class="paso"><b>Service</b>Receives the real credential on exit.</div>
</div>

Each command runs inside [sandbox-runtime](https://github.com/anthropic-experimental/sandbox-runtime) (bubblewrap on Linux) and is destroyed on exit. The agent never sees real secrets:

<div class="carriles">
<div class="carril"><span class="titulo">Normal use</span><code>placeholder token</code><span class="flecha">→</span><span class="ok">✓ authorized service</span><span class="flecha">→</span><span>server swaps in real secret on exit</span></div>
<div class="carril"><span class="titulo">Exfiltration attempt (“echo”)</span><code>placeholder sent elsewhere</code><span class="flecha">→</span><span class="no">✕ rejected (403)</span><span>A pre-swap filter enforces where each secret may travel, allowed HTTP methods (read-only for Hevy and Fitbit) and blocked routes (certificates and purchases at the domain registrar).</span></div>
</div>

Each MCP service also runs under its own Unix user, and internal ports are firewalled off from other processes. The server's browser — disabled by default — follows the same rule: an out-of-sandbox bridge drives tabs, uploads and downloads without exposing cookies or `eval`.

## Reading without asking for permission

The terminal now offers two tools instead of one: `consultar` (look up) and `ejecutar` (run). An AI client decides whether to ask for confirmation based on how each tool is announced, and a single tool that can also delete and send was, rightly, announced as destructive: Gemini asked for permission even to glance at the calendar. Now `consultar` is announced as read-only and is the default, and the server enforces it: it parses every command the way bash would and only runs it if each step is a lookup from its allowlist (`memoria buscar`, `correo leer`, `calendario eventos`, `grep`, `jq`…), with no `$(…)` and no writes to files, and it also runs it with the files mounted read-only. Anything else is rejected with the reason and pointed to `ejecutar`, which still asks for confirmation.

## Three CLI design principles

<div class="decisiones una-col">
<div class="decision"><div class="cab"><b>Per-agent, forgiving guides</b></div><p>Built from whatever connections are enabled in the dashboard — defaults, not locks. Carries a brief reference inside the tool description for web clients that ignore MCP instructions, plus English aliases and typo hints.</p></div>
<div class="decision"><div class="cab"><b>Navigable outputs</b></div><p>Short summaries and section outlines instead of dumping whole files or truncating blindly.</p></div>
<div class="decision"><div class="cab"><b>One catalogue</b></div><p>Packaged declaratively with Nix on the server; each custom CLI is moving to its own repo (<code>cli-memoria</code> was first).</p></div>
</div>

## What it connects

<ul class="chips">
<li>memory</li><li>files</li><li>mail</li><li>calendar</li><li>contacts</li><li>reminders</li><li>GitHub</li><li>Legálitas</li><li>Hevy</li><li>Fitbit</li><li>DNS</li><li>USC Moodle</li><li class="sin">browser (off by default)</li><li class="sin">deployments (not configured)</li>
</ul>
<ul class="chips genericas">
<li>web analytics</li><li>LinkedIn</li><li>Overleaf</li><li>Zenodo</li><li>the TV</li>
</ul>
<p class="leyenda"><span><i class="v2"></i>Native CLI (14)</span><span><i class="v1"></i>Generic connector (5)</span></p>

Calendar, reminders and contacts run over CalDAV and CardDAV without needing my devices awake. Custom connectors also include [campus-usc](https://ppardocotos.com/projects/campus-usc) and **open-grocery-mcp** (supermarket basket comparison with a public read-only instance at `open-grocery-mcp.vercel.app/mcp`).

All benchmarks, discarded ideas and bibliography are in [this blog post](https://ppardocotos.com/blog/measuring-mcp-and-agent-memory). The dashboard and shared memory have their own pages: [Self-hosted AI assistant](https://ppardocotos.com/projects/self-hosted-assistant) and [cli-memoria](https://ppardocotos.com/projects/cli-memoria).
