Skip to content

XidaoApi/mcp-cost-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

MCP Cost Analyzer

License Python MCP

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.

What it does

  • 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

Why this matters

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

Quick start

python mcp_cost_analyzer.py --tools-file tools.json --provider xidao --model claude-4.7-sonnet

Example output

Tool 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

Script

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()

Suggested workflow

  1. Export your MCP tool list to JSON.
  2. Run the analyzer before and after schema compression.
  3. Compare costs across routing strategies through XiDao API Gateway.
  4. Track manifest bloat regressions in CI.

Related links

License

MIT

About

Analyze MCP tool schema overhead, token usage, and per-turn cost for AI agents. Estimate context bloat, tool-calling overhead, and routing savings for OpenAI-compatible backends.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages