File size: 26,474 Bytes
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb72a5
3fbbaab
 
 
 
1b56ed2
 
 
 
 
 
3fbbaab
1b56ed2
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb72a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
fdb72a5
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
3fbbaab
 
1b56ed2
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
3fbbaab
 
1b56ed2
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
1b56ed2
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b56ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env python3
"""

mcp_add.py -- Add MCP server records to the wiki catalog with intake gate.



Records are catalog-only (no local install). Sources are merged idempotently

so the same record from two different harvesters yields one page with both

sources listed.



Usage

-----

    # Single record from JSON file

    ctx-mcp-add --from-json /path/to/record.json



    # JSONL from file (one JSON object per line)

    ctx-mcp-add --from-jsonl /path/to/records.jsonl



    # JSONL from stdin

    ctx-mcp-add --from-stdin



    [--dry-run] [--wiki PATH] [--skip-existing]

"""

import argparse
import json
import os
import re
import sys
from dataclasses import replace
from datetime import datetime, timezone
from pathlib import Path
from typing import Any

import yaml  # type: ignore[import-untyped]

from ctx.core.entity_update import build_update_review, render_update_review
from ctx_config import cfg
from intake_pipeline import IntakeRejected, check_intake, record_embedding
import mcp_canonical_index
from mcp_entity import McpRecord
from wiki_batch_entities import generate_mcp_page
from ctx.core.wiki.wiki_sync import append_log, ensure_wiki, update_index
from ctx.core.wiki.wiki_queue import enqueue_entity_upsert
from ctx.core.wiki.wiki_utils import validate_skill_name
from ctx.utils._fs_utils import reject_symlink_path, safe_atomic_write_text

TODAY = datetime.now(timezone.utc).strftime("%Y-%m-%d")

# Relative root for MCP server entity pages inside the wiki
_MCP_ENTITY_SUBDIR = "entities/mcp-servers"


def _build_corpus_text(record: McpRecord) -> str:
    """Build a SKILL.md-shaped corpus blob for the intake gate + embedding.



    The intake gate's structural check expects YAML frontmatter with

    ``name`` + ``description`` plus an H1 and H2 in the body — the

    same shape skills and agents present. We synthesize a minimal

    page from the McpRecord so the same gate that polices skills and

    agents also polices MCPs without the gate needing per-type logic.



    The synthesized text is used only for intake (gate + embedding

    cache); the actual entity page that lands on disk is the richer

    output of ``generate_mcp_page``.



    Frontmatter is rendered via ``yaml.safe_dump`` rather than f-string

    interpolation. Descriptions and slugs flow from untrusted README

    content in the awesome-mcp parser; a description containing

    ``\\n---\\n`` or ``\\nname: evil`` would otherwise corrupt the

    synthesized YAML before the intake gate sees it. Body sections are

    plain markdown with no parser-significant interpolation.

    """
    tags_line = " ".join(record.tags) if record.tags else "none"
    transports_line = " ".join(record.transports) if record.transports else "unknown"
    sources_line = " ".join(record.sources) if record.sources else "none"

    fm_yaml = yaml.safe_dump(
        {"name": record.slug, "description": record.description},
        default_flow_style=False,
        allow_unicode=True,
        sort_keys=False,
    )

    return (
        "---\n"
        f"{fm_yaml}"
        "---\n\n"
        f"# {record.name}\n\n"
        "## Overview\n\n"
        f"{record.description}\n\n"
        "## Tags\n\n"
        f"{tags_line}\n\n"
        "## Transports\n\n"
        f"{transports_line}\n\n"
        "## Sources\n\n"
        f"{sources_line}\n"
    )


def _merge_sources(existing_fm: dict[str, Any], new_sources: tuple[str, ...]) -> list[str]:
    """Return a sorted, deduplicated union of existing and new sources."""
    existing: list[str] = existing_fm.get("sources", []) or []
    merged = sorted(set(existing) | set(new_sources))
    return merged


