@markasso/mcp 0.9.1 → 0.10.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.
Files changed (2) hide show
  1. package/dist/markasso-mcp.js +209 -26
  2. package/package.json +1 -1
@@ -1993,6 +1993,20 @@ function getBundledIcon(ref) {
1993
1993
  //#region ../src/io/agent_requests_protocol.ts
1994
1994
  /** Longest reply text an agent can send back. */
1995
1995
  var MAX_AGENT_REPLY_CHARS = 4e3;
1996
+ /**
1997
+ * The valid element ids of an untrusted `elements` field: strings of 1 to
1998
+ * MAX_ELEMENT_ID_CHARS characters, without duplicates, at most
1999
+ * MAX_REPLY_ELEMENTS. Anything else yields an empty list.
2000
+ */
2001
+ function sanitizeReplyElements(value) {
2002
+ if (!Array.isArray(value)) return [];
2003
+ const ids = /* @__PURE__ */ new Set();
2004
+ for (const id of value.slice(0, 200)) {
2005
+ if (ids.size >= 50) break;
2006
+ if (typeof id === "string" && id.length > 0 && id.length <= 128) ids.add(id);
2007
+ }
2008
+ return [...ids];
2009
+ }
1996
2010
  //#endregion
1997
2011
  //#region ../src/core/geometry.ts
1998
2012
  /**
@@ -5733,7 +5747,7 @@ function escapeXml(str) {
5733
5747
  }
5734
5748
  //#endregion
5735
5749
  //#region package.json
5736
- var version = "0.9.1";
5750
+ var version = "0.10.0";
5737
5751
  //#endregion
5738
5752
  //#region ../src/core/layout.ts
5739
5753
  /** Space kept on both sides of a label between ranks. */
@@ -8258,6 +8272,17 @@ function elementLabel(el) {
8258
8272
  if (el.type === "text") return el.content;
8259
8273
  return "label" in el && el.label ? el.label : "";
8260
8274
  }
8275
+ /**
8276
+ * Whether two versions of an element hold the same values, one level deep:
8277
+ * enough to tell a no-op patch (a label set to what it was) from an edit.
8278
+ * Nested values (points, styles) compare by reference, so a rewritten array
8279
+ * counts as a change even when equal: at worst one element too many.
8280
+ */
8281
+ function sameFields(a, b) {
8282
+ const aKeys = Object.keys(a);
8283
+ if (aKeys.length !== Object.keys(b).length) return false;
8284
+ return aKeys.every((key) => a[key] === b[key]);
8285
+ }
8261
8286
  var Diagram = class {
8262
8287
  history;
8263
8288
  /** Keys given by the agent; other elements get derived keys on read. */
@@ -8337,13 +8362,48 @@ var Diagram = class {
8337
8362
  }
8338
8363
  }
8339
8364
  /** `findId`, or the error the model can act on. */
8365
+ /**
8366
+ * Element ids of the nodes `keys` names (as diagram_read shows them).
8367
+ * Throws on an unknown key, like every tool that takes keys.
8368
+ */
8369
+ idsOfKeys(keys) {
8370
+ return keys.map((key) => this.idOf(key));
8371
+ }
8340
8372
  idOf(key) {
8341
8373
  const id = this.findId(key);
8342
8374
  if (id !== void 0) return id;
8343
8375
  throw new DiagramError(`Unknown node "${key}". Call diagram_read to see the keys.`);
8344
8376
  }
8377
+ /** See onLocalChange. */
8378
+ changeListeners = /* @__PURE__ */ new Set();
8379
+ /**
8380
+ * Notified after each edit this diagram makes itself (never a remote one
8381
+ * from a live room) with the ids of the elements it created or really
8382
+ * changed (an edit that leaves every field as it was does not count), and
8383
+ * of those it deleted.
8384
+ */
8385
+ onLocalChange(cb) {
8386
+ this.changeListeners.add(cb);
8387
+ return () => {
8388
+ this.changeListeners.delete(cb);
8389
+ };
8390
+ }
8345
8391
  dispatch(command) {
8392
+ if (this.changeListeners.size === 0) {
8393
+ this.history.dispatch(command);
8394
+ return;
8395
+ }
8396
+ const before = new Map(this.elements.map((el) => [el.id, el]));
8346
8397
  this.history.dispatch(command);
8398
+ const changed = /* @__PURE__ */ new Set();
8399
+ for (const el of this.elements) {
8400
+ const old = before.get(el.id);
8401
+ before.delete(el.id);
8402
+ if (old !== el && !(old && sameFields(old, el))) changed.add(el.id);
8403
+ }
8404
+ const removed = new Set(before.keys());
8405
+ if (changed.size === 0 && removed.size === 0) return;
8406
+ for (const listener of this.changeListeners) listener(changed, removed);
8347
8407
  }
8348
8408
  /** Bottom-left of the current content, where new nodes start a row. */
8349
8409
  nextRowOrigin() {
@@ -9312,6 +9372,13 @@ async function writeFileAtomic(path, data) {
9312
9372
  var MAX_INBOX = 20;
9313
9373
  /** Delivered request ids the agent may still answer with `reply`. */
9314
9374
  var MAX_ANSWERABLE = 100;
9375
+ /**
9376
+ * How long the agent must stay idle before the room hears it: a model that
9377
+ * calls live_wait_request again right after a timeout never shows as idle.
9378
+ */
9379
+ var IDLE_GRACE_MS = 5e3;
9380
+ /** Elements remembered per request; well above what a reply carries. */
9381
+ var MAX_TRACKED_ELEMENTS = 500;
9315
9382
  var ROOM_ID = /^[a-zA-Z0-9_-]{3,64}$/;
9316
9383
  var AGENT_COLOR = "#c77dff";
9317
9384
  var JOIN_TIMEOUT_MS = 1e4;
@@ -9380,7 +9447,21 @@ var LiveLink = class LiveLink {
9380
9447
  selfId = "";
9381
9448
  open = false;
9382
9449
  inbox = [];
9383
- answerable = /* @__PURE__ */ new Set();
9450
+ /**
9451
+ * Delivered request ids the agent may answer, each with the elements the
9452
+ * agent changed for it (sent with the reply), or null once that cannot be
9453
+ * told: edits made while several requests waited for an answer could be
9454
+ * for any of them, so none of them claims those edits.
9455
+ */
9456
+ answerable = /* @__PURE__ */ new Map();
9457
+ /** Delivered requests still waiting for their first reply. */
9458
+ unanswered = /* @__PURE__ */ new Set();
9459
+ /** live_wait_request calls currently waiting. */
9460
+ waiting = 0;
9461
+ /** The activity the room last heard from this agent. */
9462
+ activitySent = null;
9463
+ idleTimer = null;
9464
+ stopTracking = null;
9384
9465
  /** Pending `waitForRequests` calls, woken by a request or a disconnect. */
9385
9466
  waiters = /* @__PURE__ */ new Set();
9386
9467
  /** Set while replaying a remote undo/redo, which History re-emits. */
@@ -9390,6 +9471,76 @@ var LiveLink = class LiveLink {
9390
9471
  this.room = room;
9391
9472
  this.url = `${origin}/?live=${encodeURIComponent(room)}`;
9392
9473
  }
9474
+ /**
9475
+ * Attributes the agent's own edits to the request it is working on, when
9476
+ * only one waits for an answer (see `answerable`). Deleted elements leave
9477
+ * every request, so they never fill the bound.
9478
+ */
9479
+ noteEdited(changed, removed) {
9480
+ for (const requestId of this.answerable.keys()) {
9481
+ const touched = this.answerable.get(requestId);
9482
+ for (const id of removed) touched?.delete(id);
9483
+ }
9484
+ if (changed.size === 0) return;
9485
+ if (this.unanswered.size > 1) {
9486
+ for (const requestId of this.unanswered) this.answerable.set(requestId, null);
9487
+ return;
9488
+ }
9489
+ for (const requestId of this.unanswered) {
9490
+ const touched = this.answerable.get(requestId);
9491
+ if (!touched) continue;
9492
+ for (const id of changed) {
9493
+ if (touched.size >= MAX_TRACKED_ELEMENTS) break;
9494
+ touched.add(id);
9495
+ }
9496
+ }
9497
+ }
9498
+ /**
9499
+ * What the agent is doing for the people in the room: waiting for
9500
+ * requests, busy with delivered ones still unanswered, or neither.
9501
+ */
9502
+ currentActivity() {
9503
+ if (this.waiting > 0) return "listening";
9504
+ if (this.unanswered.size > 0) return "working";
9505
+ return "idle";
9506
+ }
9507
+ /**
9508
+ * Tells the room when the activity changes. Going idle waits
9509
+ * IDLE_GRACE_MS, and is dropped if the agent is busy again by then.
9510
+ */
9511
+ updateActivity() {
9512
+ if (!this.open) return;
9513
+ const next = this.currentActivity();
9514
+ if (next === "idle" && this.activitySent !== "idle") {
9515
+ if (this.idleTimer) return;
9516
+ this.idleTimer = setTimeout(() => {
9517
+ this.idleTimer = null;
9518
+ if (this.currentActivity() === "idle") this.sendActivity("idle");
9519
+ }, IDLE_GRACE_MS);
9520
+ this.idleTimer.unref?.();
9521
+ return;
9522
+ }
9523
+ this.clearIdleTimer();
9524
+ if (next !== this.activitySent) this.sendActivity(next);
9525
+ }
9526
+ sendActivity(activity) {
9527
+ this.activitySent = activity;
9528
+ this.send({
9529
+ type: "presence",
9530
+ name: this.name,
9531
+ color: AGENT_COLOR,
9532
+ activity
9533
+ });
9534
+ }
9535
+ clearIdleTimer() {
9536
+ if (this.idleTimer) clearTimeout(this.idleTimer);
9537
+ this.idleTimer = null;
9538
+ }
9539
+ /** Stops attributing edits (the session is over). */
9540
+ untrack() {
9541
+ this.stopTracking?.();
9542
+ this.stopTracking = null;
9543
+ }
9393
9544
  static async join(diagram, link, name) {
9394
9545
  const { room, origin } = parseLiveLink(link);
9395
9546
  const live = new LiveLink(diagram, room, origin, name);
@@ -9441,18 +9592,21 @@ var LiveLink = class LiveLink {
9441
9592
  const taken = this.inbox;
9442
9593
  this.inbox = [];
9443
9594
  for (const request of taken) {
9444
- this.answerable.add(request.id);
9445
- this.send({
9446
- type: "agent_status",
9447
- id: request.id,
9448
- status: "received"
9449
- });
9595
+ this.answerable.set(request.id, /* @__PURE__ */ new Set());
9596
+ this.unanswered.add(request.id);
9450
9597
  }
9451
9598
  while (this.answerable.size > MAX_ANSWERABLE) {
9452
- const oldest = this.answerable.values().next().value;
9599
+ const oldest = this.answerable.keys().next().value;
9453
9600
  if (oldest === void 0) break;
9454
9601
  this.answerable.delete(oldest);
9602
+ this.unanswered.delete(oldest);
9455
9603
  }
9604
+ this.updateActivity();
9605
+ for (const request of taken) this.send({
9606
+ type: "agent_status",
9607
+ id: request.id,
9608
+ status: "received"
9609
+ });
9456
9610
  return taken;
9457
9611
  }
9458
9612
  /**
@@ -9460,29 +9614,49 @@ var LiveLink = class LiveLink {
9460
9614
  * after `timeoutMs` or when the room disconnects.
9461
9615
  */
9462
9616
  async waitForRequests(timeoutMs) {
9463
- if (this.inbox.length === 0 && this.open) await new Promise((resolve) => {
9464
- const wake = () => {
9465
- clearTimeout(timer);
9466
- this.waiters.delete(wake);
9467
- resolve();
9468
- };
9469
- const timer = setTimeout(wake, timeoutMs);
9470
- this.waiters.add(wake);
9471
- });
9617
+ if (this.inbox.length === 0 && this.open) {
9618
+ this.waiting++;
9619
+ this.updateActivity();
9620
+ await new Promise((resolve) => {
9621
+ const wake = () => {
9622
+ clearTimeout(timer);
9623
+ this.waiters.delete(wake);
9624
+ this.waiting--;
9625
+ resolve();
9626
+ };
9627
+ const timer = setTimeout(wake, timeoutMs);
9628
+ this.waiters.add(wake);
9629
+ });
9630
+ }
9472
9631
  return this.takeRequests();
9473
9632
  }
9474
- /** Sends an answer to the person behind a delivered request. */
9475
- reply(id, text) {
9633
+ /**
9634
+ * Sends an answer to the person behind a delivered request, pointing at
9635
+ * `elements` when given, else at the elements the agent changed for it.
9636
+ * Returns how many elements the reply points at, and whether the changes
9637
+ * could not be told apart from another request's (`ambiguous`).
9638
+ */
9639
+ reply(id, text, elements) {
9476
9640
  if (!this.open) throw new LiveError("Not connected to the live room.");
9477
9641
  if (!this.answerable.has(id)) throw new LiveError(`Unknown request id "${id}".`);
9642
+ const touched = this.answerable.get(id) ?? null;
9478
9643
  const trimmed = text.trim();
9479
9644
  if (!trimmed || trimmed.length > 4e3) throw new LiveError(`A reply needs 1 to ${MAX_AGENT_REPLY_CHARS} characters.`);
9645
+ const present = new Set(this.diagram.elements.map((el) => el.id));
9646
+ const ids = sanitizeReplyElements([...elements ?? touched ?? []].filter((elementId) => present.has(elementId)));
9647
+ this.unanswered.delete(id);
9648
+ this.updateActivity();
9480
9649
  this.send({
9481
9650
  type: "agent_status",
9482
9651
  id,
9483
9652
  status: "replied",
9484
- text: trimmed
9653
+ text: trimmed,
9654
+ ...ids.length > 0 ? { elements: ids } : {}
9485
9655
  });
9656
+ return {
9657
+ pointed: ids.length,
9658
+ ambiguous: !elements && touched === null
9659
+ };
9486
9660
  }
9487
9661
  wakeWaiters() {
9488
9662
  for (const wake of [...this.waiters]) wake();
@@ -9556,7 +9730,10 @@ var LiveLink = class LiveLink {
9556
9730
  history.setOnCommand((command) => {
9557
9731
  if (this.open && isSessionCommand(command)) this.publish(command);
9558
9732
  });
9733
+ this.untrack();
9734
+ this.stopTracking = this.diagram.onLocalChange((changed, removed) => this.noteEdited(changed, removed));
9559
9735
  this.open = true;
9736
+ this.sendActivity(this.currentActivity());
9560
9737
  resolve({
9561
9738
  seeded,
9562
9739
  discarded
@@ -9582,6 +9759,8 @@ var LiveLink = class LiveLink {
9582
9759
  this.open = false;
9583
9760
  this.outbox.clear();
9584
9761
  history.setOnCommand(void 0);
9762
+ this.untrack();
9763
+ this.clearIdleTimer();
9585
9764
  this.wakeWaiters();
9586
9765
  });
9587
9766
  socket.addEventListener("error", () => {
@@ -9606,6 +9785,8 @@ var LiveLink = class LiveLink {
9606
9785
  this.outbox.drain();
9607
9786
  this.open = false;
9608
9787
  this.wakeWaiters();
9788
+ this.untrack();
9789
+ this.clearIdleTimer();
9609
9790
  this.diagram.history.setOnCommand(void 0);
9610
9791
  this.socket?.close();
9611
9792
  this.socket = null;
@@ -10291,15 +10472,17 @@ server.registerTool("live_wait_request", {
10291
10472
  }));
10292
10473
  server.registerTool("live_reply", {
10293
10474
  title: "Reply to a live request",
10294
- description: "Answer a request from a person in the live room. The answer appears under your name in their participant list. Keep it short; the board itself shows what you changed.",
10475
+ description: "Answer a request from a person in the live room. The answer appears in their chat with you, with a button that selects and zooms to the elements you changed for that request. When you handle several requests at once, pass nodes: changes made while more than one request is open are not attributed to any of them. Keep it short; the board itself shows what you changed.",
10295
10476
  inputSchema: {
10296
10477
  requestId: z.string().min(1).max(64).describe("Id of the request"),
10297
- text: z.string().min(1).max(MAX_AGENT_REPLY_CHARS).describe("Short answer shown to the person")
10478
+ text: z.string().min(1).max(MAX_AGENT_REPLY_CHARS).describe("Short answer shown to the person"),
10479
+ nodes: z.array(z.string().min(1)).max(50).optional().describe("Keys of the nodes the answer is about (see diagram_read). Defaults to the elements you changed for this request.")
10298
10480
  }
10299
- }, guarded(({ requestId, text }) => {
10481
+ }, guarded(({ requestId, text, nodes }) => {
10300
10482
  if (!live) return "Not in a live session.";
10301
- live.reply(requestId, text);
10302
- return "Reply sent.";
10483
+ const { pointed, ambiguous } = live.reply(requestId, text, nodes ? diagram.idsOfKeys(nodes) : void 0);
10484
+ if (ambiguous) return "Reply sent without pointing at elements: you changed the board while several requests were open, so the changes cannot be told apart. Pass nodes to point at what this request changed.";
10485
+ return pointed > 0 ? `Reply sent, pointing at ${pointed} element${pointed === 1 ? "" : "s"}.` : "Reply sent.";
10303
10486
  }));
10304
10487
  await server.connect(new StdioServerTransport());
10305
10488
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markasso/mcp",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "mcpName": "io.github.AlbertoBarrago/markasso",
5
5
  "description": "MCP server that lets AI agents create, read and edit Markasso diagrams, offline or live on a shared board",
6
6
  "type": "module",