tokenizer-mcp

tokenizer-mcp

An MCP server for counting tokens in text or files using model-specific tokenizers, and reporting file metrics like lines, characters, and size.

Category
Visit Server

README

tokenizer-mcp

tokenizer-mcp is a small MCP server that lets an LLM harness like Claude Code count the exact tokens in files (or any string) trivially easily, plus report basic file metrics — lines, characters, and size in KB. Every tool returns a single integer. For token counting the server receives the file/text and a model name and routes to the appropriate backend; the file-metric tools need no tokenization backend at all.

Installation

uv sync

Running it directly

uv run server.py

API key setup

Counting tokens for Claude requires an Anthropic API key, since the exact count comes from messages.count_tokens. Without a key, the Claude path silently falls back to tiktoken's o200k_base encoding. GPT counts work offline and need no key.

Set the key as an environment variable:

export ANTHROPIC_API_KEY=sk-ant-...

On Windows (PowerShell):

$env:ANTHROPIC_API_KEY = "sk-ant-..."

Or drop a .env file next to server.py and the server will load it on startup:

ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_TOKEN_COUNT_MODEL=claude-opus-4-8

ANTHROPIC_TOKEN_COUNT_MODEL sets the default Claude model when the caller omits one; it defaults to claude-opus-4-8. The endpoint tokenizes under the model you pass, so use a current model — the tokenizer changed at Opus 4.7 (~30% more tokens than older models for the same text).

What it exposes

Six tools, each returning an integer.

Token counting (uses the model routing / backends described below):

  • count_tokens(text, model="") — count tokens in a string.
  • count_tokens_file(file_path, model="") — count tokens in a UTF-8 file at an absolute path.
  • count_tokens_folder(folder_path, model="") — sum of per-file token counts across every text file in a folder, recursively. Dependency/VCS/build directories (.git, node_modules, .venv, __pycache__, dist, ...) are excluded, and binary files are skipped — first by extension, then by a content sniff (NUL bytes / control-character ratio) for unrecognized extensions. Files that aren't UTF-8 are decoded as UTF-16 (when a BOM is present) or cp1252 rather than skipped.

When model is omitted, these use ANTHROPIC_TOKEN_COUNT_MODEL.

File metrics (no tokenization backend; file-only — there are deliberately no raw-string variants):

  • count_lines_file(file_path) — number of lines, using Python str.splitlines() semantics (a trailing newline terminates the last line rather than adding an empty one).
  • count_chars_file(file_path) — number of characters in the decoded UTF-8 text (Unicode code points).
  • count_kb_file(file_path) — file size in kilobytes, ceil(bytes / 1024) with 1 KB = 1024 bytes (the same convention as the Tokenizer app). The size is read from the filesystem, so it works for any file regardless of encoding.

These are useful when a file's token count is only one of several limits — lines, characters, raw size — that decide whether it fits a given budget (e.g. a harness tool-call payload).

How model routing works

The lowercased model is matched against a short set of rules:

What you pass Backend used
A raw tiktoken encoding (o200k_base, cl100k_base, p50k_base, p50k_edit, r50k_base, gpt2) tiktoken.get_encoding
Anything starting with an OpenAI prefix (gpt-5, gpt-4, gpt-3.5, gpt-3, chatgpt, o1, o3, o4, text-davinci, text-embedding, code-davinci, text-curie, text-babbage, text-ada) tiktoken.encoding_for_model
Everything else (Claude models) Anthropic messages.count_tokens

Wiring it into an MCP client

Add an mcpServers entry pointing at this directory:

{
  "mcpServers": {
    "tokenizer": {
      "command": "uv",
      "args": ["--directory", "/absolute/path/to/tokenizer-mcp", "run", "server.py"],
      "env": {
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}

The env block is optional; omit it and Claude counts fall back to o200k_base, as above.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured