Audited MCP Connector
by andreakcook
README.md

 
**How do you let an AI agent post to the books without trusting it blindly?**
<!-- portfolio:glance -->
**At a glance**
**Stack:** Node · MCP · Claude
**Tests:** 12 passing
**More builds:** [andreakcook.github.io](https://andreakcook.github.io)
<!-- /portfolio:glance -->
## The problem and how we found it
**The pain.** The person who records payments is careful for good reason, and an AI agent that posts to the books has to earn that same trust.
**The discovery question that surfaces it.** "Right now, when a payment gets recorded, who checks it, and how would you know it was right?"
**The answer, in one line.** An MCP server where every write is a preview, a person's yes, a fresh balance check and an audit line naming who approved it.
The longer version:
Any company that records customer payments and invoices has someone careful doing it, and that care is what an AI agent has to earn. An agent can misread an invoice number, act on a stale balance, or repeat a call, and in a financial system each of those becomes a wrong entry. The goal is to keep the speed of an agent while keeping a person's approval, and a record of what happened, on every write.
This repo is a working answer: a Node [MCP](https://modelcontextprotocol.io) server that gives Claude (or any MCP client) read access to an accounting API and a guarded way to write to it. Tested against the QuickBooks Online sandbox API. It is a business finance pattern first, for any company posting payments and invoices, and fits nonprofits the same way.
**Where else this fits:** professional services firms, property managers, franchises, clinics, and member associations, anywhere a person must approve before money is recorded.
## How it works

1. **Preview.** The first tool never writes. It looks up the invoice, applies policy and rejects early (overpayments and already-paid invoices never get a token), and stores the payload under a random token.
2. **Confirm token.** A person reviews the preview, written for a bookkeeper to read, and says yes. The second tool needs that token as proof.
3. **Re-check balance.** The token is burned (single use, 10 minute expiry), the invoice is read again, and if the balance moved, nothing posts.
4. **Write + audit log.** The write names the person who approved the preview (`approved_by`, required). The entry posts, and every posted write and every failed post is appended to the audit log with that name. Refused attempts (expired token, changed balance) are rejected before posting and are not logged.
Every write is two tools, shown here as a sequence:
```
Agent Connector Accounting API
| preview_payment | |
|--------------------->| look up invoice |
| |--------------------------------->|
| | apply policy (reject early) |
| | store payload under a random |
| | token, 10 minute expiry |
| preview + token | |
|<---------------------| |
| (person reviews the preview and says yes) |
| record_payment | |
| (token) | burn token (single use) |
|--------------------->| re-read invoice, compare balance|
| |--------------------------------->|
| | balance moved? stop. |
| | post |
| |--------------------------------->|
| | append to audit log |
| result | |
|<---------------------| |
```
## Design decisions
- **Preview, then confirm.** No tool posts directly. The preview is written for a bookkeeper to read.
- **Single-use token, 10 minute expiry.** The token is burned before the write, even if the write fails.
- **Balance re-check.** If the invoice changed between preview and confirm, nothing posts.
- **Policy rejections happen at preview.** Overpayments and already-paid invoices never get a token.
- **Duplicates are surfaced, not guessed.** Accounting systems often allow repeated invoice numbers. If two invoices match, the tool returns both and stops.
- **Audit log.** Every posted write and every failed post is appended, with `approvedBy`, as a JSON line to `logs/payment-writes.<server-name>.jsonl` (override with `WRITE_LOG_PATH`).
- **Keys never go through chat.** `tools/enter-keys.js` serves a one-time form on localhost, writes the credentials to a git-ignored config file, and exits.
Diagram and components: [docs/architecture.md](docs/architecture.md). Each decision with the option it was weighed against and its cost: [docs/decisions.md](docs/decisions.md).
## Evaluation and guardrails
- **Orchestration:** Not in this repo. The MCP client (Claude or another) decides which tool to call and when; the server only exposes tools over stdio (`src/mcp-server.js`).
- **Tool integration:** 12 read tools and 4 write tools, each with a zod input schema, registered in `src/mcp-server.js` and calling the accounting API through `src/client.js` (OAuth and token refresh in `src/auth.js`).
- **State management:** Pending writes sit in an in-memory map under a random single-use token with a 10 minute expiry (`pendingPayments` in `src/client.js`); pending writes do not survive a restart. On disk, the server keeps only the OAuth token file (`src/auth.js`) and the audit log.
- **Evaluation:** `tests/write-guards.test.js` runs 12 offline checks against an in-memory fake of the API: preview never writes, overpayment and already-paid and unknown invoices get no token, duplicate invoice numbers come back as `ambiguous`, a token posts once and cannot be reused, an unknown token is refused, a changed balance blocks the post, a token expires after 10 minutes, the audit line records the approver, and a write with a blank approver is refused. `tests/sandbox.live.js` is a read-only smoke test against a sandbox company. Not yet: no test of the invoice write happy path, of the failed-post audit line, or of how an agent chooses tools or explains a preview.
- **Guardrails:** Policy checks and duplicate handling at preview, token check and burn, a required `approved_by`, and the payment balance re-check, all in `src/client.js`. No delete, void or edit tools exist (`src/mcp-server.js`).
- **Human-in-the-loop:** The write tools need a token from a preview and the name of the person who approved it (`src/mcp-server.js`, `src/client.js`). Showing the preview to that person is the MCP client's job; the server records the name it is given and does not verify it.
**What gets logged:** One JSON line per posted write or failed post, with time, action, result, `approvedBy` and the entry details, appended to `logs/payment-writes.<server-name>.jsonl` or `WRITE_LOG_PATH` (`logWrite` in `src/client.js`). Refusals before posting are not logged.
## Tools
Read: `get_company_info`, `get_customers`, `get_vendors`, `get_invoices`, `get_bills`, `get_accounts`, `get_payments`, `get_transactions`, `get_invoice_by_number`, `get_open_invoices` (aging buckets), `get_unapplied_payments`, `get_payment_matches`.
Write: `preview_payment` then `record_payment`; `preview_invoice` then `record_invoice` (post only, never emails a customer).
## Run it against a sandbox
You need Node 18+ and a free sandbox company from the Intuit developer portal.
```
npm install && node tools/enter-keys.js ./config-sandbox.env
```
Then start the one-time authorization server (`DOTENV_CONFIG_PATH=./config-sandbox.env node src/index.js`), visit `/auth/start`, and approve. Register the server with your MCP client using `DOTENV_CONFIG_PATH=./config-sandbox.env npm run mcp`. If the access token expires, `node refresh-tokens.js` refreshes the token files (it defaults to the sandbox environment). Intuit requires an HTTPS redirect for the first authorization, so a tunnel is needed once. Sandbox companies can start empty, so seed a customer and an invoice first.
## Tests
`npm test` runs offline tests of the write guards against an in-memory fake of the API (token reuse, expiry, stale balance, overpayment, duplicate invoice numbers, missing approver, and the audit line on a successful post). It needs no credentials. `npm run test:sandbox` runs a read smoke test against a real sandbox company and is not part of CI because it needs live credentials.
## Adapting it to another system
See [docs/Adapting.md](docs/Adapting.md) for the write pattern step by step and the API quirks found along the way.
## What it does not do
- It does not make an agent trustworthy. It makes each write visible, bounded and logged.
- It does not verify who approved. `approved_by` is the name the agent passes after a person says yes; it is recorded, not authenticated. A rollout ties it to the signed-in user.
- It does not enforce who approves. The confirm step relies on the MCP client showing the preview to a person.
- Pending tokens live in memory; restarting the server discards them.
- No delete, void or edit tools. No bill or journal entry writes.
- No document intake or extraction.
- This public copy is adapted from a connector in daily use on a firm's own books, including its write tools. That server stays private and is not in this repo.
- Not tested against a production company by this public copy, and not a substitute for your own review of policy defaults and dependency advisories (`npm audit` reports issues in transitive packages).
---
Built by Andrea K. Cook. All rights reserved, see [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues