Two weeks ago I posted my open-source project on DEV.to: CRM Insight Agent — an enterprise analytics agent that lets AI query a 300+ table legacy CRM database without letting it write DELETE, UPDATE, or DROP. No prompt-engineering Text2SQL, no fine-tuned LLM. It compiles SQL deterministically from YAML metric definitions.
Then a stranger from Somerset commented.
The Stranger from Somerset
His name is Tom Zafred. Founder of Biomed Systems Limited in the UK — they build Biobank & LIMS platforms and zero-dependency MCP governance toolchains for AI agents. He didn't come to pitch. He came with a rake.
"MySQL's ONLY_FULL_GROUP_BY has mugged me in dark alleys before." — Tom Zafred
Over 48 hours he gave me six recommendations. Three landed in GitHub Issues. One was a real SQL bug that Mockito tests let pass. Here's what happened.
The Six Recommendations
1. Reject Leading % on Indexed LIKE Columns
Problem: LIKE '%keyword' forces a full table scan, defeats BTREE indexes. We had a regex guard (ReadOnlySqlGuard) that checks for DELETE/DROP, but nothing stops the AI from writing slow queries that pin CPU at 100%.
Fix: Added WildcardPositionCheck — a compile-time gate that rejects leading % and _ but allows suffix wildcards (index-friendly). Covers member_name, member_allot_staffName, and any other indexed columns declared in our metadata YAML.
// WildcardPositionCheck.java — the core logic
private void checkLeadingWildcard(String column, String pattern) {
if (pattern.startsWith("%") || pattern.startsWith("_")) {
throw new AdhocException("ADHOC_SLOW_QUERY",
"LIKE 前导通配符触发全表扫描,已拒绝: " + column + " = '" + pattern + "'");
}
}
Commit: f299dac | Issue #1 — CLOSED
Problem: Our @Tool descriptions bloated to ~50KB because we embedded all 50+ set_level codebooks (store level codes, member tier codes, expense categories). Every MCP client pulls this entire payload on connection.
Fix: Moved the codebooks to an MCP Resource (crm://levels/set_level-codes) pulled on demand via SyncResourceSpecification. Spring AI 1.1 already supports this — we just didn't know.
Result: tool-definitions.json 50KB → 8KB
// ToolConfiguration.java — registering the resource
@Bean
public SyncResourceSpecification levelCodesResource() {
return SyncResourceSpecification.builder()
.uri("crm://levels/set_level-codes")
.name("set_level codebooks")
.mimeType("application/json")
.handler(request -> loadFromYaml("levels/set_level.yaml"))
.build();
}
Commit: fdc8836 | Issue #2 — CLOSED
Problem: We declare JOIN cardinalities (to_one / to_many) manually in metadata YAML. What if we get it wrong? Tom provided an INFORMATION_SCHEMA SQL to auto-verify.
Design decision: Only proves to_one (has unique index = to_one ✅). Never disproves to_many (no unique index → trust YAML, don't warn). Business logic ≠ DDL — member_mobile is one-to-one in business but declared as regular INDEX, not UNIQUE, in our CRM schema.
Commit: 2cd618d | Issue #3 — CLOSED
4. Pair STATISTICS with KEY_COLUMN_USAGE for Composite FKs
Problem: Our validator correctly skips member_mobile (no UNIQUE), but also can't handle expense_orderId — it's unique only when paired with expense_d_state (composite index). Tom suggested pairing INFORMATION_SCHEMA.STATISTICS with KEY_COLUMN_USAGE edges to map multi-hop FK cardinality.
Status: Pending evaluation. Discussion opened on GitHub.
5. The SQL Bug — SEQ_IN_INDEX Tautology
This is the one that hurt.
Our buggy SQL:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND NON_UNIQUE = 0
AND SEQ_IN_INDEX = 1 ← FILTER FIRST
GROUP BY TABLE_NAME, INDEX_NAME, COLUMN_NAME
HAVING COUNT(DISTINCT COLUMN_NAME) = 1 ← ALWAYS TRUE after filter
Why it's broken: SEQ_IN_INDEX = 1 in WHERE drops composite index rows (expense_orderId, expense_d_state) down to just (expense_orderId). Then HAVING COUNT(*) = 1 on a single-row group is a tautology — always passes. Mockito tests only had single-column indexes, so it slipped through.
Tom's correct version:
SELECT TABLE_NAME, MIN(COLUMN_NAME) AS key_col
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND NON_UNIQUE = 0
GROUP BY TABLE_NAME, INDEX_NAME ← don't include COLUMN_NAME
HAVING COUNT(*) = 1 ← COUNT(*) proves single-column
No WHERE filter on SEQ_IN_INDEX. Let all rows into GROUP BY. COUNT(*) = 1 means single-column unique. COUNT(*) > 1 means composite.
Commit: 2fc8483
6. Move the Discussion to GitHub, Not Forem
Problem: Deep technical debates on Forem get buried nine levels deep where only lost web crawlers will find them. Tom suggested opening a GitHub Discussion for the validateMeasureGrain() comparison.
Result: Discussion #4 opened — 40 lines of our validator vs Tom's adversarial model (his proprietary gateway stays behind the company curtain).
What Changed
| Metric | Before | After |
| GitHub Issues open | 3 | 0 (all closed) |
| tool-definitions.json | 50KB | 8KB |
| CI status | passing but flaky | green across all 4 iterations |
| Documented design decisions | 2 | 8 (METHODOLOGY.md + CASE_STUDY.md) |
| SQL bugs | 1 (tautology) | 0 |
| Peer review | None | Tom Zafred (Biomed Systems) |
What I Learned
- Don't trust Mockito for SQL tests. If your validator hits
INFORMATION_SCHEMA, run it against a real MySQL instance with composite indexes.
- MCP Resources > bloated
@Tool descriptions. Anything over 10KB belongs in a Resource, not a description.
- Business uniqueness ≠ DDL uniqueness. Your DBA probably knows something your metadata YAML doesn't.
- Peer review from strangers beats self-review. Four pairs of eyes on INFORMATION_SCHEMA SQL caught a bug three of ours missed.
Links
Built with Spring Boot 3.5.6, Spring AI 1.1.2, MCP Java SDK 0.17.0, and way too much caffeine in Harbin. ☕️