compare_models
Compare multiple AI models side by side to evaluate their pricing, context limits, and capabilities. Find the right model for your needs.
Instructions
Compare multiple models side by side.
Args: model_ids: Comma-separated model IDs
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model_ids | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/openrouter_mcp/server.py:198-230 (handler)The @mcp.tool() decorated function that implements the 'compare_models' tool logic. It fetches models, matches given comma-separated model IDs (exact or fuzzy), and renders a side-by-side comparison table with Context, Input $/1M, Output $/1M, and Tools support columns.
@mcp.tool() def compare_models(model_ids: str) -> str: """Compare multiple models side by side. Args: model_ids: Comma-separated model IDs """ models = fetch_models() ids = [x.strip() for x in model_ids.split(",")] selected = [] for mid in ids: for m in models: if m["id"] == mid or mid.lower() in m["id"].lower(): selected.append(m) break if not selected: return "No matching models found." names = [m["id"] for m in selected] max_len = max(len(n) for n in names) if names else 10 def row(label, fn): vals = " | ".join(str(fn(m)).rjust(max_len) for m in selected) return f"| {label} | {vals} |" lines = ["# Model Comparison\n"] lines.append("| Metric | " + " | ".join(n.rjust(max_len) for n in names) + " |") lines.append("|--------|" + "|".join("-" * (max_len + 2) for _ in names) + "|") lines.append(row("Context", lambda m: m.get("context_length", "?"))) lines.append(row("Input $/1M", lambda m: _price_str(float(m.get("pricing",{}).get("prompt",0))))) lines.append(row("Output $/1M", lambda m: _price_str(float(m.get("pricing",{}).get("completion",0))))) lines.append(row("Tools", lambda m: "✅" if "tools" in m.get("supported_parameters",[]) else "❌")) return "\n".join(lines) - src/openrouter_mcp/server.py:198-199 (registration)The @mcp.tool() decorator registers 'compare_models' as an MCP tool on the FastMCP server instance.
@mcp.tool() def compare_models(model_ids: str) -> str: - src/openrouter_mcp/server.py:55-58 (helper)Helper function _price_str converts per-token price to a human-readable string like '$2.50/1M tok' or 'free', used by compare_models when displaying pricing columns.
def _price_str(per_token: float) -> str: if per_token <= 0: return "free" return f"${per_token * 1_000_000:.2f}/1M tok" - src/openrouter_mcp/server.py:34-52 (helper)The fetch_models helper fetches the OpenRouter model list (with caching), called by compare_models to retrieve model data.
def fetch_models(force=False) -> list[dict]: """Fetch model list from OpenRouter with caching.""" now = time.time() if _cache["data"] is not None and (now - _cache["ts"]) < CACHE_TTL and not force: return _cache["data"] headers = {"Accept": "application/json"} if OR_API_KEY: headers["Authorization"] = f"Bearer {OR_API_KEY}" req = Request(OR_MODELS_URL, headers=headers) try: with urlopen(req, timeout=30) as resp: body = json.loads(resp.read()) _cache["data"] = body.get("data", []) _cache["ts"] = now return _cache["data"] except URLError as e: raise RuntimeError(f"Failed to fetch OpenRouter models: {e}") - src/openrouter_mcp/__init__.py:5-7 (registration)The compare_models function is exported from the package via the __init__.py __all__ list and direct import.
from .server import main, fetch_models, list_models, get_model, search_models, compare_models, refresh_cache __all__ = ["main", "fetch_models", "list_models", "get_model", "search_models", "compare_models", "refresh_cache"]