def _parse_frontmatter(text: str) -> dict[str, Any]:
    """Extract YAML frontmatter from a markdown page. Returns {} on failure."""
    match = re.match(r"^---\n(.*?\n)---\n", text, re.DOTALL)
    if not match:
        return {}
    try:
        data = yaml.safe_load(match.group(1))
        return data if isinstance(data, dict) else {}
    except yaml.YAMLError:
        return {}


def _keep_longer_description(existing_fm: dict[str, Any], record: McpRecord) -> str:
    """Return whichever description is longer — existing page's or the record's."""
    existing_desc: str = existing_fm.get("description", "") or ""
    return existing_desc if len(existing_desc) >= len(record.description) else record.description


def _rewrite_frontmatter(page_text: str, new_fm: dict[str, Any]) -> str:
    """Replace the YAML frontmatter block in *page_text* with *new_fm*."""
    body_match = re.match(r"^---\n.*?\n---\n(.*)", page_text, re.DOTALL)
    body = body_match.group(1) if body_match else page_text
    fm_str = yaml.safe_dump(new_fm, default_flow_style=False, allow_unicode=True, sort_keys=False)
    return f"---\n{fm_str}---\n{body}"


def _tuple_from_frontmatter(raw: object, fallback: tuple[str, ...]) -> tuple[str, ...]:
    if isinstance(raw, str):
        return tuple(item.strip() for item in raw.split(",") if item.strip())
    if isinstance(raw, (list, tuple, set, frozenset)):
        return tuple(str(item).strip() for item in raw if str(item).strip())
    return fallback


def _optional_text_from_frontmatter(raw: object, fallback: str | None) -> str | None:
    if raw is None:
        return fallback
    text = str(raw).strip()
    return text or fallback


def _render_existing_update_page(

    *,

    existing_fm: dict[str, Any],

    record: McpRecord,

    merged_sources: list[str],

    kept_description: str,

) -> str:
    updated_fm = {
        **existing_fm,
        "sources": merged_sources,
        "description": kept_description,
        "updated": TODAY,
    }
    render_record = replace(
        record,
        name=str(updated_fm.get("name") or record.name),
        description=kept_description,
        sources=tuple(merged_sources),
        github_url=_optional_text_from_frontmatter(
            updated_fm.get("github_url"), record.github_url
        ),
        homepage_url=_optional_text_from_frontmatter(
            updated_fm.get("homepage_url"), record.homepage_url
        ),
        tags=_tuple_from_frontmatter(updated_fm.get("tags"), record.tags),
        transports=_tuple_from_frontmatter(
            updated_fm.get("transports"), record.transports
        ),
    )
    rendered_text = generate_mcp_page(render_record)
    return _rewrite_frontmatter(rendered_text, updated_fm)


def _normalize_github_url(url: str | None) -> str | None:
    """Return the canonical lowercase GitHub URL, or None for non-GitHub inputs.



    Mirrors ``McpRecord.canonical_dedup_key`` so an existing-entity scan

    can match against new records on the same key. We strip trailing

    slashes and lowercase host+path because GitHub URLs are case-

    insensitive at the host but display case is preserved by the parser.

    """
    if not url:
        return None
    candidate = url.strip().rstrip("/").lower()
    if not candidate.startswith(("http://github.com/", "https://github.com/")):
        return None
    return candidate


def _scan_for_github_url(mcp_dir: Path, target: str) -> Path | None:
    """Walk every entity page looking for a canonical github_url match.



    O(n) fallback used when the canonical-index sidecar misses or

    returns a stale entry. ``target`` must already be normalized — this

    function does not re-normalize.

    """
    for page in mcp_dir.rglob("*.md"):
        # Skip sidecar files (``.canonical-index.json`` is not .md but
        # a future hidden .md would be caught here).
        if page.name.startswith("."):
            continue
        try:
            text = page.read_text(encoding="utf-8", errors="replace")
        except OSError:
            continue
        # Fast path: grep the text for the URL before parsing YAML.
        if target not in text.lower():
            continue
        fm = _parse_frontmatter(text)
        if _normalize_github_url(fm.get("github_url")) == target:
            return page
    return None


def _find_existing_by_github_url(

    mcp_dir: Path, target_github_url: str | None

) -> Path | None:
    """Return the path of an existing entity page for ``target_github_url``.



    Phase 3.6 cross-source dedup: when awesome-mcp and pulsemcp both

    catalog the same upstream repo, their slugs differ, and the slug-

    based existence check in ``add_mcp`` would create two separate

    entities. This helper finds the existing entity by its canonical

    github_url so the second source can merge into it instead.



    Phase 6b: canonical-index sidecar turns the O(n) scan into O(1)

    lookup on the hot path. The index is a *cache*, not authoritative:



    - Hit + file exists  -> return the indexed path.

    - Hit + file missing -> stale entry; scan and repair (upsert the

      scan result, or remove the stale mapping if nothing matches).

    - Miss               -> scan once; on hit, repair by upserting.

    - No github_url      -> return None (indexing github-only URLs).

    """
    target = _normalize_github_url(target_github_url)
    if target is None or not mcp_dir.is_dir():
        return None

    index = mcp_canonical_index.load_index(mcp_dir)
    # Distinguish "true miss" (no entry) from "stale hit" (entry exists
    # but points at nothing/something else). lookup() collapses both to
    # None which hides the information we need for repair decisions.
    raw_entry = index["by_github_url"].get(target)
    cached = mcp_canonical_index.lookup(mcp_dir, target, index=index)
    if cached is not None:
        # Defensive confirm: the indexed file might have been manually
        # edited to point at a different repo. If the stored github_url
        # still matches the target, we trust it; otherwise fall through
        # to the scan path and treat this as a stale entry.
        try:
            fm = _parse_frontmatter(cached.read_text(encoding="utf-8", errors="replace"))
            if _normalize_github_url(fm.get("github_url")) == target:
                return cached
        except OSError:
            pass  # Scan-and-repair below handles this.

    # Miss or stale hit: fall back to the authoritative scan.
    hit = _scan_for_github_url(mcp_dir, target)
    if hit is not None:
        # Repair the index so the next call is O(1).
        try:
            relpath = hit.relative_to(mcp_dir).as_posix()
            slug = hit.stem
            mcp_canonical_index.upsert(
                mcp_dir, target, slug=slug, relpath=relpath, index=index
            )
        except (OSError, ValueError):
            # Index-repair failure must not block the dedup decision;
            # the next successful add will get another chance to repair.
            pass
        return hit

    # Nothing on disk anywhere. If the index had a stale entry, drop it
    # so future lookups don't keep paying the scan cost.
    if raw_entry is not None:
        try:
            mcp_canonical_index.remove(mcp_dir, target, index=index)
        except OSError:
            pass
    return None


def add_mcp(

    *,

    record: McpRecord,

    wiki_path: Path,

    dry_run: bool = False,

    review_existing: bool = False,

    update_existing: bool = False,

) -> dict[str, Any]:
    """Add (or merge sources for) one MCP record into the wiki catalog.



    Flow:

      1. Validate slug.

      2. Compute target path: wiki/entities/mcp-servers/<first-letter>/<slug>.md

      3. If target exists -> SKIP intake gate, run merge directly. The

         merge path is for known-good entities; running intake here would

         flag the re-fetched record as DUPLICATE against its own existing

         embedding (cosine 1.0 >= dup_threshold 0.93) and block source

         merging from ever happening. Existence is the source of truth.

      4. If target does NOT exist -> run intake gate, then write a new

         entity page via generate_mcp_page(). Intake's similarity check

         here is meaningful: it catches near-duplicates *between distinct

         slugs* in the same source (e.g. two records that differ only in

         creator-prefix capitalisation).

      5. record_embedding (non-fatal). Only on first creation; re-merge

         doesn't touch the cache because the existing vector is correct.

      6. update_index + append_log (only when is_new_page).



    Args:

        record: Populated McpRecord dataclass instance.

        wiki_path: Absolute path to the wiki root directory.

        dry_run: Compute everything but skip writes and embeddings.

        review_existing: Return an update review instead of mutating existing pages.

        update_existing: Apply an existing-page update after review.



    Returns:

        dict with keys: slug, is_new_page, merged_sources, path

    """
    validate_skill_name(record.slug)

    entity_rel = record.entity_relpath()  # e.g. "f/fetch-mcp.md"
    mcp_dir = wiki_path / _MCP_ENTITY_SUBDIR
    target_path = mcp_dir / entity_rel

    # Phase 3.6: cross-source dedup by canonical github_url before the
    # slug-based check. When awesome-mcp and pulsemcp both catalog the
    # same upstream repo, their slugs differ — we want to merge into
    # the existing entity, not create a second one at our slug path.
    # Only fires when the new record carries a github_url; pulsemcp
    # listing-page records currently have only homepage_url (Phase 6
    # detail-page enrichment will populate github_url so this dedup
    # path becomes meaningful for them too).
    canonical_match = _find_existing_by_github_url(mcp_dir, record.github_url)
    if canonical_match is not None and canonical_match != target_path:
        target_path = canonical_match

    reject_symlink_path(target_path)
    target_path.parent.mkdir(parents=True, exist_ok=True)
    target_slug = target_path.stem

    is_new_page: bool
    merged_sources: list[str]
    decision = None  # Filled only on the new-record path; new entries
                     # carry their intake findings forward into the log.
    corpus_text = ""

    # Phase 1 of branching: compute the read-side state. No serialization
    # work happens here so dry-run cannot fail on a malformed existing
    # page — that's deferred to the write-gate below.
    if target_path.exists():
        # Existing entity → straight to merge. No intake call: the gate
        # would reject this as DUPLICATE against the cached embedding
        # of the original ingest, blocking the source-merge that's the
        # whole point of re-fetching. Phase 3b made this concrete.
        is_new_page = False
        existing_text = target_path.read_text(encoding="utf-8")
        existing_fm = _parse_frontmatter(existing_text)
        merged_sources = _merge_sources(existing_fm, record.sources)
        kept_description = _keep_longer_description(existing_fm, record)
    else:
        # New entity → intake gate applies. A DUPLICATE finding here
        # would mean a *different* slug has near-identical content,
        # which is a real signal we want to surface.
        corpus_text = _build_corpus_text(record)
        decision = check_intake(corpus_text, "mcp-servers")
        if not decision.allow:
            raise IntakeRejected(decision)

        is_new_page = True
        existing_text = ""
        existing_fm = {}
        merged_sources = sorted(record.sources)
        kept_description = record.description

    if is_new_page:
        final_text = generate_mcp_page(record)
    else:
        final_text = _render_existing_update_page(
            existing_fm=existing_fm,
            record=record,
            merged_sources=merged_sources,
            kept_description=kept_description,
        )

    if review_existing and not is_new_page and not update_existing:
        review = build_update_review(
            entity_type="mcp-server",
            slug=target_slug,
            existing_text=existing_text,
            proposed_text=final_text,
        )
        return {
            "slug": target_slug,
            "is_new_page": False,
            "merged_sources": merged_sources,
            "path": str(target_path),
            "skipped": True,
            "update_required": True,
            "update_review": render_update_review(review),
            "queued_job_id": None,
        }

    queue_job = None
    if not dry_run:
        # Phase 2 of branching: render and write. Any YAML serialization
        # failure now is a real error, not a dry-run side-effect.
        safe_atomic_write_text(target_path, final_text, encoding="utf-8")
        queue_job = enqueue_entity_upsert(
            wiki_path=wiki_path,
            entity_type="mcp-server",
            slug=target_slug,
            entity_path=target_path,
            content=final_text,
            action="created" if is_new_page else "updated",
            source="mcp_add",
        )

        # Phase 6b: keep the canonical sidecar index hot. Upsert on
        # every successful write so the first cross-source dedup after
        # this add is O(1) without needing a rebuild. Applies to both
        # new pages AND source merges — a merge can land a github_url
        # from the second source when the first source lacked one.
        # Index-write failures must not break the add; the next lookup
        # will scan-and-repair.
        canonical = _normalize_github_url(record.github_url)
        if canonical is not None:
            try:
                relpath = target_path.relative_to(mcp_dir).as_posix()
                mcp_canonical_index.upsert(
                    mcp_dir,
                    canonical,
                    slug=target_slug,
                    relpath=relpath,
                )
            except (OSError, ValueError) as exc:
                print(
                    f"Warning: failed to update canonical index for {record.slug}: {exc}",
                    file=sys.stderr,
                )

        # Embed + index + log only on first creation. Re-merging an
        # existing entity does not invalidate its embedding (content
        # is the same — sources are metadata, not corpus text), and
        # logging every re-merge would blow up log.md at scale.
        if is_new_page:
            try:
                record_embedding(
                    subject_id=record.slug,
                    raw_md=corpus_text,
                    subject_type="mcp-servers",
                )
            except Exception as exc:  # noqa: BLE001 — cache failure must not break catalog
                print(
                    f"Warning: failed to record intake embedding for {record.slug}: {exc}",
                    file=sys.stderr,
                )

            update_index(str(wiki_path), [target_slug], subject_type="mcp-servers")

            log_details = [
                f"Slug: {target_slug}",
                f"Path: {target_path}",
                f"Sources: {', '.join(merged_sources) if merged_sources else 'none'}",
                f"Tags: {', '.join(record.tags) if record.tags else 'none'}",
                f"Transports: {', '.join(record.transports) if record.transports else 'unknown'}",
            ]
            # ``decision`` is set whenever ``is_new_page`` is True (intake
            # ran on the new-record path). The assert is a typing invariant,
            # not a runtime guard.
            assert decision is not None
            warnings = decision.warnings
            if warnings:
                log_details.append(
                    "Warnings: " + "; ".join(f"{w.code}:{w.message}" for w in warnings)
                )
            append_log(str(wiki_path), "add-mcp", target_slug, log_details)

    return {
        "slug": target_slug,
        "is_new_page": is_new_page,
        "merged_sources": merged_sources,
        "path": str(target_path),
        "skipped": False,
        "update_required": False,
        "queued_job_id": queue_job.id if queue_job is not None else None,
    }


# ── CLI ───────────────────────────────────────────────────────────────────────


def _process_batch(

    records: list[dict[str, Any]],

    wiki_path: Path,

    dry_run: bool,

    skip_existing: bool,

    update_existing: bool,

    mcp_entity_dir: Path,

) -> tuple[int, int, int, int, int]:
    """Process records. Returns (added, merged, reviewed, rejected, errors)."""
    added = merged = reviewed = rejected = errors = 0
    total = len(records)

    for i, raw in enumerate(records, 1):
        slug = raw.get("slug", "<unknown>")
        try:
            record = McpRecord.from_dict(raw)
        except Exception as exc:  # noqa: BLE001 — batch CLI must not crash on one bad record
            errors += 1
            print(f"  [{i}/{total}] ERROR: {slug}: {exc}", file=sys.stderr)
            continue

        entity_rel = record.entity_relpath()
        target_path = mcp_entity_dir / entity_rel

        if skip_existing and target_path.exists():
            merged += 1
            print(f"  [{i}/{total}] [skipped] {record.slug}")
            continue

        try:
            result = add_mcp(
                record=record,
                wiki_path=wiki_path,
                dry_run=dry_run,
                review_existing=True,
                update_existing=update_existing,
            )
            if result["is_new_page"]:
                added += 1
                status = "added"
            elif result.get("update_required"):
                reviewed += 1
                status = "update-review"
                if result.get("update_review"):
                    print(result["update_review"])
            else:
                merged += 1
                status = "updated" if update_existing else "merged"
            print(f"  [{i}/{total}] [{status}] {record.slug}")
        except IntakeRejected as exc:
            rejected += 1
            codes = ", ".join(f.code for f in exc.decision.failures) or "unknown"
            print(f"  [{i}/{total}] [rejected] {record.slug}: {codes}", file=sys.stderr)
        except Exception as exc:  # noqa: BLE001 — batch CLI must continue past one failure
            errors += 1
            print(f"  [{i}/{total}] ERROR: {record.slug}: {exc}", file=sys.stderr)

    return added, merged, reviewed, rejected, errors


