mcp-ksef-pl 🇵🇱
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.
🏗️ 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 |
🚀 Installation
Via PyPI (recommended)
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, test, or demo |
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 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
-
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/. -
Request a challenge. Call the KSeF API to obtain a challenge XML envelope:
curl -s https://ksef-test.mf.gov.pl/api/online/Session/AuthorisationChallenge \ -H "Accept: application/json" \ -d '{"contextIdentifier": {"type": "onip", "identifier": "YOUR_NIP"}}' \ -H "Content-Type: application/json"
The response contains a
challengestring and atimestamp. -
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
xmlsec1with 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
-
Submit the signed challenge. POST the signed XML to obtain an AccessToken:
curl -s https://ksef-test.mf.gov.pl/api/online/Session/AuthoriseXades \ -H "Content-Type: application/octet-stream" \ --data-binary @signed-challenge.xml
The response contains
sessionToken.token(the AccessToken) andsessionToken.context.referenceNumber. -
Set the token. Export the token for this MCP server:
export KSEF_SESSION_TOKEN="<the AccessToken from step 4>"
The token is valid for approximately 2 hours from issuance (per MF documentation). After expiry, repeat steps 2-4.
References
- KSeF technical documentation: https://www.podatki.gov.pl/ksef/dokumentacja-techniczna-ksef/
- Authentication spec (CIRFMF): https://github.com/CIRFMF/ksef-docs/blob/main/uwierzytelnianie.md
- Interactive session spec (CIRFMF): https://github.com/CIRFMF/ksef-docs/blob/main/sesja-interaktywna.md
- FA(3) migration announcement:
specs/ksef-v2-fa3-migration-announcement-20250630.pdf
🤖 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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_ksef_pl-0.5.0.tar.gz.
File metadata
- Download URL: mcp_ksef_pl-0.5.0.tar.gz
- Upload date:
- Size: 804.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f66035a19e4c849211a395be67ee1662acac93b680af7f861a4ec7b485abf84
|
|
| MD5 |
7ed36fec2bba77731e09f9c399959a88
|
|
| BLAKE2b-256 |
d042f3c964fc8a0c59ebaf57154417fadbba237d687145de695b01c87fd68b32
|
Provenance
The following attestation bundles were made for mcp_ksef_pl-0.5.0.tar.gz:
Publisher:
publish.yml on cmendezs/mcp-ksef-pl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_ksef_pl-0.5.0.tar.gz -
Subject digest:
2f66035a19e4c849211a395be67ee1662acac93b680af7f861a4ec7b485abf84 - Sigstore transparency entry: 2173039270
- Sigstore integration time:
-
Permalink:
cmendezs/mcp-ksef-pl@4df73d61c53d97d40acdd78d03ecf8f1573b41de -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/cmendezs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4df73d61c53d97d40acdd78d03ecf8f1573b41de -
Trigger Event:
push
-
Statement type:
File details
Details for the file mcp_ksef_pl-0.5.0-py3-none-any.whl.
File metadata
- Download URL: mcp_ksef_pl-0.5.0-py3-none-any.whl
- Upload date:
- Size: 94.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f506b30fc8d5d7cd9984a046b67779ff03a798bae846e5708f947f75711bdc65
|
|
| MD5 |
37cde44c3d114d04614331462d539a89
|
|
| BLAKE2b-256 |
75b3a4eed9604ad2321cf7046ef60c576e6de3e80693f4f3eb5d1138e133689f
|
Provenance
The following attestation bundles were made for mcp_ksef_pl-0.5.0-py3-none-any.whl:
Publisher:
publish.yml on cmendezs/mcp-ksef-pl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_ksef_pl-0.5.0-py3-none-any.whl -
Subject digest:
f506b30fc8d5d7cd9984a046b67779ff03a798bae846e5708f947f75711bdc65 - Sigstore transparency entry: 2173039278
- Sigstore integration time:
-
Permalink:
cmendezs/mcp-ksef-pl@4df73d61c53d97d40acdd78d03ecf8f1573b41de -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/cmendezs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4df73d61c53d97d40acdd78d03ecf8f1573b41de -
Trigger Event:
push
-
Statement type: