Skip to main content
Glama
README.md
# mcp-brazil-erp

An [MCP](https://modelcontextprotocol.io) server that gives an AI assistant the small Brazilian "plumbing" checks that ERP work runs into every day: is this CNPJ/CPF valid, what does this boleto say, which company issued this NF-e, where is this CEP. It is written in TypeScript on the official `@modelcontextprotocol/sdk` and uses the stdio transport.

Four of the six tools run fully offline and are pure functions. The other two make read-only calls to the public [BrasilAPI](https://brasilapi.com.br).

## Tools

| Tool | Input | Output | Network |
|---|---|---|---|
| `validate_cnpj` | `cnpj` | valid, normalized, formatted, reason. Also accepts the new **alphanumeric CNPJ** (IN RFB 2.229/2024) | no |
| `validate_cpf` | `cpf` | valid, normalized, formatted, reason | no |
| `lookup_cnpj` | `cnpj` | legal name, trade name, status, opening date, main activity (CNAE), size, address. The CNPJ is validated locally first | BrasilAPI `/cnpj/v1` |
| `parse_boleto` | `line`, optional `reference_date` | 47 digits (bank): bank code, currency, due date, amount, per-field and general check digits. 48 digits starting with 8 (utilities/taxes): segment, effective/reference value, block check digits | no |
| `parse_nfe_key` | `key` (44 positions) | UF, year-month, issuer CNPJ, model (NF-e/NFC-e/CT-e/MDF-e), series, number, emission type, random code, check digit | no |
| `lookup_cep` | `cep` | street, district, city, UF | BrasilAPI `/cep/v2` |

**Boleto due dates after the 2025 reset.** The due-date factor counts days from 1997-10-07. It reached 9999 on 2025-02-21 and restarted at 1000 on 2025-02-22, so every factor now maps to two dates about 27 years apart. `parse_boleto` takes the candidate closest to `reference_date` (default: today). The tests cover both cycles.

## Install

```bash
npm install
npm run build
npm test
```

**Claude Code:**

```bash
claude mcp add brazil-erp -- node /absolute/path/to/mcp-brazil-erp/dist/src/index.js
```

**Claude Desktop** (`claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "brazil-erp": {
      "command": "node",
      "args": ["/absolute/path/to/mcp-brazil-erp/dist/src/index.js"]
    }
  }
}
```

## Safety notes

- **Read-only.** No tool writes anything. All of them are annotated `readOnlyHint: true` and `destructiveHint: false`, and only the two lookups are `openWorldHint: true`.
- **No credentials.** BrasilAPI is public, so there are no keys or tokens to configure or leak.
- **Validated input.** Every tool's input is described with zod schemas and length limits. `lookup_cnpj` rejects an invalid CNPJ before any network call.
- **Bounded network calls.** Lookups have a timeout (15 s by default). Failures come back as tool errors with a `kind` (`not_found`, `invalid_input`, `rate_limited`, `timeout`, `upstream`, `network`) and BrasilAPI's own message, so the model can explain the failure instead of retrying blindly.
- **Mind the data.** `lookup_cnpj` returns public company-registry data. Validating a CPF only checks the arithmetic of the number. It does not look up a person.
- stdout carries the MCP protocol, so the server logs to stderr only.

## Tests

The tests cover check-digit validation (with valid and invalid examples), CNPJ/CPF, NF-e keys, both boleto layouts, both due-date cycles, typo detection, and the MCP server end to end through the SDK's in-memory transport. Network tools are tested against a fake `fetch`, so the test suite never touches the network.

The test values are either textbook test numbers (CNPJ `11.222.333/0001-81`, CPF `111.444.777-35`, and Receita's alphanumeric example `12.ABC.345/01DE-35`) or synthetic numbers whose check digits are computed in the test. No real person's CPF is used.

## Trade-offs

- **Two date candidates instead of one rule.** Picking the due date closest to a reference date handles the 2025 factor reset without a hard cutover. It is wrong only for a boleto due more than about 13 years from the reference date, which is not a realistic case.
- **No due date for arrecadação.** Utility and tax slips have no standard due-date field (when one exists, it is inside the free field and its layout varies by agreement), so the tool returns `null` rather than guessing.
- **The alphanumeric CNPJ in NF-e keys** is accepted using the same ASCII-48 rule as the CNPJ check digits. Confirm this against the latest technical note before relying on it for a SEFAZ submission.
- **Single upstream provider.** BrasilAPI's CEP endpoint aggregates other providers and is sometimes unavailable. A production version would add a fallback provider and a cache.
- **Compact output.** `lookup_cnpj` returns a stable subset of the BrasilAPI payload instead of the full JSON. That keeps the model's context small, but some fields are dropped.

## AI-assisted development

<!-- TODO(Leonardo): confirm or edit this wording before publishing. -->
This server was written with Claude Code (Anthropic) as a coding assistant. I chose the tools and the safety constraints from what my ERP work needs, and I reviewed and verified the output with the test suite and manual runs.

## License

MIT, see [LICENSE](LICENSE).

TDQS

A3.8/5.0

Scored across 6 tools

Disambiguation5/5

Each tool targets a distinct artifact and action: validate_* performs offline check-digit verification, lookup_* queries a remote registry, and parse_* decodes a fixed-length Brazilian payment/fiscal string. The validate_cnpj vs. lookup_cnpj pair is explicitly separated by the 'Offline'/'via BrasilAPI' language, and parse_boleto vs. parse_nfe_key differ by input format (47/48 vs. 44 digits).

Naming Consistency5/5

All six tools follow a strict verb_noun snake_case pattern with a small, reused verb vocabulary (validate_, lookup_, parse_). No casing or style deviations.

Tool Count5/5

Six tools is well within the ideal range and each one maps to a concrete Brazilian ERP validation/lookup/parsing need. Nothing looks redundant or filler.

Completeness4/5

CNPJ has both validation and registry lookup, CPF is validated, boletos and NF-e keys are decoded, and CEP resolves to an address — covering the common ERP intake paths. Gaps include no offline CEP format validation, no inscrição estadual (state registration) handling, and no boleto/NF-e emission or encoding counterparts, though agents can work around these.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues