Skip to content

Commit 9338516

Browse files
committed
wip(runtime): ask the person in place when the MCP door can elicit
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5
1 parent a8063f9 commit 9338516

3 files changed

Lines changed: 215 additions & 10 deletions

File tree

‎packages/runtime/src/action-dispatch.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,18 @@ export interface ResolvedActionCall {
160160
* hand-off's own members (`ActionConfirmationHandOff`). A door without it
161161
* (REST, a host's door) keeps the member gate.
162162
*/
163-
confirmationHandOff?: { door: string; namespace?: string };
163+
confirmationHandOff?: {
164+
door: string;
165+
namespace?: string;
166+
/**
167+
* [#33712] The door's channel to the person's client (step 1 of the
168+
* amended contract), present only when the client can be elicited. The
169+
* person is then asked in place, after the same verdict the hand-off
170+
* asks: `accept` dispatches the call as any admitted call, any other
171+
* answer refuses it, and nothing is handed off.
172+
*/
173+
elicitConfirmation?: actionExec.ActionConfirmationElicitor;
174+
};
164175
/**
165176
* [ADR-0106 D1] This door's answer for a name nothing declares, where it
166177
* is not the sequence's own undeclared `404` ({@link undeclaredActionRefusal}):
@@ -277,7 +288,11 @@ export async function runResolvedAction(
277288
): Promise<ResolvedActionRun> {
278289
const admission = await admitResolvedAction(deps, requestContext, call);
279290
if ('refused' in admission) return admission;
280-
if (admission.handOff) return handOffAdmittedAction(deps, requestContext, call, admission);
291+
if (admission.handOff) {
292+
const confirmation = await confirmAdmittedAction(deps, requestContext, call, admission);
293+
// [#33712] Only an elicited `accept` falls through to the dispatch.
294+
if (confirmation !== 'accepted') return confirmation;
295+
}
281296
const outcome = await dispatchAdmittedAction(deps, requestContext, call, admission);
282297
return {
283298
admitted: { actionDef: admission.actionDef, objectName: call.objectName, actionName: call.actionName, recordId: admission.recordId },
@@ -301,13 +316,16 @@ interface Admission {
301316
* the host's pending-confirmation service (or the `no_confirmation_service`
302317
* refusal). Nothing dispatches, and no invocation row is written: the call
303318
* did not run. A verdict refusal is the arm's own, rendered as a refusal.
319+
* [#33712] At a door that holds the person's client (`elicitConfirmation`),
320+
* the person is asked there instead, after the same verdict: `'accepted'`
321+
* sends the caller on to the dispatch, any other answer is the refusal.
304322
*/
305-
async function handOffAdmittedAction(
323+
async function confirmAdmittedAction(
306324
deps: ActionExecutionDeps,
307325
requestContext: HttpProtocolContext,
308326
call: ResolvedActionCall,
309327
admission: Admission,
310-
): Promise<ResolvedActionRun> {
328+
): Promise<ResolvedActionRun | 'accepted'> {
311329
const { objectName, wiring } = call;
312330
const { actionDef, recordId, params, subject } = admission;
313331
const subjectRecordId = recordId === undefined || recordId === null || recordId === '' ? undefined : String(recordId);
@@ -321,8 +339,29 @@ async function handOffAdmittedAction(
321339
return { refused: { message: thrown.message, status: thrown.status, details: { code: thrown.code } } };
322340
}
323341
const handOff = call.confirmationHandOff!;
342+
const actionName = typeof actionDef?.name === 'string' && actionDef.name ? actionDef.name : call.actionName;
343+
// [#33712] Step 1: a client that can be elicited is asked in place, after
344+
// the same verdict, and its answer stands: `accept` dispatches, anything
345+
// else refuses. The hand-off below is the step-2 path for every other call.
346+
if (handOff.elicitConfirmation) {
347+
const elicited = await actionExec.elicitActionConfirmation(
348+
handOff.elicitConfirmation,
349+
actionExec.actionConfirmationElicitSubject(
350+
actionDef, actionName, objectName, subjectRecordId, call.declaration?.obj, subject?.record,
351+
),
352+
);
353+
if ('accepted' in elicited) return 'accepted';
354+
const { refused } = elicited;
355+
return {
356+
refused: {
357+
message: refused.message,
358+
status: refused.status,
359+
details: { code: refused.code, ...(refused.details as unknown as Record<string, unknown>) },
360+
},
361+
};
362+
}
324363
const answer = await actionExec.handOffActionConfirmation(deps, requestContext, wiring.envId, {
325-
actionName: typeof actionDef?.name === 'string' && actionDef.name ? actionDef.name : call.actionName,
364+
actionName,
326365
...(actionExec.isObjectLessActionKey(objectName) ? {} : { objectName }),
327366
...(handOff.namespace !== undefined ? { namespace: handOff.namespace } : {}),
328367
...(subjectRecordId !== undefined ? { recordId: subjectRecordId } : {}),

‎packages/runtime/src/action-execution.ts‎

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import {
109109
reconcileActionRegistrations as reconcileActionRegistrationsPure,
110110
resolveActionByPackage,
111111
resolveActionHandlerKeys,
112+
resolveRecordTitle,
112113
standaloneActionOwnerKey,
113114
SummaryRecomputeError,
114115
type PackageScopedRefusal,
@@ -3258,6 +3259,127 @@ export async function handOffActionConfirmation(
32583259
};
32593260
}
32603261

3262+
/**
3263+
* [#33712] What an MCP door asks the person to confirm (step 1 of the amended
3264+
* contract, `AI_ACTION_CONFIRMATION_CAPABILITY`): the action, the object and
3265+
* the record it runs on, its id and, when the subject load read one, its
3266+
* title. Structurally the `McpActionConfirmationSubject` of `@objectstack/mcp`,
3267+
* which this package does not depend on.
3268+
*/
3269+
export interface ActionConfirmationElicitSubject {
3270+
actionName: string;
3271+
actionLabel?: string;
3272+
objectName?: string;
3273+
recordId?: string;
3274+
recordTitle?: string;
3275+
}
3276+
3277+
/** [#33712] The person's answer, as the door read it (`McpActionConfirmationAnswer`). */
3278+
export type ActionConfirmationElicitAnswer = 'accept' | 'decline' | 'cancel' | 'timed_out';
3279+
3280+
/**
3281+
* [#33712] The door's channel to the person's client for ONE call
3282+
* (`McpActionConfirmationElicitor`). Present only when the client can be
3283+
* elicited; its absence means "hand off" (step 2), never a refusal.
3284+
*/
3285+
export type ActionConfirmationElicitor = (subject: ActionConfirmationElicitSubject) => Promise<ActionConfirmationElicitAnswer>;
3286+
3287+
/**
3288+
* [#33712] The subject an elicitation names, built from what the admitted
3289+
* sequence already holds: the declaration, the object and the record the
3290+
* caller-scope subject load read. The title is the object's own
3291+
* (`resolveRecordTitle`: its `nameField`, formula or column), and is left out
3292+
* rather than replaced by the id when it does not resolve.
3293+
*/
3294+
export function actionConfirmationElicitSubject(
3295+
action: any,
3296+
actionName: string,
3297+
objectName: string | undefined,
3298+
recordId: string | undefined,
3299+
obj: unknown,
3300+
record: Record<string, unknown> | undefined,
3301+
): ActionConfirmationElicitSubject {
3302+
const label = typeof action?.label === 'string' && action.label !== '' ? action.label : undefined;
3303+
let recordTitle: string | undefined;
3304+
if (recordId !== undefined && record && Object.keys(record).length > 0) {
3305+
try {
3306+
recordTitle = resolveRecordTitle(obj, record);
3307+
} catch {
3308+
// A title that cannot be composed is left out: the id still names the record.
3309+
recordTitle = undefined;
3310+
}
3311+
}
3312+
return {
3313+
actionName,
3314+
...(label !== undefined ? { actionLabel: label } : {}),
3315+
...(objectName && !isObjectLessActionKey(objectName) ? { objectName } : {}),
3316+
...(recordId !== undefined ? { recordId } : {}),
3317+
...(recordTitle !== undefined && recordTitle !== '' ? { recordTitle } : {}),
3318+
};
3319+
}
3320+
3321+
/**
3322+
* [#33712] Step 1 of the amended contract: ask the person in their client, and
3323+
* read the answer as step 4 does. Asked by a door that holds an elicitor, once
3324+
* every admission gate has passed (the subject load and its caller-scope
3325+
* verdict included) and never for an AI service account, which the
3326+
* confirmation step refuses before it gets here.
3327+
*
3328+
* - `accept`: `{ accepted: true }`; the caller dispatches the call exactly
3329+
* once, as any admitted call. Nothing is stored: the next call is asked again.
3330+
* - `decline`: refused `428`, `reason: 'declined'`.
3331+
* - `cancel` (a dismissal, an error answer, a dropped connection): refused,
3332+
* `reason: 'cancelled'`.
3333+
* - `timed_out` (no answer within the door's bound): refused,
3334+
* `reason: 'timed_out'`.
3335+
*
3336+
* An elicitor that throws, or answers anything else, reads as `cancel`:
3337+
* nothing runs for want of a readable answer. Nothing is handed off on any of
3338+
* these answers: the person was asked, and their answer (or their silence)
3339+
* stands.
3340+
*/
3341+
export async function elicitActionConfirmation(
3342+
elicit: ActionConfirmationElicitor,
3343+
subject: ActionConfirmationElicitSubject,
3344+
): Promise<{ accepted: true } | { refused: ActionConfirmationRefusal }> {
3345+
let answer: unknown;
3346+
try {
3347+
answer = await elicit(subject);
3348+
} catch {
3349+
answer = 'cancel';
3350+
}
3351+
if (answer === 'accept') return { accepted: true };
3352+
const on = confirmationSubjectOn(subject.objectName);
3353+
const declared = `Action '${subject.actionName}'${on} declares ai.requiresConfirmation: true, so the person was asked to confirm it in their client`;
3354+
if (answer === 'decline') {
3355+
return {
3356+
refused: confirmationRefusalOf(
3357+
subject.actionName, subject.objectName,
3358+
`${declared}, and they declined — nothing was run. Do not retry unless they ask again.`,
3359+
'declined',
3360+
),
3361+
};
3362+
}
3363+
if (answer === 'timed_out') {
3364+
return {
3365+
refused: confirmationRefusalOf(
3366+
subject.actionName, subject.objectName,
3367+
`${declared}, and no answer arrived in time — nothing was run, and a later answer runs nothing. `
3368+
+ 'Retry only when they are there to answer.',
3369+
'timed_out',
3370+
),
3371+
};
3372+
}
3373+
return {
3374+
refused: confirmationRefusalOf(
3375+
subject.actionName, subject.objectName,
3376+
`${declared}, and the question was dismissed without an answer — nothing was run. `
3377+
+ 'Retry only if they ask again.',
3378+
'cancelled',
3379+
),
3380+
};
3381+
}
3382+
32613383
/**
32623384
* [#33690] The caller-scope subject verdict, as the dispatch arm that would
32633385
* run `action` reads it, asked BEFORE a hand-off (the amended contract, step
@@ -4814,9 +4936,16 @@ export async function invokeBusinessAction(deps: ActionExecutionDeps,
48144936
// metadata service's `loadMany('action')` is read, as before.
48154937
readActions?: () => Promise<readonly any[] | undefined>;
48164938
callData: (action: string, params: any, dataDriver?: any, scopeId?: string, ec?: ExecutionContext) => Promise<any>;
4939+
/**
4940+
* [#33712] The MCP door's channel to the person's client for this call
4941+
* (step 1 of the amended contract), present only when the client can
4942+
* be elicited. A declared-gated call is then asked in place instead of
4943+
* handed off; see {@link elicitActionConfirmation}.
4944+
*/
4945+
elicitConfirmation?: ActionConfirmationElicitor;
48174946
},
48184947
): Promise<any> {
4819-
const { driver, envId, ec, getMeta, readObjects, readActions, callData } = wiring;
4948+
const { driver, envId, ec, getMeta, readObjects, readActions, callData, elicitConfirmation } = wiring;
48204949
const meta: any = await getMeta();
48214950
const params = input?.params && typeof input.params === 'object' ? input.params : {};
48224951
const recordId = typeof input?.recordId === 'string' && input.recordId.length > 0 ? input.recordId : undefined;
@@ -4901,7 +5030,13 @@ export async function invokeBusinessAction(deps: ActionExecutionDeps,
49015030
// [#33690] This door hands a declared-gated call to the person it runs
49025031
// as: the caller's `confirm` is not passed on, because it never counts
49035032
// there (the amended contract, `AI_ACTION_CONFIRMATION_CAPABILITY`).
4904-
confirmationHandOff: { door: 'mcp', ...(namespace !== undefined ? { namespace } : {}) },
5033+
// [#33712] With the door's elicitor, the person is asked in their
5034+
// client first (step 1); the hand-off is then never taken.
5035+
confirmationHandOff: {
5036+
door: 'mcp',
5037+
...(namespace !== undefined ? { namespace } : {}),
5038+
...(elicitConfirmation ? { elicitConfirmation } : {}),
5039+
},
49055040
ql: engine,
49065041
wiring: {
49075042
ec, envId, driver, callData,

‎packages/runtime/src/domains/mcp.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,17 +1373,23 @@ export function buildMcpBridge(
13731373
// [ADR-0150 D5] `namespace` rides through like `confirm`: the whole
13741374
// request object is forwarded, never rebuilt without it.
13751375
input: { objectName?: string; namespace?: string; recordId?: string; params?: Record<string, unknown> } & AIActionConfirmation,
1376+
// [#33712] The door's channel to the person's client, set by the
1377+
// tool only when the client can be elicited (`McpActionCallDoor`).
1378+
door?: { elicitConfirmation?: actionExec.ActionConfirmationElicitor },
13761379
) => actionExec.invokeBusinessAction(deps, context, name, input ?? {}, {
13771380
driver, envId, ec, getMeta, readObjects: readServedObjects, readActions: readServedActions, callData,
1381+
...(typeof door?.elicitConfirmation === 'function' ? { elicitConfirmation: door.elicitConfirmation } : {}),
13781382
}),
13791383
// [#15705] `resume_run`: continue the caller's own paused screen run.
13801384
// Admitted by the same gates as `run_action`; see
13811385
// {@link resumeActionRun}.
13821386
resumeRun: async (
13831387
runId: string,
13841388
input: { values?: Record<string, unknown> } & AIActionConfirmation,
1389+
door?: { elicitConfirmation?: actionExec.ActionConfirmationElicitor },
13851390
) => resumeActionRun(deps, context, runId, input ?? {}, {
13861391
driver, envId, ec, getMeta, readObjects: readServedObjects, readActions: readServedActions, callData,
1392+
...(typeof door?.elicitConfirmation === 'function' ? { elicitConfirmation: door.elicitConfirmation } : {}),
13871393
}),
13881394
// ── Federation surface (McpFederationBridge, #27745) ───────
13891395
// The `external-datasource` service Studio's datasource page reaches
@@ -1920,9 +1926,11 @@ export async function resumeActionRun(
19201926
/** [#25297] As `invokeBusinessAction`'s: the standalone `action` items that admit the run. */
19211927
readActions?: () => Promise<readonly any[] | undefined>;
19221928
callData: (action: string, params: any, dataDriver?: any, scopeId?: string, ec?: any) => Promise<any>;
1929+
/** [#33712] As `invokeBusinessAction`'s: the person asked in their client, in place of a hand-off. */
1930+
elicitConfirmation?: actionExec.ActionConfirmationElicitor;
19231931
},
19241932
): Promise<any> {
1925-
const { driver, envId, ec, getMeta, readObjects, readActions, callData } = wiring;
1933+
const { driver, envId, ec, getMeta, readObjects, readActions, callData, elicitConfirmation } = wiring;
19261934
if (typeof runId !== 'string' || runId === '') {
19271935
throw resumeDoorError(deps, 'runId is required', 400);
19281936
}
@@ -2024,7 +2032,28 @@ export async function resumeActionRun(
20242032
// [#33690] A gated action's resume is handed to the person, after steps 4
20252033
// and 5 so nobody is asked to confirm a resume refused anyway: nothing
20262034
// resumes, the run stays parked, and no invocation row is written.
2027-
if (handOff) {
2035+
//
2036+
// [#33712] Where the person's client can be elicited, they are asked there
2037+
// instead, after the same steps: `accept` resumes the run below, as any
2038+
// admitted resume; any other answer refuses the call and the run stays
2039+
// parked. Nothing is handed off then.
2040+
if (handOff && elicitConfirmation) {
2041+
const subject = await refuseUnresumableAdmittedRun(deps, automation, runId, { objectName, recordId, driver, envId, ec, callData });
2042+
const elicited = await actionExec.elicitActionConfirmation(
2043+
elicitConfirmation,
2044+
actionExec.actionConfirmationElicitSubject(
2045+
action,
2046+
typeof action?.name === 'string' && action.name ? action.name : String(flowName),
2047+
objectName, recordId, objectsByName.get(objectName), subject.record,
2048+
),
2049+
);
2050+
if ('refused' in elicited) {
2051+
throw resumeDoorError(deps, elicited.refused.message, elicited.refused.status, {
2052+
code: elicited.refused.code,
2053+
...elicited.refused.details,
2054+
});
2055+
}
2056+
} else if (handOff) {
20282057
await refuseUnresumableAdmittedRun(deps, automation, runId, { objectName, recordId, driver, envId, ec, callData });
20292058
const answer = await actionExec.handOffActionConfirmation(deps, context, envId, {
20302059
actionName: typeof action?.name === 'string' && action.name ? action.name : String(flowName),
@@ -2122,7 +2151,7 @@ async function refuseUnresumableAdmittedRun(
21222151
ec: any;
21232152
callData: (action: string, params: any, dataDriver?: any, scopeId?: string, ec?: any) => Promise<any>;
21242153
},
2125-
): Promise<void> {
2154+
): Promise<actionExec.ActionSubjectRecordLoad> {
21262155
const { objectName, recordId, driver, envId, ec, callData } = admitted;
21272156

21282157
// ── 4. the subject record, read again in the caller's own scope ──────────
@@ -2140,6 +2169,8 @@ async function refuseUnresumableAdmittedRun(
21402169
409,
21412170
);
21422171
}
2172+
// [#33712] The subject as read, so an elicitation can name the record by its title.
2173+
return subject;
21432174
}
21442175

21452176
/**

0 commit comments

Comments
 (0)