Skip to main content
Glama

MCP File Compaction

An MCP server that reduces Claude context window costs by automatically summarizing files to their public interfaces.

The Problem

When Claude works on large tasks across multiple files, the context window grows continuously. Each API request costs based on the full size of the context, not just new tokens. This leads to quadratic cost growth:

  1. Implement ptr.rs (2KB) → Context: 2KB

  2. Implement raw_page.rs using ptr.rs (3KB) → Context: 5KB

  3. Implement paged_pool.rs using both (4KB) → Context: 9KB

After finishing a file, Claude doesn't need the full implementation—just the public interface (structs, functions, traits).

Related MCP server: mcp-injector

The Solution

This MCP server:

  • Tracks the "active" file — the one you're currently editing (full contents)

  • Auto-summarizes inactive files — when you switch files, the previous one is summarized to just its public API

  • Uses AST parsing — deterministic, fast, no LLM calls for summarization

  • Handles unsupported languages gracefully — returns full contents without tracking

Installation

npx github:YOUR_USERNAME/mcp-file-compaction

Local development

git clone https://github.com/YOUR_USERNAME/mcp-file-compaction.git
cd mcp-file-compaction
npm install
npm run build

Configuration

Add to your Claude Code MCP settings:

{
  "mcpServers": {
    "file-compaction": {
      "command": "npx",
      "args": ["github:YOUR_USERNAME/mcp-file-compaction"]
    }
  }
}

Or for local development:

{
  "mcpServers": {
    "file-compaction": {
      "command": "node",
      "args": ["/path/to/mcp-file-compaction/dist/index.js"]
    }
  }
}

Add to your CLAUDE.md:

## File Operations

Use the file-compaction MCP server for file operations:
- `read_file` instead of `Read` when you need full file contents
- `peek_file` when you only need to check interfaces
- `edit_file` instead of `Edit` for modifications
- `write_file` instead of `Write` for new files
- `file_status` to see tracked files and context savings

This reduces context window size by keeping only summaries of inactive files.

Tools

read_file

Read a file and mark it as the active file. When you switch to a different file, the previous file is automatically summarized.

{ "path": "src/lib.rs" }

peek_file

Get a summary of a file's public interface without changing the active file. Useful for checking APIs.

{ "path": "src/ptr.rs" }

edit_file

Edit a file by replacing a specific string. The file becomes (or remains) the active file.

{
  "path": "src/lib.rs",
  "old_string": "fn old_name(",
  "new_string": "fn new_name("
}

write_file

Write content to a file, creating it if needed. The file becomes the active file.

{
  "path": "src/new_module.rs",
  "content": "//! New module\n\npub fn hello() {}\n"
}

file_status

Show all tracked files with size comparison and savings.

Context Status
==============
Active: src/paged_pool.rs (full, 4.2 KB)

Cached Summaries:
  src/ptr.rs        312 B  (was 2.1 KB, saved 1.8 KB)
  src/raw_page.rs   428 B  (was 3.4 KB, saved 3.0 KB)

Total Context:        5.2 KB
Without Compaction:   11.5 KB
Savings:              6.3 KB (55%)

forget_file

Remove a file from tracking entirely.

{ "path": "src/old_file.rs" }

Supported Languages

Currently supported for summarization:

  • Rust (.rs) — extracts public structs, enums, traits, functions, type aliases, constants, and re-exports

Unsupported file types are read/edited normally without tracking—they won't interfere with compaction.

How Summaries Work

For a Rust file like:

//! Type-safe pointer wrappers.

use std::marker::PhantomData;

#[derive(Debug, Clone)]
pub struct Ptr<T> {
    raw: *mut T,
    _marker: PhantomData<T>,
}

impl<T> Ptr<T> {
    pub fn new(raw: *mut T) -> Self {
        Self { raw, _marker: PhantomData }
    }

    pub fn is_null(&self) -> bool {
        self.raw.is_null()
    }

    // Private helper
    fn internal_check(&self) -> bool {
        !self.raw.is_null()
    }
}

The summary becomes:

// Purpose: Type-safe pointer wrappers.

#[derive(Debug, Clone)]
pub struct Ptr<T> { ... }
impl<T> Ptr<T> {
    pub fn new(raw: *mut T) -> Self;
    pub fn is_null(&self) -> bool;
}

Private items, implementation details, and doc comments are condensed—only the public interface remains.

License

MIT

Available Tools

6 tools
edit_fileA

Edit a file by replacing a specific string. The file becomes (or remains) the active file.

The old_string must:

  • Match exactly, including whitespace and indentation

  • Appear exactly once in the file (for safety)

After editing, the file's cached summary is updated.

Inputs: path (required), old_string (required), new_string (required).

file_statusB

Show the status of all tracked files including:

  • The currently active file (full contents in context)

  • Cached summaries with size comparison

  • Total context savings from compaction

Inputs: no named parameters.

forget_fileB

Remove a file from tracking. Useful for cleanup or when you no longer need a file's interface in context.

Inputs: path (required).

peek_fileA

Get a summary of a file's public interface without changing the active file. Useful for checking APIs of files you've already worked on.

Returns:

  • For the active file: full contents

  • For previously read files: cached summary (public structs, functions, traits, etc.)

  • For unsupported file types: full contents

Inputs: path (required).

read_fileA

Read a file and mark it as the active file. When you switch to a different file, the previous file is automatically summarized to just its public interface, reducing context size.

Supported languages for summarization: .rs, .py, .ts, .tsx, .js, .jsx, .php, .cs, .gd

For unsupported file types, returns full contents without tracking (same as standard file read).

Inputs: path (required).

write_fileA

Write content to a file, creating it if it doesn't exist. The file becomes the active file.

Creates parent directories if needed.

Inputs: path (required), content (required).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool has a clearly distinct purpose with no overlap: edit_file modifies content, file_status shows tracking info, forget_file removes tracking, peek_file summarizes without activation, read_file reads and activates, and write_file creates/writes and activates. The descriptions clearly differentiate their functions, preventing misselection.

    Naming Consistency5/5

    All tool names follow a consistent verb_noun pattern (e.g., edit_file, read_file, write_file) with no deviations in style or casing. This uniformity makes the tool set predictable and easy to navigate for an agent.

    Tool Count5/5

    With 6 tools, the server is well-scoped for file compaction and management, covering essential operations like reading, writing, editing, tracking, and summarizing files. Each tool serves a necessary function without redundancy or bloat.

    Completeness4/5

    The tool set provides strong coverage for file compaction workflows, including CRUD-like operations (read, write, edit, forget) and status/summary features. A minor gap is the lack of a tool for batch operations or handling multiple files at once, but agents can work around this by iterating through individual tools.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      A
      quality
      D
      maintenance
      Persistent daemon that compresses codebases via AST body folding before indexing them for AI coding assistants like Claude Code and Cursor 57-89% token reduction with sub-millisecond queries, verified on Django, Spring, and Next.js.
      12
      4
      -
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI coding agents to efficiently analyze codebases by extracting AST skeletons, reducing token usage while preserving type contracts and interfaces.
      2 npm
      1
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Provides Claude Code with a repository symbol graph via tree-sitter, enabling instant symbol lookup, file skeletons, call-site analysis, and persistent notes, reducing token usage by reading signatures instead of whole files.
      12
      1
      MIT