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

An agent can open a deal, add a date, add a party and reopen a date over MCP, like on the page #14

Merged opened this

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

Merged

mason/deal-clock:mason/6-an-agent-can-open-a-deal-add-a-date-add → main 1 commit fork

  • Merged. mason/6-an-agent-can-open-a-deal-add-a-date-add was merged into main as d8f7cb2.

Review

Sign in to review this change.
3 files changed +135 −2
README.md +2 −1
@@ -66,7 +66,8 @@ marked sent, as JSON — call it each morning as a signed-in account (a
6666 stranger gets 401; it sends nothing and changes nothing). `GET /deals/:id/ics` is a deal's calendar file. The JSON API is
6767 `/api/deals`, described at `/openapi.json`; the same operations are MCP
6868 tools at `POST /mcp` (`list_deals`, `get_deal`, `reminders_today`,
69 `mark_done`, `move_date`, `send_reminder`).
69 `open_deal`, `add_date`, `add_party`, `mark_done` — with `done: false` to
70 reopen a date — `move_date`, `send_reminder`).
7071
7172 The terminal does the page's work too. It opens on every open date, soonest
7273 first: `n` opens a new deal (address, buyer, seller, contract date, closing
src/mcp.ts +81 −1
@@ -9,7 +9,17 @@
99 import type { Database } from "bun:sqlite";
1010 import type { Result } from "./actions.ts";
1111 import { today } from "./dates.ts";
12 import { getDeal, listDeals, markDone, moveDeadline, sendReminder } from "./deals.ts";
12 import { PARTY_ROLES } from "./deadlines.ts";
13 import {
14 addDeadline,
15 addParty,
16 createDeal,
17 getDeal,
18 listDeals,
19 markDone,
20 moveDeadline,
21 sendReminder,
22 } from "./deals.ts";
1323 import { agent, title } from "./env.ts";
1424 import { dueOn } from "./reminders.ts";
1525 import { fields } from "./http.ts";
@@ -87,6 +97,76 @@ export const tools: readonly Tool[] = [
8797 run: (db, who, args) =>
8898 sendReminder(db, who, { deadlineId: Number(args.deadline_id), partyId: Number(args.party_id), moment: args.moment }),
8999 },
100 {
101 name: "open_deal",
102 description: "Open a deal with its buyer and seller; the standard dates are scheduled from the contract and closing dates.",
103 inputSchema: {
104 type: "object",
105 properties: {
106 address: { type: "string", minLength: 3, maxLength: 200 },
107 buyer: { type: "string", maxLength: 100 },
108 seller: { type: "string", maxLength: 100 },
109 contract_date: { type: "string", format: "date" },
110 closing_date: { type: "string", format: "date" },
111 inspection: { type: "integer", minimum: 0 },
112 appraisal: { type: "integer", minimum: 0 },
113 financing: { type: "integer", minimum: 0 },
114 title: { type: "integer", minimum: 0 },
115 walkthrough: { type: "integer", minimum: 0 },
116 business_days: { type: "boolean" },
117 },
118 required: ["address", "buyer", "seller", "contract_date", "closing_date"],
119 },
120 run: (db, who, a) =>
121 createDeal(db, who, {
122 address: a.address,
123 buyer: a.buyer,
124 seller: a.seller,
125 contractDate: a.contract_date,
126 closingDate: a.closing_date,
127 lengths: {
128 inspection: a.inspection,
129 appraisal: a.appraisal,
130 financing: a.financing,
131 title: a.title,
132 walkthrough: a.walkthrough,
133 },
134 businessDays: a.business_days,
135 }),
136 },
137 {
138 name: "add_date",
139 description: "Add a custom date to a deal, saying which party owes it.",
140 inputSchema: {
141 type: "object",
142 properties: {
143 deal_id: { type: "integer" },
144 label: { type: "string", maxLength: 100 },
145 due: { type: "string", format: "date" },
146 owes: { enum: [...PARTY_ROLES] },
147 details: { type: "string", maxLength: 200 },
148 },
149 required: ["deal_id", "label", "due", "owes"],
150 },
151 run: (db, who, a) =>
152 addDeadline(db, who, { dealId: Number(a.deal_id), label: a.label, due: a.due, owes: a.owes, what: a.details }),
153 },
154 {
155 name: "add_party",
156 description: "Add a party (lender, title company, the other agent…) to a deal.",
157 inputSchema: {
158 type: "object",
159 properties: {
160 deal_id: { type: "integer" },
161 role: { enum: [...PARTY_ROLES] },
162 name: { type: "string", maxLength: 100 },
163 contact: { type: "string", maxLength: 120 },
164 },
165 required: ["deal_id", "role", "name"],
166 },
167 run: (db, who, a) =>
168 addParty(db, who, { dealId: Number(a.deal_id), role: a.role, name: a.name, contact: a.contact }),
169 },
90170 ];
91171
92172 /** One JSON-RPC message in, an HTTP status and a body out (a notification gets 202 and no body). */
src/server.test.ts +52
@@ -28,6 +28,7 @@ const as = (
2828 };
2929 };
3030 const alice = as("u1", "alice");
31 const carol = as("u3", "carol");
3132 const bob = as("u2", "bob");
3233 const root = as("u0", "root", "admin");
3334 const jsonBody = (body: unknown): RequestInit => ({
@@ -157,6 +158,9 @@ test("the MCP server lists the tools and calls them as the caller", async () =>
157158 "mark_done",
158159 "move_date",
159160 "send_reminder",
161 "open_deal",
162 "add_date",
163 "add_party",
160164 ]);
161165 const call = (name: string, args: unknown, who = {}) =>
162166 rpc(
@@ -184,6 +188,54 @@ test("the MCP server lists the tools and calls them as the caller", async () =>
184188 expect(notified.status).toBe(202);
185189 });
186190
191 test("an agent opens a deal, adds a date and a party and reopens a date over MCP", async () => {
192 const call = async (name: string, args: unknown, who = {}) =>
193 (
194 await rpc(
195 { jsonrpc: "2.0", id: 7, method: "tools/call", params: { name, arguments: args } },
196 who,
197 )
198 ).result;
199 const read = async (id: number) => JSON.parse((await call("get_deal", { id }, carol)).content[0].text);
200 const deal = {
201 address: "7 Mcp Court",
202 buyer: "Ann",
203 seller: "Sam",
204 contract_date: "2026-10-01",
205 closing_date: "2026-11-20",
206 inspection: 5,
207 };
208 expect((await call("open_deal", deal)).isError).toBe(true);
209 const bad = await call("open_deal", { ...deal, address: "" }, carol);
210 expect(bad.isError).toBe(true);
211 expect(bad.content[0].text).toContain("invalid");
212 const opened = await call("open_deal", deal, carol);
213 const { id } = JSON.parse(opened.content[0].text);
214 const got = await read(id);
215 expect(got.address).toBe("7 Mcp Court");
216 expect(got.parties.map((p: { name: string }) => p.name)).toEqual(["Ann", "Sam"]);
217
218 const date = { deal_id: id, label: "Survey due", due: "2026-10-20", owes: "seller", details: "ALTA" };
219 expect((await call("add_date", date)).isError).toBe(true);
220 expect((await call("add_date", { ...date, owes: "nobody" }, carol)).isError).toBe(true);
221 expect((await call("add_date", date, carol)).isError).toBeUndefined();
222 const survey = (await read(id)).deadlines.find((d: { label: string }) => d.label === "Survey due");
223 expect(survey.what).toBe("ALTA");
224
225 const party = { deal_id: id, role: "lender", name: "First Bank", contact: "[email protected]" };
226 expect((await call("add_party", party)).isError).toBe(true);
227 expect((await call("add_party", { ...party, role: "pirate" }, carol)).isError).toBe(true);
228 expect((await call("add_party", party, carol)).isError).toBeUndefined();
229 expect((await read(id)).parties.map((p: { name: string }) => p.name)).toContain("First Bank");
230
231 await call("mark_done", { id: survey.id }, carol);
232 const closed = (await read(id)).deadlines.find((d: { id: number }) => d.id === survey.id);
233 expect(closed.done_at).not.toBeNull();
234 await call("mark_done", { id: survey.id, done: false }, carol);
235 const again = (await read(id)).deadlines.find((d: { id: number }) => d.id === survey.id);
236 expect(again.done_at).toBeNull();
237 });
238
187239 test("a change is on the event stream, and a late client hears what it missed", async () => {
188240 const stream = await fetch(base + "/events");
189241 expect(stream.headers.get("content-type")).toBe("text/event-stream");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs