context-usage-mcp
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., "@context-usage-mcp what's my current token usage?"
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.
context-usage-mcp
A tiny Model Context Protocol server (TypeScript/Node, stdio)
that exposes a single tool, get_context_usage, reporting the current session's raw token
usage — so an agent can check after each task whether context has grown large and branch its
behavior on an absolute threshold. Runs under both Claude Code and OpenAI Codex CLI, reading
each host's own session transcript/rollout behind an auto-detected host-adapter layer.
What it does
Neither host hands an stdio MCP server the live token counts, so this server reads usage indirectly from the host's own on-disk session file, behind an auto-detected host-adapter layer. The output shape is identical across hosts.
Claude Code passes only CLAUDE_PROJECT_DIR (no session id, transcript path, or counts):
Derive the project folder from
CLAUDE_PROJECT_DIR(non-alphanumerics →-).Pick the most-recently-modified
*.jsonlin~/.claude/projects/<folder>/as the current session (its transcript is being written while the tool is called).Scan backward for the last assistant message carrying a
usageblock.Sum
input_tokens + cache_creation_input_tokens + cache_read_input_tokens→context_tokens.
The ~/.claude root can be relocated with CLAUDE_CONFIG_DIR.
OpenAI Codex CLI passes no project/session env var at all, but launches the server with its
working directory set to the session's project dir. So the server uses process.cwd():
Resolve the Codex root (
CODEX_HOME, else~/.codex); rollouts live under<root>/sessions/YYYY/MM/DD/rollout-*.jsonl.Scan recent day-folders (today + yesterday, widening to ~7 days), reading only each candidate's first
session_metaline to match itscwdagainstprocess.cwd(); pick the freshest match.Scan backward for the last
token_countevent and read its per-turnlast_token_usage.Map to the shared, disjoint breakdown. Codex's
cached_input_tokensis a subset ofinput_tokens(verified in source), soinput←input − cached; thencontext_tokens = input + cache_read + cache_creation— no double-count.
Output is output_tokens + reasoning_output_tokens. The Codex model_context_window is ignored
(raw counts only, strict parity with the Claude host).
Related MCP server: Claude Telemetry MCP
Install & run
Run the published package directly with npx — no clone or build required:
npx -y @beeltec/context-usage-mcpThis is the recommended way to wire it into a host (see the registration sections below). The -y
flag skips the install prompt on first run.
Run from source (local dev) — for hacking on the server itself:
npm install npm run build # tsc → dist/, sets +x on dist/index.js npm test # parser unit testsThen register the absolute path to
dist/index.js(see the "from source" note in each registration section).
Host detection
The server auto-detects its host: CLAUDE_PROJECT_DIR set → Claude Code; else CODEX_HOME set or
~/.codex/sessions present → Codex; else it falls back to Claude. Set the environment variable
CONTEXT_USAGE_HOST=claude|codex to force a host (the override wins over auto-detection).
Register with Claude Code (user scope)
claude mcp add --scope user context-usage -- npx -y @beeltec/context-usage-mcpVerify with claude mcp list. Because MCP servers are loaded at session start, start a new
Claude Code session before calling the tool.
From source: register the absolute path to this repo's built binary instead:
claude mcp add --scope user context-usage -- node /absolute/path/to/dist/index.js
Register with Codex CLI
Add an entry to ~/.codex/config.toml (root movable with CODEX_HOME):
[mcp_servers.context-usage]
command = "npx"
args = ["-y", "@beeltec/context-usage-mcp"]Or use the CLI equivalent:
codex mcp add context-usage -- npx -y @beeltec/context-usage-mcpDo not set a custom cwd for the server: Codex passes no project/session identifier, so
discovery relies on the server inheriting the session's working directory via process.cwd().
Setting cwd breaks the cwd-match. MCP servers load at session start, so start a new Codex
session before calling the tool.
From source: use
command = "node"withargs = ["/absolute/path/to/dist/index.js"](orcodex mcp add context-usage -- node /absolute/path/to/dist/index.js).
Distribution
Published to npm as @beeltec/context-usage-mcp
and run via npx (see Install & run). Releases are published from CI with
provenance on each GitHub Release; running from a local build is also supported for development.
The tool: get_context_usage
No arguments. Returns raw counts only — no percentage, no context-window detection. It may be
unavailable early in a session before the first model response. Both a JSON text block and
typed structuredContent (declared outputSchema) are returned with the same object. The shape is
identical under Claude Code and Codex.
Available:
{
"available": true,
"context_tokens": 86026,
"breakdown": {
"input_tokens": 2,
"cache_creation_input_tokens": 1001,
"cache_read_input_tokens": 85023,
"output_tokens": 225
},
"session_id": "…",
"model": "claude-opus-4-8",
"timestamp": "2026-07-20T14:17:50.751Z",
"reason": null
}Unavailable (never throws): numeric fields are null and reason explains why (no
transcript/rollout found, empty file, or no usage-bearing message/token_count event yet).
Field | Meaning |
| Whether a reading was obtained |
|
|
| Raw per-category counts, including |
| Metadata from the source message/event |
| Why the reading is unavailable ( |
Intended usage
The server returns raw numbers only; the agent decides what "too long" means. The pattern:
call get_context_usage after each task and branch on an absolute token threshold defined in the
agent's prompt/CLAUDE.md (e.g. wrap up or hand off when context_tokens exceeds a limit).
Known risks
Internal file format. Both the Claude transcript JSONL and the Codex rollout JSONL are undocumented internal formats that may change between host versions; parsing them directly is an accepted fragility.
Concurrent-session race. The freshest-file heuristic (freshest
*.jsonlfor Claude; freshest cwd-matchingrollout-*.jsonlfor Codex) is racy if multiple sessions run in the same project at once — it may pick another session's file.
Available Tools
1 toolget_context_usageGet context usageA
Reports the current session's raw token usage, read directly from the host's own session transcript/rollout (auto-detected: Claude Code or OpenAI Codex CLI). Returns raw counts only — context_tokens (input + cache_creation + cache_read) plus a full breakdown and session metadata (session_id, model, timestamp) — with no percentage or context-window detection. May be unavailable early in a session before the first model response; then it returns { available: false, reason } instead of failing.
Inputs: no named parameters. Returns: model, reason, available, breakdown, +3 more
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 1 tool
With only one tool, there is no possibility of ambiguity or misselection. The tool's purpose is clearly defined and distinct by default.
The single tool name 'get_context_usage' follows a clear verb_noun pattern. Consistency is trivially maintained as there are no other tools to contrast with.
The server has only one tool, which feels thin for a typical server but is arguably sufficient for its narrow purpose. It is not trivial, but the count is on the lower end of the acceptable range.
The tool covers the core raw token usage reporting, but it explicitly lacks percentage or context-window detection, leaving a notable gap for users who need derived insights. The domain is narrowly focused, but the surface is somewhat incomplete.
Maintenance
Related MCP Connectors
Every employee asks your codebase from Claude, without access to it. Statistics on each developer.
Exact Claude API cost calc with real cache economics, plus a tiktoken-misuse scanner.
Research-backed lint + generation for agent context files (CLAUDE.md, AGENTS.md) + prompt enhancer.
Use your Mac, Windows or Linux computer from ChatGPT, Claude or Codex: files, commands, documents.
Related MCP Servers
- AlicenseBqualityDmaintenanceProvides intelligent context management for AI development sessions, allowing users to track token usage, manage conversation context, and seamlessly restore context when reaching token limits.810 npm2Apache 2.0
- FlicenseBqualityNot gradedmaintenanceProvides comprehensive telemetry and usage analytics for Claude Code sessions, including token usage tracking, cost monitoring, and tool usage patterns. Enables users to monitor their Claude usage with detailed metrics, warnings, and trend analysis.12-
- FlicenseNot gradedqualityNot gradedmaintenanceProvides intelligent analysis of token usage patterns and optimization recommendations to improve efficiency and reduce costs in Claude Code sessions. Offers real-time analysis, cost metrics, and actionable insights for better context window and tool usage optimization.3 npm-
- AlicenseNot gradedqualityCmaintenanceSurface Claude Code token usage, estimated cost, and plan-limit status in any MCP client. Enables agents to query usage data from local logs and Anthropic API.MIT