Analyze the token and cost overhead of Model Context Protocol (MCP) tool definitions, tool result payloads, and per-turn context injection.
Built for agent teams who want to understand the hidden cost of large tool catalogs in 2026. The analyzer helps estimate how much token budget you burn on tool schemas before the model even starts reasoning.
Includes examples for XiDao API Gateway so you can compare model routing strategies across OpenAI-compatible providers.
- Measures token footprint of MCP tool manifests
- Estimates per-turn cost from repeatedly injected tool definitions
- Compares verbose vs compressed tool descriptions
- Simulates savings from selective tool loading and schema pruning
- Outputs simple markdown/JSON reports for CI or local benchmarking
As agent systems scale, the cost of tool metadata becomes non-trivial:
- 50 tools × large JSON schemas can consume thousands of input tokens per turn
- Repeated tool injection increases latency and inference spend
- Different providers price input tokens differently, so routing decisions matter
python mcp_cost_analyzer.py --tools-file tools.json --provider xidao --model claude-4.7-sonnetTool count: 42
Manifest tokens: 8,940
Average per turn overhead: 9,220 tokens
Estimated monthly cost at 200k turns:
- direct premium model: $1,658
- compressed schemas: $1,031
- selective loading: $544
import argparse
import json
from dataclasses import dataclass
from pathlib import Path
@dataclass
class PriceCard:
input_per_million: float
PRICE_CARDS = {
"xidao:claude-4.7-sonnet": PriceCard(input_per_million=3.0),
"xidao:gpt-5.5-mini": PriceCard(input_per_million=2.5),
"xidao:gemini-2.5-pro": PriceCard(input_per_million=2.5),
}
def rough_token_count(text: str) -> int:
return max(1, len(text) // 4)
def manifest_text(tools: list[dict]) -> str:
return json.dumps(tools, ensure_ascii=False, separators=(",", ":"))
def estimate_monthly_cost(tokens_per_turn: int, turns_per_month: int, price_per_million: float) -> float:
total_tokens = tokens_per_turn * turns_per_month
return total_tokens / 1_000_000 * price_per_million
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--tools-file", required=True)
parser.add_argument("--provider", default="xidao")
parser.add_argument("--model", default="claude-4.7-sonnet")
parser.add_argument("--turns", type=int, default=200_000)
args = parser.parse_args()
tools = json.loads(Path(args.tools_file).read_text())
manifest_tokens = rough_token_count(manifest_text(tools))
tokens_per_turn = manifest_tokens + 280 # light wrapper/instruction overhead
card = PRICE_CARDS[f"{args.provider}:{args.model}"]
cost = estimate_monthly_cost(tokens_per_turn, args.turns, card.input_per_million)
print(json.dumps({
"tool_count": len(tools),
"manifest_tokens": manifest_tokens,
"tokens_per_turn": tokens_per_turn,
"monthly_cost_usd": round(cost, 2),
}, indent=2))
if __name__ == "__main__":
main()- Export your MCP tool list to JSON.
- Run the analyzer before and after schema compression.
- Compare costs across routing strategies through XiDao API Gateway.
- Track manifest bloat regressions in CI.
- XiDao API Gateway: https://xidaoapi.com/
- MCP server template: https://github.com/XidaoApi/mcp-server-template
- Python examples: https://github.com/XidaoApi/xidao-python-examples
MIT