MCP令牌分析工具

MCP令牌分析工具是一个CLI工具,用于连接MCP服务器,检索工具定义和模式,并分析每个工具的令牌使用情况,帮助开发者优化工具描述和模式以提高上下文使用效率。

  • by tjhop
  • Go
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 tjhop/mcp-token-analyzer

使用说明

MCP令牌分析器

关于

mcp-token-analyzer 是一个CLI工具,旨在连接到模型上下文协议(MCP)服务器,检索其工具定义和模式,并分析每个工具的令牌使用情况。 此工具可帮助开发人员优化他们的工具描述和模式,以便在LLM中高效地使用上下文。 它提供了类似的数据 /context claude代码中的slash命令,但它与claude无关,并且专门用于分析MCP服务器。

实例分析 普罗米修斯mcp服务器:

~/go/src/github.com/tjhop/mcp-token-analyzer (main [ ]) -> ./mcp-token-analyzer --mcp.transport http --mcp.url "http://localhost:8080/mcp"
Tool Analysis (sorted by total tokens)
┌───────────────────────┬───────────────────┬──────┬──────┬────────┬────────┬────────┬───────┐
│        Server         │       Tool        │ Name │ Desc │ Schema │ Output │ Annot. │ Total │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ label_values      │ 2    │ 16   │ 159    │ 0      │ 11     │ 188   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ range_query       │ 2    │ 8    │ 156    │ 0      │ 11     │ 177   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ label_names       │ 2    │ 18   │ 138    │ 0      │ 11     │ 169   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ series            │ 1    │ 6    │ 146    │ 0      │ 11     │ 164   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ exemplar_query    │ 4    │ 9    │ 132    │ 0      │ 13     │ 158   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ delete_series     │ 2    │ 11   │ 110    │ 0      │ 12     │ 135   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ query             │ 1    │ 8    │ 91     │ 0      │ 11     │ 111   │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ targets_metadata  │ 2    │ 10   │ 68     │ 0      │ 11     │ 91    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ docs_search       │ 2    │ 14   │ 49     │ 0      │ 11     │ 76    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ metric_metadata   │ 2    │ 11   │ 48     │ 0      │ 11     │ 72    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ snapshot          │ 1    │ 29   │ 30     │ 0      │ 12     │ 72    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ docs_read         │ 2    │ 15   │ 34     │ 0      │ 11     │ 62    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ ready             │ 1    │ 23   │ 10     │ 0      │ 12     │ 46    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ reload            │ 1    │ 19   │ 10     │ 0      │ 12     │ 42    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ clean_tombstones  │ 4    │ 14   │ 10     │ 0      │ 13     │ 41    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ quit              │ 1    │ 15   │ 10     │ 0      │ 11     │ 37    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ tsdb_stats        │ 3    │ 10   │ 10     │ 0      │ 12     │ 35    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ healthy           │ 1    │ 12   │ 10     │ 0      │ 11     │ 34    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ list_rules        │ 2    │ 10   │ 10     │ 0      │ 11     │ 33    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ alertmanagers     │ 3    │ 7    │ 10     │ 0      │ 12     │ 32    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ wal_replay_status │ 4    │ 5    │ 10     │ 0      │ 13     │ 32    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ docs_list         │ 2    │ 7    │ 10     │ 0      │ 11     │ 30    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ list_targets      │ 2    │ 6    │ 10     │ 0      │ 11     │ 29    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ list_alerts       │ 3    │ 4    │ 10     │ 0      │ 11     │ 28    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ runtime_info      │ 2    │ 4    │ 10     │ 0      │ 11     │ 27    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ build_info        │ 2    │ 4    │ 10     │ 0      │ 11     │ 27    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ flags             │ 1    │ 3    │ 10     │ 0      │ 11     │ 25    │
├───────────────────────┼───────────────────┼──────┼──────┼────────┼────────┼────────┼───────┤
│ prometheus-mcp-server │ config            │ 1    │ 3    │ 10     │ 0      │ 10     │ 24    │
└───────────────────────┴───────────────────┴──────┴──────┴────────┴────────┴────────┴───────┘

Resource Analysis (sorted by total tokens)
┌───────────────────────┬─────────────────────────────────────────────────┬──────┬─────┬──────┬───────┐
│        Server         │                    Resource                     │ Name │ URI │ Desc │ Total │
├───────────────────────┼─────────────────────────────────────────────────┼──────┼─────┼──────┼───────┤
│ prometheus-mcp-server │ Official Prometheus Documentation               │ 3    │ 8   │ 15   │ 26    │
├───────────────────────┼─────────────────────────────────────────────────┼──────┼─────┼──────┼───────┤
│ prometheus-mcp-server │ List of Official Prometheus Documentation Files │ 6    │ 4   │ 15   │ 25    │
├───────────────────────┼─────────────────────────────────────────────────┼──────┼─────┼──────┼───────┤
│ prometheus-mcp-server │ TSDB Stats                                      │ 3    │ 6   │ 9    │ 18    │
├───────────────────────┼─────────────────────────────────────────────────┼──────┼─────┼──────┼───────┤
│ prometheus-mcp-server │ Targets                                         │ 1    │ 4   │ 10   │ 15    │
├───────────────────────┼─────────────────────────────────────────────────┼──────┼─────┼──────┼───────┤
│ prometheus-mcp-server │ List metrics                                    │ 2    │ 5   │ 3    │ 10    │
└───────────────────────┴─────────────────────────────────────────────────┴──────┴─────┴──────┴───────┘

Token Analysis Summary
┌───────────────────────┬──────────────┬───────┬─────────┬───────────┬──────────────┐
│      MCP Server       │ Instructions │ Tools │ Prompts │ Resources │ Total Tokens │
├───────────────────────┼──────────────┼───────┼─────────┼───────────┼──────────────┤
│ prometheus-mcp-server │ 4,527        │ 1,997 │ 0       │ 94        │ 6,618        │
├───────────────────────┼──────────────┼───────┼─────────┼───────────┼──────────────┤
│         TOTAL         │    4,527     │ 1,997 │    0    │    94     │    6,618     │
└───────────────────────┴──────────────┴───────┴─────────┴───────────┴──────────────┘

特性

  • 运输支持
    • stdio:执行本地二进制文件
    • http:连接到流式HTTP端点
    • streamable-http:别名 http,被一些MCP客户端使用(Continue、Roo Code、Anthropic MCP Registry)
  • 配置文件支持
    • 从以下位置加载服务器配置 mcp.json 文件
    • 与Claude Desktop、Cursor、VS Code和Continue格式兼容
    • 并行分析多个MCP服务器
    • 通过以下方式筛选到单个服务器 --server
  • MCP综合分析
    • 服务器指令:服务器级指令文本的令牌计数
    • 工具:名称、描述和输入模式的令牌细分
    • 提示:名称、描述和参数的令牌细分
    • 资源和模板:名称、URI和描述的令牌细分
  • 令牌计数
    • 用途 tiktoken 通过 应用 (默认为 cl100k_base /GPT-4)
    • 通过以下方式配置标记器模型 --tokenizer.model
    • 看 支持的令牌化器模型 关于可用的型号和编码
  • 报告
    • 显示每台服务器令牌使用情况的汇总表
    • 显示每个组件令牌细分的详细表格(始终显示单个服务器,通过选择加入 --detail 对于多服务器)
    • 通过以下方式计算上下文窗口百分比 --limit

安装与使用

走

在工作环境中,该工具可以按如下方式安装:

go install github.com/tjhop/mcp-token-analyzer@latest
/path/to/mcp-token-analyzer <flags>

系统包

从下载适合您系统的版本 发布 页面。

# install system package (example assuming Debian based)
apt install /path/to/package

备注:虽然包是为几个系统构建的,但目前没有计划尝试将包提交到上游包存储库。

从源头构建

make build
./mcp-token-analyzer --help

安全与认证

OAuth 2.0和自定义TLS配置字段已从配置文件中解析,但尚未实现。当存在时,会发出警告。看 路线图 用于计划身份验证支持。

发展

Devbox+Direnv开发环境

如果你使用 Devbox 和 Direnv,然后简单地输入repo的目录 应该设置所需的软件。

建筑

包含的Makefile有几个目标来帮助开发:

