Skip to main content

MCP server for Brazilian electronic invoicing (NF-e / NFC-e)

Project description

mcp-nfe-br ๐Ÿ‡ง๐Ÿ‡ท

English | Portugues (Brasil)

PyPI version Python License


Introduction

mcp-nfe-br is an MCP (Model Context Protocol) server providing tools for issuing and validating Brazilian electronic fiscal documents: NF-e (modelo 55) and NFC-e (modelo 65), per SEFAZ XML schema version 4.00. This server is part of the mcp-einvoicing-* / mcp-*-* family, built on mcp-einvoicing-core, which provides the base data model, HTTP/OAuth2 utilities, and shared MCP server infrastructure.

Current status (v0.2.0): Phase 1 of the roadmap, covering CPF/CNPJ tax-ID validation, unsigned NF-e/NFC-e XML generation, and XSD validation against the official PL_010d schema (unsigned variant). ICP-Brasil digital signing and SEFAZ webservice submission are planned for future releases. Generated documents are unsigned and are not transmitted to SEFAZ by this server. NFS-e (service invoices) and CT-e (transport documents) are later phases, out of scope for this version.


Installation

Requirements

Using uv (recommended)

uv add mcp-nfe-br

Using pip

pip install mcp-nfe-br

From source

git clone https://github.com/cmendezs/mcp-nfe-br.git
cd mcp-nfe-br
uv sync --all-extras

Configuration

Add the server to your MCP client configuration. For Claude Desktop, edit claude_desktop_config.json:

{
  "mcpServers": {
    "nfe-br": {
      "command": "uvx",
      "args": ["mcp-nfe-br"]
    }
  }
}

For a local development installation:

{
  "mcpServers": {
    "nfe-br": {
      "command": "uv",
      "args": ["run", "mcp-nfe-br"],
      "cwd": "/path/to/mcp-nfe-br"
    }
  }
}

Environment variables

Variable Description Default
BR_READ_ONLY Set to 1 to disable SEFAZ write tools (br__submit_nfe, br__distribute_dfe). Safe mode for exploration. The SEFAZ environment (production/homologation) is selected per call via the tp_amb argument. โ€”
LOG_LEVEL Log level: DEBUG, INFO, WARNING, ERROR INFO

Available tools

br__validate_cpf

Validates a CPF (Cadastro de Pessoas Fรญsicas), the individual taxpayer identification number, using the Receita Federal modulo 11 algorithm.

Parameter Type Required Description
cpf string yes CPF with or without ./- separators

Returns a TaxIdValidationResult with valid=True and the cleaned value (11 digits) on success, or valid=False with an error message in Portuguese.


br__validate_cnpj

Validates a CNPJ (Cadastro Nacional da Pessoa Jurรญdica), the business taxpayer identification number. Accepts both the traditional numeric format (14 digits) and the alphanumeric format introduced by NT 2026.004 (PL_010d), effective in homologation from 2026-06-01 and in production from 2026-07-01.

Parameter Type Required Description
cnpj string yes CNPJ with or without .///- separators

Returns a TaxIdValidationResult with valid=True and the cleaned value (14 characters) on success, or valid=False with an error message in Portuguese.

โš ๏ธ [Unverified]: the check-digit algorithm for the alphanumeric CNPJ format was implemented based on secondary sources, as the primary source ("NT Conjunta DFe 2025.001") is not yet available locally. See context-library/countries/br.md for details.


br__generate_nfe

Generates an unsigned NF-e/NFC-e 4.00 document (<NFe><infNFe>โ€ฆ</infNFe></NFe>) from a BRInvoice object.

Parameter Type Required Description
invoice object yes BRInvoice document (modelo 55 or 65, groups ide/emit/dest/det/total/transp/pag)

Returns {"xml": ..., "chave_acesso": ..., "warnings": [...]}. The warnings in Portuguese remind that the document is not signed (ICP-Brasil) and was not transmitted to SEFAZ. Both steps are the responsibility of a separate process.

Phase 1 coverage for per-item tax groups:

Tax Supported codes Behavior
ICMS CST 00 (normal regime) or CSOSN 102 (Simples Nacional) other codes raise DocumentGenerationError
PIS/COFINS CST 01/02 (rate-based) or 04-09 (non-taxed) group omitted if pis_cst/cofins_cst are None
IPI CST 00/49/50/99 (taxed) or other (non-taxed) group omitted if ipi_cst is None

[NEED: IBS/CBS/Imposto Seletivo โ€” Grupo UB/W03 (NT 2025.002-RTC) not yet modeled, see context-library/countries/br.md "Known gaps"].


br__validate_nfe_xml

Validates an NF-e/NFC-e 4.00 XML document against the official PL_010d XSD (local "unsigned" variant, see note below).

Parameter Type Required Description
xml_content string no* XML as a string
xml_base64 string no* Base64-encoded XML

* Exactly one of xml_content/xml_base64 must be provided.

Returns {"valid": bool, "errors": [...], "metadata": {"schema_version": ...}}.

[Inference]: the official XSD (nfe_v4.00.xsd/leiauteNFe_v4.00.xsd, PL_010d) requires <ds:Signature> as a mandatory child of <NFe>. Since Phase 1 generates unsigned documents, this tool validates against a local derived copy (nfe_v4.00_unsigned.xsd) where <ds:Signature> has been made optional (minOccurs="0"). Validation of signed documents (future phase) should use the official XSD without modifications.


br__build_access_key

Builds an access key (chNFe, 44 characters) with a modulo 11 check digit, from the components cUF, dhEmi, issuer CNPJ, model, series, and document number.

Parameter Type Required Description
c_uf string yes IBGE state code (2 digits)
dh_emi string yes Issue date/time (ISO 8601)
cnpj string yes Issuer CNPJ (numeric or alphanumeric PL_010d)
modelo string yes 55 (NF-e) or 65 (NFC-e)
serie string yes Document series
nnf string yes Document number
tp_emis string no Issuance type (default "1")
c_nf string no Random numeric code (cNF, 8 digits); auto-generated if omitted

Returns {"chave_acesso": ..., "cnf": ...}.


Architecture

mcp-nfe-br/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ mcp_nfe_br/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ server.py              # MCP entry point and tool registration
โ”‚       โ”œโ”€โ”€ models/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ””โ”€โ”€ invoice.py         # BRInvoice, BRInvoiceLine, NFeModelo, TipoOperacao
โ”‚       โ”œโ”€โ”€ standards/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ””โ”€โ”€ nfe_generator.py   # NFeGenerator โ€” generates unsigned NF-e/NFC-e 4.00
โ”‚       โ”œโ”€โ”€ validators/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ””โ”€โ”€ nfe_xsd.py         # NFeXSDValidator โ€” validates against PL_010d XSD (unsigned variant)
โ”‚       โ”œโ”€โ”€ schemas/nfe/           # Bundled XSDs (official + "_unsigned" variants)
โ”‚       โ”œโ”€โ”€ tools/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ”œโ”€โ”€ validation.py      # br__validate_cpf, br__validate_cnpj
โ”‚       โ”‚   โ””โ”€โ”€ generation.py      # br__generate_nfe, br__validate_nfe_xml, br__build_access_key
โ”‚       โ””โ”€โ”€ utils/
โ”‚           โ”œโ”€โ”€ __init__.py
โ”‚           โ”œโ”€โ”€ document_ids.py    # validate_cpf, validate_cnpj
โ”‚           โ””โ”€โ”€ access_key.py      # build_access_key, access_key_check_digit
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ conftest.py
โ”‚   โ”œโ”€โ”€ fixtures/
โ”‚   โ”œโ”€โ”€ test_tools/
โ”‚   โ”‚   โ”œโ”€โ”€ test_validation.py
โ”‚   โ”‚   โ””โ”€โ”€ test_generation.py
โ”‚   โ”œโ”€โ”€ test_standards/
โ”‚   โ”‚   โ””โ”€โ”€ test_nfe_generator.py
โ”‚   โ”œโ”€โ”€ test_validators/
โ”‚   โ”‚   โ””โ”€โ”€ test_nfe_xsd.py
โ”‚   โ””โ”€โ”€ test_utils/
โ”‚       โ””โ”€โ”€ test_access_key.py
โ”œโ”€โ”€ specs/nfe/                     # Normative material (XSDs, MOC, Technical Notes, not published)
โ”œโ”€โ”€ audit/
โ”‚   โ”œโ”€โ”€ audit_vs_core.py
โ”‚   โ””โ”€โ”€ report.json
โ”œโ”€โ”€ .github/workflows/publish.yml
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ RELEASE.md
โ””โ”€โ”€ LICENSE

Relationship with mcp-einvoicing-core

mcp-einvoicing-core provides:

  • Base Pydantic models for invoices, parties, line items, and validation results (InvoiceDocument, InvoiceLineItem, TaxIdValidationResult)
  • Shared MCP server infrastructure (EInvoicingMCPServer)
  • HTTP/OAuth2 client, token cache, structured logging, exception hierarchy

mcp-nfe-br adds Brazil-specific logic:

  • BRInvoice (extends InvoiceDocument, as NF-e/NFC-e has no EN 16931 lineage)
  • Group I fields (NCM, CFOP, ICMS/IPI/PIS/COFINS) in BRInvoiceLine
  • CPF/CNPJ validation (including the alphanumeric CNPJ from NT 2026.004)

Contributing

Contributions are welcome. Please open an issue to discuss significant changes before submitting a pull request.

git clone https://github.com/cmendezs/mcp-nfe-br.git
cd mcp-nfe-br
uv sync --all-extras
uv run pytest
uv run ruff check src/mcp_nfe_br tests audit
uv run mypy src/mcp_nfe_br

Other e-invoicing MCP servers

Country Server
๐ŸŒ Global mcp-einvoicing-core
๐Ÿ‡ง๐Ÿ‡ช Belgium mcp-einvoicing-be
๐Ÿ‡ง๐Ÿ‡ท Brazil mcp-nfe-br
๐Ÿ‡ซ๐Ÿ‡ท France mcp-facture-electronique-fr
๐Ÿ‡ฉ๐Ÿ‡ช Germany mcp-einvoicing-de
๐Ÿ‡ฎ๐Ÿ‡น Italy mcp-fattura-elettronica-it
๐Ÿ‡ต๐Ÿ‡ฑ Poland mcp-ksef-pl
๐Ÿ‡ช๐Ÿ‡ธ Spain mcp-facturacion-electronica-es

License

This project is licensed under Apache 2.0. See LICENSE for details.


Changelog

See RELEASE.md for the full version history.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_nfe_br-0.5.4.tar.gz (256.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_nfe_br-0.5.4-py3-none-any.whl (170.3 kB view details)

Uploaded Python 3

File details

Details for the file mcp_nfe_br-0.5.4.tar.gz.

File metadata

  • Download URL: mcp_nfe_br-0.5.4.tar.gz
  • Upload date:
  • Size: 256.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcp_nfe_br-0.5.4.tar.gz
Algorithm Hash digest
SHA256 0b35e49144161cb5a96d5ed40c36dd24724d9770a605082c9fc4aac6f17f4c7a
MD5 47c3b4537e43c25d2f8353c259c297de
BLAKE2b-256 e510c2df4473f32a113f0ecdb527b38410ca4b2c8b6cc267d4f92645479eae80

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_nfe_br-0.5.4.tar.gz:

Publisher: publish.yml on cmendezs/mcp-nfe-br

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcp_nfe_br-0.5.4-py3-none-any.whl.

File metadata

  • Download URL: mcp_nfe_br-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 170.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcp_nfe_br-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2fc7bbaadfcfd88acf45103933a8d62afd2bfa5a4031872c930ffbb700f8d673
MD5 c2c56e39a64722892d9b89f5aaa0be24
BLAKE2b-256 4fb75af35e745e9ae470ab1905c722ebeb145ebcb038c3e6e5a8722f794acd55

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_nfe_br-0.5.4-py3-none-any.whl:

Publisher: publish.yml on cmendezs/mcp-nfe-br

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page