# How I Cut 45,000 Next.js Error Tokens to 118 in <0.2ms Using a Rust MCP Server

> Source: <https://dev.to/daffa2555/how-i-cut-45000-nextjs-error-tokens-to-118-in-02msusing-a-rust-mcp-server-1c7j>
> Published: 2026-09-15 03:19:09+00:00

If you use Cursor, Claude Desktop, or Cline daily,

  you have probably watched your rate limits evaporate

  because of a single runtime crash.

```
A Next.js build fails or a Python script panics, and
```

your terminal vomits 500 lines of stack traces. The

  LLM eagerly ingests all 45,000 tokens of internal

`node_modules` machinery, webpack bundles, and event

  loop frames.

```
You just paid $0.15 for an AI model to read code it
```

can't edit, your prompt cache is wiped, and your agent

  is now hallucinating because its context window is

  full of garbage.

```
Worse, your terminal stderr probably just leaked
```

`DATABASE_URL=postgres://admin:password@...` straight

  to an external API.

```
I got tired of paying for framework noise, so I
```

built an open-source Rust Model Context Protocol (MCP)

  server called **Tokenectomy Razor** to fix it locally

  before logs ever touch the LLM.

```
## What is eating your context window?

A typical Next.js error trace looks like this:
text
    TypeError: Cannot read properties of undefined
  (reading 'digest')
        at Object.<anon> (/node_modules/next/bundle5.
  js:142:31)
        at __webpack_require__
  (/node_modules/next/bundle5.js:198:12)
        at Object.execute (/node_modules/next/dev-
  server.js:412:19)
        at processTicksAndRejections (task_queues:95:5)
        Database connection failed:
  postgresql://admin:super_secret_password@db.prod.
  internal:5432/primary
        API key leaked: sk-ant-api03-
  abcdef1234567890abcdef1234567890
        [... 480 internal dependency frames flooding
  context ...]

  Notice three things:

  1. 98% of those frames are inside node_modules. Your
  AI agent is not going to edit webpack's internal
  bundle logic. It only cares about the one line in
  src/components/Header.tsx:42 where you missed a
  parenthesis.
  2. The database password and API key are sitting
  unmasked in plain text.
  3. The raw token count for this single error dump was
  45,820 tokens.

  ## What happens after Tokenectomy runs

  When the AI agent invokes get_error_context via MCP,
  Tokenectomy intercepts the log, strips framework
  internals, redacts all secrets locally using a
  deterministic DFA regex, and grabs bounded source code
  lines around the actual crash:

    [:TOKENECTOMY:M2M_CONTROL_PLANE:v1.3.0]
    [STATE=FRAMEWORK_NOISE_PURGED]
    [STRATEGY_APPLIED=AGGRESSIVE]
    [ORIGINAL_BYTES=45820 | CLEAN_BYTES=118 |
  REDUCTION=99%]
    [PRIMARY_CRASH_COORDINATES=src/components/Header.
  tsx:42]

  [COGNITIVE_DIRECTIVE=INSPECT_CALLER_AT_src/components/
  Header.tsx:42]
    [:END_CONTROL_PLANE]

    src/components/Header.tsx:42:15 - SyntaxError
      42 |   const user = useSession( ;
         |                           ^ Expected ')'
    🛡️ [CONNECTION_STRING_REDACTED]
    🛡️ [REDACTED]
  ANTHROPIC_API_KEY=[REDACTED_SECRET_KEY]

  Final token count: 118 tokens.
  Reduction: 99.7%.
  Zero credentials leaked to the cloud.

  ## Why Rust and why local-first?

  I did not want another slow node script or cloud proxy
  adding 300ms of network latency to an agent loop.

  1. Sub-millisecond latency: The core log surgery and
  secret redaction pipeline runs in <0.2 milliseconds on
  an Intel i5 CPU.
  2. Zero cloud leaks: It runs 100% locally over stdio.
  Your error logs, environment variables, and
  proprietary code never touch an external server.
  3. AST verification & rollback: The apply_code_patch
  tool parses modified code with Tree-sitter before
  saving to disk. If the agent generates invalid syntax,
  it immediately rolls back with zero dirty git diff.
  4. Lightweight footprint: Baseline process memory is
  3.45 MB VmRSS.

  Glama.ai audited the server definition under their
  Tool Definition Quality Score (TDQS) and awarded it
  Grade A (4.7 / 5.0) across all tools.

  ## How to set it up (Takes 30 seconds)

  You don't need to install Rust or compile anything. We
  distribute pre-built native binaries via npm for
  Linux, macOS (Apple Silicon & Intel), and Windows.

  Add this to your claude_desktop_config.json or Cursor
  MCP settings:

    {
      "mcpServers": {
        "tokenectomy": {
          "command": "npx",
          "args": ["-y", "tokenectomy-razor", "--mcp"]
        }
      }
    }

  Or if you prefer native cargo:

    cargo install tokenectomy

  Then configure:

    {
      "mcpServers": {
        "tokenectomy": {
          "command": "razor",
          "args": ["--mcp"]
        }
      }
    }

  ## Open Source & Repositories

  Everything is open source under the MIT license:

  1. GitHub: https://github.com/Tokenectomy-
  Labs/Tokenectomy
  2. Glama: https://glama.ai/mcp/servers/Tokenectomy-
  Labs/Tokenectomy
  3. npm: https://www.npmjs.com/package/tokenectomy-
  razor
  4. crates.io: https://crates.io/crates/tokenectomy

  Give it a run next time your agent is about to eat a
  40,000-token crash log. Your token bill will thank
  you.
```