~/go/src/github.com/tjhop/mcp-token-analyzer (main [ ]) -> make

Usage:
  make <target>

Targets:
  help                           print this help message
  tidy                           tidy modules
  fmt                            apply go code style formatter
  lint                           run linters
  binary                         build a binary
  build                          alias for `binary`
  build-all                      test release process with goreleaser, does not publish/upload
  test                           run tests

配置文件

该工具可以从JSON文件加载MCP服务器配置,支持Claude Desktop、Cursor、VS Code和Continue使用的格式。

支持格式

克劳德桌面/光标格式 (mcpServers 按键):

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-server-filesystem", "/tmp"],
      "env": { "DEBUG": "true" }
    },
    "remote-api": {
      "url": "http://localhost:3000/mcp",
      "headers": { "Authorization": "Bearer token123" }
    }
  }
}

VS代码格式 (servers 按键):

{
  "servers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-server-postgres"]
    }
  }
}

继续格式化 (mcpServers 明确 streamable-http 类型):

{
  "mcpServers": {
    "remote-api": {
      "type": "streamable-http",
      "url": "http://localhost:3000/mcp"
    }
  }
}

两者 mcpServers 和 servers 密钥可以存在于同一文件中; mcpServers 对于重复的服务器名称优先。

服务器配置选项

字段 描述
type 传输类型(可选,从中推断 command 或 url 如果省略)
command 要执行的命令(stdio传输)
args 命令参数(stdio传输)
url 服务器URL(http传输,必须是http://或https://)
env 过程的环境变量
envFile .env文件的路径(相对于配置文件位置)
headers 请求的HTTP标头(HTTP传输)

运输类型(stdio 或 http)根据以下内容的存在自动推断 command 或 url 领域。这 streamable-http 类型被接受并规范化为 http.

多服务器输出

在分析多个服务器时,该工具会显示一个汇总表,显示所有服务器的令牌使用情况。使用 --detail 额外显示每个组件的详细信息表(工具、提示、资源),其中包含按总令牌排序的所有服务器的条目。每一行都包含服务器名称,以便可以追溯到组件的来源。

分析单个服务器时(通过CLI标志或通过 --server),详细信息表始终自动显示。

支持的令牌化器模型

这 --tokenizer.model flag接受以下对象识别的任何模型名称 应用模型名称决定了使用哪种编码(标记化方案)进行计数。默认值为 gpt-4 (cl100k_base).

编码和模型

编码 型号
o200k_base gpt-4.5, gpt-4.1, gpt-4o (以及诸如 gpt-4o-2024-05-13)
cl100k_base gpt-4, gpt-3.5-turbo (以及注明日期的变体), text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large
p50k_base text-davinci-003, text-davinci-002, code-davinci-002, code-cushman-002
r50k_base text-davinci-001, text-curie-001, text-babbage-001, text-ada-001, davinci, curie, babbage, ada

您还可以直接传递编码名称(例如。, --tokenizer.model o200k_base)如果您更喜欢指定编码而不是模型名称。

局限性

  • 不支持拟人克劳德模型 通过tiktoken go。克劳德模型没有官方的标记器。在分析与Claude一起使用的MCP服务器时,令牌计数是近似值。使用 o200k_base 或 cl100k_base 提供了一个合理的估计,但与Claude的实际标记化不符。
  • tiktoken-go的模型列表反映了OpenAI的公共模型。在库更新之前,可能无法识别较新的或实验性的模型。

命令行标志

usage: mcp-token-analyzer [<flags>]

Flags:
  -h, --[no-]help                Show context-sensitive help (also try
                                 --help-long and --help-man).
  -t, --mcp.transport=stdio      Transport to use (stdio, http, streamable-http)
  -c, --mcp.command=MCP.COMMAND  Command to run (for stdio transport)
  -u, --mcp.url=MCP.URL          URL to connect to (for http transport)
  -m, --tokenizer.model="gpt-4"  Tokenizer model to use (e.g. gpt-4,
                                 gpt-3.5-turbo)
  -f, --config=CONFIG            Path to mcp.json config file
  -s, --server=SERVER            Analyze only this named server from config
      --[no-]detail              Show detailed per-server tables
      --limit=LIMIT              Context window limit for percentage calculation