serpkin 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/bin/serpkin.mjs +13 -3
- package/lib/agents.mjs +57 -29
- package/lib/commands.mjs +8 -3
- package/lib/config.mjs +11 -5
- package/lib/mcp-bridge.mjs +59 -12
- package/lib/version.mjs +2 -0
- package/package.json +1 -1
- package/plugin/serpkin/.claude-plugin/plugin.json +1 -1
- package/plugin/serpkin/.mcp.json +1 -1
- package/plugin/serpkin/commands/check.md +2 -2
- package/plugin/serpkin/commands/ship.md +1 -1
- package/plugin/serpkin/commands/status.md +2 -2
- package/plugin/serpkin/skills/serpkin-seo/SKILL.md +2 -1
- package/templates/github-action.yml +3 -2
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ Requires Node 22 or newer. No dependencies. Builder and Pro start with a 7-day f
|
|
|
39
39
|
- **Cursor**: merges into `.cursor/mcp.json` and writes the project rule `.cursor/rules/serpkin.mdc` with the SEO loop.
|
|
40
40
|
- **Codex**: appends `[mcp_servers.serpkin]` to `~/.codex/config.toml` once and adds a short "Serpkin SEO" section to the project's `AGENTS.md` (it asks first, and creates the file if it's missing).
|
|
41
41
|
|
|
42
|
-
All three configs run a small local MCP bridge, `npx -y serpkin@0.2.
|
|
42
|
+
All three configs run a small local MCP bridge, `npx -y serpkin@0.2.11 mcp`, which reads the key saved at sign-in and forwards requests to `https://serpkin.com/api/mcp` (a self-hosted server adds `--server <origin>` to the args). The configs hold no key and no environment variable, so there is nothing to export.
|
|
43
43
|
|
|
44
44
|
The rule and the `AGENTS.md` section sit between `<!-- serpkin:start -->` and `<!-- serpkin:end -->`. Re-running init refreshes only that block and keeps everything else.
|
|
45
45
|
4. **Registers the site**, which queues its initial audit on the server, and uploads a route map (URL path → source file), so missions can name the files to change.
|
|
@@ -60,8 +60,8 @@ claude plugin marketplace add apwn/serpkin && claude plugin install serpkin@serp
|
|
|
60
60
|
Inside a session you can run `/plugin marketplace add apwn/serpkin`, then `/plugin install serpkin@serpkin`. The plugin lives in [`plugins/serpkin`](https://github.com/apwn/serpkin/tree/main/plugins/serpkin) and includes:
|
|
61
61
|
|
|
62
62
|
- the **`serpkin-seo` skill**, which runs the loop: next mission → implement → run checks → check the preview → submit → verify the preview → verify production after you deploy. It treats page text and search data as untrusted and never deploys.
|
|
63
|
-
- **commands**: `/serpkin:next` gets the next mission and implements it. `/serpkin:ship <preview-url>` submits it, checks the preview and verifies. `/serpkin:check <preview-url>` runs `
|
|
64
|
-
- the **Serpkin MCP server**, run as the same stdio bridge (`npx -y serpkin@0.2.
|
|
63
|
+
- **commands**: `/serpkin:next` gets the next mission and implements it. `/serpkin:ship <preview-url>` submits it, checks the preview and verifies. `/serpkin:check <preview-url>` runs `serpkin check` (the pinned CLI version from the public npm registry). `/serpkin:status` shows the site status.
|
|
64
|
+
- the **Serpkin MCP server**, run as the same stdio bridge (`npx -y serpkin@0.2.11 mcp`). It uses the key saved at sign-in, so there is nothing to export. `SERPKIN_API_KEY` is an optional override, and `SERPKIN_SERVER` picks a self-hosted or local server (default `https://serpkin.com`).
|
|
65
65
|
|
|
66
66
|
- **hooks** (quiet by design, only in repositories with `.serpkin.json`):
|
|
67
67
|
- **Session start**: one line with the next mission and "run /serpkin:next". It is fetched with a 2.5-second timeout and cached for 6 hours per site (failures for 30 minutes), and it says nothing when you are offline, signed out or the project isn't linked.
|
|
@@ -77,9 +77,9 @@ Clients without plugins get the same loop as MCP prompts: `next_mission`, `ship_
|
|
|
77
77
|
|
|
78
78
|
### Where the key goes
|
|
79
79
|
|
|
80
|
-
The key is **never written into your repository.** It is stored in `~/.config/serpkin/credentials.json` (mode 600, keyed by server). Agent configs run `npx -y serpkin@0.2.
|
|
80
|
+
The key is **never written into your repository.** It is stored in `~/.config/serpkin/credentials.json` (mode 600, keyed by server). Agent configs run `npx -y serpkin@0.2.11 mcp`, which reads it from there, so they hold no key and no environment variable, and you don't need to export anything.
|
|
81
81
|
|
|
82
|
-
`SERPKIN_API_KEY` still works and takes precedence over the saved key: use it in CI and GitHub Actions, or if you prefer it. Configs written by older versions (`${SERPKIN_API_KEY}` / `${env:SERPKIN_API_KEY}` / `bearer_token_env_var`) keep working while the variable is set. Re-running `npx serpkin` switches `.mcp.json` and `.cursor/mcp.json` to the bridge; an existing Codex table is left alone and the CLI prints the new block to replace it.
|
|
82
|
+
`SERPKIN_API_KEY` still works and takes precedence over the saved key: use it in CI and GitHub Actions, or if you prefer it. Configs written by older versions (`${SERPKIN_API_KEY}` / `${env:SERPKIN_API_KEY}` / `bearer_token_env_var`) keep working while the variable is set. Re-running `npx serpkin` switches `.mcp.json` and `.cursor/mcp.json` to the bridge; an existing Codex table that reads the variable is left alone and the CLI prints the new block to replace it. A Codex bridge entry pinned to an older CLI version gets just that version updated.
|
|
83
83
|
|
|
84
84
|
To connect by hand without Node (or an agent on another machine), point an HTTP MCP client at `https://serpkin.com/api/mcp` with `Authorization: Bearer <key>`, using a key from Agents.
|
|
85
85
|
|
|
@@ -170,6 +170,7 @@ Details: [privacy policy](https://serpkin.com/legal/privacy).
|
|
|
170
170
|
- **Preview check returns 401**: your preview is behind deployment protection. Allow Serpkin's requests or check a public preview.
|
|
171
171
|
- **The agent doesn't see Serpkin**: restart the agent after setup, and check `node --version` is 22 or newer where it runs.
|
|
172
172
|
- **401 or "not signed in" from the agent**: the saved key was revoked or is missing. Run `npx serpkin --login`, then restart the agent. With an older or by-hand config, make sure `SERPKIN_API_KEY` is set in the shell your agent starts from.
|
|
173
|
+
- **Windows**: the written configs start the bridge with `command: "npx"`. Some agents on Windows can't launch `npx` directly (it is `npx.cmd`). If the agent reports that it can't start the server, run the agent in WSL, or edit the config to `"command": "cmd"` with `"args": ["/c", "npx", …the existing args]`. This note is untested on Windows.
|
|
173
174
|
- **Start over**: re-run `npx serpkin`. It reuses the stored key and site and refreshes only its own blocks.
|
|
174
175
|
|
|
175
176
|
## Support and license
|
package/bin/serpkin.mjs
CHANGED
|
@@ -114,8 +114,18 @@ async function main(argv) {
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
// Set the exit code and let the process end by itself: process.exit() right after a large write can
|
|
118
|
+
// cut piped output short (`serpkin routes --json | jq`). Once stdout and stderr have flushed, exit, so
|
|
119
|
+
// a leftover handle (an open stdin, a keep-alive socket) can't keep a finished command running.
|
|
120
|
+
function finish(code) {
|
|
121
|
+
process.exitCode = code;
|
|
122
|
+
process.stdout.write("", () => process.stderr.write("", () => process.exit(code)));
|
|
123
|
+
}
|
|
124
|
+
// A closed pipe (`serpkin routes --json | head -1`) ends the command quietly instead of throwing.
|
|
125
|
+
process.stdout.on("error", (e) => process.exit(e?.code === "EPIPE" ? 0 : 2));
|
|
126
|
+
|
|
117
127
|
main(process.argv.slice(2)).then(
|
|
118
|
-
(code) =>
|
|
128
|
+
(code) => finish(code ?? 0),
|
|
119
129
|
(e) => {
|
|
120
130
|
if (
|
|
121
131
|
e?.code === "ERR_PARSE_ARGS_UNKNOWN_OPTION" ||
|
|
@@ -123,13 +133,13 @@ main(process.argv.slice(2)).then(
|
|
|
123
133
|
) {
|
|
124
134
|
// Node's parseArgs messages don't always end with a period.
|
|
125
135
|
out.fail(`${e.message.replace(/([^.])$/, "$1.")} Run \`npx serpkin --help\`.`);
|
|
126
|
-
|
|
136
|
+
return finish(2);
|
|
127
137
|
}
|
|
128
138
|
out.fail(
|
|
129
139
|
e instanceof CliError
|
|
130
140
|
? e.message
|
|
131
141
|
: `Unexpected error: ${e?.message ?? e}`,
|
|
132
142
|
);
|
|
133
|
-
|
|
143
|
+
finish(e instanceof CliError ? e.exitCode : 2);
|
|
134
144
|
},
|
|
135
145
|
);
|
package/lib/agents.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { join, delimiter } from "node:path";
|
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
6
|
import { statSync, readFileSync } from "node:fs";
|
|
7
7
|
import { home, writeFileAtomic } from "./config.mjs";
|
|
8
|
+
import { olderThan } from "./version.mjs";
|
|
8
9
|
|
|
9
10
|
const exists = (p) =>
|
|
10
11
|
stat(p).then(
|
|
@@ -48,8 +49,11 @@ export async function detectAgents(cwd, env = process.env) {
|
|
|
48
49
|
* `npx serpkin`: no key and no environment variable in any config. A self-hosted server is named in
|
|
49
50
|
* the args. `mcpUrl` is accepted for compatibility and used only to derive that server.
|
|
50
51
|
*/
|
|
51
|
-
// Exact version
|
|
52
|
-
//
|
|
52
|
+
// Exact version and an explicit registry. What that buys: the bridge an agent starts doesn't change until
|
|
53
|
+
// the config does (no silent jump to a newer or compromised release), and a project .npmrc can't point
|
|
54
|
+
// the download at another registry. What it does NOT do: `npx` still looks in the project's own
|
|
55
|
+
// node_modules first and runs a `serpkin` it finds there when that package's package.json claims this
|
|
56
|
+
// version, so the pin is no defence against a hostile repository that ships its own node_modules/serpkin.
|
|
53
57
|
const CLI_VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
54
58
|
export const BRIDGE_PACKAGE = `serpkin@${CLI_VERSION}`;
|
|
55
59
|
export const BRIDGE_REGISTRY = "--registry=https://registry.npmjs.org";
|
|
@@ -108,7 +112,11 @@ export async function mergeMcpJson(path, entry) {
|
|
|
108
112
|
return status;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Append the Codex block once. An existing [mcp_servers.serpkin] table is never rewritten, with one
|
|
117
|
+
* exception: when it is this same bridge entry pinned to an older CLI version, that version string is
|
|
118
|
+
* moved to the current one ("bumped").
|
|
119
|
+
*/
|
|
112
120
|
export async function appendCodexConfig(path, mcpUrl) {
|
|
113
121
|
let raw = "";
|
|
114
122
|
try {
|
|
@@ -120,15 +128,32 @@ export async function appendCodexConfig(path, mcpUrl) {
|
|
|
120
128
|
// An older entry that reads SERPKIN_API_KEY still works, but only with the variable set.
|
|
121
129
|
// From the serpkin table header up to the next table header (horizontal whitespace only, so a
|
|
122
130
|
// blank line before the header doesn't shift the match).
|
|
123
|
-
const
|
|
131
|
+
const tableAt = raw.search(/^[ \t]*\[mcp_servers\.serpkin\][ \t]*$/m);
|
|
132
|
+
const table = raw.slice(tableAt).split(/\n(?=[ \t]*\[[^\]]+\][ \t]*$)/m)[0];
|
|
124
133
|
if (/bearer_token_env_var\s*=\s*"SERPKIN_API_KEY"/.test(table)) return "legacy";
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
134
|
+
const argsLine = table.match(/^\s*args\s*=\s*(\[.*\])\s*$/m);
|
|
135
|
+
if (argsLine) {
|
|
136
|
+
const want = bridgeArgs(mcpUrl);
|
|
137
|
+
let have;
|
|
127
138
|
try {
|
|
128
|
-
|
|
139
|
+
have = JSON.parse(argsLine[1]);
|
|
129
140
|
} catch {
|
|
130
141
|
return "different";
|
|
131
142
|
}
|
|
143
|
+
if (JSON.stringify(have) === JSON.stringify(want)) return "unchanged";
|
|
144
|
+
// Same entry from an earlier CLI release: only the pinned `serpkin@x.y.z` differs. Move that one
|
|
145
|
+
// string to this version and leave every other byte of the file alone.
|
|
146
|
+
const pin = /^serpkin@(\d+\.\d+\.\d+)$/;
|
|
147
|
+
const i = Array.isArray(have) ? have.findIndex((v) => typeof v === "string" && pin.test(v)) : -1;
|
|
148
|
+
if (i === -1 || have.length !== want.length || !have.every((v, j) => (j === i ? want[j] === BRIDGE_PACKAGE : v === want[j]))) return "different";
|
|
149
|
+
// A config written by a newer CLI than this one is left alone.
|
|
150
|
+
if (!olderThan(pin.exec(have[i])[1], CLI_VERSION)) return "unchanged";
|
|
151
|
+
const at = tableAt + argsLine.index;
|
|
152
|
+
const line = argsLine[0];
|
|
153
|
+
const quoted = JSON.stringify(have[i]);
|
|
154
|
+
if (line.split(quoted).length !== 2) return "different";
|
|
155
|
+
await writeFileAtomic(path, raw.slice(0, at) + line.replace(quoted, () => JSON.stringify(BRIDGE_PACKAGE)) + raw.slice(at + line.length));
|
|
156
|
+
return "bumped";
|
|
132
157
|
}
|
|
133
158
|
const url = raw.match(
|
|
134
159
|
/\[mcp_servers\.serpkin\][^[]*?url\s*=\s*"([^"]*)"/,
|
|
@@ -144,22 +169,30 @@ export async function appendCodexConfig(path, mcpUrl) {
|
|
|
144
169
|
export const codexConfigPath = (env = process.env) =>
|
|
145
170
|
join(home(env), ".codex", "config.toml");
|
|
146
171
|
|
|
147
|
-
/**
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Start the `claude` CLI. On Windows an npm-installed claude is `claude.cmd`, which only cmd.exe can
|
|
174
|
+
* launch, so it goes through `cmd.exe /d /s /c` there. Arguments are always passed as an array and never
|
|
175
|
+
* joined into a command line by us; because cmd.exe re-reads the line, on Windows every argument must
|
|
176
|
+
* also be free of spaces, quotes and shell metacharacters (a server origin can come from a repo's
|
|
177
|
+
* .serpkin.json), otherwise nothing is run.
|
|
178
|
+
*/
|
|
179
|
+
const WINDOWS_SAFE_ARG = /^[A-Za-z0-9@._:\/=+-]+$/;
|
|
180
|
+
export function runClaude(args, options = {}, { platform = process.platform, spawn = spawnSync } = {}) {
|
|
181
|
+
const opts = { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], ...options };
|
|
182
|
+
if (platform !== "win32") return spawn("claude", args, opts);
|
|
183
|
+
const bad = args.find((a) => !WINDOWS_SAFE_ARG.test(String(a)));
|
|
184
|
+
if (bad !== undefined)
|
|
185
|
+
return { status: 1, stdout: "", stderr: `Can't pass ${JSON.stringify(String(bad))} to claude on Windows safely. Add the server by hand: Settings → Agents in the web app shows the snippet.` };
|
|
186
|
+
return spawn("cmd.exe", ["/d", "/s", "/c", "claude", ...args], opts);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* `claude mcp add` registers the same key-free bridge the JSON configs use (local scope: just this
|
|
191
|
+
* user, this project). No key is passed on the command line, where other local users could read it
|
|
192
|
+
* from the process list.
|
|
193
|
+
*/
|
|
194
|
+
export function claudeMcpAdd(mcpUrl, run = runClaude) {
|
|
195
|
+
const r = run(["mcp", "add", "--transport", "stdio", "serpkin", "--", "npx", ...bridgeArgs(mcpUrl)]);
|
|
163
196
|
const text = `${r.stdout ?? ""}${r.stderr ?? ""}`;
|
|
164
197
|
if (r.status === 0) return { ok: true };
|
|
165
198
|
return {
|
|
@@ -192,12 +225,7 @@ export const pluginSessionCommands = (env = process.env) => [
|
|
|
192
225
|
*/
|
|
193
226
|
export function installClaudePlugin(
|
|
194
227
|
env = process.env,
|
|
195
|
-
run = (args) =>
|
|
196
|
-
spawnSync("claude", args, {
|
|
197
|
-
encoding: "utf8",
|
|
198
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
199
|
-
env,
|
|
200
|
-
}),
|
|
228
|
+
run = (args) => runClaude(args, { env }),
|
|
201
229
|
) {
|
|
202
230
|
const steps = [
|
|
203
231
|
["plugin", "marketplace", "add", pluginMarketplace(env)],
|
package/lib/commands.mjs
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
claudeEntry,
|
|
42
42
|
cursorEntry,
|
|
43
43
|
claudeMcpAdd,
|
|
44
|
+
BRIDGE_PACKAGE,
|
|
44
45
|
installClaudePlugin,
|
|
45
46
|
pluginInstallCommand,
|
|
46
47
|
} from "./agents.mjs";
|
|
@@ -563,7 +564,7 @@ async function setupAgents(flags, ctx, { mcpUrl, apiKey }) {
|
|
|
563
564
|
},
|
|
564
565
|
{
|
|
565
566
|
value: "cli",
|
|
566
|
-
label: `Run \`claude mcp add\` ${c.dim("(just you;
|
|
567
|
+
label: `Run \`claude mcp add\` ${c.dim("(just you; holds no key)")}`,
|
|
567
568
|
},
|
|
568
569
|
],
|
|
569
570
|
{ opts: flags, def: "plugin" },
|
|
@@ -582,7 +583,7 @@ async function setupAgents(flags, ctx, { mcpUrl, apiKey }) {
|
|
|
582
583
|
how = "json";
|
|
583
584
|
}
|
|
584
585
|
if (how === "cli") {
|
|
585
|
-
const r = claudeMcpAdd(mcpUrl
|
|
586
|
+
const r = (ctx.claudeMcpAdd ?? claudeMcpAdd)(mcpUrl);
|
|
586
587
|
if (r.ok)
|
|
587
588
|
out.ok(
|
|
588
589
|
"Claude Code: added the serpkin server with `claude mcp add`.",
|
|
@@ -637,7 +638,11 @@ async function setupAgents(flags, ctx, { mcpUrl, apiKey }) {
|
|
|
637
638
|
const s = await appendCodexConfig(path, mcpUrl);
|
|
638
639
|
if (s === "different")
|
|
639
640
|
out.warn(
|
|
640
|
-
`Codex: ${tilde(path, ctx.env)} already has [mcp_servers.serpkin]
|
|
641
|
+
`Codex: ${tilde(path, ctx.env)} already has a different [mcp_servers.serpkin] entry; left as is.`,
|
|
642
|
+
);
|
|
643
|
+
else if (s === "bumped")
|
|
644
|
+
out.ok(
|
|
645
|
+
`Codex: ${tilde(path, ctx.env)} updated to ${BRIDGE_PACKAGE.split("@")[1]}. Restart Codex to load it.`,
|
|
641
646
|
);
|
|
642
647
|
else if (s === "legacy") {
|
|
643
648
|
out.note(
|
package/lib/config.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Credentials live outside the repository; the repo only gets a non-secret .serpkin.json.
|
|
2
|
-
import { mkdir, readFile, writeFile, rename, chmod } from "node:fs/promises";
|
|
2
|
+
import { mkdir, readFile, writeFile, rename, chmod, realpath } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { join, dirname } from "node:path";
|
|
5
5
|
import { CliError } from "./ui.mjs";
|
|
@@ -37,13 +37,19 @@ export async function readJsonFile(path) {
|
|
|
37
37
|
return JSON.parse(raw);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Write via a temp file + rename so an interrupted write never leaves half a file.
|
|
42
|
+
* A symlinked target (AGENTS.md → CLAUDE.md, a dotfile-managed config) keeps its link: the temp file
|
|
43
|
+
* goes next to the real file and is renamed over that.
|
|
44
|
+
*/
|
|
41
45
|
export async function writeFileAtomic(path, text, mode) {
|
|
42
46
|
await mkdir(dirname(path), { recursive: true });
|
|
43
|
-
|
|
47
|
+
// ENOENT: a new file, or a link whose target is missing (then the link itself is replaced).
|
|
48
|
+
const target = await realpath(path).catch(() => path);
|
|
49
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
44
50
|
await writeFile(tmp, text, mode ? { mode } : undefined);
|
|
45
|
-
await rename(tmp,
|
|
46
|
-
if (mode) await chmod(
|
|
51
|
+
await rename(tmp, target);
|
|
52
|
+
if (mode) await chmod(target, mode);
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
export async function loadCredentials(env = process.env) {
|
package/lib/mcp-bridge.mjs
CHANGED
|
@@ -6,13 +6,28 @@
|
|
|
6
6
|
// The key is never printed. Serpkin's endpoint is stateless and answers with JSON (or one SSE event).
|
|
7
7
|
import { createInterface } from "node:readline";
|
|
8
8
|
import { apiKeyFor, loadProject, resolveServer, projectServerTrusted } from "./config.mjs";
|
|
9
|
-
import { CLI_HEADER, CLI_VERSION } from "./version.mjs";
|
|
9
|
+
import { CLI_HEADER, CLIENT_HEADER, CLI_VERSION } from "./version.mjs";
|
|
10
10
|
|
|
11
11
|
const NOT_SIGNED_IN = -32001;
|
|
12
12
|
const UNREACHABLE = -32002;
|
|
13
|
+
const SERVER_REFUSED = -32003;
|
|
13
14
|
|
|
14
15
|
const write = (msg) => process.stdout.write(JSON.stringify(msg) + "\n");
|
|
15
|
-
const
|
|
16
|
+
const hasId = (m) => !!m && typeof m === "object" && !Array.isArray(m) && m.id !== undefined && m.id !== null;
|
|
17
|
+
/** Ids that expect an answer: one request or a batch. Notifications (no id) expect none. */
|
|
18
|
+
export const requestIds = (msg) => (Array.isArray(msg) ? msg : [msg]).filter((m) => hasId(m) && typeof m.method === "string").map((m) => m.id);
|
|
19
|
+
const isRpc = (m) => !!m && typeof m === "object" && !Array.isArray(m) && m.jsonrpc === "2.0";
|
|
20
|
+
|
|
21
|
+
/** The message for an HTTP error from the endpoint, whose body is `{error, code}` rather than JSON-RPC. */
|
|
22
|
+
export function httpErrorMessage(status, bodyMessages, retryAfter) {
|
|
23
|
+
const text = bodyMessages.map((m) => (m && typeof m === "object" && typeof m.error === "string" ? m.error : "")).find(Boolean);
|
|
24
|
+
if (status === 429) {
|
|
25
|
+
const secs = Number(retryAfter);
|
|
26
|
+
const wait = Number.isFinite(secs) && secs > 0 ? ` Try again in ${Math.ceil(secs)} seconds.` : " Wait a moment and try again.";
|
|
27
|
+
return `Serpkin rate limit reached (HTTP 429).${wait}`;
|
|
28
|
+
}
|
|
29
|
+
return text ? `Serpkin: ${text.slice(0, 300)} (HTTP ${status})` : `Serpkin returned HTTP ${status}.`;
|
|
30
|
+
}
|
|
16
31
|
|
|
17
32
|
/** Parse an HTTP MCP response body: plain JSON (one message or a batch) or text/event-stream data lines. */
|
|
18
33
|
export function parseMcpBody(body, contentType = "") {
|
|
@@ -45,10 +60,18 @@ export async function mcpBridge(flags = {}, ctx = {}) {
|
|
|
45
60
|
const say = (s) => process.stderr.write(`serpkin mcp: ${s}\n`);
|
|
46
61
|
if (!cred) say(`not signed in to ${server}. Run \`npx serpkin\` in your project (or set SERPKIN_API_KEY).`);
|
|
47
62
|
|
|
63
|
+
const failAll = (ids, code, message) => {
|
|
64
|
+
for (const id of ids) write({ jsonrpc: "2.0", id, error: { code, message } });
|
|
65
|
+
};
|
|
66
|
+
// The MCP client names itself in `initialize` ("claude-code", "cursor-vscode"…). Sent along so mission
|
|
67
|
+
// history can say which agent acted; the server only accepts names it knows.
|
|
68
|
+
let client = "";
|
|
48
69
|
const forward = async (msg) => {
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
70
|
+
const ids = requestIds(msg);
|
|
71
|
+
const named = msg?.method === "initialize" ? msg?.params?.clientInfo?.name : null;
|
|
72
|
+
if (typeof named === "string") client = named.replace(/[^\w .-]/g, "").slice(0, 60);
|
|
73
|
+
if (!cred) return failAll(ids, NOT_SIGNED_IN, `Not signed in to Serpkin (${server}). Run \`npx serpkin\` in your project, approve the sign-in, then restart your agent.`);
|
|
74
|
+
let r, body;
|
|
52
75
|
try {
|
|
53
76
|
r = await fetchImpl(endpoint, {
|
|
54
77
|
method: "POST",
|
|
@@ -57,28 +80,44 @@ export async function mcpBridge(flags = {}, ctx = {}) {
|
|
|
57
80
|
accept: "application/json, text/event-stream",
|
|
58
81
|
authorization: `Bearer ${cred.api_key}`,
|
|
59
82
|
[CLI_HEADER]: CLI_VERSION,
|
|
83
|
+
...(client ? { [CLIENT_HEADER]: client } : {}),
|
|
60
84
|
...(msg?.params?.protocolVersion ? { "mcp-protocol-version": String(msg.params.protocolVersion) } : {}),
|
|
61
85
|
},
|
|
62
86
|
body: JSON.stringify(msg),
|
|
63
87
|
signal: AbortSignal.timeout(ctx.timeoutMs ?? 120_000),
|
|
64
88
|
});
|
|
89
|
+
// The body read can fail too (connection reset, or the timeout after the headers arrived).
|
|
90
|
+
body = await r.text();
|
|
65
91
|
} catch (e) {
|
|
66
92
|
say(`couldn't reach ${server} (${e?.name === "TimeoutError" ? "timed out" : "network error"}).`);
|
|
67
|
-
return
|
|
93
|
+
return failAll(ids, UNREACHABLE, `Couldn't reach Serpkin at ${server}. Check your connection and try again.`);
|
|
68
94
|
}
|
|
69
|
-
const body = await r.text();
|
|
70
95
|
if (r.status === 401 || r.status === 403) {
|
|
71
96
|
say(`${server} refused the saved key (HTTP ${r.status}).`);
|
|
72
|
-
return
|
|
97
|
+
return failAll(ids, NOT_SIGNED_IN, "Serpkin refused the saved key (revoked or expired). Run `npx serpkin --login` in your project, then restart your agent.");
|
|
73
98
|
}
|
|
74
99
|
let messages;
|
|
75
100
|
try {
|
|
76
101
|
messages = parseMcpBody(body, r.headers.get("content-type") ?? "");
|
|
77
102
|
} catch {
|
|
78
|
-
return
|
|
103
|
+
return failAll(ids, UNREACHABLE, `Serpkin returned an unreadable response (HTTP ${r.status}).`);
|
|
104
|
+
}
|
|
105
|
+
// Only JSON-RPC goes to stdout. The endpoint answers 429, 413, 500 and 503 with `{error, code}`,
|
|
106
|
+
// which an MCP client can't match to a request: every id left unanswered gets a JSON-RPC error.
|
|
107
|
+
const answered = new Set();
|
|
108
|
+
for (const m of messages) {
|
|
109
|
+
if (!isRpc(m)) continue;
|
|
110
|
+
if (hasId(m)) answered.add(m.id);
|
|
111
|
+
write(m);
|
|
112
|
+
}
|
|
113
|
+
const unanswered = ids.filter((id) => !answered.has(id));
|
|
114
|
+
if (!unanswered.length) return;
|
|
115
|
+
if (r.status >= 400) {
|
|
116
|
+
const message = httpErrorMessage(r.status, messages, r.headers.get("retry-after"));
|
|
117
|
+
say(`${server} answered HTTP ${r.status}.`);
|
|
118
|
+
return failAll(unanswered, r.status >= 500 ? UNREACHABLE : SERVER_REFUSED, message);
|
|
79
119
|
}
|
|
80
|
-
|
|
81
|
-
for (const m of messages) write(m);
|
|
120
|
+
failAll(unanswered, UNREACHABLE, `Serpkin returned no answer for this request (HTTP ${r.status}).`);
|
|
82
121
|
};
|
|
83
122
|
|
|
84
123
|
const rl = createInterface({ input: ctx.stdin ?? process.stdin, crlfDelay: Infinity });
|
|
@@ -93,7 +132,15 @@ export async function mcpBridge(flags = {}, ctx = {}) {
|
|
|
93
132
|
continue;
|
|
94
133
|
}
|
|
95
134
|
// Requests run concurrently; responses carry their ids.
|
|
96
|
-
|
|
135
|
+
// forward() answers its own failures; the catch keeps an unexpected one from ending the process.
|
|
136
|
+
const p = forward(msg)
|
|
137
|
+
.catch((e) => {
|
|
138
|
+
say(`unexpected error (${e?.name ?? "Error"}).`);
|
|
139
|
+
try {
|
|
140
|
+
failAll(requestIds(msg), UNREACHABLE, `Couldn't reach Serpkin at ${server}. Check your connection and try again.`);
|
|
141
|
+
} catch {}
|
|
142
|
+
})
|
|
143
|
+
.finally(() => pending.delete(p));
|
|
97
144
|
pending.add(p);
|
|
98
145
|
}
|
|
99
146
|
await Promise.all(pending);
|
package/lib/version.mjs
CHANGED
|
@@ -3,6 +3,8 @@ import { readFileSync } from "node:fs";
|
|
|
3
3
|
|
|
4
4
|
export const CLI_VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
5
5
|
export const CLI_HEADER = "x-serpkin-cli";
|
|
6
|
+
/** The MCP client the bridge is serving, as it named itself. */
|
|
7
|
+
export const CLIENT_HEADER = "x-serpkin-client";
|
|
6
8
|
export const CLI_LATEST_HEADER = "x-serpkin-cli-latest";
|
|
7
9
|
|
|
8
10
|
const SEMVER = /^(\d{1,4})\.(\d{1,4})\.(\d{1,4})$/;
|
package/package.json
CHANGED
package/plugin/serpkin/.mcp.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Check a preview deployment for SEO regressions against production
|
|
3
3
|
argument-hint: "<preview-url>"
|
|
4
|
-
allowed-tools: Bash(npx serpkin check:*)
|
|
4
|
+
allowed-tools: Bash(npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 check:*)
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
Run `npx serpkin check $ARGUMENTS` from the repository root. If no URL was given, ask for the preview URL.
|
|
7
|
+
Run `npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 check $ARGUMENTS` from the repository root. If no URL was given, ask for the preview URL.
|
|
8
8
|
|
|
9
9
|
Summarize the result: regressions (path, check, production vs preview) and warnings. For each regression, find the code that causes it and propose a fix. Header-level noindex on preview hosts is expected and only a warning.
|
|
@@ -5,7 +5,7 @@ argument-hint: "<preview-url> [mission-id]"
|
|
|
5
5
|
|
|
6
6
|
Use the serpkin-seo skill to ship the mission you just implemented (mission id from the arguments, otherwise the one in progress from `serpkin_next_mission`). Arguments: $ARGUMENTS
|
|
7
7
|
|
|
8
|
-
1. Run `npx serpkin check <preview-url>`. If it exits 1, fix the regressions, push and run it again.
|
|
8
|
+
1. Run `npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 check <preview-url>`. If it exits 1, fix the regressions, push and run it again.
|
|
9
9
|
2. Call `serpkin_submit_result` with the PR URL, the preview URL and a short summary of the tests you ran.
|
|
10
10
|
3. Call `serpkin_verify_mission`: `environment: "preview"` with the preview_url for technical missions, `environment: "evidence"` for content and research missions.
|
|
11
11
|
4. Report the verification status and tier. After I deploy, verify with `environment: "production"` (Serpkin does this by itself if deploy checks are on in Settings → Automation). Don't merge or deploy.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Show the Serpkin site status, next mission and credits
|
|
3
|
-
allowed-tools: Bash(npx serpkin status:*)
|
|
3
|
+
allowed-tools: Bash(npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 status:*)
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Run `npx serpkin status` from the repository root and show the output. If it fails because the repository isn't set up, tell me to run `npx serpkin`.
|
|
6
|
+
Run `npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 status` from the repository root and show the output. If it fails because the repository isn't set up, tell me to run `npx serpkin`.
|
|
@@ -12,6 +12,7 @@ Serpkin ranks this site's SEO work into missions and verifies the result. Use it
|
|
|
12
12
|
- The MCP connection already knows the workspace: no `project_id` needed.
|
|
13
13
|
- `site_id`: `site_id` in `.serpkin.json` at the repository root, or `serpkin_get_workspace`.
|
|
14
14
|
- No `.serpkin.json` and no site: ask the user to run `npx serpkin` first.
|
|
15
|
+
- Several sites, or "how are my sites doing?": call `serpkin_sites_overview` for every site at once, with what needs the user ranked across them.
|
|
15
16
|
|
|
16
17
|
## Unlock the full audit
|
|
17
18
|
|
|
@@ -28,7 +29,7 @@ An unverified site gets a 6-page preview. To verify it:
|
|
|
28
29
|
- Otherwise do the `suggested_action` (usually `serpkin_scan_site`) and stop.
|
|
29
30
|
2. **Implement.** First call `serpkin_update_mission` with `action: "start"` so the user and other agents see it is in progress. Research missions are a tool call (`serpkin_research_keywords`), not a code change. Then follow `mission.implementation.steps` in order. Each step names a `file` (from the route map), an `action` (edit, create or run) and an `intent`. Adapt each `snippet` to the existing code; don't paste blindly. Use `recipe` and `acceptance_criteria` for anything the steps don't cover. Keep the change focused on this one mission.
|
|
30
31
|
3. **Run checks.** Run the project's tests, type check and build.
|
|
31
|
-
4. **Check the preview.** Once the branch has a preview deployment, run `npx serpkin check <preview-url>`. Exit code 1 means regressions; fix them and re-run.
|
|
32
|
+
4. **Check the preview.** Once the branch has a preview deployment, run `npx -y --registry=https://registry.npmjs.org serpkin@0.2.11 check <preview-url>`. Exit code 1 means regressions; fix them and re-run.
|
|
32
33
|
5. **Submit.** Call `serpkin_submit_result` with the PR URL, preview URL and a short test summary.
|
|
33
34
|
6. **Verify the preview.** Technical missions: `serpkin_verify_mission` with `environment: "preview"` and `preview_url`. Content and research missions: `environment: "evidence"`.
|
|
34
35
|
7. **Verify production.** Only after the human has merged and deployed, call `serpkin_verify_mission` with `environment: "production"`. If deploy checks are on (Settings → Automation), Serpkin does this by itself after the deploy.
|
|
@@ -23,7 +23,8 @@ jobs:
|
|
|
23
23
|
# No checkout: the job never runs code or config from the pull request while the secret is
|
|
24
24
|
# present (a PR could otherwise change .npmrc or .serpkin.json). The site id comes from a
|
|
25
25
|
# repository variable instead. Find it in .serpkin.json after `npx serpkin init`.
|
|
26
|
-
|
|
26
|
+
# Pinned to a commit: a moved tag can't change what runs next to the API key.
|
|
27
|
+
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
|
27
28
|
with:
|
|
28
29
|
node-version: 22
|
|
29
30
|
- name: Compare preview with production
|
|
@@ -36,4 +37,4 @@ jobs:
|
|
|
36
37
|
# Passed through env, not interpolated into the script, so the URL can't inject shell.
|
|
37
38
|
PREVIEW_URL: ${{ github.event.deployment_status.target_url }}
|
|
38
39
|
# Pinned version and registry; update the version deliberately.
|
|
39
|
-
run: npx --yes --registry=https://registry.npmjs.org serpkin@0.2.
|
|
40
|
+
run: npx --yes --registry=https://registry.npmjs.org serpkin@0.2.11 check "$PREVIEW_URL" --site "$SERPKIN_SITE_ID"
|