Skip to main content
Glama
cmendezs

mcp-ksef-pl

by cmendezs

mcp-ksef-pl ๐Ÿ‡ต๐Ÿ‡ฑ

English | Polski

License PyPI version Python mcp-ksef-pl MCP server

A Python MCP server providing tools for Polish electronic invoicing compliant with KSeF (FA(2)) and Peppol BIS Billing 3.0 / EN 16931. It enables AI agents (Claude, IDEs) to generate, validate, and submit invoices to the Krajowy System e-Faktur (KSeF), as well as validate Polish tax identifiers (NIP and REGON).

Built on

This package is built on mcp-einvoicing-core, the shared base library for European e-invoicing MCP servers. It provides an OAuth2 HTTP client, token cache, data models, logging utilities, and an exception hierarchy.

mcp-einvoicing-core is installed automatically as a dependency, no additional step is required.


Related MCP server: mcp-fattura-elettronica-it

๐Ÿ—๏ธ Architecture

The server acts as an intelligent communication interface between the AI agent and the KSeF platform and the Peppol network:

[ ERP System / Application ] <--> [ MCP Server ] <--> [ KSeF (MF) / Peppol Network ]
          ^                           |
          |                           v
   [ AI Agent (Claude) ] <--- (FA(2) / EN 16931)

๐Ÿ› ๏ธ Available tools

FA(3) / FA(2) invoice handling

Tool

Description

generate_fa3_invoice

Generates a KSeF-compliant FA(3) XML invoice (required for KSeF API v2 submissions)

generate_fa2_invoice

Generates a KSeF-compliant FA(2) XML invoice (legacy format, read-only use)

validate_fa3_invoice

Validates FA(3) XML: XSD validation and FA(3)-specific business rules

validate_fa2_invoice

Validates FA(2) XML: XSD validation (if the schema is available) and business rules

parse_fa2_invoice

Parses FA(2) XML into a structured dictionary

KSeF lifecycle

Tool

Description

submit_invoice_to_ksef

Submits an FA(3) invoice to the KSeF platform and returns a reference number

get_ksef_invoice_status

Retrieves the processing status of an invoice by its reference number

search_ksef_invoices

Searches invoices in KSeF by date range and direction (seller/buyer)

Identifier validation

Tool

Description

validate_polish_nip

Validates a NIP (10-digit tax identification number) using a checksum algorithm

validate_polish_regon

Validates a REGON (9- or 14-digit registry number) using a checksum algorithm

Peppol / EN 16931

Tool

Description

generate_peppol_invoice

Generates a UBL 2.1 invoice compliant with Peppol BIS Billing 3.0 / EN 16931

validate_peppol_invoice

Validates a UBL 2.1 Peppol invoice against the CEN EN 16931 base Schematron rules (en16931-base-only scope โ€” does not check the Peppol-specific overlay)

Peppol network tools

Peppol participant lookup, service-endpoint lookup, a DNS-only diagnostic, AS4 send, and the OpenPeppol eDEC codelist tools are provided by the shared core Peppol tool plugin (mcp_einvoicing_core.peppol.tools.register_peppol_tools), mounted in server.py with a Poland-specific identifier adapter: a bare NIP (e.g. 1234563218) is normalized to the 9945:<digits> Peppol scheme (PL:VAT, per the OpenPeppol eDEC Participant Identifier Schemes code list); an already scheme-qualified identifier (e.g. 9945:1234563218) passes through unchanged. Use these tools to check PEF (Poland's Peppol Access Point for public-procurement B2G invoicing) registration status ahead of generate_peppol_invoice.

Tool

Description

peppol_lookup_participant

Check whether a business is registered on the Peppol network; returns registration status and supported document types

peppol_get_service_endpoint

Fetch the AS4 endpoint for a participant's document type

resolve_peppol_dns

DNS-only (SML) diagnostic, independent of SMP reachability

peppol_send

Transmit a UBL/CII invoice via AS4

list_participant_id_schemes, list_document_type_ids, list_process_ids, list_spis_use_case_ids

OpenPeppol eDEC codelist lookups (require EINVOICING_PEPPOL_CODELIST_DIR)

check_document_type_id_in_codelist, check_process_id_in_codelist, check_participant_id_scheme_in_codelist, get_peppol_codelist_version

OpenPeppol eDEC codelist checks and version reporting

See the mcp-einvoicing-core README for full parameter documentation on these tools.


๐Ÿš€ Installation

pip install mcp-ksef-pl

Or without prior installation using uvx:

uvx mcp-ksef-pl

From source

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

โš™๏ธ Configuration (environment variables)

Variable

Default

Description

KSEF_ENVIRONMENT

test

KSeF environment: production or test

KSEF_SESSION_TOKEN

โ€”

KSeF session token (obtained through the challenge-response flow with MF)

KSEF_NIP

โ€”

NIP of the entity submitting invoices

KSEF_TIMEOUT

30

HTTP request timeout in seconds

KSEF_VERIFY_MF_KEY_PINNING

false

Enforce SPKI SHA-256 pinning on the MF encryption certificate. No-op until fingerprints are populated for the active environment, even when set to true

EINVOICING_PEPPOL_CODELIST_DIR

โ€”

Local directory containing your own copy of the OpenPeppol eDEC Code Lists, required by the Peppol codelist tools (not bundled with this package; see mcp-einvoicing-core README)


๐Ÿ” KSeF authentication

KSeF API v2 uses a multi-step challenge/redeem flow to issue an AccessToken. This MCP server accepts an already-obtained token and cannot automate the signing step (it requires a qualified electronic signature).

Step-by-step flow

  1. Account setup. Register at the KSeF portal: https://ksef.mf.gov.pl/. Select the target environment (test or production). The test environment is at https://ksef-test.mf.gov.pl/.

  2. Request a challenge. Call the KSeF API to obtain a challenge XML envelope:

    curl -s https://ksef-test.mf.gov.pl/auth/challenge \
      -H "Accept: application/json" \
      -d '{"contextIdentifier": {"type": "onip", "identifier": "YOUR_NIP"}}' \
      -H "Content-Type: application/json"

    The response contains a challenge string and a timestamp.

  3. Sign the challenge. Build an <InitSessionTokenRequest> XML envelope containing the challenge, then sign it with your qualified e-signature. Accepted signing tools:

    • Qualified e-signature providers: KIR (Szafir), Certum, Sigillum

    • podpis.gov.pl (government signing portal)

    • Profil Zaufany (Trusted Profile): https://www.podatki.gov.pl/ksef/

    Example using xmlsec1 with a PKCS#12 certificate:

    # Build the challenge XML (template at specs/przyklad-wyzwania.xml)
    xmlsec1 --sign --pkcs12 your-cert.p12 --pwd "password" \
      --output signed-challenge.xml challenge-template.xml
  4. Submit the signed challenge. POST the signed XML to receive an authOperation reference:

    curl -s https://ksef-test.mf.gov.pl/auth/xades-signature \
      -H "Content-Type: application/octet-stream" \
      --data-binary @signed-challenge.xml
  5. Redeem the AccessToken. Exchange the authenticated operation for an AccessToken:

    curl -s https://ksef-test.mf.gov.pl/auth/token/redeem \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer <referenceNumber-or-authOperation-token-from-step-4>"

    The response contains accessToken.token and accessToken.context.referenceNumber.

  6. Set the token. Export the token for this MCP server:

    export KSEF_SESSION_TOKEN="<the AccessToken from step 5>"

    The token is valid for approximately 2 hours from issuance (per MF documentation). After expiry, repeat steps 2-5.

References


๐Ÿค– Claude Desktop integration

Add the following configuration to your claude_desktop_config.json file:

{
  "mcpServers": {
    "ksef-pl": {
      "command": "uvx",
      "args": ["mcp-ksef-pl"],
      "env": {
        "KSEF_ENVIRONMENT": "test",
        "KSEF_SESSION_TOKEN": "<your-ksef-session-token>",
        "KSEF_NIP": "<your-nip>"
      }
    }
  }
}

โŒจ๏ธ Cursor integration

Cursor supports MCP servers via stdio. Add the configuration to:

  • Globally (all projects): ~/.cursor/mcp.json

  • Per project (this repository only): .cursor/mcp.json

{
  "mcpServers": {
    "ksef-pl": {
      "command": "uvx",
      "args": ["mcp-ksef-pl"],
      "env": {
        "KSEF_ENVIRONMENT": "test",
        "KSEF_SESSION_TOKEN": "<your-ksef-session-token>",
        "KSEF_NIP": "<your-nip>"
      }
    }
  }
}

Reload the Cursor window (Ctrl+Shift+P โ†’ Reload Window) after saving changes.


๐Ÿช Kiro integration

Kiro supports MCP servers through a dedicated configuration file:

  • Globally: ~/.kiro/settings/mcp.json

  • Workspace: .kiro/settings/mcp.json

{
  "mcpServers": {
    "ksef-pl": {
      "command": "uvx",
      "args": ["mcp-ksef-pl"],
      "env": {
        "KSEF_ENVIRONMENT": "test",
        "KSEF_SESSION_TOKEN": "<your-ksef-session-token>",
        "KSEF_NIP": "<your-nip>"
      },
      "disabled": false,
      "autoApprove": []
    }
  }
}

Security tip: instead of entering the token directly, use the syntax "KSEF_SESSION_TOKEN": "${KSEF_SESSION_TOKEN}", as Kiro resolves shell environment variables at startup.


๐Ÿ“‹ XSD schema

The official FA(2) and FA(3) XSD schemas ship inside the package (src/mcp_ksef_pl/schemas/) and are loaded automatically via importlib.resources โ€” no manual download or configuration is required. validate_fa2_invoice and validate_fa3_invoice run full XSD validation out of the box for every installation.


๐Ÿงช Tests

# Run unit tests
uv run pytest tests/ -v

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 distributed under the Apache 2.0 license. See the LICENSE file for details.


Project maintained by cmendezs. For questions about the KSeF or Peppol implementation, open an Issue.

Install Server
A
license - permissive license
A
quality
B
maintenance

Maintenance

โ€“Maintainers
10hResponse time
1wRelease cycle
12Releases (12mo)
Commit activity
Issues opened vs closed

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    A
    quality
    B
    maintenance
    Model Context Protocol (MCP) server for French Electronic Invoicing (NF XP Z12-013). Provide tools to validate, generate, and explore API specifications for PDP/OD interoperability.
    34
    Apache 2.0
  • A
    license
    A
    quality
    A
    maintenance
    Model Context Protocol (MCP) server for Italian Electronic Invoicing (FatturaPA / SDI). Provide tools to validate, generate, and explore API specifications for Sistema di Interscambio (SDI) interoperability.
    43
    1
    Apache 2.0
  • A
    license
    A
    quality
    A
    maintenance
    Model Context Protocol (MCP) server for Belgian Electronic Invoicing (Peppol BIS 3.0 / PINT-BE / Mercurius). Provides tools to validate, generate, and transform UBL 2.1 e-invoices, and look up BCE/KBO enterprise data and Peppol participants.
    50
    Apache 2.0
  • A
    license
    A
    quality
    A
    maintenance
    Model Context Protocol (MCP) server for German Electronic Invoicing (ZUGFeRD 2.x / XRechnung 3.x). Provides tools to validate, generate, parse, and convert invoices compliant with EN 16931 and KoSIT.
    8
    1
    Apache 2.0

View all related MCP servers

Related MCP Connectors

  • MCP Spec Compliance MCP โ€” audits any MCP server.json against the official Model Context Protocol

  • MCP server for the PDFGate API. Generate PDFs, manage documents and handle e-signatures.

  • MCP server for AI access to Swagger by SmartBear.

View all MCP Connectors

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-ksef-pl'

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