kftc-decisions
by hyeokyun-kr
README.md
# KFTC Decision Search
[한국어](README.ko.md)
Full-text search over every decision the Korea Fair Trade Commission (KFTC) publishes, running on your own machine. KFTC Decision Search downloads the decision list and PDFs from the KFTC case system (case.ftc.go.kr), extracts the text, masks text that the public PDFs left under black redaction boxes, and builds one SQLite file. You search it from the command line or let an AI assistant search it through an MCP server.
- **Coverage:** 20,331 decision PDFs from 1981 to 2026 (21,760 list rows; several cases can share one decision), 208,773 pages, as of November 2026.
- **Local:** search terms and questions never leave your machine. The only network traffic is collecting from case.ftc.go.kr.
- **Three MCP tools:** `index_info`, `search_decisions` (full text, with page numbers and surrounding sentences) and `read_decision` (outline or selected pages).
- **Monthly update:** picks up decisions that were published late, which is common (median 59 days after the decision, longest 634 days in 2025–26).
- **Ships no decision text.** You build your own index with the collector.
## Why
KFTC decisions are also searchable through the law.go.kr Open API, which is what [korean-law-mcp](https://github.com/chrisryugj/korean-law-mcp) uses. Its KFTC tool sends only the query, so law.go.kr searches **case names**. Questions about reasoning or penalty calculation return nothing. law.go.kr also has a full-text mode (`search=2`), but it splits a query into loose word parts.
Same queries, 2026-11-26:
| Query | law.go.kr, case names | law.go.kr, full text | …of which contain the term (sampled) | This index, fast | This index, exact |
| --- | ---: | ---: | ---: | ---: | ---: |
| 독립경영 (independent management) | 0 | 423 | 0/20 | 44 | 83 |
| 관련매출액 계약금액 (relevant sales, contract value) | 0 | 1,973 | 7/20 | 773 | 812 |
| 정상가격 부당지원 (arm's-length price, undue support) | 0 | 282 | 3/20 | 132 | 141 |
| 정보교환 (information exchange) | 0 | 585 | 4/20 | 212 | 276 |
| 감면 취소 (leniency revoked) | 0 | 36 | 20/20 | 123 | 144 |
| 조사 협력 감경 (reduction for cooperation) | 0 | 890 | 20/20 | 1,600 | 1,746 |
| 지정자료 허위 (false designation data) | 64 | 234 | 5/20 | 84 | 86 |
All 11 queries are in [eval/](eval/). Case-name search returned 0 for 9 of them. law.go.kr full text holds 8,330 KFTC decisions, mostly from 2011 on; this index starts in 1981. In a 20-hit sample per query, all 220 hits of this index contained the query words.
## How it works
```mermaid
flowchart LR
S[(case.ftc.go.kr)] -->|list pages| L[list collect / build<br/>decisions.jsonl]
L --> P[pdf plan / collect<br/>20,331 PDFs]
P --> T[text run<br/>PDFKit]
T --> M[match<br/>case no. in text?]
T --> R[redact<br/>text under black boxes]
M --> I[(index<br/>decisions.sqlite)]
R --> I
I --> C[search CLI]
I --> X[MCP server]
I --> H[KR Public Data Hub<br/>decision tab]
```
1. **List.** The case system shows 10 rows per page. The collector pages through the full list and each of the 21 violation-type filters. Rows that share a decision date come back in a different order on each request, so rows repeat or go missing at page boundaries. Dates with duplicates are fetched again one day at a time until the day's count matches the site.
2. **PDFs.** One request at a time. When the session expires (about every 10 minutes) and the site answers with a page instead of a PDF, a new session is opened.
3. **Text.** macOS PDFKit, one `.txt` per PDF with `=== p.N ===` page markers.
4. **Match.** The first 4,000 characters must contain the case number, the decision number or the respondent's name. Files that fail go to `text/review_<date>.tsv`; almost all are site-side problems such as "full text withheld" notices or another case's document attached.
5. **Redact.** Some public PDFs put a black box over text that is still in the text layer, so text extraction returns what the box hides. Every page is rendered, filled black rectangles are located, and characters at least half inside one are recorded by position. The ledger stores positions and a page fingerprint, never the hidden characters. The index writes ■ in their place. When it fails, it hides: a page whose ledger entries cannot be verified against the text is left out of the index, and so is every page of a decision that was not scanned.
6. **Index.** SQLite FTS5 (unicode61 tokens, prefix match) plus zlib-compressed page text, about 310 MB for 208,773 pages.
7. **Search.** *Fast* uses the index: the start of each word must match. It misses terms glued inside longer words (독립경영 inside 친족독립경영) and terms written with a space (정보 교환). *Exact* scans every page with spaces and punctuation removed and finds those too, in about 8 seconds.
## Quick start
Python 3.10 or later. Text extraction and the redaction check need macOS with the Xcode Command Line Tools (`xcode-select --install`). Searching and the MCP server run anywhere once you have an index.
```bash
git clone https://github.com/hyeokyun-kr/kftc-decision-search
cd kftc-decision-search
python3 -m venv .venv && .venv/bin/pip install -e ".[mcp]"
.venv/bin/kftc all # list → PDFs → text → match → redact → index, into ./data
.venv/bin/kftc search '"부당한 공동행위" 관련매출액 -하도급'
.venv/bin/kftc update # later, about once a month
```
The first full run is estimated at 10–12 hours with the default one-second pause between requests, almost all of it PDF downloads (the original collection took 6 hours for the PDFs with a 0.3-second pause), and needs about 3.7 GB (PDFs 3.0 GB, text 0.4 GB, index 0.3 GB). Every step can be stopped and resumed by running the same command again. Steps can also be run one by one: `kftc list collect`, `list build`, `pdf plan`, `pdf collect`, `text run`, `match`, `redact`, `index`. Use `--data <folder>` or `KFTC_DATA_DIR` to put the data elsewhere. Tests: `.venv/bin/pip install -e ".[test]" && .venv/bin/python -m pytest`.
## MCP server
```bash
.venv/bin/kftc mcp # stdio
.venv/bin/kftc mcp --http 8792 # Streamable HTTP on 127.0.0.1
```
Claude Code:
```bash
claude mcp add kftc-decisions -e KFTC_DATA_DIR=/path/to/data -- /path/to/.venv/bin/kftc mcp
```
Claude Desktop and other MCP clients:
```json
{
"mcpServers": {
"kftc-decisions": {
"command": "/path/to/.venv/bin/kftc",
"args": ["mcp"],
"env": {"KFTC_DATA_DIR": "/path/to/data"}
}
}
}
```
| Tool | What it returns |
| --- | --- |
| `index_info` | Count, years, build date, and the violation and remedy types to filter by |
| `search_decisions` | Matching decisions: decision and case numbers, date, name, pages with hits, two snippets with page numbers, link to the KFTC record. Filters: violation type, remedy, year range. `exact=true` for a literal scan. Suggests next steps when there are fewer than three hits |
| `read_decision` | By decision or case number. Without `pages`: outline with the pages where the query appears (up to 40 listed, with the total). With `pages` (`3`, `3-5`, `1,12-14`): page text, up to 10 pages or 40,000 characters |
Query syntax for both: space-separated words must all appear, `"quoted phrase"` must appear as written, `-word` excludes decisions containing it.
## Verification
The rows down to *MCP server* checked the author's original pipeline, from which this repository was extracted. The last row checks that this repository's code gives the same results on the same inputs.
| What | Check | By | Result |
| --- | --- | --- | --- |
| List (2026-11-13) | Rows collected vs. site total; 60 rows across decades re-searched on the site | Author | 21,752 = 21,752; 60 of 60 matched on all 7 compared fields (spaces ignored) |
| PDFs (2026-11-16) | Downloads, PDF header and end marker | Author | 20,323 files, 0 failures, all with both markers |
| PDF vs. case | Case number, decision number or name in the first 4,000 characters | Author, script | 20,230 matched. The other 93 opened by hand: 2 fine, 91 site-side (54 withheld-text notices, 21 another case's document, 12 damaged, 2 DRM, 1 test file, 1 empty), 0 download errors |
| Index (2026-11-17) | Re-read all text files independently of the indexer | Author, separate script | Page and character totals equal; 300 random pages identical; 12 terms: 24,145 of 24,187 term–decision matches found (99.83%), 0 false hits |
| Redaction (2026-11-26) | 32 flagged spots cropped and viewed, 8 per size band | Author | 32 of 32 were text under a black box (3 of them ■-shaped bullets); 0 false positives |
| Monthly update (2026-11-25) | First run, then site total re-counted and each new decision searched on the site | Author, separate script | 8 min 30 s, 8 new decisions, 8 of 8 confirmed |
| MCP server | 19 tool calls on the full index | Author | 19 of 19 pass |
| This repository (2026-11-30) | Same inputs through this code vs. the original pipeline | Author, script | List 21,752 of 21,752 rows identical; PDF plan 20,323 of 20,323; index 20,331 decisions in every column and 208,773 pages identical; text of 20 sampled PDFs byte-identical; redaction scan of 50 sampled PDFs 1,424 of 1,424 positions identical; the 19 MCP checks pass on this server; one live PDF download byte-identical; live `update --dry-run`: 173 list requests, site total 21,764 = 21,760 known + 4 new, no data files written. A full `update` has been run only against a stub site (offline test) |
The offline test suite (43 tests) builds an index from made-up decisions and runs search, masking, the hide-on-failure rules, the CLI and all MCP tools. On macOS it also writes PDFs with a black box over a number, checks that extraction finds the number and the index does not, and runs a full `update` against a stub site.
## Limitations
- Only decisions the KFTC case system publishes. Press releases, examiner reports and court rulings are not included. Some files on the site are notices that the full text is withheld.
- Text extraction and the redaction check use macOS PDFKit. On other systems you can search an index built elsewhere.
- The redaction check misses boxes narrower than 8 pt (one or two characters), dark grey rather than black fills, marks placed beside rather than over the text, and characters less than half covered.
- The data folder keeps the original PDFs and extracted text, including anything under black boxes, and the ledger records where it is. Share the index file, not the data folder.
- Fast search misses terms inside longer words; use `exact=true` when you need every case.
- Scraping depends on the current layout of case.ftc.go.kr.
## Site etiquette
Requests go one at a time with a one-second pause (`KFTC_LIST_DELAY`, `KFTC_PDF_DELAY`), back off 15, 60 and 180 seconds on errors, stop after ten failures in a row, and identify the tool with a User-Agent that links to this repository. Saved list pages are reused so a rebuild does not ask the site again.
## Use with KR Public Data Hub
The decision tab of [KR Public Data Hub](https://github.com/hyeokyun-kr/kr-public-data-hub) reads the same file. Point `DATAHUB_DECISION_DB` to `data/index/decisions.sqlite`.
## License
MIT for the code. Decisions are published by the Korea Fair Trade Commission; this repository contains none of them.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues