Skip to content

tokenmeter mcp: read-only MCP server for AI assistants - #24

Merged
coyl merged 2 commits into
mainfrom
feat/tokenmeter-mcp
Oct 24, 2026
Merged

coyl merged 2 commits into
mainfrom
feat/tokenmeter-mcp

Conversation

@coyl

@coyl coyl commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Closes ai-process/docs#133 (epic ai-process/docs#137).

  • tokenmeter mcp on modelcontextprotocol/go-sdk v1.8.0: stdio by default, --listen serves streamable HTTP at /mcp with a required bearer token (TOKENMETER_MCP_TOKEN / --mcp-token-file), DNS-rebinding and cross-origin protection; warns on non-loopback.
  • Tools, all readOnlyHint: usage_summary, top_spenders, cost_per_action (per action: cost and tokens per request, latency, failed share and cost), usage_by_meta, health. Amounts as exact cost_usd (no float) and cost_nanousd; UTC half-open windows; capped windows fetched in parts via pkg/client; truncated results carry a note that they are not totals; an empty window says "no traffic".
  • At startup the token must read, and one that can also write is refused: an empty RecordUsageEvents batch fails with PERMISSION_DENIED without the write scope and INVALID_ARGUMENT with it, so nothing is recorded. No token is allowed with a warning (service without auth).
  • Prompt usage_report. Health probing shared with tokenmeter health; one gRPC connection and one HTTP client for the server's life.
  • Tests: SDK-client protocol tests against the real servers on Postgres (tool list, sums equal the API's, shares, failed share, by-meta, bad input as tool error, prompt; write and read+write tokens refused); HTTP transport with and without the token. scripts/cli-e2e.sh drives the built binary over stdio and compares usage_summary with GET /v1/usage/summary, on Postgres alone and with ClickHouse.
  • README section with client configs, CHANGELOG; MCP tools marked Experimental until 1.2 with the CLI.

coyl added 2 commits October 24, 2026 02:58
stdio by default, streamable HTTP with a bearer token on --listen.
usage_summary, top_spenders, cost_per_action, usage_by_meta and health,
with exact USD and nano-USD, UTC windows and truncated results marked;
a usage_report prompt. A token that can write events is refused, found
by an empty batch that fails after the scope check. The end-to-end job
checks the stdio server against GET /v1/usage/summary.
The HTTP server drains running calls before the connection closes, and a
signal ends a stdio server cleanly. The bearer scheme is matched without
case; the MCP token is checked before any call to the service. A token
that can write exits 4. Ranking, sorting and totals are shared with the
CLI, top_spenders keeps all rows with limit 0 and formats only those it
returns, and the server takes its client as a parameter.
@coyl
coyl merged commit e25c6bb into main Oct 24, 2026
6 checks passed
@coyl
coyl deleted the feat/tokenmeter-mcp branch October 24, 2026 01:12
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.

1 participant