Public: anyone can read this; the crew can change it.

An agent over MCP can correct or remove a party, remove a dropped date and remove a deal, as the page and the terminal can #19

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/deal-clock:mason/17-an-agent-over-mcp-can-correct-or-remove → main 3 commits fork

  • Merged. mason/17-an-agent-over-mcp-can-correct-or-remove was merged into main as 6354d9b.

Review

Sign in to review this change.
3 files changed +94 −2
docs/TECHNICAL.md +3 −1
@@ -29,7 +29,9 @@ marked sent, as JSON — call it each morning as a signed-in account (a
2929 stranger gets 401; it sends nothing and changes nothing). `GET /deals/:id/ics` is a deal's calendar file. The JSON API is
3030 `/api/deals`, described at `/openapi.json`; the same operations are MCP
3131 tools at `POST /mcp` (`list_deals`, `get_deal`, `reminders_today`,
32 `open_deal`, `add_date`, `add_party`, `mark_done` — with `done: false` to
32 `open_deal`, `add_date`, `add_party`, `edit_party`, `remove_party`,
33 `remove_date` — custom dates only — `remove_deal` — its author or an admin —
34 `mark_done` — with `done: false` to
3335 reopen a date — `move_date`, `send_reminder`). `send_reminder` takes an
3436 optional `subject` and `body` to send edited words; without them the draft
3537 goes as it is.
src/mcp.ts +46
@@ -14,10 +14,14 @@ import {
1414 addDeadline,
1515 addParty,
1616 createDeal,
17 deleteDeal,
18 editParty,
1719 getDeal,
1820 listDeals,
1921 markDone,
2022 moveDeadline,
23 removeDeadline,
24 removeParty,
2125 sendReminder,
2226 } from "./deals.ts";
2327 import { agent, title } from "./env.ts";
@@ -170,6 +174,48 @@ export const tools: readonly Tool[] = [
170174 run: (db, who, a) =>
171175 addParty(db, who, { dealId: Number(a.deal_id), role: a.role, name: a.name, contact: a.contact }),
172176 },
177 {
178 name: "edit_party",
179 description: "Correct a party's name and contact (their role stays); the next reminder draft uses the new contact. Party ids are in get_deal.",
180 inputSchema: {
181 type: "object",
182 properties: {
183 deal_id: { type: "integer" },
184 party_id: { type: "integer" },
185 name: { type: "string", maxLength: 100 },
186 contact: { type: "string", maxLength: 120 },
187 },
188 required: ["deal_id", "party_id", "name", "contact"],
189 },
190 run: (db, who, a) =>
191 editParty(db, who, { dealId: Number(a.deal_id), id: Number(a.party_id), name: a.name, contact: a.contact }),
192 },
193 {
194 name: "remove_party",
195 description: "Remove a party from a deal; reminders already sent stay in the record.",
196 inputSchema: {
197 type: "object",
198 properties: { deal_id: { type: "integer" }, party_id: { type: "integer" } },
199 required: ["deal_id", "party_id"],
200 },
201 run: (db, who, a) => removeParty(db, who, { dealId: Number(a.deal_id), id: Number(a.party_id) }),
202 },
203 {
204 name: "remove_date",
205 description: "Remove a custom date the contract dropped; a standard contract date is refused (move it or mark it done). Date ids are in get_deal.",
206 inputSchema: {
207 type: "object",
208 properties: { deal_id: { type: "integer" }, date_id: { type: "integer" } },
209 required: ["deal_id", "date_id"],
210 },
211 run: (db, who, a) => removeDeadline(db, who, { dealId: Number(a.deal_id), id: Number(a.date_id) }),
212 },
213 {
214 name: "remove_deal",
215 description: "Remove a deal with its dates, parties and reminders; only its author or an admin may.",
216 inputSchema: { type: "object", properties: { deal_id: { type: "integer" } }, required: ["deal_id"] },
217 run: (db, who, a) => deleteDeal(db, who, { id: Number(a.deal_id) }),
218 },
173219 ];
174220
175221 /** One JSON-RPC message in, an HTTP status and a body out (a notification gets 202 and no body). */
src/server.test.ts +45 −1
@@ -2,7 +2,8 @@ import { afterAll, expect, test } from "bun:test";
22 import { mkdtempSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
5 import { env } from "./env.ts";
5 import { agent, env } from "./env.ts";
6 import { drafts } from "./reminders.ts";
67 import { serve } from "./server.ts";
78 import { sign } from "./who.ts";
89
@@ -161,6 +162,10 @@ test("the MCP server lists the tools and calls them as the caller", async () =>
161162 "open_deal",
162163 "add_date",
163164 "add_party",
165 "edit_party",
166 "remove_party",
167 "remove_date",
168 "remove_deal",
164169 ]);
165170 const call = (name: string, args: unknown, who = {}) =>
166171 rpc(
@@ -609,6 +614,45 @@ test("a party is corrected and removed from the page and the API; a custom date
609614 expect((await api(`/api/deals/${deal.id}/deadlines/${hoa.id}`, { method: "DELETE" }, mia)).status).toBe(404);
610615 });
611616
617 test("an agent corrects a party, removes dates, a party and a deal over MCP, as the actions allow", async () => {
618 const dan = as("u11", "dan");
619 const eve = as("u12", "eve");
620 const call = async (name: string, args: unknown, who: Record<string, string> = {}) =>
621 (await rpc({ jsonrpc: "2.0", id: 11, method: "tools/call", params: { name, arguments: args } }, who)).result;
622 const read = async (id: number, who = dan) => JSON.parse((await call("get_deal", { id }, who)).content[0].text);
623 const opened = await call("open_deal", { address: "9 Fix Mcp Row", buyer: "Bo", seller: "Su", contract_date: "2027-05-01", closing_date: "2027-06-10" }, dan);
624 const { id } = JSON.parse(opened.content[0].text);
625 const deal = await read(id);
626 const buyer = deal.parties.find((p: { role: string }) => p.role === "buyer");
627 const standard = deal.deadlines.find((d: { kind: string }) => d.kind === "inspection");
628 expect(buyer.id).toBeNumber();
629
630 const edit = { deal_id: id, party_id: buyer.id, name: "Bo B.", contact: "[email protected]" };
631 expect((await call("edit_party", edit)).isError).toBe(true);
632 expect((await call("edit_party", { ...edit, name: "" }, dan)).isError).toBe(true);
633 expect((await call("edit_party", edit, dan)).isError).toBeUndefined();
634 expect((await read(id)).parties.find((p: { id: number }) => p.id === buyer.id)).toMatchObject({ name: "Bo B.", contact: "[email protected]" });
635 const draft = drafts(await read(id), agent).find((d) => d.contact === "[email protected]");
636 expect(draft).toBeDefined();
637
638 const refused = await call("remove_date", { deal_id: id, date_id: standard.id }, dan);
639 expect(refused.isError).toBe(true);
640 expect(refused.content[0].text).toContain("standard contract date");
641 await call("add_date", { deal_id: id, label: "HOA review", due: "2027-05-20", owes: "buyer" }, dan);
642 const withCustom = (await read(id)).deadlines.find((d: { label: string }) => d.label === "HOA review");
643 expect(withCustom).toBeDefined();
644 expect((await call("remove_date", { deal_id: id, date_id: withCustom.id }, dan)).isError).toBeUndefined();
645 expect((await read(id)).deadlines.some((d: { label: string }) => d.label === "HOA review")).toBe(false);
646
647 expect((await call("remove_party", { deal_id: id, party_id: buyer.id }, dan)).isError).toBeUndefined();
648 expect((await read(id)).parties.some((p: { id: number }) => p.id === buyer.id)).toBe(false);
649
650 expect((await call("remove_deal", { deal_id: id }, eve)).isError).toBe(true);
651 expect((await read(id)).id).toBe(id);
652 expect((await call("remove_deal", { deal_id: id }, dan)).isError).toBeUndefined();
653 expect((await call("get_deal", { id }, dan)).isError).toBe(true);
654 });
655
612656 test("Check the dates lists what Open deal would create, and writes nothing", async () => {
613657 const dana = as("u7", "dana");
614658 const count = async () => ((await (await api("/api/deals", {}, dana)).json()) as unknown[]).length;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs