@paigy/mcp 0.4.0 → 0.6.0

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 CHANGED
@@ -7,7 +7,7 @@ A voice inbox for your AI agents. This MCP server lets an agent **notify a user*
7
7
  ### As a Claude Code plugin (one step)
8
8
 
9
9
  ```
10
- /plugin marketplace add mauurda/paigy
10
+ /plugin marketplace add paigy-ai/claude
11
11
  /plugin install paigy
12
12
  ```
13
13
 
@@ -0,0 +1,77 @@
1
+ // src/device.ts
2
+ import { execFile } from "child_process";
3
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
4
+ import { homedir, platform } from "os";
5
+ import { join } from "path";
6
+ var BACKEND_URL = process.env.PAIGY_BACKEND_URL ?? "https://paigy.ai";
7
+ var AGENT_NAME = process.env.PAIGY_AGENT ?? "mcp-agent";
8
+ var TOKEN_PATH = join(homedir(), ".paigy", "token.json");
9
+ function openBrowser(url) {
10
+ const cmd = platform() === "darwin" ? "open" : platform() === "win32" ? "cmd" : "xdg-open";
11
+ const args = platform() === "win32" ? ["/c", "start", url] : [url];
12
+ execFile(cmd, args, () => {
13
+ });
14
+ }
15
+ function saveToken(token) {
16
+ mkdirSync(join(homedir(), ".paigy"), { recursive: true });
17
+ writeFileSync(TOKEN_PATH, JSON.stringify(token, null, 2) + "\n", { mode: 384 });
18
+ }
19
+ var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
20
+ function readToken() {
21
+ if (process.env.PAIGY_TOKEN) return process.env.PAIGY_TOKEN;
22
+ if (existsSync(TOKEN_PATH)) {
23
+ try {
24
+ return JSON.parse(readFileSync(TOKEN_PATH, "utf8")).access_token ?? "";
25
+ } catch {
26
+ }
27
+ }
28
+ return "";
29
+ }
30
+ function deleteToken() {
31
+ if (!existsSync(TOKEN_PATH)) return false;
32
+ rmSync(TOKEN_PATH);
33
+ return true;
34
+ }
35
+ async function revokeToken(token) {
36
+ const res = await fetch(`${BACKEND_URL}/api/device/revoke`, {
37
+ method: "POST",
38
+ headers: { authorization: `Bearer ${token}` }
39
+ });
40
+ return res.ok;
41
+ }
42
+ async function requestCode(agent = AGENT_NAME) {
43
+ const res = await fetch(`${BACKEND_URL}/api/device/code`, {
44
+ method: "POST",
45
+ headers: { "content-type": "application/json" },
46
+ body: JSON.stringify({ agent })
47
+ });
48
+ if (!res.ok) throw new Error(`/api/device/code failed: ${res.status} ${await res.text()}`);
49
+ return await res.json();
50
+ }
51
+ async function pollToken(deviceCode) {
52
+ const res = await fetch(`${BACKEND_URL}/api/device/token`, {
53
+ method: "POST",
54
+ headers: { "content-type": "application/json" },
55
+ body: JSON.stringify({ device_code: deviceCode })
56
+ });
57
+ if (res.ok) return await res.json();
58
+ const body = await res.json().catch(() => ({ error: "unknown" }));
59
+ const err = body.error ?? "unknown";
60
+ if (err === "authorization_pending") return null;
61
+ if (err === "expired_token") throw new Error("Device code expired. Start pairing again.");
62
+ if (err === "access_denied") throw new Error("Access denied by user. Pairing cancelled.");
63
+ throw new Error(`Unexpected error from /api/device/token: ${err}`);
64
+ }
65
+
66
+ export {
67
+ AGENT_NAME,
68
+ TOKEN_PATH,
69
+ openBrowser,
70
+ saveToken,
71
+ sleep,
72
+ readToken,
73
+ deleteToken,
74
+ revokeToken,
75
+ requestCode,
76
+ pollToken
77
+ };
package/dist/index.js CHANGED
@@ -1,4 +1,14 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ deleteToken,
4
+ openBrowser,
5
+ pollToken,
6
+ readToken,
7
+ requestCode,
8
+ revokeToken,
9
+ saveToken,
10
+ sleep
11
+ } from "./chunk-XXBSXCOC.js";
2
12
 
