Public: anyone can read this; the crew can change it.
An agent over MCP or the API finds a deal by address, client or party name with the same query the clock's Find box uses #46
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/44-an-agent-over-mcp-or-the-api-finds-a-dea → main 1 commit fork
6 files
changed +66
−10
README.md +5 −1
| @@ -27,7 +27,11 @@ party's name, phone or email, into **Find a deal** above the clock and press | ||
| 27 | 27 | Find (try `harbor`). The clock, This week and today's reminders narrow to the |
| 28 | 28 | deals that match, with no JavaScript needed; **Clear** brings them all back. |
| 29 | 29 | In the terminal, press / and type the same, then enter; the dates, week and |
| 30 | reminders views narrow to it, and / then enter alone clears it. | |
| 30 | reminders views narrow to it, and / then enter alone clears it. An agent finds | |
| 31 | a deal by name the same way: over MCP, `list_deals` takes an optional `query` | |
| 32 | ("what's left on the Elm Street deal?"), and `GET /api/deals?q=elm` does the | |
| 33 | same; a query over 100 characters or with control characters is refused (400, | |
| 34 | `{error:"invalid", field:"q"}`). | |
| 31 | 35 | |
| 32 | 36 | Signed out, `/` shows what deal-clock does, three short points, a still picture of the clock drawn from a made-up deal, and one **Sign in** button; no real deal is read. |
| 33 | 37 |
contract/openapi.json +12 −2
| @@ -52,11 +52,21 @@ | ||
| 52 | 52 | }, |
| 53 | 53 | "/api/deals": { |
| 54 | 54 | "get": { |
| 55 | "summary": "Every deal with its dates, parties and sent reminders, soonest closing first.", | |
| 55 | "summary": "Every deal with its dates, parties and sent reminders, soonest closing first; q keeps the deals whose address or a party's name or contact contains it.", | |
| 56 | 56 | "operationId": "getApiDeals", |
| 57 | "parameters": [ | |
| 58 | { | |
| 59 | "name": "q", | |
| 60 | "in": "query", | |
| 61 | "schema": { | |
| 62 | "type": "string", | |
| 63 | "maxLength": 100 | |
| 64 | } | |
| 65 | } | |
| 66 | ], | |
| 57 | 67 | "responses": { |
| 58 | 68 | "200": { |
| 59 | "description": "Every deal with its dates, parties and sent reminders, soonest closing first.", | |
| 69 | "description": "Every deal with its dates, parties and sent reminders, soonest closing first; q keeps the deals whose address or a party's name or contact contains it.", | |
| 60 | 70 | "content": { |
| 61 | 71 | "application/json": { |
| 62 | 72 | "schema": { |
src/find.ts +3
| @@ -7,6 +7,9 @@ import type { DealView } from "./deals.ts"; | ||
| 7 | 7 | |
| 8 | 8 | export const FIND_MAX = 100; |
| 9 | 9 | |
| 10 | /** What a refused query says, on the page, over the API and over MCP alike. */ | |
| 11 | export const FIND_REFUSAL = `Find takes up to ${FIND_MAX} characters, with no control characters.`; | |
| 12 | ||
| 10 | 13 | /** The text a deal is found by: address, every party (buyer and seller included)'s name and contact. */ |
| 11 | 14 | const haystack = (deal: DealView): string => |
| 12 | 15 | [deal.address, ...deal.parties.flatMap((p) => [p.name, p.contact])] |
src/mcp.ts +9 −3
| @@ -28,6 +28,7 @@ import { | ||
| 28 | 28 | } from "./deals.ts"; |
| 29 | 29 | import { agent, title } from "./env.ts"; |
| 30 | 30 | import { dueOn } from "./reminders.ts"; |
| 31 | import { FIND_REFUSAL, find, query } from "./find.ts"; | |
| 31 | 32 | import { fields } from "./http.ts"; |
| 32 | 33 | import { timelineRows } from "./timeline.ts"; |
| 33 | 34 | import { owner, thisWeek } from "./week.ts"; |
| @@ -47,9 +48,14 @@ interface Tool { | ||
| 47 | 48 | export const tools: readonly Tool[] = [ |
| 48 | 49 | { |
| 49 | 50 | name: "list_deals", |
| 50 | description: "Every deal with its dates, parties and sent reminders, soonest closing first.", | |
| 51 | inputSchema: { type: "object", properties: {} }, | |
| 52 | run: (db, who) => listDeals(db, who), | |
| 51 | description: "Every deal with its dates, parties and sent reminders, soonest closing first. With query, only the deals whose address, or a party's name, phone or email, contains it (case-insensitive, up to 100 characters): ask for \"Elm Street\" or a client's name instead of listing every deal.", | |
| 52 | inputSchema: { type: "object", properties: { query: { type: "string", maxLength: 100 } } }, | |
| 53 | run: (db, who, args) => { | |
| 54 | const q = args.query === undefined ? "" : typeof args.query === "string" ? query(args.query) : undefined; | |
| 55 | if (q === undefined) return { ok: false, error: "invalid", message: FIND_REFUSAL }; | |
| 56 | const deals = listDeals(db, who); | |
| 57 | return deals.ok ? { ok: true, value: find(deals.value, q) } : deals; | |
| 58 | }, | |
| 53 | 59 | }, |
| 54 | 60 | { |
| 55 | 61 | name: "get_deal", |
src/routes.ts +13 −4
| @@ -59,7 +59,7 @@ import { | ||
| 59 | 59 | } from "./deals.ts"; |
| 60 | 60 | import { calendar } from "./ics.ts"; |
| 61 | 61 | import { rpc } from "./mcp.ts"; |
| 62 | import { FIND_MAX, query } from "./find.ts"; | |
| 62 | import { FIND_REFUSAL, find, query } from "./find.ts"; | |
| 63 | 63 | import { clock } from "./pages/clock.ts"; |
| 64 | 64 | import { dealPage, newDealPage } from "./pages/deal.ts"; |
| 65 | 65 | import { refusedPage } from "./pages/refused.ts"; |
| @@ -337,7 +337,7 @@ export const routes: readonly Route[] = [ | ||
| 337 | 337 | path: "/", |
| 338 | 338 | heading: "The clock", |
| 339 | 339 | error: "invalid", |
| 340 | message: `Find takes up to ${FIND_MAX} characters, with no control characters.`, | |
| 340 | message: FIND_REFUSAL, | |
| 341 | 341 | }), |
| 342 | 342 | 400, |
| 343 | 343 | clear, |
| @@ -581,8 +581,17 @@ export const routes: readonly Route[] = [ | ||
| 581 | 581 | { |
| 582 | 582 | method: "GET", |
| 583 | 583 | path: "/api/deals", |
| 584 | handle: (c) => answer(listDeals(c.db, c.who)), | |
| 585 | api: { summary: "Every deal with its dates, parties and sent reminders, soonest closing first.", answer: list(DEAL) }, | |
| 584 | handle: (c) => { | |
| 585 | const q = query(c.url.searchParams.get("q")); | |
| 586 | if (q === undefined) return json({ error: "invalid", field: "q", message: FIND_REFUSAL }, 400); | |
| 587 | const deals = listDeals(c.db, c.who); | |
| 588 | return answer(deals.ok ? { ok: true, value: find(deals.value, q) } : deals); | |
| 589 | }, | |
| 590 | api: { | |
| 591 | summary: "Every deal with its dates, parties and sent reminders, soonest closing first; q keeps the deals whose address or a party's name or contact contains it.", | |
| 592 | answer: list(DEAL), | |
| 593 | query: { q: { type: "string", maxLength: 100 } }, | |
| 594 | }, | |
| 586 | 595 | }, |
| 587 | 596 | { |
| 588 | 597 | method: "POST", |
src/server.test.ts +24
| @@ -983,3 +983,27 @@ test("a Find narrows This week, today's reminders and the Closed card, and a mis | ||
| 983 | 983 | expect(section(miss, "today")).toBe(""); |
| 984 | 984 | expect(section(miss, "week")).toBe(""); |
| 985 | 985 | }); |
| 986 | ||
| 987 | test("an agent finds a deal by name over MCP and GET /api/deals?q=, with one refusal for a bad query", async () => { | |
| 988 | const alice = as("u-namefind", "nina"); | |
| 989 | const call = async (args: unknown) => | |
| 990 | (await rpc({ jsonrpc: "2.0", id: 5, method: "tools/call", params: { name: "list_deals", arguments: args } }, alice)).result; | |
| 991 | const everything = JSON.parse((await call({})).content[0].text); | |
| 992 | expect(everything.length).toBeGreaterThan(1); | |
| 993 | expect(JSON.parse((await call({ query: "" })).content[0].text)).toHaveLength(everything.length); | |
| 994 | const viaMcp = JSON.parse((await call({ query: " HARBOR view " })).content[0].text); | |
| 995 | expect(viaMcp.map((d: any) => d.address)).toEqual(["77 Harbor View Dr, Unit 4B"]); | |
| 996 | const res = await api("/api/deals?q=" + encodeURIComponent("harbor view"), {}, alice); | |
| 997 | expect(res.status).toBe(200); | |
| 998 | expect(await res.json()).toEqual(viaMcp); | |
| 999 | expect(await (await api("/api/deals", {}, alice)).json()).toHaveLength(everything.length); | |
| 1000 | const long = "a".repeat(101); | |
| 1001 | const refused = await api("/api/deals?q=" + long, {}, alice); | |
| 1002 | expect(refused.status).toBe(400); | |
| 1003 | expect(await refused.json()).toMatchObject({ error: "invalid", field: "q" }); | |
| 1004 | const bad = await call({ query: long }); | |
| 1005 | expect(bad.isError).toBe(true); | |
| 1006 | expect(bad.content[0].text).toContain("Find takes up to 100 characters"); | |
| 1007 | expect((await call({ query: 5 })).isError).toBe(true); | |
| 1008 | expect((await api("/api/deals?q=a%01", {}, alice)).status).toBe(400); | |
| 1009 | }); |
new above
offline