Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(extensions): make the remaining ~/.claude.json writers atomic

Agrici Danielb81566f67020
diff --git a/extensions/ahrefs/docs/AHREFS-SETUP.md b/extensions/ahrefs/docs/AHREFS-SETUP.md
index 23a5326..7d6ede8 100644
--- a/extensions/ahrefs/docs/AHREFS-SETUP.md
+++ b/extensions/ahrefs/docs/AHREFS-SETUP.md
@@ -13,7 +13,9 @@ live Ahrefs data.
The installer:
-1. Verifies Python 3 + Node 18+ are on `$PATH`.
+1. Verifies Node 18+ is on `$PATH` (the `.sh` installer also verifies Python 3,
+ used only for its `~/.claude.json` merge script; the `.ps1` installer
+ merges natively with `ConvertTo-Json` and needs no Python).
2. Prompts for your Ahrefs API token (input is hidden).
3. Pre-warms the `@ahrefs/[email protected]` npm package via `npx --yes` so the first
MCP call doesn't spend 10+ seconds downloading.
diff --git a/extensions/ahrefs/install.ps1 b/extensions/ahrefs/install.ps1
index 10ca21c..9c1500e 100644
--- a/extensions/ahrefs/install.ps1
+++ b/extensions/ahrefs/install.ps1
@@ -10,8 +10,7 @@ function Test-Cmd($name) {
return $?
}
-if (-not (Test-Cmd python)) { throw "Python 3 is required." }
-if (-not (Test-Cmd npx)) { throw "Node 18+ / npx is required." }
+if (-not (Test-Cmd npx)) { throw "Node 18+ / npx is required." }
$SkillDir = Join-Path $HOME ".claude/skills"
# MCP servers live in ~/.claude.json (the file `claude mcp add` writes).
@@ -38,27 +37,22 @@ Write-Host "✓ Installed skill: $SkillTarget"
& npx --yes --package=@ahrefs/[email protected] mcp --help *> $null
# Merge ~/.claude.json.
-$pyScript = @"
-import json, os, sys, tempfile
-path, token = sys.argv[1], sys.argv[2]
-data = {}
-if os.path.exists(path):
- try:
- data = json.load(open(path))
- except Exception:
- data = {}
-data.setdefault('mcpServers', {})['ahrefs'] = {
- 'command': 'npx',
- 'args': ['--yes', '--package=@ahrefs/[email protected]', 'mcp'],
- 'env': {'AHREFS_API_TOKEN': token},
-}
-fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path) or '.', prefix='.settings.', suffix='.json')
-with os.fdopen(fd, 'w') as fh:
- json.dump(data, fh, indent=2)
-os.replace(tmp, path)
-print(f'Wrote mcpServers.ahrefs to {path}')
-"@
-$pyScript | python - $McpConfigJson $Plain
+$settingsContent = if (Test-Path $McpConfigJson) { Get-Content $McpConfigJson -Raw | ConvertFrom-Json } else { @{} }
+if (-not $settingsContent.mcpServers) { $settingsContent | Add-Member -NotePropertyName mcpServers -NotePropertyValue @{} -Force }
+$settingsContent.mcpServers | Add-Member -NotePropertyName 'ahrefs' -NotePropertyValue @{
+ command = 'npx'
+ args = @('--yes', '--package=@ahrefs/[email protected]', 'mcp')
+ env = @{ AHREFS_API_TOKEN = $Plain }
+} -Force
+# Write atomically: stage to a temp file in the same directory, then swap
+# it into place, so a crash mid-write never leaves ~/.claude.json truncated
+# or half-written (it is shared with Claude Code and other installers).
+# -Depth 100 (not the ConvertTo-Json default of 2) so an existing
+# ~/.claude.json with deeply nested config round-trips intact.
+$TempConfigJson = Join-Path (Split-Path -Parent $McpConfigJson) ".claude.json.$([guid]::NewGuid().ToString('N')).tmp"
+$settingsContent | ConvertTo-Json -Depth 100 | Set-Content $TempConfigJson -Encoding UTF8
+Move-Item -Path $TempConfigJson -Destination $McpConfigJson -Force
+Write-Host "Wrote mcpServers.ahrefs to $McpConfigJson"
Write-Host ""
Write-Host "Done. Open a new Claude Code session and run /seo ahrefs metrics <url>."
diff --git a/extensions/dataforseo/install.ps1 b/extensions/dataforseo/install.ps1
index 4364445..783a144 100644
--- a/extensions/dataforseo/install.ps1
+++ b/extensions/dataforseo/install.ps1
@@ -99,60 +99,33 @@ Copy-Item -Force "$SourceDir\field-config.json" $FieldConfigPath
# Merge MCP config into ~/.claude.json
Write-Host "→ Configuring MCP server..." -ForegroundColor Yellow
-$python = Get-Command -Name python -ErrorAction SilentlyContinue
-if ($null -eq $python) {
- $python = Get-Command -Name py -ErrorAction SilentlyContinue
-}
-
-if ($null -ne $python) {
- $pyExe = $python.Source
- # Credentials are passed as argv (never interpolated into the source string)
- # and the settings file is written atomically with 0600 permissions.
- $pyScript = @"
-import json, os, sys, tempfile
-path, username, password, field_config = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
-settings = {}
-if os.path.exists(path):
- try:
- with open(path) as f:
- settings = json.load(f)
- except json.JSONDecodeError:
- settings = {}
-settings.setdefault('mcpServers', {})['dataforseo'] = {
- 'command': 'npx',
- 'args': ['-y', '[email protected]'],
- 'env': {
- 'DATAFORSEO_USERNAME': username,
- 'DATAFORSEO_PASSWORD': password,
- 'ENABLED_MODULES': 'SERP,KEYWORDS_DATA,ONPAGE,DATAFORSEO_LABS,BACKLINKS,DOMAIN_ANALYTICS,BUSINESS_DATA,CONTENT_ANALYSIS,AI_OPTIMIZATION',
- 'FIELD_CONFIG_PATH': field_config,
- },
-}
-os.makedirs(os.path.dirname(path) or '.', exist_ok=True)
-fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path) or '.', prefix='.settings.', suffix='.json')
-try:
- with os.fdopen(fd, 'w') as f:
- json.dump(settings, f, indent=2)
- os.chmod(tmp, 0o600)
- os.replace(tmp, path)
-except Exception:
- if os.path.exists(tmp):
- os.unlink(tmp)
- raise
-print(' ok')
-"@
-
- $result = $pyScript | & $pyExe - $McpConfigFile $DfseUsername $DfsePassword $FieldConfigPath 2>&1
- if ($LASTEXITCODE -eq 0) {
- Write-Host " ✓ MCP server configured in ~/.claude.json" -ForegroundColor Green
- } else {
- Write-Host " ⚠ Could not auto-configure MCP server." -ForegroundColor Yellow
- Write-Host " Add the dataforseo server manually to ~\.claude.json"
+$settingsContent = if (Test-Path $McpConfigFile) { Get-Content $McpConfigFile -Raw | ConvertFrom-Json } else { @{} }
+if (-not $settingsContent.mcpServers) { $settingsContent | Add-Member -NotePropertyName mcpServers -NotePropertyValue @{} -Force }
+$settingsContent.mcpServers | Add-Member -NotePropertyName 'dataforseo' -NotePropertyValue @{
+ command = 'npx'
+ args = @('-y', '[email protected]')
+ env = @{
+ DATAFORSEO_USERNAME = $DfseUsername
+ DATAFORSEO_PASSWORD = $DfsePassword
+ ENABLED_MODULES = 'SERP,KEYWORDS_DATA,ONPAGE,DATAFORSEO_LABS,BACKLINKS,DOMAIN_ANALYTICS,BUSINESS_DATA,CONTENT_ANALYSIS,AI_OPTIMIZATION'
+ FIELD_CONFIG_PATH = $FieldConfigPath
}
-} else {
- Write-Host " ⚠ Python not found. Configure MCP server manually." -ForegroundColor Yellow
- Write-Host " See: extensions\dataforseo\docs\DATAFORSEO-SETUP.md"
+} -Force
+# Write atomically: stage to a temp file in the same directory, then swap
+# it into place, so a crash mid-write never leaves ~/.claude.json truncated
+# or half-written (it is shared with Claude Code and other installers).
+# -Depth 100 (not the ConvertTo-Json default of 2) so an existing
+# ~/.claude.json with deeply nested config round-trips intact.
+$TempConfigFile = Join-Path (Split-Path -Parent $McpConfigFile) ".claude.json.$([guid]::NewGuid().ToString('N')).tmp"
+$settingsContent | ConvertTo-Json -Depth 100 | Set-Content $TempConfigFile -Encoding UTF8
+Move-Item -Path $TempConfigFile -Destination $McpConfigFile -Force
+# Restrict the credential-bearing settings file to the current user only.
+try {
+ icacls $McpConfigFile /inheritance:r /grant:r "${env:USERNAME}:F" | Out-Null
+} catch {
+ Write-Host " Note: could not restrict ~/.claude.json ACL; review manually." -ForegroundColor Yellow
}
+Write-Host " ✓ MCP server configured in ~/.claude.json" -ForegroundColor Green
# Pre-warm npx package
Write-Host "→ Pre-downloading dataforseo-mcp-server..." -ForegroundColor Yellow
diff --git a/extensions/firecrawl/uninstall.ps1 b/extensions/firecrawl/uninstall.ps1
index 28b90f6..b7f6700 100644
--- a/extensions/firecrawl/uninstall.ps1
+++ b/extensions/firecrawl/uninstall.ps1
@@ -15,7 +15,16 @@ if (Test-Path $McpConfigFile) {
$settings = Get-Content $McpConfigFile -Raw | ConvertFrom-Json
if ($settings.mcpServers.'firecrawl-mcp') {
$settings.mcpServers.PSObject.Properties.Remove('firecrawl-mcp')
- $settings | ConvertTo-Json -Depth 10 | Set-Content $McpConfigFile -Encoding UTF8
+ # Write atomically: stage to a temp file in the same directory, then
+ # swap it into place, so a crash mid-write never leaves
+ # ~/.claude.json truncated or half-written (it is shared with
+ # Claude Code and other installers).
+ # -Depth 100 (not the ConvertTo-Json default of 2, or the previous
+ # 10) so an existing ~/.claude.json with deeply nested config
+ # round-trips intact.
+ $TempConfigFile = Join-Path (Split-Path -Parent $McpConfigFile) ".claude.json.$([guid]::NewGuid().ToString('N')).tmp"
+ $settings | ConvertTo-Json -Depth 100 | Set-Content $TempConfigFile -Encoding UTF8
+ Move-Item -Path $TempConfigFile -Destination $McpConfigFile -Force
Write-Host "v Removed MCP server from ~/.claude.json" -ForegroundColor Green
}
}
diff --git a/tests/test_agent_mcp_permissions.py b/tests/test_agent_mcp_permissions.py
index a5fac2b..90e9179 100644
--- a/tests/test_agent_mcp_permissions.py
+++ b/tests/test_agent_mcp_permissions.py
@@ -41,6 +41,9 @@ def test_dataforseo_agent_bodies_stay_mirrored():
def test_dataforseo_installers_use_matching_mcp_server_name():
- for rel in ("extensions/dataforseo/install.sh", "extensions/dataforseo/install.ps1"):
- text = _text(ROOT / rel)
- assert "['dataforseo']" in text
+ # install.sh still merges the entry with a Python dict subscript;
+ # install.ps1 merges it natively with Add-Member (v2.3.1, atomic
+ # ConvertTo-Json write). Either way the server must be registered as
+ # exactly "dataforseo" to match the agents' `mcp__dataforseo__*` grant.
+ assert "['dataforseo']" in _text(ROOT / "extensions/dataforseo/install.sh")
+ assert "-NotePropertyName 'dataforseo'" in _text(ROOT / "extensions/dataforseo/install.ps1")
diff --git a/tests/test_firecrawl_install_atomic_write.py b/tests/test_firecrawl_install_atomic_write.py
index 2a71283..a4054cd 100644
--- a/tests/test_firecrawl_install_atomic_write.py
+++ b/tests/test_firecrawl_install_atomic_write.py
@@ -1,31 +1,73 @@
-"""Static contract for firecrawl's install.ps1 ~/.claude.json write.
+"""Static contract for the PowerShell installers' ~/.claude.json writes.
-install.ps1 cannot be executed in CI (no PowerShell on the Linux/macOS
-runners that run the main suite; see docs/WORKFLOW-public-private.md and the
-windows-smoke workflow for the executed coverage). This test asserts the
-source text contains the temp-then-move write pattern and the -Depth 100
-serialisation depth needed to round-trip an existing ~/.claude.json.
+None of these .ps1 files can be executed in CI (no PowerShell on the
+Linux/macOS runners that run the main suite; see
+docs/WORKFLOW-public-private.md and the windows-smoke workflow for the
+executed coverage). These tests assert the source text contains the
+temp-then-move write pattern and the -Depth 100 serialisation depth needed
+to round-trip an existing ~/.claude.json, for every PowerShell installer
+that merges into it.
+
+v2.3.0 established the pattern in extensions/firecrawl/install.ps1. v2.3.1
+extended it to the three writers that were still non-atomic: dataforseo's
+and ahrefs's installers (which merged the JSON in an embedded Python
+script with no -Depth control) and firecrawl's uninstaller (which wrote
+`ConvertTo-Json -Depth 10` straight to $McpConfigFile with no temp file).
"""
from pathlib import Path
+import pytest
+
ROOT = Path(__file__).resolve().parents[1]
+# rel path -> (config-path variable, temp-file variable)
+WRITERS = {
+ "extensions/firecrawl/install.ps1": ("$McpConfigFile", "$TempConfigFile"),
+ "extensions/firecrawl/uninstall.ps1": ("$McpConfigFile", "$TempConfigFile"),
+ "extensions/dataforseo/install.ps1": ("$McpConfigFile", "$TempConfigFile"),
+ "extensions/ahrefs/install.ps1": ("$McpConfigJson", "$TempConfigJson"),
+}
+
+
[email protected]("rel,variables", WRITERS.items())
+def test_ps1_writes_mcp_config_atomically(rel: str, variables: tuple[str, str]) -> None:
+ config_var, temp_var = variables
+ text = (ROOT / rel).read_text(encoding="utf-8")
+
+ assert temp_var in text, f"{rel}: missing temp-file variable {temp_var}"
+ assert f"Move-Item -Path {temp_var} -Destination {config_var} -Force" in text, (
+ f"{rel}: missing the atomic Move-Item swap"
+ )
+ # The final Set-Content must target the temp file, not the real config
+ # path directly, so a crash mid-write cannot leave it truncated.
+ assert f"Set-Content {temp_var} -Encoding UTF8" in text
+ assert f"Set-Content {config_var} -Encoding UTF8" not in text
+
+
[email protected]("rel", WRITERS.keys())
+def test_ps1_uses_depth_100(rel: str) -> None:
+ text = (ROOT / rel).read_text(encoding="utf-8")
-def test_firecrawl_install_ps1_writes_mcp_config_atomically() -> None:
- text = (ROOT / "extensions/firecrawl/install.ps1").read_text(encoding="utf-8")
+ assert "ConvertTo-Json -Depth 100" in text, f"{rel}: not serialising with -Depth 100"
+ assert "ConvertTo-Json -Depth 10 " not in text, f"{rel}: still uses the shallower -Depth 10"
+ assert "ConvertTo-Json -Depth 10\n" not in text, f"{rel}: still uses the shallower -Depth 10"
- assert "$TempConfigFile" in text
- assert "Move-Item -Path $TempConfigFile -Destination $McpConfigFile -Force" in text
- # The final Set-Content must target the temp file, not $McpConfigFile
- # directly, so a crash mid-write cannot leave it truncated.
- assert "Set-Content $TempConfigFile -Encoding UTF8" in text
- assert "Set-Content $McpConfigFile -Encoding UTF8" not in text
+def test_dataforseo_and_ahrefs_no_longer_shell_out_to_python_for_the_merge() -> None:
+ # Before v2.3.1 both installers piped a heredoc into `python -` to merge
+ # the MCP entry, so the write's atomicity (and its -Depth control)
+ # depended on hand-rolled `tempfile.mkstemp` + `os.replace` logic instead
+ # of the same ConvertTo-Json/Move-Item pattern every other writer uses.
+ for rel in ("extensions/dataforseo/install.ps1", "extensions/ahrefs/install.ps1"):
+ text = (ROOT / rel).read_text(encoding="utf-8")
+ assert "tempfile.mkstemp" not in text, f"{rel}: still merges via an embedded Python script"
+ assert "| python -" not in text, f"{rel}: still pipes a heredoc into python"
-def test_firecrawl_install_ps1_uses_depth_100() -> None:
- text = (ROOT / "extensions/firecrawl/install.ps1").read_text(encoding="utf-8")
- assert "ConvertTo-Json -Depth 100" in text
- assert "ConvertTo-Json -Depth 10 " not in text
- assert "ConvertTo-Json -Depth 10\n" not in text
+def test_ahrefs_install_ps1_no_longer_requires_python() -> None:
+ # The merge script was the only reason install.ps1 needed Python; the
+ # native ConvertTo-Json merge needs only Node/npx.
+ text = (ROOT / "extensions/ahrefs/install.ps1").read_text(encoding="utf-8")
+ assert 'Test-Cmd python' not in text
+ assert 'throw "Python 3 is required."' not in text