tbcontracts-mcp

tbcontracts-mcp

MCP server for priority-weighted token budget management in multi-agent LLM systems, enabling dynamic reallocation of unused tokens between agents based on priority and current spend.

Category
访问服务器

README

tbcontracts-mcp

An MCP (Model Context Protocol) server that exposes token-budget-contracts as tools, so any MCP-aware client (Claude Code, Claude Desktop, Cursor, etc.) can manage token budgets across a multi-agent LLM system.

What this actually does

Multi-agent orchestrators (a planner spawning a researcher, a writer, a critic, ...) burn tokens unevenly. A high-priority agent can starve mid-task while a low-priority agent sits on unused budget. This server's one job is answering, in real time: given these agents' priorities and current spend, how should the remaining budget move right now?

It does this by wrapping token-budget-contracts' priority-weighted Reallocator: spare budget flows from idle or lower-priority agents to whichever agent is actually starved, never below a donor's protected minimum reserve, and never "uphill" from a more important agent to a less important one.

This is not a cost-tracking dashboard or a network gateway. See Honest scope below.

Install

pip install tbcontracts-mcp

This pulls in token-budget-contracts>=0.3.0 and opentelemetry-api as dependencies. Requires Python 3.10+ (see Why not Python 3.9).

Tools

Tool What it does
register_agent Register an agent with a priority weight and initial token budget.
record_spend Record raw input/output tokens an agent consumed for a task. Automatically triggers priority-weighted reallocation if the agent goes over budget.
get_remaining_budget Look up one agent's current remaining budget, priority, and reserve.
request_reallocation The core tool. Given an agent that needs more tokens right now, runs the real priority-weighted borrowing logic and returns a concrete plan: which agents gave up how much, which agent received it, and why each donor was eligible.
get_budget_snapshot Full current state of every registered agent, as structured JSON.

Every tool takes a strict, typed JSON input schema and returns structured JSON ({"success": true/false, ...}) - never free text - so a calling agent or orchestrator can parse the result programmatically.

Error handling

Unknown agent IDs, invalid input, and budget-exceeded conditions all come back as a structured error, never a stack trace:

{
  "success": false,
  "error": {
    "type": "unknown_agent",
    "message": "Agent 'ghost' was never registered. Call register_agent first.",
    "agent_id": "ghost",
    "known_agents": ["critic", "researcher"]
  }
}

Error type is one of unknown_agent, invalid_input, budget_exceeded, tbcontracts_error, or internal_error. A bad tool call never crashes the server process - the MCP client keeps working.

Quick start (as a library, for testing)

from tbcontracts_mcp import server

server.register_agent(agent_id="researcher", priority=3, max_tokens=4000)
server.register_agent(agent_id="critic", priority=1, max_tokens=2000)

server.record_spend(agent_id="researcher", input_tokens=3800, output_tokens=100)
# -> over budget by 400 tokens; automatically borrows from critic

plan = server.request_reallocation(agent_id="researcher", tokens_needed=1000)
print(plan)

Normally you won't call these functions directly - an MCP client calls them as tools over stdio. See the client configs below.

Using it from an MCP client

The server runs over stdio and needs no network setup - just point your client at the tbcontracts-mcp command.

Claude Code

claude mcp add tbcontracts -- tbcontracts-mcp

Or add it directly to .mcp.json:

{
  "mcpServers": {
    "tbcontracts": {
      "command": "tbcontracts-mcp"
    }
  }
}

Claude Desktop

Add to claude_desktop_config.json:

{
  "mcpServers": {
    "tbcontracts": {
      "command": "tbcontracts-mcp"
    }
  }
}

Cursor

Add to .cursor/mcp.json:

{
  "mcpServers": {
    "tbcontracts": {
      "command": "tbcontracts-mcp"
    }
  }
}

Any of these can equally run it via python -m tbcontracts_mcp instead of the console script, e.g. if you've installed it into a specific venv:

{
  "mcpServers": {
    "tbcontracts": {
      "command": "/path/to/venv/bin/python",
      "args": ["-m", "tbcontracts_mcp"]
    }
  }
}

Observability

Spend recorded through record_spend is emitted two ways, both additive to your existing observability stack rather than replacing it:

  1. The underlying library's own OTel spans. token-budget-contracts already instruments every governance decision (registration, spend, reallocation) as an OpenTelemetry span when telemetry is enabled. This server wires record_spend straight through that existing hook rather than building a parallel tracer - set TBCONTRACTS_MCP_OTEL=1 in the server's environment to turn it on (uses the global OTel tracer provider; configure your exporter the usual OTel way).
  2. A gen_ai.client.token.usage counter, following the emerging OpenTelemetry gen_ai.* semantic conventions, emitted via opentelemetry-api for every record_spend call. This tracks raw input/output token counts per agent, not pre-computed dollar cost - pricing tables change constantly and a token counter shouldn't be coupled to one. Attach whatever OTel MeterProvider/exporter you like in the process that launches this server; if none is configured, this is a no-op.

Honest scope

tbcontracts-mcp is the allocation-decision layer for one thing: priority-weighted budget reallocation between agents you've already told it about. It is meant to be composed with other tools, not to replace them. Specifically, it does not:

  • do cross-provider cost tracking. It emits raw token counters, not dollar costs, and has no notion of a pricing table for OpenAI, Anthropic, or anyone else.
  • do network-level rate limiting or gateway routing. It doesn't sit in the request path between your app and an LLM provider, and it can't throttle or route calls. Tools like Bifrost, MuleSoft, or Solo.io's gateways already do that well - use one of those alongside this.
  • replace an observability platform. It emits spans/counters you can send to Grafana, Datadog, Honeycomb, etc., but it isn't a dashboard, storage backend, or alerting system itself.

What it does do: given the agents you've registered and their current spend, decide - and actually execute - how unused budget should move between them right now, based on priority.

Why not Python 3.9?

token-budget-contracts itself supports Python 3.9+, but the official mcp Python SDK this server depends on has never supported Python 3.9 (it requires 3.10+ on every released version). This package therefore requires Python 3.10+, even though the library it wraps does not.

Development

git clone https://github.com/swaranshu-borgaonkar/tbcontracts-mcp
cd tbcontracts-mcp
pip install -e ".[dev]"
pytest -v

License

MIT for the code in this package. token-budget-contracts, which this server wraps, implements the governance model described in a pending U.S. provisional patent application (see its own README for details). If you plan to use this commercially at scale, consult your own counsel.

推荐服务器

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

官方
精选