es__build_sii_invoice_record
Builds an XML SII invoice record for Spanish AEAT, supporting issued/received invoices with alta, modificación, or baja communication types.
Instructions
Construye un registro XML AEAT SII en formato SOAP (emisión FacturaExpedida o recepción FacturaRecibida) conforme a la guía técnica SII v3.0 (abril 2024). Soporta TipoComunicacion A0 (alta), A1 (modificación) y A4 (baja).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| invoice | Yes | Datos de la factura. | |
| record_type | Yes | Dirección: 'issued' (expedida) o 'received' (recibida). | |
| communication_type | No | TipoComunicacion: A0 alta (por defecto), A1 modificación, A4 baja. | A0 |
Implementation Reference
- The handler function 'handle_es_build_sii_invoice_record' that executes the tool logic. It parses invoice data, record type, and communication type, then calls build_sii_issued_record or build_sii_received_record to produce the SOAP XML envelope.
async def handle_es_build_sii_invoice_record( arguments: dict[str, Any], ) -> list[types.TextContent]: try: invoice_data = arguments.get("invoice") if not invoice_data: return err("invoice is required", "MISSING_PARAM") record_type_str = arguments.get("record_type", "issued") comm_type_str = arguments.get("communication_type", "A0") try: record_type = SIIRecordType(record_type_str) except ValueError: return err(f"Invalid record_type: {record_type_str!r}") try: comm_type = SIICommunicationType(comm_type_str) except ValueError: return err(f"Invalid communication_type: {comm_type_str!r}") invoice = parse_invoice(invoice_data) if record_type == SIIRecordType.issued: xml_bytes = build_sii_issued_record(invoice, comm_type.value) else: xml_bytes = build_sii_received_record(invoice, comm_type.value) logger.info( "SII %s record built for %s / %s (comm_type=%s)", record_type.value, invoice.seller.tax_id.identifier, invoice.number, comm_type.value, ) return ok({ "xml": xml_bytes.decode("utf-8"), "record_type": record_type.value, "communication_type": comm_type.value, "invoice_number": invoice.number, }) except EInvoicingError as exc: return err(str(exc)) except Exception as exc: logger.exception("es__build_sii_invoice_record failed") return err(str(exc)) - The tool definition 'TOOL_ES_BUILD_SII_INVOICE_RECORD' with name, description, and inputSchema. Defines the invoice object, record_type (issued/received), and communication_type (A0/A1/A4) parameters.
TOOL_ES_BUILD_SII_INVOICE_RECORD = types.Tool( name="es__build_sii_invoice_record", description=( "Construye un registro XML AEAT SII en formato SOAP (emisión FacturaExpedida o " "recepción FacturaRecibida) conforme a la guía técnica SII v3.0 (abril 2024). " "Soporta TipoComunicacion A0 (alta), A1 (modificación) y A4 (baja)." ), inputSchema={ "type": "object", "properties": { "invoice": { "type": "object", "description": "Datos de la factura.", }, "record_type": { "type": "string", "enum": ["issued", "received"], "description": "Dirección: 'issued' (expedida) o 'received' (recibida).", }, "communication_type": { "type": "string", "enum": ["A0", "A1", "A4"], "description": "TipoComunicacion: A0 alta (por defecto), A1 modificación, A4 baja.", "default": "A0", }, }, "required": ["invoice", "record_type"], }, ) - mcp_facturacion_electronica_es/server.py:34-46 (registration)Import of TOOL_ES_BUILD_SII_INVOICE_RECORD and handle_es_build_sii_invoice_record from the sii module into server.py.
from mcp_facturacion_electronica_es.tools.sii import ( TOOL_ES_BUILD_SII_INVOICE_RECORD, TOOL_ES_GENERATE_SII_CORRECTION, TOOL_ES_QUERY_SII_STATUS, TOOL_ES_SUBMIT_SII_BATCH, handle_es_build_sii_invoice_record, handle_es_generate_sii_correction, handle_es_query_sii_status, handle_es_submit_sii_batch, ) from mcp_facturacion_electronica_es.tools.ticketbai import ( TOOL_ES_GENERATE_TICKETBAI_XML, TOOL_ES_SUBMIT_TICKETBAI, - mcp_facturacion_electronica_es/server.py:77-91 (registration)Tool registered in the _ALL_TOOLS list that is returned by list_tools().
_ALL_TOOLS: list[types.Tool] = [ # VERI*FACTU TOOL_ES_GENERATE_VERIFACTU_RECORD, TOOL_ES_VALIDATE_VERIFACTU_RECORD, TOOL_ES_SUBMIT_VERIFACTU_TO_AEAT, TOOL_ES_GENERATE_QR_VERIFACTU, TOOL_ES_CANCEL_VERIFACTU_RECORD, # Facturae / FACe TOOL_ES_GENERATE_FACTURAE_XML, TOOL_ES_SIGN_FACTURAE_XADES, TOOL_ES_SUBMIT_TO_FACE, TOOL_ES_GET_FACE_INVOICE_STATUS, TOOL_ES_VALIDATE_FACTURAE_SCHEMA, # SII TOOL_ES_BUILD_SII_INVOICE_RECORD, - mcp_facturacion_electronica_es/server.py:108-122 (registration)Handler mapped in _TOOL_HANDLERS dict, dispatched by call_tool() when the tool is invoked.
_TOOL_HANDLERS: dict[str, Any] = { # VERI*FACTU "es__generate_verifactu_record": handle_es_generate_verifactu_record, "es__validate_verifactu_record": handle_es_validate_verifactu_record, "es__submit_verifactu_to_aeat": handle_es_submit_verifactu_to_aeat, "es__generate_qr_verifactu": handle_es_generate_qr_verifactu, "es__cancel_verifactu_record": handle_es_cancel_verifactu_record, # Facturae / FACe "es__generate_facturae_xml": handle_es_generate_facturae_xml, "es__sign_facturae_xades": handle_es_sign_facturae_xades, "es__submit_to_face": handle_es_submit_to_face, "es__get_face_invoice_status": handle_es_get_face_invoice_status, "es__validate_facturae_schema": handle_es_validate_facturae_schema, # SII "es__build_sii_invoice_record": handle_es_build_sii_invoice_record,