mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-22 20:29:18 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Agents use connected MCP apps through the tool gateway. > - An MCP app can ask the person a question in the middle of a tool call (MCP elicitation). There are two kinds: a form, or a link to open. > - Today a form question is a dead end: > - A 2026-07-28 server's form `input_required` stops with "needs information Paperclip can't collect yet". > - An older server's `elicitation/create` makes a card, but the answer is never sent back, and the server waits on an open stream. > - Paperclip does not declare form support, so compliant servers never ask. > - This pull request shows the question as a card in the task chat. The card names the app, and only a person can answer it. When the person answers, the agent wakes. The agent's repeated call sends the answer to the app exactly once, for both protocol eras. > - The benefit: apps that need a choice or a detail from the person now finish their work. Restarts lose nothing, and no call is left waiting. ## Linked Issues or Issue Description Refs #21140 (stateless MCP client; this PR closes its "form elicitation" follow-up). Refs #21263 (MCP Tasks; this PR shares its card code). Refs #21389 (MCP Tasks follow-ups: the card answer validation item). **Is your feature request related to a problem?** When a connected MCP app asks the person a form question during a tool call, the call stops and cannot continue. A 2026-07-28 server gets a "needs input" error. An older server gets a card whose answer never reaches it. **Describe the solution you'd like** Show the question in the task chat. Let a person answer, skip, or leave it. Wake the agent when the question is resolved. Send the answer on the agent's repeated call, once, with the server's exact state. **Describe alternatives you've considered** Holding the tool call open until the person answers. We rejected this: it holds a stream and a run slot, and it does not survive deploys or restarts. The plan (`doc/plans/2026-11-13-mcp-elicitation.md`) records nine design decisions with their alternatives. ## What Changed - **Plan:** `doc/plans/2026-11-13-mcp-elicitation.md` covers the spec, the inventory, the decisions, the journey, tests, and risks. - **New module `server/src/services/mcp-elicitation.ts`.** It is the single mapping for direct calls and MCP Tasks: - It turns each spec form field into a real card field: text with defaults, number, yes/no, single choice (`enum` and `oneOf`), and multiple choice (`items.enum` and `items.anyOf`). - It turns card answers back into typed `content`. - Answered sends `accept`. Skip sends `decline`. Withdrawn, closed, or expired sends `cancel`. - It refuses forms that ask for passwords, keys, tokens, card numbers, or one-time codes. - It keeps app text plain, so links in it are not clickable. - It seals `requestState` with AES-256-GCM, bound to the company and connection. - **New table `tool_mcp_elicitations`** (migration 0479). It is company-scoped, with one row per question round. It holds a keyed call identity, the sealed state, the questions and their cards, a claim time, and a 24-hour expiry. - **Gateway, stateless servers:** - Paperclip declares `elicitation: {form, url}` on task tool calls. - A form-only `input_required` saves the round, makes the cards, and stops with a wait message. - A repeated call before the person answers stops again without contacting the app. - After the answer, the repeated call claims the round and sends `inputResponses` and the exact `requestState`, once. - A failed send releases the claim. A crash mid-send lets the claim lapse. - **Gateway, older servers:** - Paperclip declares form support at `initialize`. - The first question gets `cancel` at once, so the server never waits. - When the same question (same words and schema) arrives on the repeated call, Paperclip answers it from the card. - **Gateway, mixed rounds and retries:** - A form beside a URL step shows the link first. - A repeated call that stopped for a question runs again in the same run. It is no longer replayed by the per-run idempotency key. - **Cards:** the title is "*App* needs your input". The resolver policy is `human_only`. "Send to *App*" sends the answer. A user comment does not supersede the card. - **Activity log:** new events `tool_gateway.elicitation_sent`, `tool_gateway.elicitation_refused`, and `tool_gateway.elicitation_expired`. They record the action only, never the values. - **MCP Tasks:** task cards use the same mapping and gain the app name, real fields, and `human_only`. A secret or an unsupported form stays unsupported. - **Storybook:** `Tasks and conversations/Questions and approvals/MCP App Questions` has five states: Asking, Answered, Skipped, Expired, and TwoQuestions. The fixtures are generated from the server mapper's output. - **Docs:** `doc/connections/MCP-ELICITATION.md` (linked from the README) is new. `MCP-PROTOCOL-VERSIONS.md` and `MCP-TASKS.md` are updated. ## Verification - `server/src/__tests__/mcp-elicitation.test.ts` has 16 tests. They pass on the branch. - **Red/green:** 9 of the 9 gateway tests fail with master's `tool-gateway.ts` and `mcp-http.ts`. The 7 pure unit tests cover the new module. - The tests use a fake 2026-07-28 server and a fake 2025-06-18 server that asks over the open stream and waits for the reply. - Covered: answer, skip (`decline`), withdraw (`cancel`), two questions at once, a restart while waiting, a crash mid-send, another company with the same app and arguments, expiry, a refused password field, an agent that tries to answer (rejected), and capabilities. - Existing suites: `mcp-tasks`, `mcp-protocol-versions`, `tool-gateway-service`, `tool-gateway`, and `remote-mcp-connectors`. See the results comment below. - Storybook: the five stories render in headless Chromium, and the play tests pass. The only console errors are the 404s from the backend-less API calls that every story makes. - `tsc --noEmit` is clean for `server` and `ui`. `node scripts/storybook-catalog.mjs` passes. ## Risks - **Older servers may start asking questions.** Declaring form support at `initialize` can make older servers ask where they did not before. Mitigation: the first question always gets `cancel` at once, so no call hangs, and the agent gets a clear next step. - **Per-call cost.** Each 2026-07-28 HTTP tool call in a task makes one indexed lookup for a waiting round. Nothing is added before the first token or to agent boot. - **Migration.** One additive table with no backfill, so it is safe to apply online. - **Changed Tasks behavior.** A task card that is withdrawn or closed now sends `cancel` instead of `decline`, as the spec defines. A skipped card still sends `decline`. - **Stale cards.** Expiry is checked only when the agent calls again. A card can sit past 24 hours until the next call. Then it is withdrawn and the app asks again. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code with tool use (shell, file edits, headless browser), with extended reasoning. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
366 lines
19 KiB
TypeScript
366 lines
19 KiB
TypeScript
import { createCipheriv, createDecipheriv, createHash, hkdfSync, randomBytes } from "node:crypto";
|
||
import type { PaperclipQuestionSetPayload, PaperclipQuestionSetQuestion } from "@paperclipai/shared";
|
||
import { redactSensitiveText } from "../redaction.js";
|
||
|
||
/**
|
||
* MCP form elicitation (spec 2026-07-28, client/elicitation): the provider's
|
||
* question becomes a task-chat question card, and the person's answer on that
|
||
* card becomes the elicitation result. Used by direct tool calls (stateless
|
||
* `input_required` and older server-initiated `elicitation/create`) and by
|
||
* MCP Tasks, so every path shows the same card and sends the same answer.
|
||
*/
|
||
|
||
export type McpFormElicitation = {
|
||
message: string;
|
||
requestedSchema: Record<string, unknown> | null;
|
||
};
|
||
|
||
export type McpElicitationResponse =
|
||
| { action: "accept"; content: Record<string, unknown> }
|
||
| { action: "decline" }
|
||
| { action: "cancel" };
|
||
|
||
/** At most this many fields per question, and form questions per round. */
|
||
export const MCP_ELICITATION_MAX_FIELDS = 10;
|
||
export const MCP_ELICITATION_MAX_FORMS_PER_ROUND = 4;
|
||
const MAX_OPTIONS = 100;
|
||
|
||
type Built =
|
||
| { ok: true; questionSet: PaperclipQuestionSetPayload }
|
||
| { ok: false; reason: "sensitive"; fields: string[] }
|
||
| { ok: false; reason: "unsupported" };
|
||
|
||
function record(value: unknown): Record<string, unknown> | null {
|
||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : null;
|
||
}
|
||
|
||
function text(value: unknown): string | undefined {
|
||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||
}
|
||
|
||
/**
|
||
* Provider text is shown as plain words. Links in a form are not clickable
|
||
* (spec: Safe URL Handling), and secrets that slipped in are redacted.
|
||
*/
|
||
/** True for a card made from an app's question (direct or MCP Task), by its idempotency key. */
|
||
export function isAppQuestionCardKey(key: string | null | undefined): boolean {
|
||
return typeof key === "string" && (key.startsWith("mcp-elicit:") || key.startsWith("mcp-task-input:") || key.startsWith("mcp-elicitation:"));
|
||
}
|
||
|
||
export function elicitationPlainText(value: string, max: number): string {
|
||
return redactSensitiveText(value)
|
||
.replace(/[<>]/g, "")
|
||
.replace(/\]\(/g, "] (")
|
||
.replace(/:\/\//g, "://")
|
||
.slice(0, max);
|
||
}
|
||
|
||
// Secrets must go through a URL (spec: Form Mode Security). Matched on the
|
||
// field's name, title, and description.
|
||
const SENSITIVE_FIELD =
|
||
/pass(?:word|wd|code|phrase)|secret|api[\s_-]?key|(?:access|refresh|auth|bearer|session|api)[\s_-]?token|\btokens?\b|credential|private[\s_-]?key|card[\s_-]?(?:number|no\b)|\bcvv\b|\bcvc\b|security[\s_-]?code|\bssn\b|social[\s_-]?security|one[\s_-]?time[\s_-]?(?:code|password)|\botp\b|\b2fa\b|\bmfa\b/i;
|
||
|
||
// The same secrets, asked for in the form's own words ("type your password
|
||
// in the notes field"). A bare "token" is left out: in prose it is too common.
|
||
const SENSITIVE_MESSAGE =
|
||
/pass(?:word|wd|code|phrase)|secret|api[\s_-]?key|(?:access|refresh|auth|bearer|session|api)[\s_-]?token|credential|private[\s_-]?key|card[\s_-]?(?:number|no\b)|\bcvv\b|\bcvc\b|security[\s_-]?code|\bssn\b|social[\s_-]?security|one[\s_-]?time[\s_-]?(?:code|password)|\botp\b|\b2fa\b|\bmfa\b/i;
|
||
|
||
/** Field keys that ask for a secret; empty when the form is safe to show. */
|
||
export function elicitationSensitiveFields(schema: Record<string, unknown> | null): string[] {
|
||
const properties = record(schema?.properties) ?? {};
|
||
return Object.entries(properties).filter(([key, raw]) => {
|
||
const property = record(raw) ?? {};
|
||
if (property.format === "password" || property.writeOnly === true) return true;
|
||
return [key, property.title, property.description].some((value) => typeof value === "string" && SENSITIVE_FIELD.test(value));
|
||
}).map(([key]) => key);
|
||
}
|
||
|
||
type Choice = { value: unknown; label: string };
|
||
|
||
/** Enum choices from `enum`, `oneOf`/`anyOf` with `const`+`title` (SEP-1330). */
|
||
function choicesOf(schema: Record<string, unknown>): Choice[] | null {
|
||
if (Array.isArray(schema.enum)) {
|
||
const titles = Array.isArray(schema.enumNames) ? schema.enumNames : [];
|
||
return schema.enum.map((value, index) => ({ value, label: String(text(titles[index]) ?? value) }));
|
||
}
|
||
const variants = Array.isArray(schema.oneOf) ? schema.oneOf : Array.isArray(schema.anyOf) ? schema.anyOf : null;
|
||
if (!variants) return null;
|
||
const choices: Choice[] = [];
|
||
for (const variant of variants) {
|
||
const entry = record(variant);
|
||
if (!entry || !("const" in entry)) return null;
|
||
choices.push({ value: entry.const, label: String(text(entry.title) ?? entry.const) });
|
||
}
|
||
return choices;
|
||
}
|
||
|
||
const primitive = (value: unknown) => typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
||
const optionId = (index: number) => `choice_${index + 1}`;
|
||
|
||
type Field = { key: string; property: Record<string, unknown>; question: PaperclipQuestionSetQuestion; choices?: Choice[]; kind: "text" | "number" | "integer" | "boolean" | "single" | "multi" };
|
||
|
||
// Patterns stay within the fixed-width subset the question form can check
|
||
// safely (no repetition), so an email is a hint and the app validates it.
|
||
const FORMAT_HINT: Record<string, { help: string; pattern?: string }> = {
|
||
email: { help: "An email address." },
|
||
uri: { help: "A web address." },
|
||
date: { help: "A date, like 2026-11-13.", pattern: "^\\d\\d\\d\\d-\\d\\d-\\d\\d$" },
|
||
"date-time": { help: "A date and time, like 2026-11-13T09:30:00Z." },
|
||
};
|
||
|
||
function fieldFor(key: string, raw: unknown, required: boolean): Field | null {
|
||
const property = record(raw);
|
||
if (!property) return null;
|
||
const prompt = elicitationPlainText(text(property.title) ?? key, 4000);
|
||
const description = text(property.description);
|
||
const help = (extra?: string) => {
|
||
const value = [description ? elicitationPlainText(description, 3000) : null, extra].filter(Boolean).join(" ");
|
||
return value ? { helpText: value.slice(0, 4000) } : {};
|
||
};
|
||
const base = { id: key.slice(0, 160), prompt, required };
|
||
const select = (choices: Choice[], multi: boolean, defaults: unknown[], extra?: string): Field | null => {
|
||
if (!choices.length || choices.length > MAX_OPTIONS || !choices.every((choice) => primitive(choice.value))) return null;
|
||
return {
|
||
key, property, choices, kind: multi ? "multi" : "single",
|
||
question: {
|
||
...base, ...help(extra), answerMode: multi ? "multi_select" : "single_select",
|
||
options: choices.map((choice, index) => ({
|
||
id: optionId(index), label: elicitationPlainText(choice.label, 1000) || `Option ${index + 1}`,
|
||
...(defaults.some((value) => value === choice.value) ? { recommended: true } : {}),
|
||
})),
|
||
},
|
||
};
|
||
};
|
||
const type = property.type;
|
||
if (type === "array") {
|
||
const items = record(property.items);
|
||
const choices = items ? choicesOf(items) : null;
|
||
if (!choices) return null;
|
||
const limits = [
|
||
typeof property.minItems === "number" ? `Choose at least ${property.minItems}.` : null,
|
||
typeof property.maxItems === "number" ? `Choose at most ${property.maxItems}.` : null,
|
||
].filter(Boolean).join(" ");
|
||
return select(choices, true, Array.isArray(property.default) ? property.default : [], limits || undefined);
|
||
}
|
||
const choices = choicesOf(property);
|
||
if (choices) return select(choices, false, property.default === undefined ? [] : [property.default]);
|
||
if (type === "boolean") {
|
||
return select([{ value: true, label: "Yes" }, { value: false, label: "No" }], false, property.default === undefined ? [] : [property.default]);
|
||
}
|
||
if (type === "number" || type === "integer") {
|
||
const minimum = typeof property.minimum === "number" && Number.isFinite(property.minimum) ? property.minimum : undefined;
|
||
const maximum = typeof property.maximum === "number" && Number.isFinite(property.maximum) ? property.maximum : undefined;
|
||
if (minimum !== undefined && maximum !== undefined && minimum > maximum) return null;
|
||
return {
|
||
key, property, kind: type,
|
||
question: {
|
||
...base, ...help(), answerMode: "text",
|
||
...(typeof property.default === "number" ? { initialText: String(property.default) } : {}),
|
||
textValidation: { inputType: type, ...(minimum !== undefined ? { minimum } : {}), ...(maximum !== undefined ? { maximum } : {}) },
|
||
},
|
||
};
|
||
}
|
||
if (type === "string" || type === undefined) {
|
||
const format = typeof property.format === "string" ? FORMAT_HINT[property.format] : undefined;
|
||
if (typeof property.format === "string" && !format) return null;
|
||
const minLength = Number.isInteger(property.minLength) && (property.minLength as number) >= 0 ? Math.min(property.minLength as number, 100_000) : undefined;
|
||
const maxLength = Number.isInteger(property.maxLength) && (property.maxLength as number) >= 0 ? Math.min(property.maxLength as number, 100_000) : undefined;
|
||
if (minLength !== undefined && maxLength !== undefined && minLength > maxLength) return null;
|
||
const validation = {
|
||
...(minLength !== undefined ? { minLength } : {}),
|
||
...(maxLength !== undefined ? { maxLength } : {}),
|
||
...(format?.pattern ? { pattern: format.pattern } : {}),
|
||
};
|
||
return {
|
||
key, property, kind: "text",
|
||
question: {
|
||
...base, ...help(format?.help), answerMode: "text",
|
||
...(typeof property.default === "string" ? { initialText: property.default.slice(0, 100_000) } : {}),
|
||
...(Object.keys(validation).length ? { textValidation: validation } : {}),
|
||
},
|
||
};
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function fieldsOf(schema: Record<string, unknown> | null): Field[] | null {
|
||
if (schema && schema.type !== undefined && schema.type !== "object") return null;
|
||
const properties = record(schema?.properties) ?? {};
|
||
const entries = Object.entries(properties);
|
||
if (entries.length > MCP_ELICITATION_MAX_FIELDS) return null;
|
||
const required = new Set(Array.isArray(schema?.required) ? schema.required.filter((key): key is string => typeof key === "string") : []);
|
||
const fields: Field[] = [];
|
||
for (const [key, raw] of entries) {
|
||
const field = fieldFor(key, raw, required.has(key));
|
||
if (!field) return null;
|
||
fields.push(field);
|
||
}
|
||
return fields;
|
||
}
|
||
|
||
/** The single "Continue" choice a form with no fields becomes. */
|
||
const CONTINUE_QUESTION_ID = "continue";
|
||
|
||
/**
|
||
* The card for one form question, or why Paperclip will not show it. A form
|
||
* asking for a secret is refused before anything is shown; a schema outside
|
||
* the spec's flat primitive subset is refused rather than shown partly.
|
||
*/
|
||
export function buildElicitationQuestionSet(input: { appName: string; request: McpFormElicitation }): Built {
|
||
const sensitive = elicitationSensitiveFields(input.request.requestedSchema);
|
||
// A form whose message asks for a secret is refused too, whatever its fields are called.
|
||
if (SENSITIVE_MESSAGE.test(input.request.message)) sensitive.unshift("message");
|
||
if (sensitive.length) return { ok: false, reason: "sensitive", fields: sensitive };
|
||
const fields = fieldsOf(input.request.requestedSchema);
|
||
if (!fields) return { ok: false, reason: "unsupported" };
|
||
const appName = elicitationPlainText(input.appName, 120) || "The app";
|
||
const description = elicitationPlainText(input.request.message, 4000) || `${appName} needs more information to continue.`;
|
||
const questions: PaperclipQuestionSetQuestion[] = fields.length
|
||
? fields.map((field) => field.question)
|
||
: [{ id: CONTINUE_QUESTION_ID, prompt: description, required: true, answerMode: "single_select", options: [{ id: "continue", label: "Continue" }] }];
|
||
return {
|
||
ok: true,
|
||
questionSet: {
|
||
schema: "paperclip.question_set.v1",
|
||
title: `${appName} needs your input`,
|
||
description,
|
||
submitLabel: `Send to ${appName}`.slice(0, 200),
|
||
questions,
|
||
},
|
||
};
|
||
}
|
||
|
||
type CardState = { status: string; result: unknown; payload?: unknown };
|
||
|
||
/**
|
||
* The elicitation result for a resolved card, or null while it waits.
|
||
* Answered: `accept` with typed content. Skipped: `decline` (the person said
|
||
* no). Anything else that ended the card (withdrawn, task closed, expired,
|
||
* cancelled): `cancel`, since nobody made a choice.
|
||
*/
|
||
export function elicitationResponseForCard(request: McpFormElicitation, card: CardState | null | undefined): McpElicitationResponse | null {
|
||
if (!card || card.status === "pending") return null;
|
||
const result = record(card.result);
|
||
if (card.status === "answered" && result?.outcome === undefined) {
|
||
// A card saved before typed fields (an MCP Task waiting across the
|
||
// upgrade) keeps its own options; read it the way it was built.
|
||
const payload = record(card.payload);
|
||
const content = payload && !payload.questionSet && Array.isArray(payload.questions)
|
||
? legacyCardContent(request, payload.questions, result)
|
||
: elicitationContent(request, result);
|
||
return { action: "accept", content };
|
||
}
|
||
if (card.status === "cancelled" && result?.outcome === "skipped") return { action: "decline" };
|
||
return { action: "cancel" };
|
||
}
|
||
|
||
/** Map a person's card answers back onto the provider's schema, typed. */
|
||
export function elicitationContent(request: McpFormElicitation, result: unknown): Record<string, unknown> {
|
||
const answers = (Array.isArray(record(result)?.answers) ? record(result)!.answers as unknown[] : [])
|
||
.map(record).filter((answer): answer is Record<string, unknown> => Boolean(answer));
|
||
const fields = fieldsOf(request.requestedSchema) ?? [];
|
||
const content: Record<string, unknown> = {};
|
||
for (const field of fields) {
|
||
const answer = answers.find((candidate) => candidate.questionId === field.question.id);
|
||
if (!answer) continue;
|
||
const optionIds = Array.isArray(answer.optionIds) ? answer.optionIds.filter((id): id is string => typeof id === "string") : [];
|
||
const chosen = (field.choices ?? []).filter((_, index) => optionIds.includes(optionId(index))).map((choice) => choice.value);
|
||
const typed = typeof answer.otherText === "string" ? answer.otherText.trim() : "";
|
||
if (field.kind === "multi") { content[field.key] = chosen; continue; }
|
||
if (field.kind === "single" || field.kind === "boolean") { if (chosen.length) content[field.key] = chosen[0]; continue; }
|
||
if (!typed) continue;
|
||
if (field.kind === "number" || field.kind === "integer") {
|
||
const value = Number(typed);
|
||
if (Number.isFinite(value) && (field.kind === "number" || Number.isInteger(value))) content[field.key] = value;
|
||
continue;
|
||
}
|
||
content[field.key] = typed;
|
||
}
|
||
return content;
|
||
}
|
||
|
||
/**
|
||
* Answers on a card built before typed fields: enum choices were listed in
|
||
* the schema's order, and everything else was a written answer.
|
||
*/
|
||
function legacyCardContent(request: McpFormElicitation, storedQuestions: unknown[], result: unknown): Record<string, unknown> {
|
||
const answers = (Array.isArray(record(result)?.answers) ? record(result)!.answers as unknown[] : [])
|
||
.map(record).filter((answer): answer is Record<string, unknown> => Boolean(answer));
|
||
const properties = record(request.requestedSchema?.properties);
|
||
const textOf = (answer: Record<string, unknown> | undefined) => typeof answer?.otherText === "string" ? answer.otherText.trim() : "";
|
||
if (!properties || !Object.keys(properties).length) {
|
||
const text = textOf(answers.find((answer) => answer.questionId === "response"));
|
||
return text ? { response: text } : {};
|
||
}
|
||
const content: Record<string, unknown> = {};
|
||
for (const [key, raw] of Object.entries(properties).slice(0, MCP_ELICITATION_MAX_FIELDS)) {
|
||
const property = record(raw) ?? {};
|
||
const answer = answers.find((candidate) => candidate.questionId === key.slice(0, 120));
|
||
if (!answer) continue;
|
||
const enumValues = Array.isArray(property.enum) ? property.enum : [];
|
||
const stored = storedQuestions.map(record).find((question) => question?.id === key.slice(0, 120));
|
||
const options = Array.isArray(stored?.options) ? stored!.options.map(record) : [];
|
||
const optionIds = Array.isArray(answer.optionIds) ? answer.optionIds : [];
|
||
const index = options.findIndex((option) => typeof option?.id === "string" && optionIds.includes(option.id));
|
||
if (index >= 0 && index < enumValues.length) { content[key] = enumValues[index]; continue; }
|
||
const text = textOf(answer);
|
||
if (!text) continue;
|
||
if (property.type === "number" || property.type === "integer") {
|
||
const value = Number(text);
|
||
if (Number.isFinite(value) && (property.type === "number" || Number.isInteger(value))) content[key] = value;
|
||
} else if (property.type === "boolean") {
|
||
if (/^(true|yes|y|1)$/i.test(text)) content[key] = true;
|
||
else if (/^(false|no|n|0)$/i.test(text)) content[key] = false;
|
||
} else {
|
||
content[key] = text;
|
||
}
|
||
}
|
||
return content;
|
||
}
|
||
|
||
/** Identifies one question by its words and schema, for answering an older server's repeat. */
|
||
export function elicitationQuestionHash(request: McpFormElicitation): string {
|
||
const canonical = (value: unknown): unknown => Array.isArray(value)
|
||
? value.map(canonical)
|
||
: record(value) ? Object.fromEntries(Object.keys(value as object).sort().map((key) => [key, canonical((value as Record<string, unknown>)[key])])) : value;
|
||
return createHash("sha256").update(JSON.stringify(["paperclip:mcp-elicitation-question:v1", request.message, canonical(request.requestedSchema ?? {})])).digest("hex");
|
||
}
|
||
|
||
/** A form request from a stateless `inputRequests` entry or an older `elicitation/create` message. */
|
||
export function formElicitationOf(params: Record<string, unknown>): McpFormElicitation | null {
|
||
if (params.mode !== undefined && params.mode !== "form") return null;
|
||
return {
|
||
message: typeof params.message === "string" ? params.message : "",
|
||
requestedSchema: record(params.requestedSchema),
|
||
};
|
||
}
|
||
|
||
/*
|
||
* The server's requestState is opaque and may carry authority, so it is sealed
|
||
* at rest, bound to the company and connection that received it.
|
||
*/
|
||
const STATE_CONTEXT = "paperclip:mcp-elicitation-state:v1";
|
||
const stateKey = (secret: string) => Buffer.from(hkdfSync("sha256", secret, Buffer.alloc(0), STATE_CONTEXT, 32));
|
||
const stateAad = (scope: { companyId: string; connectionId: string }) => Buffer.from(JSON.stringify([STATE_CONTEXT, scope.companyId, scope.connectionId]));
|
||
|
||
export function sealElicitationState(secret: string, scope: { companyId: string; connectionId: string }, value: string): string {
|
||
const iv = randomBytes(12);
|
||
const cipher = createCipheriv("aes-256-gcm", stateKey(secret), iv);
|
||
cipher.setAAD(stateAad(scope));
|
||
const ciphertext = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
|
||
return `v1.${iv.toString("base64url")}.${ciphertext.toString("base64url")}.${cipher.getAuthTag().toString("base64url")}`;
|
||
}
|
||
|
||
export function openElicitationState(secret: string, scope: { companyId: string; connectionId: string }, sealed: string): string | null {
|
||
const [version, iv, ciphertext, tag] = sealed.split(".");
|
||
if (version !== "v1" || !iv || ciphertext === undefined || !tag) return null;
|
||
try {
|
||
const decipher = createDecipheriv("aes-256-gcm", stateKey(secret), Buffer.from(iv, "base64url"));
|
||
decipher.setAAD(stateAad(scope));
|
||
decipher.setAuthTag(Buffer.from(tag, "base64url"));
|
||
return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64url")), decipher.final()]).toString("utf8");
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|