FreightLens MCP
by fanuman
README.md
# FreightLens MCP
An [MCP](https://modelcontextprotocol.io) server that lets AI apps such as Claude Desktop read freight documents
and check invoices against quotes in [FreightLens](https://github.com/fanuman/freight-doc-intelligence).
> **Principle: the LLM reads, software decides.** Every number the model sees was computed by FreightLens's code,
> including the formatted amounts (`USD 56.00`). The model only explains results; it never does arithmetic,
> and it cannot approve or reject documents.
**Status:** works locally over stdio with Claude Desktop and the MCP Inspector. Remote deployment and OAuth
are next (see [Roadmap](#roadmap)). All data is synthetic, from FreightLens's own generator.
## What you can ask
With the server connected, a user can ask Claude in plain English:
> "Do any of my FreightLens invoices have overcharges?"
Claude lists the documents, reads each invoice's stored comparison with its quote, and answers from the computed
results. In a test on the synthetic data it found, for example:
| Invoice | Charge | Quoted | Invoiced | Over |
|---|---|---|---|---|
| INV-791511 | Origin Terminal Handling | USD 230.00 | USD 286.00 | USD 56.00 |
## How it fits together
```mermaid
flowchart LR
U[User] --> H[Claude Desktop<br/>MCP host]
H -- "MCP over stdio" --> S[freightlens-mcp<br/>this server]
S -- "HTTPS + login token" --> A[FreightLens API]
A --> D[(Postgres<br/>row-level security)]
W[FreightLens web app] --> A
```
The server is **one more client of the FreightLens API**, like the web app. It never touches the database, so
login, tenant isolation and validation all stay in FreightLens.
## What the server offers
| Kind | Name | What it does | Behaviour |
|---|---|---|---|
| Tool | `list_documents` | Newest quotes and invoices with type and status | Read-only |
| Tool | `get_document` | What FreightLens read from one document: route, charges, total, issues, comparison | Read-only |
| Tool | `reconcile_invoice` | Compares an invoice with its quote and lists every discrepancy | Writes; safe to repeat (idempotent) |
| Resource | `freightlens://documents/{id}` | One document as JSON, for a user to attach to a chat | Read-only |
| Prompt | `explain_discrepancies` | "Explain this invoice's differences for the person who pays it" | Picked by the user |
## Design decisions
- **Adapter over the REST API, not a second backend.** The server reuses FreightLens's authentication and
row-level security instead of holding database credentials.
- **Results shaped for a model** (`views.py`). Only the useful fields are returned; money comes as integer cents
*and* a `display` string formatted by code; each reconcile outcome comes with a fixed sentence explaining it.
- **Human decisions stay human.** Approving, correcting or rejecting a document under review is not exposed:
FreightLens requires a person's note to approve over an error, and that should not be a model's choice.
- **Errors the model can act on.** Known failures become MCP tool errors with a next step
("Document not found. Call list_documents to see valid ids."). Unexpected failures show the model only a
generic message; the details go to the server log (stderr), never into the conversation.
- **Input checked twice.** The tool schema limits values (for example `limit` is 1 to 100), and the client accepts
only UUIDs as document ids, so model input can never change the API path.
- **Tool annotations** mark which tools are read-only and which are idempotent, so a host can decide when to ask
the user for approval.
## Quick start
Requirements: Python 3.11+, and FreightLens running locally (see its
[Quick start](https://github.com/fanuman/freight-doc-intelligence#quick-start)) with an account and a few
processed documents.
```bash
git clone https://github.com/fanuman/freightlens-mcp.git
cd freightlens-mcp
python3 -m venv venv && source venv/bin/activate
pip install -e ".[dev]"
cp .env.example .env # then fill in FREIGHTLENS_EMAIL and FREIGHTLENS_PASSWORD
```
### Try it in the MCP Inspector
```bash
npx @modelcontextprotocol/inspector "$PWD/venv/bin/freightlens-mcp"
```
### Connect it to Claude Desktop
Settings → Desktop app → Extensions → Local MCP servers → Edit config, then add:
```json
{
"mcpServers": {
"freightlens": {
"command": "/absolute/path/to/freightlens-mcp/venv/bin/freightlens-mcp"
}
}
}
```
Restart Claude Desktop. The server finds its `.env` on its own, so no other settings are needed.
## Development
```bash
pytest # 28 tests: the API client against a fake API, the server through a real MCP client
ruff check . && ruff format --check .
mypy # strict
```
The server tests connect a real MCP `Client` to the server in-process and send real `tools/list`,
`tools/call`, `resources/read` and `prompts/get` requests. FreightLens is faked with `httpx.MockTransport`,
so the tests need no API, database or network.
```
src/freightlens_mcp/
client.py HTTP client for the FreightLens API (login, token refresh, clear errors)
views.py what the tools return: small typed models, money formatted by code
server.py the MCP tools, resource template and prompt
tests/
test_client.py
test_server.py
```
## Roadmap
- [x] Local server over stdio: three tools, a resource template, a prompt, 28 tests
- [ ] Remote server over Streamable HTTP, deployed to AWS
- [ ] OAuth 2.1 instead of a password in `.env`, with each caller mapped to their own FreightLens company
- [ ] Threat model (tool poisoning, confused deputy, token passthrough)
- [ ] Tool-selection evaluation: does the model pick the right tool with the right arguments, and does it ever
do arithmetic itself?