3
13
  // src/index.ts
4
14
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
@@ -183,7 +193,7 @@ function loadToken() {
183
193
  }
184
194
  return "";
185
195
  }
186
- var ONBOARD_MSG = "Not paired with Paigy yet \u2014 run /paigy-onboard (or `npx -y @paigy/mcp paigy-mcp-onboard`) to pair this agent, then retry.";
196
+ var ONBOARD_MSG = "Not paired with Paigy yet \u2014 call the `pair` tool to connect this agent (it returns an approval link to show the user), then retry. Manual fallback: `npx -y @paigy/mcp paigy-mcp-onboard`.";
187
197
  function ensureAuthed(res) {
188
198
  if (res.status === 401) throw new Error(ONBOARD_MSG);
189
199
  return res;
@@ -198,11 +208,11 @@ async function submitNotification(req) {
198
208
  if (!res.ok) throw new Error(`notify failed: ${res.status} ${await res.text()}`);
199
209
  return await res.json();
200
210
  }
201
- var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
211
+ var sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
202
212
  async function awaitReply(notificationId, opts = {}) {
203
213
  const intervalMs = opts.intervalMs ?? 5e3;
204
214
  const windowMs = opts.windowMs ?? 3e5;
205
- const doSleep = opts.sleep ?? sleep;
215
+ const doSleep = opts.sleep ?? sleep2;
206
216
  const now = opts.now ?? Date.now;
207
217
  const token = loadToken();
208
218
  const start = now();
@@ -251,6 +261,9 @@ var AwaitReplySchema = z2.object({
251
261
  notificationId: z2.string().describe("The id returned by notify_user \u2014 waits for the user's reply to THIS notification only.")
252
262
  });
253
263
  var CheckRepliesSchema = z2.object({});
264
+ var PairSchema = z2.object({
265
+ device_code: z2.string().optional().describe("Omit to start pairing (returns an approval link to show the user). Pass the device_code from that first call to finish, once the user has approved.")
266
+ });
254
267
  var SetTaskStateToolSchema = z2.object({
255
268
  notificationId: z2.string(),
256
269
  state: SetTaskStateSchema.shape.state
@@ -280,6 +293,16 @@ var server = new Server(
280
293
  );
281
294
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
282
295
  tools: [
296
+ {
297
+ name: "pair",
298
+ description: "Pair this agent with the user's Paigy account (one-time) \u2014 required before notify_user/await_reply work. Two steps: (1) call with NO args to start; it opens the user's browser and returns { verification_uri_complete, user_code, device_code } \u2014 show the user the URL + user_code and ask them to approve. (2) call again passing that device_code to finish; it waits for approval and saves the token. If it returns { status:'pending' }, the user hasn't approved yet \u2014 call again with the same device_code to keep waiting.",
299
+ inputSchema: zodToJsonSchema(PairSchema, { target: "openApi3" })
300
+ },
301
+ {
302
+ name: "unpair",
303
+ description: "Log out / unpair this agent from the user's Paigy account: revokes the token server-side (it stops working everywhere) and deletes the local ~/.paigy/token.json. Takes no arguments. After this, notify_user/await_reply won't work until the user pairs again with the pair tool.",
304
+ inputSchema: zodToJsonSchema(z2.object({}), { target: "openApi3" })
305
+ },
283
306
  {
284
307
  name: "notify_user",
285
308
  description: "Notify the user via Paigy and get a request id to poll for their answer. Provide context.title (a specific, non-empty one-line headline \u2014 this is what the user sees first, and what shows on the ring for a call) and context.description (an array of standalone, non-empty detail chunks the user can selectively ask you to expand). Set `urgency`: 'inbox' (default) drops it silently in their inbox; 'call' rings their phone now as a voice call \u2014 use 'call' only when you genuinely need them in the moment (blocked/waiting, time-sensitive), not for routine FYIs. Plus optional options (answerable choices; each may carry a sandboxed `html` or an `image` preview for a visual 'pick one' \u2014 e.g. show layout/UI alternatives) and visuals. Use `select` to control how the user answers options: 'one' (default) = pick a single option; 'many' = multi-select, user checks any subset \u2192 answer arrives as {kind:'multi', optionIds:[...]}; 'rank' = select and order, user taps options in preferred order \u2192 answer arrives as {kind:'ranked', optionIds:[...]} (ordered by choice). If a reply comes back as {kind:'clarify', chunks:[...]}, the user wants more detail on those chunks \u2014 respond via notify_user with the SAME threadId and an expanded description. Pass `threadId` from a prior notify_user result or an await_reply reply to continue that conversation thread; omit it to start a new one.",
@@ -309,6 +332,74 @@ server.setRequestHandler(ListToolsRequestSchema, async () => ({
309
332
  }));
310
333
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
311
334
  switch (request.params.name) {
335
+ case "pair": {
336
+ const { device_code } = PairSchema.parse(request.params.arguments ?? {});
337
+ if (!device_code) {
338
+ const code = await requestCode();
339
+ openBrowser(code.verification_uri_complete);
340
+ return {
341
+ content: [{
342
+ type: "text",
343
+ text: JSON.stringify({
344
+ status: "awaiting_approval",
345
+ verification_uri_complete: code.verification_uri_complete,
346
+ user_code: code.user_code,
347
+ device_code: code.device_code,
348
+ expires_in: code.expires_in,
349
+ message: "Show the user verification_uri_complete and user_code (their browser should have opened). After they approve, call pair again with this device_code to finish."
350
+ })
351
+ }]
352
+ };
353
+ }
354
+ const start = Date.now();
355
+ const capMs = 3e4;
356
+ while (Date.now() - start < capMs) {
357
+ const token = await pollToken(device_code);
358
+ if (token) {
359
+ saveToken(token);
360
+ return {
361
+ content: [{
362
+ type: "text",
363
+ text: JSON.stringify({ status: "paired", nickname: token.nickname, agent: token.agent, device: token.device })
364
+ }]
365
+ };
366
+ }
367
+ await sleep(2e3);
368
+ }
369
+ return {
370
+ content: [{
371
+ type: "text",
372
+ text: JSON.stringify({
373
+ status: "pending",
374
+ device_code,
375
+ message: "Still awaiting approval after ~30s. Call pair again with this device_code to keep waiting."
376
+ })
377
+ }]
378
+ };
379
+ }
380
+ case "unpair": {
381
+ const token = readToken();
382
+ let revoked = false;
383
+ if (token) {
384
+ try {
385
+ revoked = await revokeToken(token);
386
+ } catch {
387
+ revoked = false;
388
+ }
389
+ }
390
+ const removed = deleteToken();
391
+ return {
392
+ content: [{
393
+ type: "text",
394
+ text: JSON.stringify({
395
+ ok: true,
396
+ revoked,
397
+ removed,
398
+ message: token ? `Unpaired. Server token ${revoked ? "revoked" : "revoke not confirmed"}; local token ${removed ? "deleted" : "was already absent"}.` : "No token found \u2014 already unpaired."
399
+ })
400
+ }]
401
+ };
402
+ }
312
403
  case "notify_user": {
313
404
  const parsed = NotifyRequestSchema.parse(request.params.arguments);
314
405
  const git = detectGit();
package/dist/onboard.js CHANGED
@@ -1,50 +1,15 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ AGENT_NAME,
4
+ TOKEN_PATH,
5
+ openBrowser,
6
+ pollToken,
7
+ requestCode,
8
+ saveToken,
9
+ sleep
10
+ } from "./chunk-XXBSXCOC.js";
2
11
 
3
12
  // src/onboard.ts
4
- import { execFile } from "child_process";
5
- import { mkdirSync, writeFileSync } from "fs";
6
- import { homedir, platform } from "os";
7
- import { join } from "path";
8
- var BACKEND_URL = process.env.PAIGY_BACKEND_URL ?? "https://paigy.ai";
9
- var AGENT_NAME = process.env.PAIGY_AGENT ?? "mcp-agent";
10
- var TOKEN_PATH = join(homedir(), ".paigy", "token.json");
11
- function openBrowser(url) {
12
- const cmd = platform() === "darwin" ? "open" : platform() === "win32" ? "cmd" : "xdg-open";
13
- const args = platform() === "win32" ? ["/c", "start", url] : [url];
14
- execFile(cmd, args, (err) => {
15
- if (err) {
16
- }
17
- });
18
- }
19
- function saveToken(token) {
20
- const dir = join(homedir(), ".paigy");
21
- mkdirSync(dir, { recursive: true });
22
- writeFileSync(TOKEN_PATH, JSON.stringify(token, null, 2) + "\n", { mode: 384 });
23
- }
24
- var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
25
- async function requestCode() {
26
- const res = await fetch(`${BACKEND_URL}/api/device/code`, {
27
- method: "POST",
28
- headers: { "content-type": "application/json" },
29
- body: JSON.stringify({ agent: AGENT_NAME })
30
- });
31
- if (!res.ok) throw new Error(`/api/device/code failed: ${res.status} ${await res.text()}`);
32
- return await res.json();
33
- }
34
- async function pollToken(deviceCode) {
35
- const res = await fetch(`${BACKEND_URL}/api/device/token`, {
36
- method: "POST",
37
- headers: { "content-type": "application/json" },
38
- body: JSON.stringify({ device_code: deviceCode })
39
- });
40
- if (res.ok) return await res.json();
41
- const body = await res.json().catch(() => ({ error: "unknown" }));
42
- const err = body.error ?? "unknown";
43
- if (err === "authorization_pending") return null;
44
- if (err === "expired_token") throw new Error("Device code expired. Please re-run onboarding.");
45
- if (err === "access_denied") throw new Error("Access denied by user. Onboarding cancelled.");
46
- throw new Error(`Unexpected error from /api/device/token: ${err}`);
47
- }
48
13
  async function main() {
49
14
  console.log(`
50
15
  Paigy MCP onboarding (agent: "${AGENT_NAME}")
@@ -91,6 +56,3 @@ main().catch((err) => {
91
56
  console.error(err.message);
92
57
  process.exit(1);
93
58
  });
94
- export {
95
- TOKEN_PATH
96
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paigy/mcp",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Paigy MCP server — a voice inbox for your AI agents. Lets an agent notify a user and await their reply.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -20,22 +20,23 @@
20
20
  "url": "git+https://github.com/mauurda/paigy.git",
21
21
  "directory": "apps/mcp"
22
22
  },
23
+ "scripts": {
24
+ "build": "tsup src/index.ts src/onboard.ts --format esm --clean",
25
+ "dev": "tsup src/index.ts src/onboard.ts --format esm --watch",
26
+ "typecheck": "tsc --noEmit",
27
+ "test": "vitest run",
28
+ "prepublishOnly": "pnpm --filter @paigy/schema build && pnpm build"
29
+ },
23
30
  "dependencies": {
24
31
  "@modelcontextprotocol/sdk": "^1.0.4",
25
32
  "zod": "^3.24.1",
26
33
  "zod-to-json-schema": "^3.24.1"
27
34
  },
28
35
  "devDependencies": {
36
+ "@paigy/schema": "workspace:*",
29
37
  "@types/node": "^22.0.0",
30
38
  "tsup": "^8.3.5",
31
39
  "typescript": "^5.7.2",
32
- "vitest": "^2.1.8",
33
- "@paigy/schema": "0.0.0"
34
- },
35
- "scripts": {
36
- "build": "tsup src/index.ts src/onboard.ts --format esm --clean",
37
- "dev": "tsup src/index.ts src/onboard.ts --format esm --watch",
38
- "typecheck": "tsc --noEmit",
39
- "test": "vitest run"
40
+ "vitest": "^2.1.8"
40
41
  }
41
- }
42
+ }