context-usage-mcp

context-usage-mcp

Exposes a get_context_usage tool that reports raw token usage from session transcripts for Claude Code and OpenAI Codex CLI, enabling agents to check context size and branch behavior.

Category
访问服务器

README

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):

  1. Derive the project folder from CLAUDE_PROJECT_DIR (non-alphanumerics → -).
  2. Pick the most-recently-modified *.jsonl in ~/.claude/projects/<folder>/ as the current session (its transcript is being written while the tool is called).
  3. Scan backward for the last assistant message carrying a usage block.
  4. Sum input_tokens + cache_creation_input_tokens + cache_read_input_tokenscontext_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():

  1. Resolve the Codex root (CODEX_HOME, else ~/.codex); rollouts live under <root>/sessions/YYYY/MM/DD/rollout-*.jsonl.
  2. Scan recent day-folders (today + yesterday, widening to ~7 days), reading only each candidate's first session_meta line to match its cwd against process.cwd(); pick the freshest match.
  3. Scan backward for the last token_count event and read its per-turn last_token_usage.
  4. Map to the shared, disjoint breakdown. Codex's cached_input_tokens is a subset of input_tokens (verified in source), so inputinput − cached; then context_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).

Install & run

Run the published package directly with npx — no clone or build required:

npx -y @beeltec/context-usage-mcp

This 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 tests

Then 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-mcp

Verify 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-mcp

Do 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" with args = ["/absolute/path/to/dist/index.js"] (or codex 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
available Whether a reading was obtained
context_tokens input + cache_creation + cache_read (the context fed back to the model)
breakdown Raw per-category counts, including output_tokens
session_id / model / timestamp Metadata from the source message/event
reason Why the reading is unavailable (null when available)

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 *.jsonl for Claude; freshest cwd-matching rollout-*.jsonl for Codex) is racy if multiple sessions run in the same project at once — it may pick another session's file.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选