Commit 8857702
committed
scripts(feat[mcp_swap]): add cross-CLI MCP config detect/swap/revert tool
why: when developing an MCP server locally, each agent CLI (Claude, Codex,
Cursor, Gemini) needs its config pointed at the checkout so uv run picks
up source edits editable-style. Doing that by hand across four config
formats (three JSON shapes + TOML) is fragile — Codex's TOML in particular
needs comment/order preservation that sed/jq can't offer. A scripted swap
with timestamped backups + a state file makes the flow reversible and
reproducible across any MCP repo, not just this one.
what:
- Add scripts/mcp_swap.py as a PEP 723 single-file script modeled on
~/scripts/py/agentex_mcp.py: inline uv deps (tomlkit>=0.13),
CLIName = Literal["claude","codex","cursor","gemini"], per-CLI
get/set/delete on the MCP server entry. Subcommands: detect, status,
use-local, revert. Server name + entry command auto-derived from the
repo's pyproject.toml (libtmux-mcp -> libtmux, first [project.scripts]
key for the entry).
- Claude's per-project keying (projects."<abs-repo>".mcpServers) is
handled explicitly — only the current repo's key is rewritten, leaving
other projects untouched. Claude's full entry shape is preserved
(type/env fields).
- Codex TOML edits go through tomlkit so [notice] blocks, top-level
comments, and sibling tables survive the round-trip. When no
libtmux entry exists yet (common for Codex), use-local records
action="added" so revert correctly removes the block.
- Safety: atomic tempfile+os.replace writes, timestamped
.bak.mcp-swap-<ts> backups (never overwrites existing .bak.*), post-
write re-parse validation with rollback on failure, --dry-run that
prints unified diffs and writes nothing, state file at
~/.local/state/libtmux-mcp/mcp_swap.json tracking per-CLI backup paths.
- Add scripts/README.md with usage + extension notes (add an entry to
CLIS and extend the three per-CLI branches).
- Add 12 tests in tests/test_mcp_swap.py using importlib.util to load
the out-of-tree script: JSON round-trip (cursor/gemini), Claude
per-project keying, Codex comment preservation + add-then-revert,
unrelated-server preservation, --dry-run writes nothing,
second-swap-is-noop, state-file cleared on full revert, spec helpers.
- Add four [group: 'mcp'] recipes to justfile: mcp-detect, mcp-status,
mcp-use-local, mcp-revert.
- Add tomlkit>=0.13 to the dev dependency group so pytest can import
the PEP 723 script without uv run bootstrapping.1 parent d5362a9 commit 8857702
6 files changed
Lines changed: 1091 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
122 | 142 | | |
123 | 143 | | |
124 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments