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
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/6-an-agent-can-open-a-deal-add-a-date-add → main 1 commit fork
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 | ||
| 66 | 66 | stranger gets 401; it sends nothing and changes nothing). `GET /deals/:id/ics` is a deal's calendar file. The JSON API is |
| 67 | 67 | `/api/deals`, described at `/openapi.json`; the same operations are MCP |
| 68 | 68 | 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`). | |
| 70 | 71 | |
| 71 | 72 | The terminal does the page's work too. It opens on every open date, soonest |
| 72 | 73 | first: `n` opens a new deal (address, buyer, seller, contract date, closing |
src/mcp.ts +81 −1
| @@ -9,7 +9,17 @@ | ||
| 9 | 9 | import type { Database } from "bun:sqlite"; |
| 10 | 10 | import type { Result } from "./actions.ts"; |
| 11 | 11 | 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"; | |
| 13 | 23 | import { agent, title } from "./env.ts"; |
| 14 | 24 | import { dueOn } from "./reminders.ts"; |
| 15 | 25 | import { fields } from "./http.ts"; |
| @@ -87,6 +97,76 @@ export const tools: readonly Tool[] = [ | ||
| 87 | 97 | run: (db, who, args) => |
| 88 | 98 | sendReminder(db, who, { deadlineId: Number(args.deadline_id), partyId: Number(args.party_id), moment: args.moment }), |
| 89 | 99 | }, |
| 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 | }, | |
| 90 | 170 | ]; |
| 91 | 171 | |
| 92 | 172 | /** 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 = ( | ||
| 28 | 28 | }; |
| 29 | 29 | }; |
| 30 | 30 | const alice = as("u1", "alice"); |
| 31 | const carol = as("u3", "carol"); | |
| 31 | 32 | const bob = as("u2", "bob"); |
| 32 | 33 | const root = as("u0", "root", "admin"); |
| 33 | 34 | const jsonBody = (body: unknown): RequestInit => ({ |
| @@ -157,6 +158,9 @@ test("the MCP server lists the tools and calls them as the caller", async () => | ||
| 157 | 158 | "mark_done", |
| 158 | 159 | "move_date", |
| 159 | 160 | "send_reminder", |
| 161 | "open_deal", | |
| 162 | "add_date", | |
| 163 | "add_party", | |
| 160 | 164 | ]); |
| 161 | 165 | const call = (name: string, args: unknown, who = {}) => |
| 162 | 166 | rpc( |
| @@ -184,6 +188,54 @@ test("the MCP server lists the tools and calls them as the caller", async () => | ||
| 184 | 188 | expect(notified.status).toBe(202); |
| 185 | 189 | }); |
| 186 | 190 | |
| 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 | ||
| 187 | 239 | test("a change is on the event stream, and a late client hears what it missed", async () => { |
| 188 | 240 | const stream = await fetch(base + "/events"); |
| 189 | 241 | expect(stream.headers.get("content-type")).toBe("text/event-stream"); |
new above
offline