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
访问服务器

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.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选