# MCP server

Read keys, the endpoint, the read-only tools and a JSON-RPC example.

AI Spendbook documentation, How it works. Web page: https://www.aispendbook.com/docs/mcp-server

The MCP server lets your own assistant or agent read the same ledger the app shows. It speaks the Model Context Protocol over HTTP at `POST https://www.aispendbook.com/api/v1/mcp` and offers read-only tools. It is part of the Business [plan](https://www.aispendbook.com/docs/plans-and-billing).

## Read keys

- An admin creates a read key in **Settings**, under Read keys. The key starts with `ark_` and is shown once. Only its hash is stored.
- A key carries scopes and can have a last day. The MCP endpoint and the daily ledger endpoint (`GET https://www.aispendbook.com/api/v1/ledger/daily`) need the `mcp` scope. An ingestion key does not work there, and a read key can't send events. Scopes and expiry are on [SSO, MFA, SCIM and keys](https://www.aispendbook.com/docs/sso-mfa-scim).
- The workspace is fixed by the key. No tool takes a workspace argument. A firm-wide key names the client in `X-Organization-Id`.
- Revoking a key stops it at once. An expired key is refused. Creating and revoking keys is recorded in the audit log.
- A read key belongs to the workspace, not to a member. It carries no [member limits](https://www.aispendbook.com/docs/permissions) and answers for the whole workspace, revenue included, unless it is marked “No revenue”. It never sees employee data. The keys a member created are revoked when that member is removed or limited.

## Protocol

- Send one JSON-RPC 2.0 message per request with `Authorization: Bearer ark_…`. Batches are not supported.
- Methods: `initialize`, `ping`, `tools/list` and `tools/call`. A notification is answered with 202 and no body.
- The server is stateless. There are no sessions and no server-sent streams, and each request gets one JSON response.
- The protocol version reported by `initialize` is `2025-06-18`.
- Requests are rate limited per read key and per client IP address.

## Tools

| Tool | Returns |
| --- | --- |
| get_spending_summary | Total AI spend for a date range, with the billed and estimated basis. |
| get_usage_by_dimension | Spend, share, requests and cost per request broken down by one dimension. |
| get_customer_unit_economics | Per-customer AI cost per request and per active user, and the revenue share when revenue data exists. |
| forecast_spending | The baseline monthly projection, or the month-end figure for this month. |
| get_budget_status | Budgets with spend to date, month-end forecast, percent used and overrun risk. |
| get_monthly_actuals | Actual spend per calendar month, oldest first, with the basis mix, requests, tokens and active users. The current month is flagged as partial. |
| get_close_status | Whether a month is ready to close: status, spend, billed against accrual, days without a bill, open variances and what blocks closing. |
| get_changes_since_close | What changed in a closed month since it was locked: drift, provider corrections and customers whose allocated spend changed. |
| get_forecast_accuracy | Recorded forecasts scored against actual spend, and a backtest of the method, by months ahead. |
| get_decision_outcomes | Recommendation decisions with projected and measured savings, and frozen scenarios against actual spend. |
| get_credit_runway | Credit and prepaid balances, projected exhaustion dates, the amount projected to expire unused and the monthly cash step-up. |
| get_cost_views | List, effective and cash cost by provider and month, with credits applied, grant balances and commitments. |
| get_accounting_split | One month's AI spend by accounting class, with account codes and spend by provider. |
| get_customer_margin | Allocated AI spend, revenue and margin per customer, and where total spend went. |
| get_outcome_unit_costs | Cost per accepted outcome for one outcome definition, with token and non-token cost by kind. |
| get_change_reviews | Reviews of proposed model changes: cost delta, quality delta per metric, evidence status and the decision. |
| evaluate_deal | A proposed price against the AI cost of the usage it covers, and the saved deals. It computes only and saves nothing. |
| get_correction_impact | Provider corrections in a date range, and which customers' allocated spend the billed changes touch. |
| get_subscription_summary | Employee AI tool seats for one month: fixed subscription cost by tool and department, inactive seats, renewals and overlaps. |
| get_infrastructure_economics | Observed and modelled cost per deployment, cost per request and per million tokens, idle share, and the same tokens priced on a hosted model. |

These are the AI Analyst's read-only tools, run through the same code path, so the figures match the rest of the product. `tools/list` returns the current set with each tool's input schema. Each result has a text summary in `content` and the data in `structuredContent`.

## Example

```bash
curl -sS -X POST "https://www.aispendbook.com/api/v1/mcp" \
  -H "Authorization: Bearer $SPENDBOOK_READ_KEY" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
```

```bash
curl -sS -X POST "https://www.aispendbook.com/api/v1/mcp" \
  -H "Authorization: Bearer $SPENDBOOK_READ_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/call",
    "params": {"name": "get_monthly_actuals", "arguments": {"months": 3}}
  }'
```

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [{"type": "text", "text": "2026-09: ..."}],
    "structuredContent": {"months": [...]},
    "isError": false
  }
}
```

A tool that fails returns a result with `isError` set to true and the reason as text. An unknown tool or method returns a JSON-RPC error.
