← العودة للرئيسية

🧠 Context Mode — حل مشكلة نافذة السياق

كل مرة تطلب فيها من نموذج الذكاء الاصطناعي قراءة ملف، جلب صفحة ويب، أو تشغيل أمر — تُضخ البيانات الخام مباشرةً في نافذة السياق. Playwright snapshot = 56 KB. عشرون issue على GitHub = 59 KB. سجل access واحد = 45 KB. بعد 30 دقيقة، ينتهي 40% من نافذة السياق. وعند ضغط المحادثة، ينسى النموذج ما كان يعمل عليه.

Context Mode هو خادم MCP يحل هذه المشكلة من أربع جهات دفعة واحدة.


المشكلة بالأرقام

ما يحدث بدون Context Mode:

  • كل استدعاء أداة يضخ بياناته كاملةً في السياق
  • جلسة عمل من 30 دقيقة تستهلك النافذة بأكملها
  • عند الضغط، النموذج ينسى الملفات المفتوحة، المهام الجارية، والقرارات السابقة
  • المستخدم مضطر لإعادة الشرح من البداية

ما يحدث مع Context Mode:

  • 315 KB من المخرجات الخام → 5.4 KB فعلي في السياق
  • مدة الجلسة المنتجة: من 30 دقيقة → 3 ساعات
  • بعد الضغط، النموذج يكمل من حيث توقف بالضبط

الحلول الأربعة

١. حفظ السياق (Context Saving)

أدوات sandbox تعالج البيانات الخام في بيئة معزولة — فقط النتيجة المُلخَّصة تدخل السياق. الفارق: 56 KB → 299 B. أي 99% توفير.

المنطق الذي يغير طريقة التفكير:
بدلاً من أن يقرأ النموذج 50 ملفاً ليعدّ الدوال فيها، يكتب سكريبتاً يقوم بالعدّ ويطبع النتيجة فقط. سكريبت واحد يحل محل 10 استدعاءات ويوفر 100x من السياق.

// Before: 47 × Read() = 700 KB في السياق
// After:  1 × ctx_execute() = 3.6 KB
ctx_execute("javascript", `
  const files = fs.readdirSync('src').filter(f => f.endsWith('.ts'));
  files.forEach(f => console.log(f + ': ' +
    fs.readFileSync('src/'+f,'utf8').split('\\n').length + ' lines'));
`);

٢. استمرارية الجلسة (Session Continuity)

كل تعديل ملف، عملية git، مهمة، خطأ، وقرار مستخدم يُحفظ في قاعدة بيانات SQLite. عند ضغط المحادثة، لا تُعاد البيانات كلها إلى السياق — بل يُفهرَس كل شيء ويُسترجع منه فقط ما هو ذو صلة. النموذج يكمل من آخر نقطة توقف دون أن يسألك عن شيء.

٣. التفكير بالكود (Think in Code)

مبدأ جوهري: النموذج يُولِّد الكود الذي يُحلِّل البيانات، ولا يُحلِّل البيانات بنفسه. هذا يُطبَّق على 11 لغة برمجة: JavaScript، TypeScript، Python، Shell، Ruby، Go، Rust، PHP، Perl، R، وElixir.

٤. ضغط المخرجات (Output Compression)

تخفيض 65–75% من توكنات المخرجات مع الحفاظ على الدقة التقنية الكاملة. حذف الحشو، التحيات، والشروحات المطولة. فقط الجوهر.


كيف تثبته

Claude Code (الأسهل — marketplace)

/plugin marketplace add mksglu/context-mode
/plugin install context-mode@context-mode

أعد تشغيل Claude Code، ثم تحقق:

/context-mode:ctx-doctor

VS Code Copilot

١. ثبّت globally:

npm install -g context-mode

٢. أنشئ ملف .vscode/mcp.json في مشروعك:

{
  "servers": {
    "context-mode": {
      "command": "context-mode"
    }
  }
}

٣. أعد تشغيل VS Code. للتحقق: اكتب ctx stats في Copilot Chat.

Cursor

npm install -g context-mode

أنشئ .cursor/mcp.json:

{
  "mcpServers": {
    "context-mode": {
      "command": "context-mode"
    }
  }
}

Gemini CLI

npm install -g context-mode

أضف إلى ~/.gemini/settings.json كل إعدادات mcpServers والـ hooks — التفاصيل الكاملة في الـ README الرسمي.


الأدوات الأساسية

الأداةالوظيفةالتوفير
ctx_executeتشغيل كود في 11 لغة. فقط stdout يدخل السياق56 KB → 299 B
ctx_batch_executeتشغيل أوامر متعددة في استدعاء واحد986 KB → 62 KB
ctx_execute_fileمعالجة الملفات في sandbox. المحتوى لا يُرى45 KB → 155 B
ctx_fetch_and_indexجلب URL وفهرسته. كاش 24 ساعة تلقائي60 KB → 40 B
ctx_searchالبحث في المحتوى المُفهرَساسترجاع عند الطلب
ctx_statsتقرير التوفير وإحصائيات الجلسة

متى تستخدمه؟

  • جلسات عمل طويلة مع Claude Code أو Copilot أو Cursor
  • عند العمل مع repos كبيرة أو قراءة ملفات كثيرة
  • عند استخدام Playwright أو جلب بيانات من APIs
  • في المشاريع التي تستمر على مدى أيام وتحتاج استمرارية
  • عند تحليل logs، CSVs، أو مخرجات ضخمة

أوامر الاستخدام اليومي

اكتبها مباشرةً في جلسة AI — النموذج يستدعي الأداة تلقائياً:

ctx stats    ← تقرير التوفير وإحصائيات الجلسة
ctx doctor   ← تشخيص التثبيت والـ hooks
ctx upgrade  ← تحديث للإصدار الأخير
ctx insight  ← لوحة تحليلات شخصية (تفتح في المتصفح)

الخصوصية

Context Mode لا يرسل أي شيء إلى الخارج. لا telemetry، لا cloud sync، لا حساب مطلوب. كل شيء محلي: السكريبتات تعمل في subprocess معزول، قواعد البيانات في مجلد home. البيانات الخام — الملفات، الـ APIs، الـ snapshots — لا تغادر الـ sandbox أبداً.


مصادر إضافية

← Back to Home

🧠 Context Mode — Solving the Context Window Problem

Every time you ask an AI model to read a file, fetch a webpage, or run a command — raw data is dumped directly into the context window. A Playwright snapshot costs 56 KB. Twenty GitHub issues cost 59 KB. One access log — 45 KB. After 30 minutes, 40% of your context window is gone. When the conversation compacts, the model forgets which files it was editing, what tasks are in progress, and what you last asked for.

Context Mode is an MCP server that solves this problem from four directions at once.


The Problem in Numbers

Without Context Mode:

  • Every tool call dumps its full output into context
  • A 30-minute working session exhausts the entire window
  • On compaction, the model forgets open files, running tasks, and past decisions
  • You have to re-explain everything from scratch

With Context Mode:

  • 315 KB of raw output → 5.4 KB in context
  • Productive session time: from ~30 minutes → ~3 hours
  • After compaction, the model picks up exactly where it left off

Four Solutions in One

1. Context Saving

Sandbox tools process raw data in an isolated subprocess — only the summarized result enters context. A Playwright snapshot goes from 56 KB to 299 B. 99% reduction.

The paradigm shift:
Instead of reading 50 files to count functions, the model writes a script that does the counting and logs only the result. One script replaces 10 tool calls and saves 100x context.

// Before: 47 × Read() = 700 KB in context
// After:  1 × ctx_execute() = 3.6 KB
ctx_execute("javascript", `
  const files = fs.readdirSync('src').filter(f => f.endsWith('.ts'));
  files.forEach(f => console.log(f + ': ' +
    fs.readFileSync('src/'+f,'utf8').split('\\n').length + ' lines'));
`);

2. Session Continuity

Every file edit, git operation, task, error, and user decision is tracked in SQLite. When the conversation compacts, context-mode indexes events into FTS5 and retrieves only what's relevant via BM25 search. The model continues from your last prompt without asking you to repeat anything.

3. Think in Code

The core principle: the model generates code that analyzes data, rather than analyzing data directly. Supported in 11 languages: JavaScript, TypeScript, Python, Shell, Ruby, Go, Rust, PHP, Perl, R, and Elixir.

4. Output Compression

65–75% reduction in output tokens while maintaining full technical accuracy. Drops filler, pleasantries, and verbose explanations. Only substance remains.


Installation

Claude Code (easiest — plugin marketplace)

/plugin marketplace add mksglu/context-mode
/plugin install context-mode@context-mode

Restart Claude Code, then verify:

/context-mode:ctx-doctor

VS Code Copilot

1. Install globally:

npm install -g context-mode

2. Create .vscode/mcp.json in your project:

{
  "servers": {
    "context-mode": {
      "command": "context-mode"
    }
  }
}

3. Restart VS Code. Verify: type ctx stats in Copilot Chat.

Cursor

npm install -g context-mode

Create .cursor/mcp.json:

{
  "mcpServers": {
    "context-mode": {
      "command": "context-mode"
    }
  }
}

Gemini CLI

npm install -g context-mode

Add MCP server and hooks to ~/.gemini/settings.json — full config in the official README.


Core Tools

ToolWhat it doesSavings
ctx_executeRun code in 11 languages. Only stdout enters context56 KB → 299 B
ctx_batch_executeRun multiple commands in a single call986 KB → 62 KB
ctx_execute_fileProcess files in sandbox. Raw content never enters context45 KB → 155 B
ctx_fetch_and_indexFetch URL and index it. 24h TTL cache60 KB → 40 B
ctx_searchSearch indexed content on demandOn-demand retrieval
ctx_statsContext savings report and session statistics

When to Use It

  • Long working sessions with Claude Code, Copilot, or Cursor
  • When working with large codebases or reading many files
  • When using Playwright or fetching API data
  • In multi-day projects that need session continuity
  • When analyzing logs, CSVs, or large command output

Daily Utility Commands

Type these directly in any AI session — the model calls the tool automatically:

ctx stats    → savings report and session statistics
ctx doctor   → diagnose installation and hooks
ctx upgrade  → update to latest version
ctx insight  → personal analytics dashboard (opens in browser)

Privacy

Context Mode sends nothing outside your machine. No telemetry, no cloud sync, no account required. Everything is local: scripts run in isolated subprocesses, databases live in your home directory. Raw data — files, API responses, snapshots — never leaves the sandbox.


Additional Resources