Built an Agentic RAG service that searches national legal information through a hybrid-search knowledge base and MCP.
Context
- Built legalQ, a public beta service where anyone can ask about Korean law in everyday language, such as whether an act is illegal or what the fine is, and get the relevant articles with sources.
- Drew on legalize-kr, an open repository of about 3,046 Korean statutes as Markdown files with Git history, and a Seahorse vector DB loaded from the same files.

Statute header
Name, ministry, promulgation and effective dates, and a source link, taken from the file frontmatter.
Verbatim articles
Article and paragraph numbers kept as written, with no summary or paraphrase.
Articles 26, 27, 28, 30, 31, and 33 omitted

Procedure table
The deadlines in the quoted articles, step by step.
Scope note and disclaimer
Ends with a scope caveat, a disclaimer, and a link to the National Law Information Center.
Problem
Scroll sideways to see the whole diagram
Components
- Before (one vector search): Question (Question); Hybrid search, one call; 3 chunks, 1,000 characters each; Answer (LLM call)
- After (agent with two sources): Question (Question); Agent, intent → route → verify (Agent loop); Vector DB, find by meaning; Statute file, exact current text; History, stats, commits, metadata; Verbatim answer, source, disclaimer (LLM call)
Connections
- Question (Before (one vector search)) to Hybrid search
- Hybrid search to 3 chunks
- 3 chunks to Answer
- Question (After (agent with two sources)) to Agent
- Agent with Vector DB
- Agent with Statute file
- Agent with History, stats
- Agent to Verbatim answer
REPEAT AS NEEDED
Pain points
- Chunks split articles (Hybrid search)
- Needs more than 3 (3 chunks)
- No revision history (Answer)
- People ask in everyday words, such as "I bought something from overseas; how is it taxed?", and the answer often spans several articles, related statutes, and enforcement decrees and rules.
- A non-agentic workflow that queried the vector DB once could not handle compound or everyday questions, and the vector DB held no revision history for questions about when a statute changed.
- Answers had to quote articles verbatim with their numbers and sources and reflect the latest amendments, since a paraphrased or outdated article misleads users.
My role
- Worked as an ML engineer and designed the overall architecture, the vector DB, and the agent's system prompt, and deployed the service.
- Loaded the statutes into the vector DB by article and paragraph, and designed an MCP server that gives the agent deterministic access to the statute repository.
Approach
- Connected two knowledge sources to one agent through MCP, using the vector DB to discover articles by meaning and the Markdown repository for exact current text, revision history, and statistics.
- Expanded the vector DB from 12 traffic and corporate statutes to all of about 3,046, and re-indexed it by article and paragraph with statute and article metadata for Dense/BM25 hybrid search.
- Wrote a system prompt that classifies each question into one of six intents and routes it to the vector DB, the statute file, its addenda, or file metadata. Instructed the agent to re-check the latest text when a question signals recency and to fall back to keyword search when vector results miss.
- Put the repository's folder layout, file naming rules, and writing conventions into the prompt, so the agent builds file paths and finds articles without searching.
Key decisions
| Decision | Why | Trade-off |
|---|---|---|
| Two complementary sources | The vector DB finds articles from everyday words, and the repository supplies the exact current text, history, and statistics that the vector DB lacks. | Two sources to keep in sync and more routing rules. |
| Intent routing inside one agent's prompt | Multi-agent setups multiply token use, and one agent with intent rules answers simple questions in fewer calls. | A long system prompt that grows with every rule. |
| A repository map inside the prompt | The agent builds the file path and finds the article heading directly, skipping search calls. | The prompt must change when the repository layout changes. |
| Article-level chunks | Default 1,000-character chunks split or merged articles and forced extra adjacent-chunk calls, while one row per article answers citation queries in one hit. | A custom parser and re-indexing whenever statutes change. |
Two knowledge sources
| Criterion | Vector DB (Seahorse) | Repository (legalize-kr) |
|---|---|---|
| Role in the agent | Discover: find where the article is by meaning | Verify: exact current text, history, statistics |
| Search | Dense/BM25 hybrid | Keyword search, file by path |
| Everyday words | Strong | Weak, needs legal terms |
| Context cost | Only the matching chunks | Whole file; large statutes read in parts |
| Revision history | None, current text only | From Git commits |
| Freshness | Periodic re-index, may lag | Latest commit |
| Build cost | High: chunk, embed, load | Low: connect the repository |
The agent finds articles with the vector DB and confirms them in the repository.
System prompt
Scroll sideways to see the whole diagram
Components
- Multi-turn, per-turn intent; Normalize, 4 rules; Fallback #0, appendix stop; Intent classes, 6 intents; Routing table, 6 rows; Workflows, 5 task types; Recency check, 17 keywords; Fallback #1–#5, no hits, errors; Output format, quote, source; Knowledge: tool guide, repository layout and schema, read by retrieval and verification (Reference knowledge); Exceptions (12 cases), not found, ambiguous, API errors …
Connections
- Knowledge: tool guide, repository layout and schema back to Workflows
- Knowledge: tool guide, repository layout and schema back to Fallback #1–#5
Each box = one section of the system prompt, second line = rules it holds
Scroll sideways to see the whole diagram
Repository (legalize-kr): kr/, 근로기준법/, 법률.md, 시행령.md, 시행규칙.md, 민법/, 법률.md, 도로교통법/, 법률.md …
about 3,046 statute folders
folder = act, decree, and rule set
One statute file: kr/근로기준법/법률.md
- Metadata (header and stats): 제목: 근로기준법 / 법령구분: 법률 / 소관부처: [고용노동부] / 공포일자: 2026-08-04 / 시행일자: 2026-10-02 / 상태: 시행 / 출처: law.go.kr/법령/근로기준법
- Article heading (found without search): ## 제23조 (해고 등의 제한) / ① 사용자는 근로자에게 정당한 이유 없이 …
- Addenda marker (source for history): ## 부칙 / 부칙 <제{번호}호,{공포일자}>
Commit message: 법률: 근로기준법 (일부개정). one amendment = one commit → compare
Architecture
Scroll sideways to see the whole diagram
Components
- Users: legalQ web chat, desktop, mobile (User); Chat history, encrypted in browser
- Agent server: Guardrails, rate, concurrency, PII; Agent, tool loop, up to 8 turns (Agent, LLM); Claude Sonnet 4.6 (Agent, LLM); System prompt, intents, routing, repo map (System prompt)
- Seahorse MCP: search; adjacent chunks
- GitHub MCP: search_code; get_file_contents; list_commits
- Knowledge sources: Seahorse vector DB, Dense/BM25 hybrid, article chunks; legalize-kr repo, about 3,046 statutes as Markdown, Git history; Re-index by article; Source: law.go.kr (Upstream source)
Connections
- legalQ web chat with Agent
- Agent with Seahorse MCP
- Agent with GitHub MCP
- Seahorse MCP with Seahorse vector DB
- GitHub MCP with legalize-kr repo
- legalize-kr repo to Re-index by article
- Re-index by article to Seahorse vector DB
- Source: law.go.kr to legalize-kr repo
Service flow
Scroll sideways to see the whole diagram
Components
- Question (Conditional call); Preprocess, normalize names; Appendix?; Point to law.go.kr, stop, no more calls; Classify intent, 6 intents (LLM step); Verbatim quote, source, disclaimer, article, paragraph, and item numbers as written (LLM step)
- Vector DB (Seahorse MCP): search, by meaning; search, if 404, check name (Conditional call); search, "<statute> addenda"; search, if by topic (Conditional call)
- Repository (GitHub MCP): get_file_contents, if recency signal (Conditional call); search_code, if empty or off-topic (Conditional call); get_file_contents, straight to the path; get_file_contents, else parse addenda (Conditional call); list_commits → get_file_contents ×2, two commits by date; get_file_contents ×N, one call per statute; search_code, frontmatter (approx.)
Connections
- Question to Preprocess
- Preprocess to Appendix?
- Appendix? to Point to law.go.kr
- Appendix? to Classify intent
- search (Vector DB (Seahorse MCP)) to get_file_contents (Repository (GitHub MCP))
- search (Vector DB (Seahorse MCP)) back to search_code (Repository (GitHub MCP))
- get_file_contents (Repository (GitHub MCP)) back to search (Vector DB (Seahorse MCP))
- search (Vector DB (Seahorse MCP)) back to get_file_contents (Repository (GitHub MCP))
- get_file_contents ×N back to search (Vector DB (Seahorse MCP))
- Vector DB (Seahorse MCP) to Verbatim quote, source, disclaimer
- Repository (GitHub MCP) to Verbatim quote, source, disclaimer
Meaning search
default
Article lookup
Revision history
Compare two dates
Compare statutes
Statistics
Outcome
- Opened legalQ to the public as a beta that answers everyday legal questions with verbatim articles, sources, and a disclaimer.
- Widened search coverage from 12 statutes in the first vector DB to all of about 3,046 in the repository.
- Refined the system prompt over six revisions, covering revision history, multi-turn follow-ups, identity protection, and rejection of personal data in input.