MCP.so
Sign In

flatten-mcp

@shayaShav

About flatten-mcp

An MCP server that flattens Claude Code sessions — keeping every prompt and event verbatim while reclaiming context tokens, so you resume the exact same raw conversation at a lower token count instead of compacting it into a lossy summary. It moves bulky tool output (large file r

Basic information

Category

AI & Agents

Transports

stdio

Publisher

shayaShav

Submitted by

Shaya Shaviv

Config

Add this server to your MCP-compatible client using the configuration below.

{
  "mcpServers": {
    "flatten": {
      "command": "npx",
      "args": [
        "-y",
        "flatten-mcp@latest"
      ]
    }
  }
}

Tools

6

Move bulky tool results into a sidecar, leaving `[FLATTENED …]` markers. Crash-safe and reversible. Supports `dry_run`, `min_size`, `force`, and `include_tool_use_result`.

Fetch one original block back by its id — returns the original text, or re-renders a flattened screenshot as a real image.

Reverse a flatten completely: re-inline every block from the sidecar, restoring each flattened result to its exact original value.

Reclaim disk by deleting leftover `.bak` / `.tmp` files (and, opt-in, sidecars). Defaults to a safe dry run.

List a project's sessions with branch, message count, size, and first prompt.

Keyword / branch / date search across past sessions — scans prose, tool I/O, **and** flatten sidecars so nothing goes dark after flattening.

Overview

What is flatten-mcp?

flatten-mcp is a Model Context Protocol server for Claude Code that reduces a session's token footprint by moving bulky tool output (large file reads, command logs, base64 screenshots) into a sidecar file, leaving a lightweight retrievable reference in its place. It preserves the exact chronological flow and all user prompts verbatim.

How to use flatten-mcp?

Requires Node.js ≥ 18 and Claude Code. Install user-wide via claude mcp add flatten -s user -- npx -y flatten-mcp@latest. To flatten a session, exit it with Ctrl‑C, then in a new Claude Code window use the /flatten latest or /flatten <session-id> command, or ask the model directly. Resume the original session afterward. Dry runs and unflatten are supported.

Key features of flatten-mcp

  • Lossless and reversible – unflatten restores every block to its exact original value
  • Idempotent – re‑running skips already‑flattened blocks
  • Crash‑safe – originals are persisted to sidecar before removal, with atomic file writes
  • Reports both disk bytes saved and context tokens saved
  • Provides compact [FLATTENED …] markers with retrieval instructions
  • Includes tools for preview, retrieval, unflatten, and artifact cleanup

Use cases of flatten-mcp

  • Reclaim context window space in token‑heavy sessions (around 200k–300k tokens)
  • Preserve verbatim conversation history without lossy compaction
  • Retrieve original block content on demand via retrieve_flattened
  • Run a dedicated inexpensive model window as a flatten‑only station

FAQ from flatten-mcp

How does flatten differ from compaction?

Compaction rewrites the entire history into a lossy summary. Flatten instead moves only the bulky tool output to a sidecar while keeping every user and assistant message untouched, making it lossless.

Is flatten reversible?

Yes. unflatten_session re‑inlines every block from the sidecar, restoring each flattened result to its exact original value.

What are the system requirements?

Node.js ≥ 18 and Claude Code. The server is installed from npm and registered as a Claude Code MCP server.

Does flatten work with agents other than Claude Code?

No. flatten-mcp reads Claude Code’s session store at ~/.claude/projects/*.jsonl and is tested exclusively with Claude Code. The paths and JSONL schema are specific to it.

How does the server handle interruptions or crashes?

The session file is rewritten atomically via temp‑file‑and‑rename, and originals are written to the sidecar before removal from the session. An interrupted run never leaves a half‑written session file.

Comments

More AI & Agents MCP servers