def main() -> None:
    """Entry point for the ctx-mcp-add CLI."""
    parser = argparse.ArgumentParser(
        description="Add MCP server records to the wiki catalog"
    )

    source_group = parser.add_mutually_exclusive_group(required=True)
    source_group.add_argument(
        "--from-json",
        metavar="PATH",
        help="Single record as a JSON object file",
    )
    source_group.add_argument(
        "--from-jsonl",
        metavar="PATH",
        help="Batch of records as a JSONL file (one object per line)",
    )
    source_group.add_argument(
        "--from-stdin",
        action="store_true",
        help="Read JSONL records from stdin (one object per line)",
    )

    parser.add_argument("--dry-run", action="store_true", help="Parse and validate but skip writes")
    parser.add_argument("--wiki", default=str(cfg.wiki_dir), help="Wiki root path")
    parser.add_argument(
        "--skip-existing",
        action="store_true",
        help="Skip records whose entity page already exists (no source merge)",
    )
    parser.add_argument(
        "--update-existing",
        action="store_true",
        help="Apply the reviewed replacement when an MCP entity already exists",
    )
    args = parser.parse_args()

    wiki_path = Path(os.path.expanduser(args.wiki))
    ensure_wiki(str(wiki_path))
    mcp_entity_dir = wiki_path / _MCP_ENTITY_SUBDIR

    raw_records: list[dict[str, Any]] = []

    if args.from_json:
        json_path = Path(os.path.expanduser(args.from_json))
        if not json_path.exists():
            print(f"Error: {json_path} does not exist.", file=sys.stderr)
            sys.exit(1)
        try:
            raw_records = [json.loads(json_path.read_text(encoding="utf-8"))]
        except json.JSONDecodeError as exc:
            print(f"Error: failed to parse JSON: {exc}", file=sys.stderr)
            sys.exit(1)

    elif args.from_jsonl:
        jsonl_path = Path(os.path.expanduser(args.from_jsonl))
        if not jsonl_path.exists():
            print(f"Error: {jsonl_path} does not exist.", file=sys.stderr)
            sys.exit(1)
        for lineno, line in enumerate(
            jsonl_path.read_text(encoding="utf-8").splitlines(), 1
        ):
            line = line.strip()
            if not line:
                continue
            try:
                raw_records.append(json.loads(line))
            except json.JSONDecodeError as exc:
                print(f"Warning: line {lineno} skipped (bad JSON): {exc}", file=sys.stderr)

    elif args.from_stdin:
        for lineno, line in enumerate(sys.stdin, 1):
            line = line.strip()
            if not line:
                continue
            try:
                raw_records.append(json.loads(line))
            except json.JSONDecodeError as exc:
                print(f"Warning: line {lineno} skipped (bad JSON): {exc}", file=sys.stderr)

    if not raw_records:
        print("No records to process.", file=sys.stderr)
        sys.exit(0)

    added, merged, reviewed, rejected, errors = _process_batch(
        records=raw_records,
        wiki_path=wiki_path,
        dry_run=args.dry_run,
        skip_existing=args.skip_existing,
        update_existing=args.update_existing,
        mcp_entity_dir=mcp_entity_dir,
    )

    dry_label = " (dry-run)" if args.dry_run else ""
    print(
        f"\nDone{dry_label}: {added} added, {merged} updated, "
        f"{reviewed} reviewed, {rejected} rejected, {errors} errors"
    )


if __name__ == "__main__":
    main()