Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/33690-ai-action-confirmation-hand-off.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@objectstack/runtime': minor
'@objectstack/mcp': minor
---

feat(runtime,mcp)!: an action declaring `ai.requiresConfirmation: true` never runs on the MCP caller's own `confirm: true` — `run_action` and `resume_run` hand it to the person in the app

Clause-②: no (narrowing)
Metadata-protocol: semantic
Skills-docs: follow-up #34210

<!-- adr-0087: not-required (already-registered action-requires-confirmation-elicited) The semantic entry registered with the contract already carries this move to the upgrade guide and os migrate meta: no key is added, removed or re-shaped, and nothing an author wrote is rewritten. -->

**BREAKING** for an MCP client, an agent or a script that runs an action declaring `ai.requiresConfirmation: true` through the `run_action` or `resume_run` tool by sending `confirm: true` itself. Ruled by the account owner on the safety fix: the caller's own `confirm: true` never counts on such a call; the person it runs as confirms it in the app.

This release implements the hand-off and refusal steps of the amended confirmation contract (`AI_ACTION_CONFIRMATION_CAPABILITY`, `@objectstack/spec/contracts`). The MCP doors do not elicit in the client yet: every declared-gated call takes the hand-off, whatever the client advertised.

- After every admission gate, the subject load and the caller-scope subject verdict included, a declared-gated call is handed to the host's pending-confirmation service (`IActionConfirmationService`, registered under `ACTION_CONFIRMATION_SERVICE` = `'ai.actionConfirmation'`) through its `submit` member, and the call is answered with `status: 'pending_confirmation'`, the service's `pendingId`, a plain message, `actionName` and `objectName`. That answer is the tool's successful result. Nothing runs, and no `action_invocation` row is written. A `resume_run` hand-off carries its run (`run: { runId, values }`) and the run stays paused; approving it is the host's, as is what happens after.
- A host that registers no such service (the open-source build ships none) refuses the call `428 ACTION_CONFIRMATION_REQUIRED` with `details.reason: 'no_confirmation_service'`, with or without `confirm: true`. The person performs the action in the app.
- A service that answers no `pendingId` is a fault: the call fails and nothing runs.
- An AI service account acting for itself is refused whatever it sends, as before (ADR-0151 D3 rule 3). Its refusal now carries `details.reason: 'service_account'` at every action door, REST `/actions` and a host's door included.
- Unchanged: REST `/actions` stays outside the gate for a person; a host's door (`dispatchAction`) under the `ai` posture still reads `confirm`; the MCP `delete_record` and `delete_records` gates still read `confirm: true`. The `confirm` member stays accepted on `run_action` and `resume_run` and is not read.

What changes for a caller:

- FROM confirm with the person yourself, then send `confirm: true` on `run_action` / `resume_run` TO relay the pending answer's message and let the person approve it in the app; do not retry the call.
- FROM a script or agent that runs a gated action unattended with `confirm: true` TO a pending answer the person decides in the app, or the `no_confirmation_service` refusal on a host without the service. Where the action is safe unattended, its author declares `ai.requiresConfirmation: false`.
- A host that wants its people to approve these calls registers an `IActionConfirmationService` under `ACTION_CONFIRMATION_SERVICE`.
34 changes: 16 additions & 18 deletions examples/app-todo/test/mcp-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('app-todo business actions over MCP (CE runtime, no service-ai)', () =>
// bridge never sees the runtime gate. Here the member travels from a real
// JSON-RPC `tools/call`, through both of those layers, into the real gate —
// and the record afterwards says whether anything ran.
it('step 7: ai.requiresConfirmation is ENFORCED, and satisfiable', async () => {
it('step 7: ai.requiresConfirmation is ENFORCED, and the caller\'s confirm: true never satisfies it', async () => {
// Same app, but todo_complete_task now declares the author's gate.
const gatedConfirmObjects = withTaskAction(mergedObjects, 'todo_complete_task', (a) => ({
...a,
Expand All @@ -241,23 +241,21 @@ describe('app-todo business actions over MCP (CE runtime, no service-ai)', () =>

const gatedId = await seedTask('Needs a human');

// 7a — WITHOUT the member: refused, with the declared code, and NOTHING ran.
const refused = await callMcp(confirmBridge, toolsCall(12, 'run_action', { actionName: 'todo_complete_task', recordId: gatedId }));
expect(refused.result?.isError).toBe(true);
const refusedEnvelope = JSON.parse(refused.result?.content?.[0]?.text ?? '{}');
expect(refusedEnvelope?.error?.code).toBe('ACTION_CONFIRMATION_REQUIRED');
expect(refusedEnvelope?.error?.status).toBe(428);
expect(refusedEnvelope?.error?.details?.actionName).toBe('todo_complete_task');
expect(refusedEnvelope?.error?.details?.confirmationMember).toBe('confirm');
expect(await statusOf(gatedId)).toBe('not_started');

// 7b — WITH the member: the identical call succeeds and the handler runs.
// This is the leg that proves the member is not stripped: before the door
// grew it, this call was refused exactly like 7a and the action was
// permanently un-invokable.
const confirmed = await callMcp(confirmBridge, toolsCall(13, 'run_action', { actionName: 'todo_complete_task', recordId: gatedId, confirm: true }));
expect(confirmed.result?.isError, confirmed.result?.content?.[0]?.text).not.toBe(true);
expect(await statusOf(gatedId)).toBe('completed');
// 7a/7b — [#33690] WITH the member and WITHOUT it alike: this app registers
// no pending-confirmation service, so the call is refused with the
// declared code and `details.reason: 'no_confirmation_service'`, and
// NOTHING ran. Before the amended contract 7b (with `confirm: true`) ran
// the handler; the member now travels the whole path and confers nothing.
for (const [id, extra] of [[12, {}], [13, { confirm: true }]] as const) {
const refused = await callMcp(confirmBridge, toolsCall(id, 'run_action', { actionName: 'todo_complete_task', recordId: gatedId, ...extra }));
expect(refused.result?.isError).toBe(true);
const refusedEnvelope = JSON.parse(refused.result?.content?.[0]?.text ?? '{}');
expect(refusedEnvelope?.error?.code).toBe('ACTION_CONFIRMATION_REQUIRED');
expect(refusedEnvelope?.error?.status).toBe(428);
expect(refusedEnvelope?.error?.details?.actionName).toBe('todo_complete_task');
expect(refusedEnvelope?.error?.details?.reason).toBe('no_confirmation_service');
expect(await statusOf(gatedId)).toBe('not_started');
}

// 7c — the heuristic must NOT gate: todo_delete_completed is `variant:'danger'`
// and the listing calls it requiresConfirmation, but its author declared
Expand Down
134 changes: 134 additions & 0 deletions packages/mcp/src/mcp-action-confirmation-handoff-33690.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#33690] The `run_action` and `resume_run` tools relay the amended
* confirmation contract's two answers through the real MCP door
* (`MCPServerRuntime.handleHttpRequest`, JSON-RPC `tools/call`):
*
* - a hand-off (`ActionConfirmationPending`, `status: 'pending_confirmation'`)
* is the call's SUCCESSFUL answer: not an error result, the pending id and
* message intact, so an agent tells its person where to approve;
* - the `no_confirmation_service` refusal keeps the registered envelope
* (`428 ACTION_CONFIRMATION_REQUIRED`) with `details.reason`.
*
* The bridge is a double standing in for the runtime door, whose hand-off is
* pinned in `packages/runtime/src/action-confirmation-handoff-33690.test.ts`;
* this package asserts the tool's half (relay, envelope) without depending on
* `@objectstack/runtime`, which deliberately does not depend back.
*/

import { describe, it, expect } from 'vitest';

import { AI_ACTION_CONFIRMATION_MEMBER, type ActionConfirmationPending } from '@objectstack/spec/contracts';

import { MCPServerRuntime } from './mcp-server-runtime.js';
import type { McpDataBridge, McpActionBridge } from './mcp-http-tools.js';

const GATED = 'archive_account';

const pendingOf = (n: number): ActionConfirmationPending => ({
status: 'pending_confirmation',
pendingId: `pa_${n}`,
message: `Action '${GATED}' on 'account' declares ai.requiresConfirmation: true, so a person confirms it. Pending confirmation: pa_${n}.`,
actionName: GATED,
objectName: 'account',
});

const NO_SERVICE = () => Object.assign(
new Error(`Action '${GATED}' on 'account' declares ai.requiresConfirmation: true, so a person confirms it — nothing was run.`),
{
code: 'ACTION_CONFIRMATION_REQUIRED',
status: 428,
details: { actionName: GATED, objectName: 'account', confirmationMember: AI_ACTION_CONFIRMATION_MEMBER, reason: 'no_confirmation_service' },
},
);

function makeBridge(mode: 'service' | 'none'): McpDataBridge & McpActionBridge & { calls: any[] } {
const calls: any[] = [];
let n = 0;
const answer = async () => {
if (mode === 'none') throw NO_SERVICE();
return pendingOf(++n);
};
return {
calls,
async listObjects() { return []; },
async describeObject() { return null; },
async query() { return { records: [] }; },
async get() { return null; },
async create() { return {}; },
async update() { return {}; },
async remove() { return {}; },
async listActions() {
return [{ name: GATED, objectName: 'account', type: 'script', requiresRecord: true, requiresConfirmation: true }];
},
async runAction(name: string, input: any) {
calls.push(['runAction', name, input]);
return answer();
},
async resumeRun(runId: string, input: any) {
calls.push(['resumeRun', runId, input]);
return answer();
},
};
}

function mcpRequest(body: unknown): Request {
return new Request('http://localhost/api/v1/mcp', {
method: 'POST',
headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' },
body: JSON.stringify(body),
});
}

const toolsCall = (id: number, name: string, args: Record<string, unknown>) => ({
jsonrpc: '2.0', id, method: 'tools/call', params: { name, arguments: args },
});

async function call(bridge: McpDataBridge & McpActionBridge, body: unknown): Promise<any> {
const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' });
const res = await runtime.handleHttpRequest(mcpRequest(body), { bridge, parsedBody: body });
return res.json();
}

describe('[#33690] run_action and resume_run relay a hand-off as the call\'s successful answer', () => {
it('run_action: each confirm: true call answers pending_confirmation with its own pending id, never an error', async () => {
const bridge = makeBridge('service');
for (let i = 1; i <= 3; i++) {
const json = await call(bridge, toolsCall(i, 'run_action', { actionName: GATED, recordId: 'a1', [AI_ACTION_CONFIRMATION_MEMBER]: true }));
expect(json.result.isError).toBeFalsy();
expect(JSON.parse(json.result.content[0].text)).toEqual(pendingOf(i));
}
});

it('resume_run: the hand-off is relayed the same way', async () => {
const bridge = makeBridge('service');
const json = await call(bridge, toolsCall(1, 'resume_run', { runId: 'run_1', values: { reason: 'x' }, [AI_ACTION_CONFIRMATION_MEMBER]: true }));
expect(json.result.isError).toBeFalsy();
expect(JSON.parse(json.result.content[0].text)).toEqual(pendingOf(1));
expect(bridge.calls[0]).toEqual(['resumeRun', 'run_1', { values: { reason: 'x' }, [AI_ACTION_CONFIRMATION_MEMBER]: true }]);
});

it('no service: the refusal keeps the registered envelope and carries details.reason', async () => {
for (const [tool, args] of [
['run_action', { actionName: GATED, recordId: 'a1', [AI_ACTION_CONFIRMATION_MEMBER]: true }],
['resume_run', { runId: 'run_1', [AI_ACTION_CONFIRMATION_MEMBER]: true }],
] as const) {
const json = await call(makeBridge('none'), toolsCall(1, tool, args));
expect(json.result.isError, tool).toBe(true);
const envelope = JSON.parse(json.result.content[0].text);
expect({ code: envelope.error.code, status: envelope.error.status }, tool).toEqual({ code: 'ACTION_CONFIRMATION_REQUIRED', status: 428 });
expect(envelope.error.details, tool).toEqual({
actionName: GATED, objectName: 'account', confirmationMember: AI_ACTION_CONFIRMATION_MEMBER, reason: 'no_confirmation_service',
});
}
});

it('the member stays accepted on both tools, so a call carrying it is not refused for it', async () => {
const json = await call(makeBridge('service'), { jsonrpc: '2.0', id: 1, method: 'tools/list' });
for (const name of ['run_action', 'resume_run']) {
const tool = json.result.tools.find((t: any) => t.name === name);
expect(tool?.inputSchema.properties[AI_ACTION_CONFIRMATION_MEMBER]?.type, name).toBe('boolean');
}
});
});
47 changes: 29 additions & 18 deletions packages/mcp/src/mcp-http-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ export interface McpActionBridge {
* as the caller, loads the subject record under RLS when row-context, then
* dispatches through the framework action runner. Throws on
* denial / not-found / handler failure so the tool surfaces a tool-error.
* [#33690] An action declaring `ai.requiresConfirmation: true` is not run:
* the host hands it to the person and resolves to the contract's
* `ActionConfirmationPending` (`status: 'pending_confirmation'`), which the
* tool relays as its successful result; the request's `confirm` is not read.
*/
runAction(
name: string,
Expand Down Expand Up @@ -469,7 +473,9 @@ export interface McpActionBridge {
* caller, and the run is parked on a screen. Resolves to `runAction`'s
* envelope (`{ ok, action, objectName, recordId?, result }`). A run that
* pauses on its next screen comes back paused again. Throws on refusal, with
* the ADR-0112 `code` / `status` on the thrown value.
* the ADR-0112 `code` / `status` on the thrown value. [#33690] A resume of a
* gated action resolves to `ActionConfirmationPending` instead, as
* {@link runAction} does, and the run stays parked.
*
* OPTIONAL, like {@link McpDataBridge.aggregate}. A host that cannot resume
* runs omits it, and `resume_run` is then not registered.
Expand Down Expand Up @@ -3364,10 +3370,16 @@ export function registerActionTools(
'this can mutate data or trigger flows. Invocation is gated (author AI opt-in + your capabilities), ' +
'but the action body itself runs as trusted application code with the app\'s full data authority. ' +
'Supply recordId for actions that operate on a specific record, and params for any declared inputs. ' +
'An action the author gated (list_actions reports requiresConfirmation) is REFUSED unless you also ' +
'send confirm: true — ask the human first, then retry; nothing runs on a refused call. ' +
// [ADR-0151 D3 rule 3] `actionConfirmationRefusal` (runtime) never reads
// the member for an AI service account, so no retry is taught to one.
// [#33690] The amended confirmation contract: the caller's `confirm`
// never counts on an action whose author declared
// `ai.requiresConfirmation: true`; the person is asked in the app.
'An action its author declared ai.requiresConfirmation: true never runs on this call, and confirm: true ' +
'does not change that: the call is handed to the person you act for, who approves or rejects it in ' +
'the app, and you are answered status "pending_confirmation" with a pendingId and a message to pass on. ' +
'Nothing has run then; do not retry, tell the user to approve it. Where the server cannot hand it off, ' +
'the call is refused ACTION_CONFIRMATION_REQUIRED (details.reason "no_confirmation_service"): the user ' +
'performs the action in the app. ' +
// [ADR-0151 D3 rule 3] An AI service account is refused, never handed off.
'Under an AI service account\'s own key (an agent acting for itself, for no person) the call is ' +
'refused whatever it sends and nothing runs: hand the action to a person, or its author declares ' +
'ai.requiresConfirmation: false when it is safe unattended. ' +
Expand Down Expand Up @@ -3446,11 +3458,9 @@ export function registerActionTools(
.boolean()
.optional()
.describe(
'Set to true to confirm a call the app author gated with ai.requiresConfirmation '
+ '(list_actions reports requiresConfirmation for each action). Assert this only when '
+ 'the human in the loop has approved THIS call; without it a gated action is refused '
+ 'and nothing runs. Under an AI service account\'s own key it is not read: the call is '
+ 'refused whatever it sends.',
'Not read by this tool: an action its author declared ai.requiresConfirmation: true is handed '
+ 'to the person to approve in the app whatever this member says, and any other action runs '
+ 'without it. Accepted so that a call carrying it is not refused for it.',
),
},
),
Expand Down Expand Up @@ -3545,9 +3555,12 @@ export function registerActionTools(
'pauses again on its NEXT screen with a new screen to fill (call resume_run again for it). Only ' +
'the user whose call started the run can resume it, and it is gated exactly as run_action is ' +
'(author AI opt-in, your capabilities, your read access to the record). The rest of the flow runs ' +
'the app\'s business logic and can mutate data. When list_actions reports requiresConfirmation for ' +
'the action that started the run, send confirm: true (ask the human first); without it the call ' +
'is refused and the run stays paused. Under an AI service account\'s own key the call is refused ' +
'the app\'s business logic and can mutate data. When the action that started the run was declared ' +
'ai.requiresConfirmation: true, the run does not continue on this call and confirm: true does not ' +
'change that: it is handed to the person you act for, who approves or rejects it in the app, and you ' +
'are answered status "pending_confirmation" with a pendingId; the run stays paused until they ' +
'approve. Where the server cannot hand it off, the call is refused ACTION_CONFIRMATION_REQUIRED and ' +
'the run stays paused. Under an AI service account\'s own key the call is refused ' +
'whatever it sends and the run stays paused: hand it to a person. A run that fails answers as ' +
'run_action does: tell the user the reason in the second text block, never the flow and node chain ' +
'in the error message.',
Expand Down Expand Up @@ -3589,11 +3602,9 @@ export function registerActionTools(
.boolean()
.optional()
.describe(
'Set to true to confirm resuming a run of an action the app author gated with '
+ 'ai.requiresConfirmation (list_actions reports requiresConfirmation). Assert this only when '
+ 'the human in the loop has approved THIS call; without it the call is refused and the run '
+ 'stays paused. Under an AI service account\'s own key it is not read: the call is refused '
+ 'whatever it sends.',
'Not read by this tool: a resume of a run whose action was declared ai.requiresConfirmation: '
+ 'true is handed to the person to approve in the app whatever this member says. Accepted so '
+ 'that a call carrying it is not refused for it.',
),
},
),
Expand Down
Loading
Loading