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

Merged opened this

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

Merged

mason/deal-clock:mason/44-an-agent-over-mcp-or-the-api-finds-a-dea → main 1 commit fork

  • Merged. mason/44-an-agent-over-mcp-or-the-api-finds-a-dea was merged into main as df54c25.

Review

Sign in to review this change.
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
2727 Find (try `harbor`). The clock, This week and today's reminders narrow to the
2828 deals that match, with no JavaScript needed; **Clear** brings them all back.
2929 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"}`).
3135
3236 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.
3337
contract/openapi.json +12 −2
@@ -52,11 +52,21 @@
5252 },
5353 "/api/deals": {
5454 "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.",
5656 "operationId": "getApiDeals",
57 "parameters": [
58 {
59 "name": "q",
60 "in": "query",
61 "schema": {
62 "type": "string",
63 "maxLength": 100
64 }
65 }
66 ],
5767 "responses": {
5868 "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.",
6070 "content": {
6171 "application/json": {
6272 "schema": {
src/find.ts +3
@@ -7,6 +7,9 @@ import type { DealView } from "./deals.ts";
77
88 export const FIND_MAX = 100;
99
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
1013 /** The text a deal is found by: address, every party (buyer and seller included)'s name and contact. */
1114 const haystack = (deal: DealView): string =>
1215 [deal.address, ...deal.parties.flatMap((p) => [p.name, p.contact])]
src/mcp.ts +9 −3
@@ -28,6 +28,7 @@ import {
2828 } from "./deals.ts";
2929 import { agent, title } from "./env.ts";
3030 import { dueOn } from "./reminders.ts";
31 import { FIND_REFUSAL, find, query } from "./find.ts";
3132 import { fields } from "./http.ts";
3233 import { timelineRows } from "./timeline.ts";
3334 import { owner, thisWeek } from "./week.ts";
@@ -47,9 +48,14 @@ interface Tool {
4748 export const tools: readonly Tool[] = [
4849 {
4950 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 },
5359 },
5460 {
5561 name: "get_deal",
src/routes.ts +13 −4
@@ -59,7 +59,7 @@ import {
5959 } from "./deals.ts";
6060 import { calendar } from "./ics.ts";
6161 import { rpc } from "./mcp.ts";
62 import { FIND_MAX, query } from "./find.ts";
62 import { FIND_REFUSAL, find, query } from "./find.ts";
6363 import { clock } from "./pages/clock.ts";
6464 import { dealPage, newDealPage } from "./pages/deal.ts";
6565 import { refusedPage } from "./pages/refused.ts";
@@ -337,7 +337,7 @@ export const routes: readonly Route[] = [
337337 path: "/",
338338 heading: "The clock",
339339 error: "invalid",
340 message: `Find takes up to ${FIND_MAX} characters, with no control characters.`,
340 message: FIND_REFUSAL,
341341 }),
342342 400,
343343 clear,
@@ -581,8 +581,17 @@ export const routes: readonly Route[] = [
581581 {
582582 method: "GET",
583583 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 },
586595 },
587596 {
588597 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
983983 expect(section(miss, "today")).toBe("");
984984 expect(section(miss, "week")).toBe("");
985985 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs