mcp-working-context-optimizer
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mcp-working-context-optimizer compress the history and clear recent logs"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
mcp-working-context-optimizer
An MCP (Model Context Protocol) server designed to optimize the working context of AI agents. It prevents context bloat and the "Lost in the Middle" phenomenon by distilling action histories into concise summaries while maintaining a clear core objective.
🌟 The Problem it Solves
Current autonomous AI agents (like AutoGPT, Cline, or custom smolagents) tend to append all tool execution logs and error messages directly into their context window. This naive approach quickly leads to:
Context Bloat & OOM: Exceeding the token limit or causing Out-Of-Memory errors on local LLM servers due to massive KV cache expansion.
Lost in the Middle: The agent gets distracted by recent, verbose error logs and forgets the original, overarching objective.
Related MCP server: ContextBridge
💡 The Solution: Dual-Track Memory
This MCP server acts as an external "Working Memory" for the agent. It enforces a structured context containing:
Current Task: The immediate next step or user instruction.
Core Objective: The ultimate goal (never truncated, always focused).
Summarized History: A compressed version of past events.
Recent Actions: Raw logs of the last few steps (auto-truncated to prevent bloat).
When the recent actions limit is reached, the server proactively prompts the agent to summarize the history and clear the logs, keeping the context perfectly optimized.
🚀 Installation
Ensure you have Python 3.10 or higher. Using uv is recommended.
# Clone the repository
git clone [https://github.com/your-username/mcp-working-context-optimizer.git](https://github.com/your-username/mcp-working-context-optimizer.git)
cd mcp-working-context-optimizer
# Install via uv (or pip)
uv pip install -e .⚙️ Usage / Configuration
To use this with an MCP client (like Claude Desktop, Cursor, or Cline), add the following to your MCP settings file (e.g., mcp_config.json or claude_desktop_config.json):
{
"mcpServers": {
"working-context-optimizer": {
"command": "mcp-working-context-optimizer"
}
}
}(Note: If using uv, you might need to specify the absolute path to the executable or run via uvx depending on your environment).
🛠️ Provided Tools & Resources
Resources
working-context://state: Returns the optimized Markdown representation of the current working context. The agent should read this when losing track of the context or starting a new task.
Tools
set_core_objective(objective: str): Sets the primary goal and constraints.update_current_task(task: str): Sets the immediate, short-term focus.log_action(action: str, result: str): Logs a tool action and its result. Extremely long results are automatically truncated to 2000 characters.compress_history(new_summary: str): Used by the agent to update the summarized history and clear the recent action logs, freeing up context space.
🤖 Agent Workflow Example
The agent reads the user prompt and calls
set_core_objectiveandupdate_current_task.The agent executes a tool (e.g., reading a file, running a shell command) and calls
log_actionto store the result.The agent reads
working-context://stateto decide the next step.If
working-context://statereturns a warning that the recent actions limit is reached, the agent callscompress_historyto summarize the past actions, thereby keeping its own context window clean.
📄 License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Available Tools
4 toolscompress_historyA
履歴を要約し、Recent Actions をクリアします。
Inputs: new_summary (required). Returns: result
log_actionC
直近の行動とその結果をログに記録します。
Inputs: action (required), result (required). Returns: result
set_core_objectiveC
大目標(Core Objective)を設定します。
Inputs: objective (required). Returns: result
update_current_taskC
現在の最優先タスク(Current Task)を更新します。
Inputs: task (required). Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: compress_history summarizes and clears recent actions, log_action records actions/results, set_core_objective sets a core goal, and update_current_task updates the priority task. No tool overlaps with another.
All tool names follow a consistent verb_noun pattern with underscores (compress_history, log_action, set_core_objective, update_current_task). No mixing of conventions or inconsistent verb styles.
With 4 tools, the server is well-scoped for managing working context. Each tool covers a distinct aspect (logging, compressing, setting objectives, updating tasks) without unnecessary clutter.
The tool set covers core lifecycle operations: setting objectives, updating tasks, logging actions, and compressing history. A minor gap exists for reading current context or viewing history, but the surface is largely complete for the stated purpose.
Maintenance
Related MCP Connectors
Same functionality, consuming only 1/20 of the context window tokens.
Rules-based context reduction, planning checks, research prompts and fixed experiences for agents.
Stop re-explaining yourself to Agents. Give it the right context, right when needed.
Knowledge accumulation for AI coding agents. Records decisions, problems, and insights as context.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables AI agents to maintain context across conversation sessions by saving and retrieving summaries of key points from past interactions.-
- AlicenseAqualityDmaintenanceEnables compressing an in-progress AI conversation into a structured context block preserving user goal, decisions, and state, then formats it for another AI to continue seamlessly.454 npm3MIT
- AlicenseNot gradedqualityBmaintenanceProvides deterministic context compaction and safety gating for AI agents, preserving original messages verbatim while dropping irrelevant ones and flagging destructive commands.2MIT
- AlicenseNot gradedqualityCmaintenanceEnables coding agents to shrink bulky tool output, logs, test runs, and file dumps down to compact briefs, and to record and recall decisions in a small session ledger. This keeps long-running tasks within context limits while preserving errors, findings, and key notes.8 npmMIT