mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
* fix(mcp)!: deliver over-budget delivery facts inside the structured result
Codex and VS Code forward only structuredContent to the model, and Gemini
CLI and LangChain forward only the text blocks; _meta reaches no model. An
over-budget result put its delivery facts in a second text block and in
_meta, so Codex and VS Code agents received a summary with no sign that it
was one.
An over-budget result is now {summary, delivery} in structuredContent,
the shape the CLI already prints, and its one text block is that value's
serialization. The delivery layer owns the alternative: every budgeted
operation's registered output schema admits it, so canonical and compact
advertisements validate the result.
An overflowing completed result no longer picks its recovery by tool
name. The producer declares an overflow recovery (stateless, retained or
committed, with its next call and identities): list_unknowns,
get_evidence_bundle, inspect_address_context and committed unknowns
declare theirs in their bindings, and retained Evidence declares its own.
Undeclared results fall back to the contract's effects. Only a CLI
contract that reads no session is told to use its `rea` command, so
session reads and committed mutations are never told to use CLI JSON.
BREAKING CHANGE: When a budgeted tool's omitted detail returns a retained
summary, structuredContent is {"summary": <summary Evidence>, "delivery":
<delivery facts>} instead of the summary Evidence, the second
{"delivery": ...} text block is gone, and _meta["rea/agent-budget-delivery"]
is no longer set. Read the summary from structuredContent.summary and the
delivery facts from structuredContent.delivery (or the same members of the
text block). Pass detail "summary" or "complete" to receive Evidence at the
root as before.
Refs #15338
Refs #15326
* fix(delivery)!: deliver every error result under the agent result budget
A failure whose text exceeds delivery.agent_result_budget_bytes is now
retained whole as Evidence (mcp_tool_error in the MCP session,
cli_command_error in the CLI's private cache) and replaced by a bounded
summary. The summary keeps the code, category, retry, recovery and
remediation and every detail field that fits, names the fields it left
out, and carries details.failure_delivery with the inspect_analysis_view
call that pages the complete failure. Non-Evidence facts such as a
mid-extraction artifact inventory were previously inlined whole.
The transport's unretained string-ceiling fallback no longer points at
complete CLI JSON output.
BREAKING CHANGE: An MCP error, or a one-shot CLI failure without
--evidence_output, larger than the agent result budget is delivered as a
bounded summary: oversized details fields are listed in
details.omitted_details instead of being inlined, and
details.failure_delivery names the retained failure Evidence and the
inspect_analysis_view call that pages it. Pass detail complete (CLI
--detail complete) for the complete failure inline, or raise
delivery.agent_result_budget_bytes.
Refs #15326
* fix(delivery): keep overflow recovery from repeating effects or losing the session target
A retained overflow no longer offers the CLI command: the retained record is the complete result, and a call with effects is told not to repeat it. The CLI fallback for an unretained result excludes session tools. The MCP result guarantee is limited to successful results. The schema validity test selects the complete-result alternative of a budgeted output schema.
* fix(delivery): keep failure summaries schema-valid and CLI failure retention self-contained
boundedFailureSummary restores the details a code's schema requires. The CLI retains the failure as it was before its partial observation was summarized, so the record does not depend on a partial file the cache could evict, and a cache write that commits before failing or leaves cleanup incomplete keeps its receipt and cleanup owner.
* fix(delivery): retain the delivered CLI failure with its complete partial restored
* fix(delivery): budget CLI-enveloped failures through the envelope's owner
Provider-backed CLI commands wrap the canonical failure in their error envelope (a label and input diagnostics). The strict projection schema rejected the wrapped failure, so an over-budget failure printed whole without being retained. The envelope's owner now splits it; delivery budgets the canonical failure and keeps the envelope fields on the bounded result. CLI logging uses the same splitter instead of its own destructuring.
* fix(cli): print a bounded failure's retrieval facts on a terminal
A failure bounded to the agent budget carries its retained record and paging call in details.failure_delivery. The terminal presentation printed only the concise error for it, because its preserved-Evidence check did not know that field. It now does, so the structured failure is printed whole.
619 lines
21 KiB
TypeScript
619 lines
21 KiB
TypeScript
import type {
|
|
CallToolResult,
|
|
ServerContext,
|
|
} from "@modelcontextprotocol/server";
|
|
import type { Logger } from "pino";
|
|
import type { z } from "zod";
|
|
|
|
import type { ProgressReporter } from "../ports/ProgressReporter.js";
|
|
import { mcpProgressReporter } from "./mcpProgress.js";
|
|
import type {
|
|
AnalysisOperationPort,
|
|
ExecutionOptions,
|
|
} from "../ports/AnalysisProvider.js";
|
|
import {
|
|
projectRetainedAnalysis,
|
|
summarizeRetainedAnalysis,
|
|
} from "../application/analysisView/AnalysisViewService.js";
|
|
import {
|
|
requireAnalysisViewRetention,
|
|
type AnalysisViewRetention,
|
|
} from "../application/analysisView/AnalysisViewRetention.js";
|
|
import type { AnalysisViewRequest } from "../domain/analysisView/analysisViewRequest.js";
|
|
import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js";
|
|
import type { ToolContract } from "../contracts/toolContractTypes.js";
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import type { Evidence } from "../domain/evidence.js";
|
|
import type { JsonValue } from "../domain/jsonValue.js";
|
|
import { isRecord } from "../kernel/isRecord.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
|
|
import type { EvidenceMcpServer } from "./EvidenceMcpServer.js";
|
|
import type { ToolResultEncoding } from "./toolResultEncoding.js";
|
|
import type { OverflowRecovery } from "./ToolResultDelivery.js";
|
|
import { logToolExecution } from "./toolLogging.js";
|
|
import type { WithAdmittedAnalysis } from "./analysisAdmission.js";
|
|
import { runAdmittedToolOperation } from "./admittedToolOperation.js";
|
|
import { sessionRefusalError } from "../application/binary/sessionAdmission.js";
|
|
import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js";
|
|
import type { InvestigationWorkflowExecution } from "../application/investigation/InvestigationWorkflows.js";
|
|
import { withAdmittedAnalysis as sessionAdmission } from "./analysisAdmission.js";
|
|
import { deliverInvestigationWorkflow } from "./deliverInvestigationWorkflow.js";
|
|
import { runDerivedOperation } from "./runDerivedOperation.js";
|
|
import { createOperationProgress } from "./operationProgress.js";
|
|
import {
|
|
deliverApplicationExecution,
|
|
type ApplicationExecutionRecords,
|
|
} from "./deliverApplicationExecution.js";
|
|
|
|
/** Application service executing one named contract operation. */
|
|
export interface ContractToolService<TOperation extends string> {
|
|
execute(
|
|
operation: TOperation,
|
|
input: unknown,
|
|
options?: ExecutionOptions,
|
|
): Promise<Result<Evidence, AnalysisError>>;
|
|
}
|
|
|
|
/** Record and deliver one Evidence result using the tool's exact contract. */
|
|
export const deliverEvidenceResult = (
|
|
server: EvidenceMcpServer,
|
|
result: Result<Evidence, AnalysisError>,
|
|
contract: ToolContract,
|
|
recordEvidence?: EvidenceWriter["recordEvidence"],
|
|
viewSource?: JsonValue | (() => JsonValue | undefined),
|
|
): CallToolResult => {
|
|
if (!result.ok)
|
|
return server.delivery.toPartialErrorToolResult(
|
|
result.error,
|
|
contract.returnsTargetContent,
|
|
recordEvidence,
|
|
);
|
|
const recorded = recordEvidence?.(result.value);
|
|
return server.delivery.toEvidenceToolResult(
|
|
result.value,
|
|
contract,
|
|
recorded,
|
|
viewSource,
|
|
recordEvidence,
|
|
);
|
|
};
|
|
|
|
/** Input admission owns whether inspection receives defaults or caller selections. */
|
|
export type RetainedSummaryOperation<Input> =
|
|
| {
|
|
readonly admission: "sdk";
|
|
readonly summary: (
|
|
input: Omit<Input, "detail" | "view">,
|
|
options: ExecutionOptions,
|
|
) => Promise<Result<Evidence, AnalysisError>>;
|
|
}
|
|
| {
|
|
readonly admission: "application";
|
|
readonly summary: (
|
|
input: unknown,
|
|
options: ExecutionOptions,
|
|
) => Promise<Result<Evidence, AnalysisError>>;
|
|
};
|
|
|
|
/** Validate delivery selection before inspection and retain its complete source for summaries. */
|
|
export const createRetainedSummaryToolHandler = <
|
|
Name extends string,
|
|
InputSchema extends z.ZodType<
|
|
Readonly<Record<string, unknown>> & {
|
|
readonly detail?: "complete" | "summary" | undefined;
|
|
readonly view?: AnalysisViewRequest;
|
|
}
|
|
>,
|
|
>(
|
|
server: EvidenceMcpServer,
|
|
contract: ToolContract<Name, InputSchema>,
|
|
operation: RetainedSummaryOperation<z.output<InputSchema>>,
|
|
logger: Logger,
|
|
retention: AnalysisViewRetention,
|
|
withAdmittedAnalysis?: WithAdmittedAnalysis,
|
|
) => {
|
|
return createEvidenceToolHandler(
|
|
server,
|
|
async (rawInput, options) => {
|
|
if (operation.admission === "sdk") {
|
|
// The MCP SDK already validated `rawInput` against `contract.inputSchema`.
|
|
const { detail, view, ...input } = rawInput as z.output<InputSchema>;
|
|
if (detail === "summary") {
|
|
const ready = requireAnalysisViewRetention(contract.name, retention);
|
|
if (!ready.ok) return ready;
|
|
}
|
|
const inspected = await operation.summary(input, options);
|
|
return detail === "summary" && inspected.ok
|
|
? projectRetainedAnalysis(
|
|
inspected.value,
|
|
retention,
|
|
view ?? { kind: "summary" },
|
|
)
|
|
: inspected;
|
|
}
|
|
const parsed = contract.inputSchema.safeParse(rawInput);
|
|
if (!parsed.success)
|
|
return err(
|
|
analysisInputErrorFromIssues(
|
|
contract.name,
|
|
parsed.error.issues,
|
|
rawInput,
|
|
{ cause: parsed.error },
|
|
),
|
|
);
|
|
const { detail, view } = parsed.data;
|
|
if (detail === "summary") {
|
|
const ready = requireAnalysisViewRetention(contract.name, retention);
|
|
if (!ready.ok) return ready;
|
|
}
|
|
const inspected = await operation.summary(
|
|
isRecord(rawInput)
|
|
? Object.fromEntries(
|
|
Object.entries(rawInput).filter(
|
|
([field]) => field !== "detail" && field !== "view",
|
|
),
|
|
)
|
|
: rawInput,
|
|
options,
|
|
);
|
|
return detail === "summary" && inspected.ok
|
|
? projectRetainedAnalysis(
|
|
inspected.value,
|
|
retention,
|
|
view ?? { kind: "summary" },
|
|
)
|
|
: inspected;
|
|
},
|
|
logger,
|
|
retention.recordEvidence,
|
|
withAdmittedAnalysis,
|
|
)(contract);
|
|
};
|
|
|
|
/**
|
|
* Bind a named Evidence-producing operation to shared logging and delivery.
|
|
* The operation receives the request signal and its MCP progress reporter.
|
|
*/
|
|
export const createEvidenceToolHandler =
|
|
(
|
|
server: EvidenceMcpServer,
|
|
operation: (
|
|
input: unknown,
|
|
options: ExecutionOptions,
|
|
) => Promise<Result<Evidence, AnalysisError>>,
|
|
logger: Logger,
|
|
recordEvidence?: EvidenceWriter["recordEvidence"],
|
|
withAdmittedAnalysis?: WithAdmittedAnalysis,
|
|
viewSource?: (input: unknown) => JsonValue | undefined,
|
|
) =>
|
|
<TOperation extends string>(contract: ToolContract<TOperation>) =>
|
|
(input: unknown, context: ServerContext) =>
|
|
runAdmittedToolOperation(
|
|
server,
|
|
withAdmittedAnalysis,
|
|
contract.name,
|
|
context.mcpReq.signal,
|
|
async () => {
|
|
const result = await logToolExecution(logger, contract.name, () =>
|
|
operation(input, {
|
|
signal: context.mcpReq.signal,
|
|
progress: mcpProgressReporter(context),
|
|
}),
|
|
);
|
|
return deliverEvidenceResult(
|
|
server,
|
|
result,
|
|
contract,
|
|
recordEvidence,
|
|
viewSource === undefined ? undefined : () => viewSource(input),
|
|
);
|
|
},
|
|
);
|
|
|
|
/** The MCP request a value-returning workflow runs for. */
|
|
export interface ValueToolRequest {
|
|
readonly signal: AbortSignal;
|
|
readonly progress: ProgressReporter;
|
|
/** The request itself, for workflows that report the client's own facts. */
|
|
readonly context: ServerContext;
|
|
}
|
|
|
|
/**
|
|
* Smaller forms of a value, offered in order when the complete value does not
|
|
* fit the response budget. Each keeps the contract's result shape.
|
|
*/
|
|
export type ValueAlternatives = (
|
|
failure: Extract<ToolResultEncoding, { readonly ok: false }>,
|
|
budgetBytes: number,
|
|
) => Iterable<JsonValue>;
|
|
|
|
/**
|
|
* A value with the delivery facts only its producer knows: what a caller can
|
|
* do when the value does not fit one response (a committed record's receipt,
|
|
* say), and smaller forms of the value to deliver first, in order, each in
|
|
* the contract's result shape.
|
|
*/
|
|
export class DeclaredValue {
|
|
constructor(
|
|
readonly value: JsonValue,
|
|
readonly declared: {
|
|
readonly overflow?: OverflowRecovery;
|
|
readonly alternatives?: ValueAlternatives;
|
|
},
|
|
) {}
|
|
}
|
|
|
|
/**
|
|
* A value followed by content blocks an upstream tool returned (a build
|
|
* computer's screenshot, say), delivered unchanged after REA's JSON.
|
|
*/
|
|
export class ForwardedValue {
|
|
constructor(
|
|
readonly value: JsonValue,
|
|
readonly content: readonly Readonly<Record<string, JsonValue>>[],
|
|
) {}
|
|
}
|
|
|
|
/**
|
|
* A failure delivered with facts its operation observed alongside the error
|
|
* (an open's selection at failure, say), added to the error's details.
|
|
*/
|
|
export class DetailedFailure {
|
|
constructor(
|
|
readonly error: AnalysisError,
|
|
readonly details: Readonly<Record<string, JsonValue>>,
|
|
) {}
|
|
}
|
|
|
|
/** What a value workflow returns. */
|
|
export type ValueOutcome = Result<
|
|
JsonValue | DeclaredValue | ForwardedValue,
|
|
AnalysisError | DetailedFailure
|
|
>;
|
|
|
|
/**
|
|
* Bind a named workflow returning a plain JSON value to shared logging and
|
|
* delivery. A success records no Evidence; a failure's partial Evidence (a
|
|
* cancelled session's cleanup record, say) is delivered through the session
|
|
* writer like any other. It runs under analysis admission only when one is
|
|
* supplied, and then receives the analysis that admission grants.
|
|
*/
|
|
export const createValueToolHandler =
|
|
(
|
|
server: EvidenceMcpServer,
|
|
operation: (
|
|
input: unknown,
|
|
request: ValueToolRequest & {
|
|
readonly analysis?: AnalysisOperationPort;
|
|
},
|
|
) => Promise<ValueOutcome>,
|
|
logger: Logger,
|
|
withAdmittedAnalysis?: WithAdmittedAnalysis,
|
|
recordEvidence?: EvidenceWriter["recordEvidence"],
|
|
) =>
|
|
(contract: ToolContract) =>
|
|
async (input: unknown, context: ServerContext): Promise<CallToolResult> =>
|
|
runAdmittedToolOperation(
|
|
server,
|
|
withAdmittedAnalysis,
|
|
contract.name,
|
|
context.mcpReq.signal,
|
|
async (analysis) => {
|
|
const result = await logToolExecution(logger, contract.name, () =>
|
|
operation(input, {
|
|
signal: context.mcpReq.signal,
|
|
progress: mcpProgressReporter(context),
|
|
context,
|
|
...(analysis === undefined ? {} : { analysis }),
|
|
}),
|
|
);
|
|
if (!result.ok)
|
|
return result.error instanceof DetailedFailure
|
|
? server.delivery.toDetailedErrorToolResult(
|
|
result.error.error,
|
|
result.error.details,
|
|
contract.returnsTargetContent,
|
|
)
|
|
: server.delivery.toPartialErrorToolResult(
|
|
result.error,
|
|
contract.returnsTargetContent,
|
|
recordEvidence,
|
|
);
|
|
const { value } = result;
|
|
return value instanceof DeclaredValue
|
|
? server.delivery.toCallToolResult(
|
|
ok(value.value),
|
|
contract,
|
|
value.declared,
|
|
)
|
|
: value instanceof ForwardedValue
|
|
? server.delivery.toForwardedToolResult(
|
|
ok(value.value),
|
|
contract,
|
|
value.content,
|
|
)
|
|
: server.delivery.toCallToolResult(ok(value), contract);
|
|
},
|
|
);
|
|
|
|
/** What a derivation may read before it runs. */
|
|
export interface DerivedSources {
|
|
readonly evidenceForAnalysis: BinarySessionPort["evidenceForAnalysis"];
|
|
readonly signal: AbortSignal;
|
|
}
|
|
|
|
/**
|
|
* Read a derivation's inputs, then return the synchronous derivation over
|
|
* them. A derivation from session-held Evidence alone acquires nothing.
|
|
*/
|
|
export type DerivedAcquisition = (
|
|
input: unknown,
|
|
sources: DerivedSources,
|
|
) => Promise<
|
|
Result<
|
|
() => Result<InvestigationWorkflowExecution, AnalysisError>,
|
|
AnalysisError
|
|
>
|
|
>;
|
|
|
|
/**
|
|
* Derive Evidence from Evidence the session holds and the inputs the binding
|
|
* acquires. The session owner admits the call, resolves its session-held
|
|
* inputs, retains the sources the derivation read and records the derived
|
|
* Evidence with its unknowns, so provenance is the same whichever tool
|
|
* derives it. An acquisition failure is delivered as is.
|
|
*/
|
|
export const createDerivedToolHandler =
|
|
(
|
|
server: EvidenceMcpServer,
|
|
session: BinarySessionPort,
|
|
acquire: DerivedAcquisition,
|
|
logger: Logger,
|
|
) =>
|
|
(contract: ToolContract) => {
|
|
const admission = sessionAdmission({ kind: "session", session });
|
|
const evidenceForAnalysis = session.evidenceForAnalysis.bind(session);
|
|
return async (
|
|
input: unknown,
|
|
context: ServerContext,
|
|
): Promise<CallToolResult> =>
|
|
runAdmittedToolOperation(
|
|
server,
|
|
admission,
|
|
contract.name,
|
|
context.mcpReq.signal,
|
|
async () => {
|
|
const outcome = await logToolExecution(
|
|
logger,
|
|
contract.name,
|
|
async () => {
|
|
const acquired = await acquire(input, {
|
|
evidenceForAnalysis,
|
|
signal: context.mcpReq.signal,
|
|
});
|
|
if (!acquired.ok)
|
|
return { ...acquired, stage: "acquire" as const };
|
|
const derive = acquired.value;
|
|
const derived = await runDerivedOperation(
|
|
context,
|
|
contract.name,
|
|
() => {
|
|
const result = derive();
|
|
if (!result.ok) throw result.error;
|
|
return result.value;
|
|
},
|
|
(value) => value,
|
|
);
|
|
return { ...derived, stage: "derive" as const };
|
|
},
|
|
);
|
|
return outcome.stage === "acquire"
|
|
? server.delivery.toCallToolResult(outcome, contract)
|
|
: deliverInvestigationWorkflow(server, session, contract, outcome);
|
|
},
|
|
);
|
|
};
|
|
|
|
/** The request an application workflow runs for. */
|
|
export interface ApplicationWorkflowRequest {
|
|
readonly signal: AbortSignal;
|
|
readonly progress: ProgressReporter;
|
|
}
|
|
|
|
/** What an application workflow binding runs and the view it stages. */
|
|
export interface ApplicationWorkflow {
|
|
readonly workflow: (
|
|
input: unknown,
|
|
request: ApplicationWorkflowRequest,
|
|
) => Promise<Result<InvestigationWorkflowExecution, AnalysisError>>;
|
|
/** The caller-selected view of the completed Evidence, when the request names one. */
|
|
readonly view?:
|
|
| ((input: unknown) => AnalysisViewRequest | undefined)
|
|
| undefined;
|
|
}
|
|
|
|
/**
|
|
* Run an application workflow under analysis admission and deliver its
|
|
* completed Evidence with its sources and residual unknown. A request for a
|
|
* view (or summary) is refused before the workflow runs when the complete
|
|
* Evidence cannot be retained; a failure delivers its partial Evidence.
|
|
*/
|
|
export const createApplicationToolHandler =
|
|
(
|
|
server: EvidenceMcpServer,
|
|
{ workflow, view }: ApplicationWorkflow,
|
|
logger: Logger,
|
|
records: ApplicationExecutionRecords,
|
|
withAdmittedAnalysis?: WithAdmittedAnalysis,
|
|
) =>
|
|
(contract: ToolContract) =>
|
|
async (input: unknown, context: ServerContext): Promise<CallToolResult> =>
|
|
runAdmittedToolOperation(
|
|
server,
|
|
withAdmittedAnalysis,
|
|
contract.name,
|
|
context.mcpReq.signal,
|
|
async () => {
|
|
const selected = view?.(input);
|
|
if (selected !== undefined) {
|
|
const retention = requireAnalysisViewRetention(contract.name, {
|
|
recordEvidence: records.recordEvidence,
|
|
evidenceById: records.evidenceById,
|
|
});
|
|
if (!retention.ok)
|
|
return server.delivery.toCallToolResult(retention, contract);
|
|
}
|
|
const result = await logToolExecution(logger, contract.name, () =>
|
|
workflow(input, {
|
|
signal: context.mcpReq.signal,
|
|
progress: mcpProgressReporter(context),
|
|
}),
|
|
);
|
|
if (!result.ok)
|
|
return server.delivery.toPartialErrorToolResult(
|
|
result.error,
|
|
contract.returnsTargetContent,
|
|
records.recordEvidence,
|
|
);
|
|
return deliverApplicationExecution(
|
|
server.delivery,
|
|
records,
|
|
contract,
|
|
result.value,
|
|
selected,
|
|
context.mcpReq.signal,
|
|
);
|
|
},
|
|
);
|
|
|
|
/**
|
|
* A provider-backed workflow's Evidence and, when the workflow already
|
|
* recorded it in the session, that record's result: delivery reuses it
|
|
* rather than recording (and re-validating) the Evidence again.
|
|
*/
|
|
export interface AnalysisWorkflowOutcome {
|
|
readonly evidence: Evidence;
|
|
readonly recorded?: Result<unknown, AnalysisError>;
|
|
}
|
|
|
|
/** A provider-backed workflow run on the analysis its admission grants. */
|
|
export type AnalysisWorkflow = (
|
|
input: unknown,
|
|
analysis: AnalysisOperationPort,
|
|
request: ApplicationWorkflowRequest,
|
|
) => Promise<Result<AnalysisWorkflowOutcome, AnalysisError>>;
|
|
|
|
/** Delivery controls and the owner of this complete workflow's terminal progress. */
|
|
export interface AnalysisWorkflowOptions {
|
|
readonly detail?: "delivery" | undefined;
|
|
readonly completionProgress?: "workflow" | undefined;
|
|
}
|
|
|
|
/**
|
|
* Run a provider-backed workflow on the analysis that admission grants for
|
|
* the call, reporting its start and terminal disposition, and deliver its
|
|
* Evidence. When the binding declares that `detail` selects delivery, it
|
|
* never reaches the workflow: a summary is refused before analysis when nothing
|
|
* retains the complete Evidence, and the complete Evidence is retained
|
|
* behind the summary it delivers.
|
|
*/
|
|
export const createAnalysisToolHandler =
|
|
(
|
|
server: EvidenceMcpServer,
|
|
analyze: AnalysisWorkflow,
|
|
logger: Logger,
|
|
retention: AnalysisViewRetention,
|
|
withAdmittedAnalysis: WithAdmittedAnalysis,
|
|
options: AnalysisWorkflowOptions,
|
|
) =>
|
|
(contract: ToolContract) =>
|
|
async (
|
|
rawInput: unknown,
|
|
context: ServerContext,
|
|
): Promise<CallToolResult> => {
|
|
const { detail, input } =
|
|
options.detail === "delivery"
|
|
? deliveryDetail(rawInput)
|
|
: { detail: undefined, input: rawInput };
|
|
if (detail === "summary") {
|
|
const ready = requireAnalysisViewRetention(contract.name, retention);
|
|
if (!ready.ok) return server.delivery.toCallToolResult(ready, contract);
|
|
}
|
|
const { signal } = context.mcpReq;
|
|
const admitted = await withAdmittedAnalysis(
|
|
contract.name,
|
|
signal,
|
|
async (analysis) => {
|
|
// A composed workflow is refused as a whole, as binary_session
|
|
// reports it, not through each child operation it would dispatch.
|
|
const refusal = server.toolAdmission?.(contract.name);
|
|
if (refusal !== undefined)
|
|
return server.delivery.toErrorToolResult(
|
|
sessionRefusalError(contract.name, refusal),
|
|
contract.returnsTargetContent,
|
|
);
|
|
const progress = createOperationProgress(
|
|
mcpProgressReporter(context),
|
|
contract.name,
|
|
options.completionProgress,
|
|
);
|
|
await progress.start();
|
|
const analyzed = await logToolExecution(logger, contract.name, () =>
|
|
analyze(input, analysis, { signal, progress }),
|
|
);
|
|
const response =
|
|
!analyzed.ok || detail === "summary"
|
|
? deliverEvidenceResult(
|
|
server,
|
|
analyzed.ok
|
|
? summarizeRetainedAnalysis(
|
|
analyzed.value.evidence,
|
|
retention,
|
|
)
|
|
: analyzed,
|
|
contract,
|
|
retention.recordEvidence,
|
|
)
|
|
: analyzed.value.recorded === undefined
|
|
? deliverEvidenceResult(
|
|
server,
|
|
ok(analyzed.value.evidence),
|
|
contract,
|
|
retention.recordEvidence,
|
|
)
|
|
: server.delivery.toEvidenceToolResult(
|
|
analyzed.value.evidence,
|
|
contract,
|
|
analyzed.value.recorded,
|
|
undefined,
|
|
retention.recordEvidence,
|
|
);
|
|
return progress.complete(response);
|
|
},
|
|
);
|
|
return admitted.ok
|
|
? admitted.value
|
|
: server.delivery.toErrorToolResult(
|
|
admitted.error,
|
|
contract.returnsTargetContent,
|
|
);
|
|
};
|
|
|
|
/** Split the delivery detail an SDK-admitted request selects from its workflow input. */
|
|
const deliveryDetail = (
|
|
rawInput: unknown,
|
|
): {
|
|
readonly detail: unknown;
|
|
readonly input: unknown;
|
|
} => {
|
|
if (
|
|
typeof rawInput !== "object" ||
|
|
rawInput === null ||
|
|
!("detail" in rawInput)
|
|
)
|
|
return { detail: undefined, input: rawInput };
|
|
const { detail, ...input } = rawInput;
|
|
return { detail, input };
|
|
};
|