mcp-tokens

command
v1.18.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: MIT Imports: 15 Imported by: 0

README

mcp-tokens

Reports tiktoken-based token counts for every MCP tool exposed by the codebase (twprojects + twdesk + twspaces), sorted by cost. Introspects the local Go source — no HTTP server, no auth.

Useful for tracking tool-description token budgets across revisions and for spotting the heaviest tools when trimming.

Snapshot the current tree

go run ./cmd/mcp-tokens                   # token table for every tool
go run ./cmd/mcp-tokens -encoding=cl100k_base
go run ./cmd/mcp-tokens -json > tools.json   # full export-tools-shaped JSON

Diff against a base ref

-base=<ref> materialises that ref in a throwaway git worktree, runs the same binary against it, and prints the delta. Your working tree is never touched, so uncommitted edits in internal/ are safe — handy for the inner dev loop while you trim descriptions.

go run ./cmd/mcp-tokens -base=main                      # text summary + per-tool movers
go run ./cmd/mcp-tokens -base=main -format=markdown     # GFM table (PR-comment friendly)
go run ./cmd/mcp-tokens -base=main -format=json         # structured output for scripts
go run ./cmd/mcp-tokens -base=origin/main               # any git ref works

The diff cleanly handles tools added or removed between the two trees: missing-on-one-side rows show the absent count as 0.

Sample text output:

  base (main)         44,950
  current             33,723
  delta              -11,227  (-24.98%)

  per-tool deltas (117):
    tool                                      before     after     delta
    --------------------------------------  --------  --------  --------
    twprojects-list_tasks                        832       702      -130
    twprojects-update_timelog                    608       485      -123
    ...

Caveats

  • Counts use OpenAI's tiktoken (o200k_base by default). Treat them as a relative signal across revisions — Claude's tokenizer differs, but the percentage delta tracks closely.
  • Counts cover the tool name, description, and JSON-marshalled input schema. They exclude per-message framing the model adds at runtime, so the absolute total is a lower bound on what the LLM actually sees.
  • Diff mode requires git and a working go toolchain (it shells out to go run inside the temporary worktree). First run of a new base ref pays one Go-compile cost; subsequent runs reuse Go's build cache.

Documentation

Overview

mcp-tokens reports tiktoken-based token counts for every MCP tool exposed by the codebase, sorted by cost. It introspects the Go source directly (no HTTP server, no auth), so it always reflects the local working tree.

Usage:

go run ./cmd/mcp-tokens                       # token counts (default)
go run ./cmd/mcp-tokens -json                 # full export-tools-shaped JSON
go run ./cmd/mcp-tokens -base=main            # diff vs main, text output
go run ./cmd/mcp-tokens -base=main -format=markdown
go run ./cmd/mcp-tokens -encoding=cl100k_base

Diff mode spins up a temporary `git worktree` at the base ref and runs the same binary there, so your working tree is never touched — uncommitted or staged changes under internal/ are safe.

Token counts use OpenAI's tiktoken; treat the numbers as a *relative* signal across revisions, not as absolute Claude figures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL