Skip to main content
Glama
cmendezs

mcp-facturacion-electronica-es

es__generate_b2b_einvoice_es

Generate a B2B electronic invoice compliant with EN 16931 in UBL 2.1 or Facturae 3.2.2 formats, as required by Spanish Ley 18/2022 'Crea y Crece'.

Instructions

Genera una factura B2B conforme a EN 16931 en formato UBL 2.1 o Facturae 3.2.2 según la Ley 18/2022 'Crea y Crece'. El reglamento de desarrollo está pendiente de publicación.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
invoiceYesDatos de la factura.
formatNoFormato de salida: 'ubl' (por defecto) o 'facturae'.ubl

Implementation Reference

  • The async handler function that executes the tool logic. Parses invoice data, selects format (UBL 2.1 or Facturae 3.2.2), calls the builder, and returns the generated XML.
    async def handle_es_generate_b2b_einvoice_es(
        arguments: dict[str, Any],
    ) -> list[types.TextContent]:
        try:
            invoice_data = arguments.get("invoice")
            if not invoice_data:
                return err("invoice is required", "MISSING_PARAM")
    
            format_str = arguments.get("format", "ubl")
            try:
                fmt = B2BFormat(format_str)
            except ValueError:
                return err(f"Invalid format: {format_str!r}. Must be 'ubl' or 'facturae'.")
    
            invoice = parse_invoice(invoice_data)
    
            if fmt == B2BFormat.ubl:
                xml_bytes = build_ubl_invoice(invoice)
                schema_desc = "UBL 2.1 (EN 16931)"
            else:
                xml_bytes = build_facturae_xml(invoice)
                schema_desc = "Facturae 3.2.2 (EN 16931)"
    
            logger.info(
                "B2B e-invoice generated for %s (format=%s)", invoice.number, fmt.value
            )
    
            return ok({
                "xml": xml_bytes.decode("utf-8"),
                "format": fmt.value,
                "schema": schema_desc,
                "invoice_number": invoice.number,
                "disclaimer": (
                    "El reglamento de desarrollo de la Ley 18/2022 (Crea y Crece) está pendiente "
                    "de publicación. El formato definitivo puede variar."
                ),
            })
    
        except EInvoicingError as exc:
            return err(str(exc))
        except Exception as exc:
            logger.exception("es__generate_b2b_einvoice_es failed")
            return err(str(exc))
  • Tool definition with inputSchema specifying 'invoice' (object, required) and 'format' (enum: ubl/facturae, default: ubl).
    TOOL_ES_GENERATE_B2B_EINVOICE_ES = types.Tool(
        name="es__generate_b2b_einvoice_es",
        description=(
            "Genera una factura B2B conforme a EN 16931 en formato UBL 2.1 o Facturae 3.2.2 "
            "según la Ley 18/2022 'Crea y Crece'. "
            "El reglamento de desarrollo está pendiente de publicación."
        ),
        inputSchema={
            "type": "object",
            "properties": {
                "invoice": {"type": "object", "description": "Datos de la factura."},
                "format": {
                    "type": "string",
                    "enum": ["ubl", "facturae"],
                    "description": "Formato de salida: 'ubl' (por defecto) o 'facturae'.",
                    "default": "ubl",
                },
            },
            "required": ["invoice"],
        },
    )
  • Registration of the tool name to its handler in the _TOOL_HANDLERS dict (line 131) and inclusion in _ALL_TOOLS list (line 100).
        "es__generate_b2b_einvoice_es":       handle_es_generate_b2b_einvoice_es,
        "es__check_b2b_mandate_applicability": handle_es_check_b2b_mandate_applicability,
        # Utilities
        "es__detect_regional_regime":         handle_es_detect_regional_regime,
        "es__get_compliance_status":          handle_es_get_compliance_status,
        "es__parse_aeat_response":            handle_es_parse_aeat_response,
    }
  • Builds a UBL 2.1 EN 16931-compliant XML invoice from an InvoiceDocument. Called by the handler when format='ubl'.
    def build_ubl_invoice(invoice: InvoiceDocument) -> bytes:
        """Build an EN 16931-compliant UBL 2.1 Invoice from a core InvoiceDocument.
    
        Args:
            invoice: Core InvoiceDocument.
    
        Returns:
            UTF-8 UBL 2.1 XML bytes.
        """
        nsmap = {
            None: _UBL_INVOICE_NS,
            "cac": _CAC_NS,
            "cbc": _CBC_NS,
        }
        root = etree.Element(f"{{{_UBL_INVOICE_NS}}}Invoice", nsmap=nsmap)
    
        _sub(root, _CBC_NS, "CustomizationID",
             "urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0")
        _sub(root, _CBC_NS, "ProfileID", "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0")
        _sub(root, _CBC_NS, "ID", invoice.number)
        _sub(root, _CBC_NS, "IssueDate", invoice.date)
        _sub(root, _CBC_NS, "InvoiceTypeCode", "380")  # 380 = commercial invoice
        _sub(root, _CBC_NS, "DocumentCurrencyCode", invoice.currency.upper())
    
        if invoice.note:
            _sub(root, _CBC_NS, "Note", invoice.note[:500])
    
        # AccountingSupplierParty (seller)
        asp = _sub(root, _CAC_NS, "AccountingSupplierParty")
        party_s = _sub(asp, _CAC_NS, "Party")
        ep_s = _sub(party_s, _CAC_NS, "EndpointID")
        ep_s.set("schemeID", "0088")
        ep_s.text = invoice.seller.tax_id.identifier
        pid_s = _sub(party_s, _CAC_NS, "PartyIdentification")
        pid_id_s = _sub(pid_s, _CBC_NS, "ID")
        pid_id_s.set("schemeID", "0002")
        pid_id_s.text = invoice.seller.tax_id.identifier
        pn_s = _sub(party_s, _CAC_NS, "PartyName")
        _sub(pn_s, _CBC_NS, "Name", invoice.seller.display_name[:100])
        ptv_s = _sub(party_s, _CAC_NS, "PartyTaxScheme")
        _sub(ptv_s, _CBC_NS, "CompanyID", invoice.seller.tax_id.identifier)
        ts_s = _sub(ptv_s, _CAC_NS, "TaxScheme")
        _sub(ts_s, _CBC_NS, "ID", "VAT")
        pli_s = _sub(party_s, _CAC_NS, "PartyLegalEntity")
        _sub(pli_s, _CBC_NS, "RegistrationName", invoice.seller.display_name[:100])
        _sub(pli_s, _CBC_NS, "CompanyID", invoice.seller.tax_id.identifier)
    
        # AccountingCustomerParty (buyer)
        acp = _sub(root, _CAC_NS, "AccountingCustomerParty")
        party_b = _sub(acp, _CAC_NS, "Party")
        ep_b = _sub(party_b, _CAC_NS, "EndpointID")
        ep_b.set("schemeID", "0088")
        ep_b.text = invoice.buyer.tax_id.identifier
        pn_b = _sub(party_b, _CAC_NS, "PartyName")
        _sub(pn_b, _CBC_NS, "Name", invoice.buyer.display_name[:100])
        ptv_b = _sub(party_b, _CAC_NS, "PartyTaxScheme")
        _sub(ptv_b, _CBC_NS, "CompanyID", invoice.buyer.tax_id.identifier)
        ts_b = _sub(ptv_b, _CAC_NS, "TaxScheme")
        _sub(ts_b, _CBC_NS, "ID", "VAT")
        pli_b = _sub(party_b, _CAC_NS, "PartyLegalEntity")
        _sub(pli_b, _CBC_NS, "RegistrationName", invoice.buyer.display_name[:100])
        _sub(pli_b, _CBC_NS, "CompanyID", invoice.buyer.tax_id.identifier)
    
        # Payment means (if payment provided)
        if invoice.payment:
            pm = _sub(root, _CAC_NS, "PaymentMeans")
            _sub(pm, _CBC_NS, "PaymentMeansCode", invoice.payment.payment_method_code or "30")
            if invoice.payment.due_date:
                _sub(pm, _CBC_NS, "PaymentDueDate", invoice.payment.due_date)
            if invoice.payment.iban:
                pfa = _sub(pm, _CAC_NS, "PayeeFinancialAccount")
                _sub(pfa, _CBC_NS, "ID", invoice.payment.iban)
    
        # TaxTotal
        tax_base_total = sum((v.taxable_base for v in invoice.vat_summary), Decimal("0"))
        tax_amount_total = sum((v.vat_amount for v in invoice.vat_summary), Decimal("0"))
        grand_total = tax_base_total + tax_amount_total
    
        tt = _sub(root, _CAC_NS, "TaxTotal")
        tta = _sub(tt, _CBC_NS, "TaxAmount", fmt_amount(tax_amount_total))
        tta.set("currencyID", invoice.currency.upper())
        for vat in invoice.vat_summary:
            ts_elem = _sub(tt, _CAC_NS, "TaxSubtotal")
            tsb = _sub(ts_elem, _CBC_NS, "TaxableAmount", fmt_amount(vat.taxable_base))
            tsb.set("currencyID", invoice.currency.upper())
            tsa = _sub(ts_elem, _CBC_NS, "TaxAmount", fmt_amount(vat.vat_amount))
            tsa.set("currencyID", invoice.currency.upper())
            tc = _sub(ts_elem, _CAC_NS, "TaxCategory")
            _sub(tc, _CBC_NS, "ID", "S")
            _sub(tc, _CBC_NS, "Percent", fmt_amount(vat.vat_rate))
            tsch = _sub(tc, _CAC_NS, "TaxScheme")
            _sub(tsch, _CBC_NS, "ID", "VAT")
    
        # LegalMonetaryTotal
        lmt = _sub(root, _CAC_NS, "LegalMonetaryTotal")
        lab = _sub(lmt, _CBC_NS, "LineExtensionAmount", fmt_amount(tax_base_total))
        lab.set("currencyID", invoice.currency.upper())
        teb = _sub(lmt, _CBC_NS, "TaxExclusiveAmount", fmt_amount(tax_base_total))
        teb.set("currencyID", invoice.currency.upper())
        tib = _sub(lmt, _CBC_NS, "TaxInclusiveAmount", fmt_amount(grand_total))
        tib.set("currencyID", invoice.currency.upper())
        pab = _sub(lmt, _CBC_NS, "PayableAmount", fmt_amount(grand_total))
        pab.set("currencyID", invoice.currency.upper())
    
        # InvoiceLine
        for i, line in enumerate(invoice.lines, start=1):
            il = _sub(root, _CAC_NS, "InvoiceLine")
            _sub(il, _CBC_NS, "ID", str(i))
            ilq = _sub(il, _CBC_NS, "InvoicedQuantity", fmt_amount(line.quantity or Decimal("1")))
            ilq.set("unitCode", line.unit_of_measure or "EA")
            ila = _sub(il, _CBC_NS, "LineExtensionAmount", fmt_amount(line.total_price))
            ila.set("currencyID", invoice.currency.upper())
            item = _sub(il, _CAC_NS, "Item")
            _sub(item, _CBC_NS, "Description", line.description[:500])
            itc = _sub(item, _CAC_NS, "ClassifiedTaxCategory")
            _sub(itc, _CBC_NS, "ID", "S")
            _sub(itc, _CBC_NS, "Percent", fmt_amount(line.vat_rate))
            isc = _sub(itc, _CAC_NS, "TaxScheme")
            _sub(isc, _CBC_NS, "ID", "VAT")
            pp = _sub(il, _CAC_NS, "Price")
            ppa = _sub(pp, _CBC_NS, "PriceAmount", fmt_amount(line.unit_price))
            ppa.set("currencyID", invoice.currency.upper())
    
        return etree.tostring(root, xml_declaration=True, encoding="UTF-8", pretty_print=True)
  • B2BFormat StrEnum defining the two allowed output formats: 'ubl' and 'facturae'.
    class B2BFormat(StrEnum):
        """Output format for Crea y Crece B2B e-invoices (Ley 18/2022)."""
    
        ubl = "ubl"
        facturae = "facturae"
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It describes a generation action (no side effects stated) but does not disclose authentication needs, rate limits, or whether the invoice is validated or submitted. This is adequate but not thorough for a critical business document generation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences, front-loaded with action and key specifics (law, formats, pending regulation). No unnecessary words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The tool has two parameters (one nested object) and no output schema. The description does not mention return type (likely XML string) or error handling. For a generation tool, explaining the output format beyond format enum would improve completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% and both parameters have descriptions in the schema. The description adds legal context and format options but does not elaborate on the invoice object structure. Baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it generates a B2B invoice conforming to EN 16931 in UBL 2.1 or Facturae 3.2.2, tying it to Spanish law. It distinguishes from siblings like generate_facturae_xml (FACTURAE only) and generate_ticketbai_xml (regional) by specifying the B2B e-invoice scope and legal reference.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description does not provide guidance on when to use this tool versus alternatives (e.g., generate_facturae_xml, generate_ticketbai_xml) or between the two output formats (UBL vs Facturae). No when-not or prerequisite information is given.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cmendezs/mcp-facturacion-electronica-es'

If you have feedback or need assistance with the MCP directory API, please join our Discord server