Skip to content

Add mcp-tokens command to measure tool token costs#300

Merged
tw-garymeehan merged 2 commits into
mainfrom
gary/mcp-tokens-cmd
May 6, 2026
Merged

Add mcp-tokens command to measure tool token costs#300
tw-garymeehan merged 2 commits into
mainfrom
gary/mcp-tokens-cmd

Conversation

@tw-garymeehan
Copy link
Copy Markdown
Contributor

Summary

  • New cmd/mcp-tokens dev tool: tiktoken-based token counts for every MCP tool, sorted by cost.
  • -json emits the same shape as mcp-http-cli export-tools.
  • -base=<ref> materialises the ref in a throwaway git worktree and prints a diff (text/markdown/json).

Test plan

  • go run ./cmd/mcp-tokens — snapshot table renders, total matches expectation.
  • go run ./cmd/mcp-tokens -base=main — diff renders, working tree untouched.
  • go test ./cmd/mcp-tokens/ — smoke test passes.

Reports tiktoken-based token counts for every MCP tool exposed by the
codebase, sorted by cost. Useful for tracking tool-description token
budgets across revisions and spotting heavy tools when trimming.

  - Snapshot mode prints a per-tool table with totals.
  - `-json` emits the full export-tools-shaped JSON for downstream tools.
  - `-base=<ref>` materialises the ref in a throwaway git worktree and
    diffs token counts; supports text/markdown/json output.
@tw-garymeehan tw-garymeehan requested a review from a team as a code owner May 5, 2026 15:02
Comment thread cmd/mcp-tokens/main.go Outdated
Swap github.com/pkoukk/tiktoken-go (stale since Sept 2025) for
github.com/localit-io/tiktoken-go v0.2.1 (active fork) per PR review.
Drop-in API; output and tests unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@rafaeljusto rafaeljusto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! Only an optional comment that can be addressed in a future iteration (if required).

Comment thread cmd/mcp-tokens/main.go
@tw-garymeehan tw-garymeehan merged commit 053fac2 into main May 6, 2026
3 checks passed
@tw-garymeehan tw-garymeehan deleted the gary/mcp-tokens-cmd branch May 6, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants