edi-mcp-server
README.md
# edi-mcp-server
[](https://github.com/andrew-myers-collab/edi-mcp-server/actions/workflows/ci.yml)
## Problem
An AI model that reads raw X12 text guesses at segment meaning and miscounts segments, so its answers
about a failed EDI file are often wrong. `edi-mcp-server` is a small Model Context Protocol (MCP) server
that gives the model exact tools to parse, validate, explain, acknowledge, and compare X12 files. All X12
rules come from the `x12val` package of the sibling repository
[x12-edi-validator](https://github.com/andrew-myers-collab/x12-edi-validator). GS1 checks come from the
`gs1` binary of [gs1-toolkit-rs](https://github.com/andrew-myers-collab/gs1-toolkit-rs).
## Quick start
```
git clone https://github.com/andrew-myers-collab/x12-edi-validator
git clone https://github.com/andrew-myers-collab/edi-mcp-server
cd edi-mcp-server
python -m venv .venv
.venv/bin/python -m pip install -r requirements.txt # Windows: .venv\Scripts\python.exe
.venv/bin/python demo.py
```
The server looks for `x12val` in `../x12-edi-validator`. Set `X12VAL_PATH` to use another folder.
The two GS1 tools need the `gs1` binary (`cargo build --release` in `../gs1-toolkit-rs`, or set `GS1_BIN`).
Without it, all other tools work and the GS1 tools return MCP007.
Start the server: `python -m edi_mcp` (stdio) or `python -m edi_mcp --http --port 8766` (127.0.0.1 only).
| Variable | Default |
|---|---|
| `X12VAL_PATH` | `../x12-edi-validator` |
| `EDI_MCP_SAMPLES` | `samples` in this repository |
| `EDI_MCP_MAX_BYTES` | `1000000` |
| `GS1_BIN` | `../gs1-toolkit-rs/target/release/gs1` (`gs1.exe` on Windows) |
## MCP client config example
```json
{
"mcpServers": {
"edi": {
"command": "/path/to/edi-mcp-server/.venv/bin/python",
"args": ["-m", "edi_mcp"],
"env": {
"PYTHONPATH": "/path/to/edi-mcp-server",
"X12VAL_PATH": "/path/to/x12-edi-validator",
"GS1_BIN": "/path/to/gs1-toolkit-rs/target/release/gs1"
}
}
}
}
```
## Sample output
Real output of `python demo.py` from the GitHub Actions workflow (Linux, Python 3.12, with the `gs1` binary built from gs1-toolkit-rs). All 48 tests ran there with no skip. With no `gs1` binary, the `gs1_check` line prints `SKIPPED`.
```
> list_samples -> 14 samples
> validate_x12 945_qty_mismatch.edi -> 2 error(s)
seg 12 W12 QTY002: ordered 5 - shipped 4 = 1, but W1204 is 3
> explain_rule QTY002 -> QTY002 (error) | 945 W12: quantity ordered minus quantity shipped does not equal the quantity difference. | Why: W1204 must equal ordered minus shipped, or the partner books the wrong backorder.
> explain_segment 945_qty_mismatch.edi pos 12 -> W12 Warehouse Item Detail: W1202 Quantity Ordered = 5 ...
> summarize_document 945_good.edi -> 945 key DEMO-1001, ship to Acme Distribution, 2 lines, shipped 14
> diff_x12 945_good.edi 945_v2.edi (ignore control) -> 3 difference(s)
> make_997 945_qty_mismatch.edi -> AK9*R
> gs1_check 2000000000016 -> invalid, 2000000000016 INVALID GTIN-13 check digit is wrong: expected 5, found 6
> guard: sample "../README.md" -> refused MCP002
all expected results matched
```
## Tools
Every tool that reads X12 takes exactly one of `text` or `sample` (a name from `list_samples`).
Each tool returns one JSON object.
| Tool | Input | Returns |
|---|---|---|
| `list_samples` | none | `samples`: name, set, bytes, expected (`clean` or `errors`) |
| `read_sample` | `sample` | name, text |
| `parse_x12` | text/sample, `max_segments`=500 | delimiters, isa_problem, envelope, segments (pos, id, elements), truncated |
| `validate_x12` | `sources` (1-20 of {name?, text?, sample?}), `rules_config`=None | summary (files, errors, warnings), findings |
| `explain_rule` | `rule` | rule, severity, text (from `x12val.explain`) |
| `explain_segment` | text/sample, `position` (1-based) | pos, id, name, elements (ref, name, value), findings at that segment |
| `summarize_document` | text/sample | sets: set, control, key, ship_to, lines, totals, errors |
| `make_997` | text/sample, `control`=1 | ack (997 text), statuses (AK9 per group) |
| `diff_x12` | `a`, `b` (text/sample objects), `ignore_control`=false | same, differences |
| `gs1_check` | `codes` (1-20) | results: code, valid, text (from `gs1 check`) |
| `gs1_parse` | `data` (1-20), `digital_link`=false, `ref_year`=None | results: line, input, valid, elements, errors |
Resources: `x12://rules` (rule catalog as JSON) and `x12://samples/{name}` (sample text).
Prompt: `review_edi_file(sample)` gives the steps: validate, explain each rule, summarize, propose fixes.
`summarize_document` uses the same document key as x12val DUP001 (940 W0502, 945 W0602, 846 BIA03,
947 W1502, 856 BSN02, 944 W1703). `totals` holds the sum that x12val QTY001 checks, keyed by element
(940 `W0101`, 945 `W1203`, 846 `QTY02`, 856 `SN102`, 944 `W0701`; the 947 has no QTY001 sum).
## Guards
A refused call returns a tool error. The SDK puts `Error executing tool <name>: ` before the server
message; the server message starts with the rule ID.
| ID | Refuses |
|---|---|
| MCP001 | input text longer than `EDI_MCP_MAX_BYTES` (UTF-8 bytes) |
| MCP002 | a `sample` that is not exactly a name from `list_samples` (no paths, no `..`) |
| MCP003 | both or neither of `text` and `sample`; a source object with other keys |
| MCP004 | a `rules_config` that `x12val.rules_config.parse_config` rejects (carries its message) |
| MCP005 | `position` outside the segments, `max_segments` outside 1-5000, `sources` empty or over 20, `control` outside 1-999999999, an unknown rule ID in `explain_rule` |
| MCP006 | `make_997` input with no GS group |
| MCP007 | `GS1_BIN` missing or not runnable (GS1 tools only; the server still starts) |
| MCP008 | a GS1 list that is empty or over 20 items, an item over 200 characters or that starts with `-`, `ref_year` outside 2000-2099 |
`python -m edi_mcp` exits 0 on a normal stop and 2 when `x12val` cannot be imported or the samples
folder is missing.
## How it uses x12val
The server imports `x12val` from `X12VAL_PATH` (put at the front of `sys.path` once) and copies none
of its code. It calls `parser.parse_text` and `find_delimiters`, `validator.validate`, `RULES`,
`SET_SPECS` and `total`, `rules_config.parse_config`, `explain.explain`, `ack.build_997`, and
`diff.diff_texts`. `tools/make_samples.py` builds the samples with `x12val.builder`.
`validate_x12` passes all sources to one `validate` call, so DUP001 and ENV009 work across files.
## Design notes
- One module per job: `server.py` (tools, resources, prompt), `dictionary.py` (segment and element
names), `gs1.py` (runs the `gs1` binary), `settings.py` (environment and the x12val import).
- Tools never take a file path and never write a file. A sample name must match
`^[A-Za-z0-9_.-]{1,64}$` and be in `samples/manifest.json`; the path comes from the manifest.
- The GS1 tools run `gs1` with `subprocess.run([...], shell=False, timeout=10)`. Exit code 1 means
invalid data, not a tool failure. No check-digit or AI-table code lives here.
- HTTP mode binds 127.0.0.1 only and has no `--host` option.
- Tests use the in-process client (`async with Client(build_server(settings))`), plus one stdio and one
HTTP subprocess test. Run: `python -m unittest discover -s tests -t .`
- Regenerate the samples: `python tools/make_samples.py`.
## Limits
- Only the six transaction sets x12val supports (940, 945, 846, 947, 856, 944) in the 004010-style
layout it reads. The tool does not check a trading partner's implementation guide.
- No authentication: stdio is local, and HTTP binds to localhost only.
- No AS2/SFTP, no sending of EDI, no new validation rules (they belong in x12val or gs1-toolkit-rs).
- The `gs1` output shape that `gs1.py` reads (text for `check`, JSON results for `parse --json` and
`dl parse --json`, data passed as arguments) is not verified against the real binary in this
repository. The stub in `tests/gs1_stub.py` follows that assumed shape; the CI-only test class runs
the same inputs on the real binary.
- The element dictionary covers the segments x12val checks, the envelope, and the segments in the
samples. Other segments show `(not in dictionary)`.
All data in this repository is synthetic: "Acme Distribution", `DEMO-1001`, and similar fake values.
## References
- Model Context Protocol specification, version 2026-07-28. The server uses the protocol version that
the pinned `mcp` SDK 2.3.0 negotiates.
- ASC X12 transaction sets 940, 945, 846, 947, 856, 944, 997 (version 004010 layout).
- GS1 General Specifications, Release 26.0 (January 2026), read through gs1-toolkit-rs.
## License
MIT. See [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues