diff --git a/README.md b/README.md index 58edcdb..8394994 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ npm run typecheck && npm test # 252 server-side tests + 33 web tests npm run e2e # 17 real-browser tests (Playwright), incl. the two-approver flow and accessibility scans of every page ``` +**Visual card (MCP Apps):** tools return a `ui://` card hosts can render on screens (verdict, red flags, "Alexa cannot approve this"); voice-only clients still get full text. + **Connecting to Alexa+:** [docs/alexa-integration.md](docs/alexa-integration.md) says what is real (MCP server, OAuth 2.1 + PKCE account linking) and what is simulated (the Alexa+ voice assistant), and lists the steps to connect. **Safe rule changes:** `npm run backtest -- --candidate rules-2026.10.4 --baseline rules-2026.10.3` replays every stored check under two rule sets and lists who got stricter or looser, before anything goes live. diff --git a/apps/mcp-server/package.json b/apps/mcp-server/package.json index f0d3f2e..f81452e 100644 --- a/apps/mcp-server/package.json +++ b/apps/mcp-server/package.json @@ -1,3 +1 @@ -{ "name": "@second-opinion/mcp-server", "version": "0.1.0", "private": true, "type": "module", - "dependencies": { "@second-opinion/shared": "*", "@second-opinion/engine": "*", "@second-opinion/store": "*", "@second-opinion/policy": "*", "@second-opinion/notify": "*", - "@modelcontextprotocol/sdk": "1.32.1", "express": "^5.2.1", "zod": "^4.0.0" } } +{"name":"@second-opinion/mcp-server","version":"0.1.0","private":true,"type":"module","dependencies":{"@modelcontextprotocol/ext-apps":"^1.7.5","@modelcontextprotocol/sdk":"1.32.1","@second-opinion/engine":"*","@second-opinion/notify":"*","@second-opinion/policy":"*","@second-opinion/shared":"*","@second-opinion/store":"*","express":"^5.2.1","zod":"^4.0.0"},"devDependencies":{"esbuild":"^0.28.2"}} \ No newline at end of file diff --git a/apps/mcp-server/src/server.ts b/apps/mcp-server/src/server.ts index 1220832..ad1b89f 100644 --- a/apps/mcp-server/src/server.ts +++ b/apps/mcp-server/src/server.ts @@ -11,6 +11,7 @@ import { registerPlaceHold } from './tools/placeHold'; import { registerRequestFamilyApproval } from './tools/requestFamilyApproval'; import { registerResolveApproval } from './tools/resolveApproval'; import { registerTrustedPayees } from './tools/trustedPayees'; +import { registerCard } from './ui/register'; export const SERVER_NAME = 'second-opinion'; export const SERVER_VERSION = '0.1.0'; @@ -37,5 +38,6 @@ export function buildServer(deps: Deps): McpServer { registerListNotifications(server, deps); registerPolicyTools(server, deps); registerHouseholdInsights(server, deps); + registerCard(server); return server; } diff --git a/apps/mcp-server/src/tools/checkMessage.ts b/apps/mcp-server/src/tools/checkMessage.ts index 0e9dc51..d41b7fa 100644 --- a/apps/mcp-server/src/tools/checkMessage.ts +++ b/apps/mcp-server/src/tools/checkMessage.ts @@ -49,6 +49,7 @@ export function registerCheckMessage(server: McpServer, deps: Deps): void { decision: decisionSchema, }, idempotent: true, + ui: true, annotations: { title: 'Check a message or payment request', readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }, household: (a) => a.householdId, handler: ({ householdId, text, channel, context }, ctx) => { diff --git a/apps/mcp-server/src/tools/define.ts b/apps/mcp-server/src/tools/define.ts index 62682cb..19324f0 100644 --- a/apps/mcp-server/src/tools/define.ts +++ b/apps/mcp-server/src/tools/define.ts @@ -4,6 +4,7 @@ import type { CallToolResult, ToolAnnotations } from '@modelcontextprotocol/sdk/ import { ToolError, ID_PATTERN } from '@second-opinion/shared'; import { z } from 'zod'; import type { Deps } from '../deps'; +import { CARD_URI } from '../ui/register'; /** Household/person/hold ids: prefixed, bounded, safe characters. */ export const idSchema = z.string().max(80).regex(ID_PATTERN, 'must look like prefix_id'); @@ -58,6 +59,8 @@ interface Spec { annotations: ToolAnnotations; /** Adds an optional `idempotencyKey` input and `replayed` output. Handlers must be synchronous so the result and its key commit atomically. */ idempotent?: boolean; + /** Show the Second Opinion card (MCP App) when the host can render UI. */ + ui?: boolean; handler: (args: z.infer>, ctx: ToolContext) => z.infer>; /** household the call acts on, for audit + logs */ household?: (args: z.infer>) => string | undefined; @@ -74,7 +77,10 @@ export function defineTool(ser server.registerTool( spec.name, - { title: spec.title, description: spec.description, inputSchema: input, outputSchema: output, annotations: spec.annotations }, + { + title: spec.title, description: spec.description, inputSchema: input, outputSchema: output, annotations: spec.annotations, + ...(spec.ui ? { _meta: { ui: { resourceUri: CARD_URI }, 'ui/resourceUri': CARD_URI } } : {}), + }, (async (rawArgs: Record, extra: RequestExtra): Promise => { const started = performance.now(); const traceId = traceIdFrom(extra); diff --git a/apps/mcp-server/src/tools/getHold.ts b/apps/mcp-server/src/tools/getHold.ts index 7629aad..f545af5 100644 --- a/apps/mcp-server/src/tools/getHold.ts +++ b/apps/mcp-server/src/tools/getHold.ts @@ -35,6 +35,7 @@ export function registerGetHold(server: McpServer, deps: Deps): void { votes: z.array(z.object({ approverId: z.string(), name: z.string(), decision: z.enum(['approve', 'deny']), note: z.string().nullable(), decidedAt: z.string() })), timeline: z.array(z.object({ type: z.string(), label: z.string(), at: z.string(), by: z.string().nullable() })), }, + ui: true, annotations: { title: 'Get one held action with its votes and timeline', readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }, handler: ({ holdId }) => { const h = deps.repos.holds.get(holdId); diff --git a/apps/mcp-server/src/ui/card.generated.ts b/apps/mcp-server/src/ui/card.generated.ts new file mode 100644 index 0000000..3493be4 --- /dev/null +++ b/apps/mcp-server/src/ui/card.generated.ts @@ -0,0 +1,2 @@ +// GENERATED by scripts/build-card.mjs from apps/mcp-server/ui/card.ts. Do not edit; run `npm run build:card`. +export const CARD_HTML = "Second Opinion
Checking

Checking with Second Opinion...

"; diff --git a/apps/mcp-server/src/ui/register.ts b/apps/mcp-server/src/ui/register.ts new file mode 100644 index 0000000..d899c2d --- /dev/null +++ b/apps/mcp-server/src/ui/register.ts @@ -0,0 +1,17 @@ +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +import { registerAppResource, RESOURCE_MIME_TYPE } from '@modelcontextprotocol/ext-apps/server'; +import { CARD_HTML } from './card.generated'; + +/** MCP App view shown by hosts that render UI (Alexa+ on a screen). Tools without UI support still return full text and structuredContent. */ +export const CARD_URI = 'ui://second-opinion/card.html'; + +export function registerCard(server: McpServer): void { + registerAppResource( + server, + 'Second Opinion card', + CARD_URI, + // No network access is needed or declared: the view is a single self-contained document. + { mimeType: RESOURCE_MIME_TYPE, description: 'Shows the verdict, the red flags and who must approve a request.' }, + async () => ({ contents: [{ uri: CARD_URI, mimeType: RESOURCE_MIME_TYPE, text: CARD_HTML }] }), + ); +} diff --git a/apps/mcp-server/test/card.test.ts b/apps/mcp-server/test/card.test.ts new file mode 100644 index 0000000..3666e5f --- /dev/null +++ b/apps/mcp-server/test/card.test.ts @@ -0,0 +1,46 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +// @ts-expect-error plain .mjs build script +import { buildCardHtml } from '../../../scripts/build-card.mjs'; +import { CARD_HTML } from '../src/ui/card.generated'; +import { CARD_URI } from '../src/ui/register'; +import { startTestServer } from './helpers'; + +test('T-CARD-1 the visual tools point at the card and the others do not (MCP Apps metadata)', async () => { + const t = await startTestServer(); + try { + const { client } = await t.connect(); + const tools = (await client.listTools()).tools; + const meta = (name: string) => (tools.find((x) => x.name === name)?._meta ?? {}) as { ui?: { resourceUri?: string } }; + assert.equal(meta('check_message').ui?.resourceUri, CARD_URI); + assert.equal(meta('get_hold').ui?.resourceUri, CARD_URI); + assert.equal(meta('resolve_approval').ui, undefined); + assert.equal(tools.length, 12, 'no tool was added or lost'); + await client.close(); + } finally { + await t.close(); + } +}); + +test('T-CARD-2 the card is served as an MCP App resource: right MIME type, self-contained, shows the key states', async () => { + const t = await startTestServer(); + try { + const { client } = await t.connect(); + const listed = (await client.listResources()).resources.find((r) => r.uri === CARD_URI); + assert.ok(listed); + assert.match(listed!.mimeType ?? '', /^text\/html;profile=mcp-app/); + const read = await client.readResource({ uri: CARD_URI }); + const html = String((read.contents[0] as { text?: string }).text); + assert.equal(read.contents[0]?.mimeType, listed!.mimeType); + assert.match(html, /^/i); + assert.doesNotMatch(html, /]+src=|]+href=|@import/i, 'no external files, so no network permission is needed'); + for (const text of ['Paused for your family', 'Be careful', 'Looks fine', 'Alexa cannot approve this']) assert.ok(html.includes(text), text); + await client.close(); + } finally { + await t.close(); + } +}); + +test('T-CARD-3 the generated card is up to date with its source (run `npm run build:card` if this fails)', async () => { + assert.equal(CARD_HTML, await buildCardHtml()); +}); diff --git a/apps/mcp-server/ui/card.ts b/apps/mcp-server/ui/card.ts new file mode 100644 index 0000000..becf6ca --- /dev/null +++ b/apps/mcp-server/ui/card.ts @@ -0,0 +1,77 @@ +// Browser code for the Second Opinion card (an MCP App view). Bundled into one HTML string by scripts/build-card.mjs. +// It only displays what the tool returned; it never decides anything and cannot approve a hold. +import { App } from '@modelcontextprotocol/ext-apps'; + +type Level = 'allow' | 'caution' | 'hold'; +interface CheckShape { + verdict?: Level; + spokenSummary?: string; + card?: { title?: string; level?: Level; flags?: string[]; advice?: string }; + decision?: { outcome?: string; reasonText?: string[] }; +} +interface HoldShape { + hold?: { status?: string; description?: string; payee?: string | null; amountCents?: number | null }; + risk?: { verdict?: Level; flags?: string[] }; + votes?: Array<{ name: string; decision: string }>; +} + +const LABEL: Record = { hold: 'Paused for your family', caution: 'Be careful', allow: 'Looks fine' }; +const STATUS: Record = { + pending: 'Waiting for a family member to decide', + released: 'Approved by your family', + denied: 'Your family said no', + expired: 'Timed out, still blocked', +}; + +const root = document.getElementById('card')!; + +function money(cents?: number | null): string { + return typeof cents === 'number' ? `$${(cents / 100).toLocaleString('en-US', { minimumFractionDigits: cents % 100 ? 2 : 0 })}` : ''; +} + +function render(level: Level, headline: string, flags: string[], advice: string, footer: string): void { + root.className = `card ${level}`; + root.replaceChildren(); + const add = (tag: string, cls: string, text: string) => { + const el = document.createElement(tag); + el.className = cls; + el.textContent = text; + root.appendChild(el); + return el; + }; + add('div', 'badge', LABEL[level]); + add('p', 'headline', headline); + if (flags.length) { + const ul = document.createElement('ul'); + ul.className = 'flags'; + for (const f of flags.slice(0, 4)) { + const li = document.createElement('li'); + li.textContent = f; + ul.appendChild(li); + } + root.appendChild(ul); + } + if (advice) add('p', 'advice', advice); + if (footer) add('p', 'footer', footer); +} + +function show(sc: CheckShape & HoldShape | undefined): void { + if (!sc) return render('caution', 'I could not check this right now.', [], 'Please ask your family before paying.', 'Nothing was done.'); + if (sc.hold) { + const level = sc.risk?.verdict ?? 'hold'; + const amount = money(sc.hold.amountCents); + const what = [sc.hold.description, amount ? `(${amount})` : ''].filter(Boolean).join(' '); + const votes = (sc.votes ?? []).map((v) => `${v.name}: ${v.decision === 'approve' ? 'yes' : 'no'}`).join(', '); + return render(level, what || 'A request is waiting', sc.risk?.flags ?? [], '', [STATUS[sc.hold.status ?? ''] ?? '', votes].filter(Boolean).join(' ยท ')); + } + const level = sc.card?.level ?? sc.verdict ?? 'caution'; + const blocked = sc.decision?.outcome === 'block'; + const footer = blocked + ? `Not allowed by your family's rules. ${(sc.decision?.reasonText ?? []).join(' ')}`.trim() + : level === 'allow' ? '' : 'Alexa cannot approve this. Only your family can.'; + render(level, sc.spokenSummary ?? sc.card?.title ?? '', sc.card?.flags ?? [], sc.card?.advice ?? '', footer); +} + +const app = new App({ name: 'Second Opinion card', version: '0.1.0' }); +app.ontoolresult = (result) => show(result.structuredContent as (CheckShape & HoldShape) | undefined); +app.connect().catch(() => show(undefined)); diff --git a/docs/alexa-integration.md b/docs/alexa-integration.md index 747f228..e2d84c1 100644 --- a/docs/alexa-integration.md +++ b/docs/alexa-integration.md @@ -16,7 +16,7 @@ | OAuth 2.1 authorization code + PKCE (S256), `resource` parameter, protected-resource metadata, authorization-server metadata, static client (no dynamic registration), 401 without `WWW-Authenticate` | **Done** (`apps/mcp-server/src/oauth.ts`, tests T-OAUTH-1 to 9) | | Round trip under 500 ms | Likely (the engine takes about a millisecond and tools are synchronous SQLite), **not measured against Alexa+**. Measure through the tunnel | | Add-on package (`addon.json`, icons, carousel image, privacy and terms URLs) | Template only: `alexa-addon/addon.json`. Needs real URLs and artwork | -| Text-only is allowed (MCP Apps visuals optional) | OK | +| Visuals via the MCP Apps standard (optional) | **Done**: `check_message` and `get_hold` link to a `ui://second-opinion/card.html` view (verdict, red flags, who must approve), authored for Alexa's 768x480 reference size, self-contained (no network), with text fallback for voice-only devices. Tested at protocol level (T-CARD-1 to 3) and rendered in a browser with a stand-in host; **not yet seen on a real Alexa+ screen** | | Elicitation (Alexa+ asking the user to confirm) | Not covered by the quickstart page we read; not implemented | ## Account linking: how OAuth works here diff --git a/docs/assets/alexa-card-hold.png b/docs/assets/alexa-card-hold.png new file mode 100644 index 0000000..dfa1ed2 Binary files /dev/null and b/docs/assets/alexa-card-hold.png differ diff --git a/docs/decisions.md b/docs/decisions.md index cf17b68..b840d64 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -33,3 +33,4 @@ Log later changes below with date and impact on other docs. | 2026-10-09 | D19: published `rules-2026.10.4` (rule pack v3, playbook pack p3, new signal `ADVANCE_FEE`, hard rule `ADVANCE_FEE_PLUS_PRESSURE`). Rule sets 10.1-10.3 are frozen by golden tests. Before changing the detector we wrote bench split `test2` (40 scripts) and scored 10.1-10.3 on it; 10.4 was scored once. Caveat recorded in `eval-report.md`: the same author wrote test2 and the v4 rules, so the 95% is not a generalisation claim. | | 2026-10-09 | D20: replaced `better-sqlite3` with Node's built-in `node:sqlite` (Node >= 22.13, thin `Db` wrapper in `packages/store/src/db.ts`). Reason: a clean `npm install` on Windows tried to compile the native module and failed without Visual Studio; judges must be able to run `npm install && npm run demo`. Resolves D9. Cost: Node prints an ExperimentalWarning for `node:sqlite` on 22.x. | | 2026-10-10 | D21: added OAuth 2.1 (auth code + PKCE S256, static client, signed stateless tokens) because the Alexa+ MCP Toolkit requires it; an assistant token cannot call `resolve_approval` or `set_policy` and is bound to one household. Added `FIXED_NOW` so tests do not depend on the time of day (a simulator test failed after 22:00 Chicago time). The live Alexa+ connection is still **not** done or verified. | +| 2026-10-10 | D22: added an MCP Apps view (`ui://second-opinion/card.html`, `@modelcontextprotocol/ext-apps` 1.7.5, the 1.x line that matches SDK 1.32.1; 2.x needs the v2 SDK). Bundled into one HTML string by `scripts/build-card.mjs` (committed output, a test fails if stale). Rationale: Alexa+ renders MCP-App UI on screens and the rules list MCP Apps as a creative signal. Not verified on a real Alexa+ device. | diff --git a/package-lock.json b/package-lock.json index 837ef38..b36a38f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,13 +22,14 @@ "typescript": "^5.7.0" }, "engines": { - "node": ">=22" + "node": ">=22.13" } }, "apps/mcp-server": { "name": "@second-opinion/mcp-server", "version": "0.1.0", "dependencies": { + "@modelcontextprotocol/ext-apps": "^1.7.5", "@modelcontextprotocol/sdk": "1.32.1", "@second-opinion/engine": "*", "@second-opinion/notify": "*", @@ -37,6 +38,9 @@ "@second-opinion/store": "*", "express": "^5.2.1", "zod": "^4.0.0" + }, + "devDependencies": { + "esbuild": "^0.28.2" } }, "apps/simulator": { @@ -331,6 +335,431 @@ "node": ">=20.19.0" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, "node_modules/@esbuild/win32-x64": { "version": "0.28.2", "cpu": [ @@ -404,6 +833,35 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@modelcontextprotocol/ext-apps": { + "version": "1.7.5", + "resolved": "https://registry.npmjs.org/@modelcontextprotocol/ext-apps/-/ext-apps-1.7.5.tgz", + "integrity": "sha512-TjPH2S2y5UEGKhmI6+XGFuqfqOV4ppe1x6DA3txnUaEWkgtA4G5vo14jGKFZmegdkZ1H4QMLyujLvoU1BEdnAg==", + "license": "MIT", + "workspaces": [ + "examples/*" + ], + "dependencies": { + "@standard-schema/spec": "^1.1.0" + }, + "engines": { + "node": ">=20" + }, + "peerDependencies": { + "@modelcontextprotocol/sdk": "^1.29.0", + "react": "^17.0.0 || ^18.0.0 || ^19.0.0", + "react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0", + "zod": "^3.25.0 || ^4.0.0" + }, + "peerDependenciesMeta": { + "react": { + "optional": true + }, + "react-dom": { + "optional": true + } + } + }, "node_modules/@modelcontextprotocol/sdk": { "version": "1.32.1", "resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.32.1.tgz", @@ -768,6 +1226,12 @@ "resolved": "apps/web", "link": true }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, "node_modules/@testing-library/dom": { "version": "10.4.2", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.2.tgz", @@ -1517,6 +1981,8 @@ }, "node_modules/esbuild": { "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", "dev": true, "hasInstallScript": true, "license": "MIT", diff --git a/package.json b/package.json index c4c5a2b..6cdec66 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "dev:web": "npm -w @second-opinion/web run dev", "e2e": "playwright test", "eval": "tsx eval/run.ts", - "backtest": "tsx scripts/backtest.ts" + "backtest": "tsx scripts/backtest.ts", + "build:card": "node scripts/build-card.mjs" }, "devDependencies": { "@axe-core/playwright": "^4.13.0", diff --git a/scripts/build-card.mjs b/scripts/build-card.mjs new file mode 100644 index 0000000..c47c7a3 --- /dev/null +++ b/scripts/build-card.mjs @@ -0,0 +1,43 @@ +// Bundles apps/mcp-server/ui/card.ts into one self-contained HTML string (no network access, so no CSP exceptions are needed) +// and writes apps/mcp-server/src/ui/card.generated.ts. Run `npm run build:card` after editing the card; a test fails if it is stale. +import { build } from 'esbuild'; +import { mkdirSync, writeFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +const root = join(dirname(fileURLToPath(import.meta.url)), '..'); +const CSS = ` +:root{color-scheme:light dark} +*{box-sizing:border-box} +body{margin:0;font:22px/1.4 system-ui,-apple-system,"Segoe UI",sans-serif;background:#fff;color:#14141a} +.card{min-height:100vh;padding:20px 24px;border-left:14px solid #767680} +.card.hold{border-color:#c62828;background:#fff5f5}.card.caution{border-color:#b26a00;background:#fff8e6}.card.allow{border-color:#1b7f3b;background:#f1faf3} +.badge{display:inline-block;font-weight:700;font-size:20px;letter-spacing:.02em;padding:4px 12px;border-radius:999px;background:#14141a;color:#fff} +.hold .badge{background:#c62828}.caution .badge{background:#8a5300}.allow .badge{background:#1b7f3b} +.headline{margin:14px 0 8px;font-size:26px;font-weight:650} +.flags{margin:0 0 10px;padding-left:1.2em}.flags li{margin:2px 0} +.advice{margin:8px 0;font-weight:600}.footer{margin:12px 0 0;font-size:19px;opacity:.85} +@media (prefers-color-scheme:dark){body{background:#101014;color:#f2f2f6}.card.hold{background:#2a1214}.card.caution{background:#2a2110}.card.allow{background:#10231a}} +`; + +export async function buildCardHtml() { + const result = await build({ + entryPoints: [join(root, 'apps/mcp-server/ui/card.ts')], + bundle: true, write: false, minify: true, format: 'iife', target: 'es2022', platform: 'browser', legalComments: 'none', + }); + const js = result.outputFiles[0].text.replace(/<\/script/gi, '<\\/script'); + return `Second Opinion
Checking

Checking with Second Opinion...

`; +} + +export function generatedSource(html) { + return `// GENERATED by scripts/build-card.mjs from apps/mcp-server/ui/card.ts. Do not edit; run \`npm run build:card\`. +export const CARD_HTML = ${JSON.stringify(html)}; +`; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + const html = await buildCardHtml(); + mkdirSync(join(root, 'apps/mcp-server/src/ui'), { recursive: true }); + writeFileSync(join(root, 'apps/mcp-server/src/ui/card.generated.ts'), generatedSource(html)); + console.log(`card.generated.ts written (${html.length} bytes)`); +}