refactor(mcp): simplify workflow and observation scope
This commit is contained in:
@@ -38,14 +38,6 @@ const activeExample = {
|
||||
application_root: "/Applications/Example.app/Contents/Resources",
|
||||
args: [],
|
||||
actions: [{ step_id: "exercise-ipc", kind: "click", selector: "#run" }],
|
||||
limits: {
|
||||
max_duration_ms: 60_000,
|
||||
action_timeout_ms: 5_000,
|
||||
max_actions: 20,
|
||||
max_ipc_events: 2_000,
|
||||
max_processes: 32,
|
||||
max_windows: 32,
|
||||
},
|
||||
};
|
||||
|
||||
/** Root-confined Electron file-page discovery and inspection contracts. */
|
||||
@@ -72,7 +64,7 @@ export const ELECTRON_TOOL_CONTRACTS = [
|
||||
name: "inspect_electron_page",
|
||||
...toolContractMetadata("inspect_electron_page"),
|
||||
description:
|
||||
"Passively inspect one approved Electron file page through CDP. Returns root-confined frames, DOM structure, resource paths, and scripts without evaluating renderer JavaScript or invoking Electron APIs; script contents require separate approval.",
|
||||
"Passively inspect one Electron file page using its list_electron_targets Evidence and returned target ID. Pass the discovery Evidence ID to reuse its endpoint and authorized file roots; this avoids repeating scope and cannot widen permission. Returns frames, DOM structure, resource paths, and scripts without evaluating renderer JavaScript or invoking Electron APIs.",
|
||||
kind: "electron-provider",
|
||||
inputSchema: inspectElectronPageToolInputSchema,
|
||||
outputSchema: inspectionOutputSchema,
|
||||
@@ -80,8 +72,7 @@ export const ELECTRON_TOOL_CONTRACTS = [
|
||||
{
|
||||
title: "Inspect an approved Electron page",
|
||||
input: {
|
||||
cdp_endpoint: endpoint,
|
||||
allowed_file_roots: [root],
|
||||
discovery_evidence_id: "EVIDENCE_ID_FROM_LIST_ELECTRON_TARGETS",
|
||||
target_id: "TARGET_ID_FROM_LIST_ELECTRON_TARGETS",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -36,7 +36,7 @@ export const JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS = [
|
||||
name: "observe_javascript_runtime",
|
||||
...toolContractMetadata("observe_javascript_runtime"),
|
||||
description:
|
||||
"Attach passively to one exact approved Node/Electron V8 Inspector target and capture bounded Debugger.scriptParsed plus Runtime execution-context events. REA sends only Runtime.enable and Debugger.enable: it never evaluates, pauses, resumes, reads source, or instruments the target. require/import edges, EventEmitter activity, and Electron IPC remain explicit unknowns; reconcile the returned Evidence with static Application Graph Evidence using reconcile_javascript_runtime.",
|
||||
"Attach passively to one Node/Electron V8 Inspector target using its list_javascript_runtime_targets Evidence and returned target ID. Pass the discovery Evidence ID to reuse its endpoint and authorized file/origin scope; this avoids repeating scope and cannot widen permission. Captures Debugger.scriptParsed and Runtime execution-context events. REA never evaluates, pauses, resumes, reads source, or instruments the target; require/import edges, EventEmitter activity, and Electron IPC remain explicit unknowns. Reconcile the result with static Application Graph Evidence using reconcile_javascript_runtime.",
|
||||
kind: "runtime-provider",
|
||||
inputSchema: observeJavaScriptRuntimeToolInputSchema,
|
||||
outputSchema: evidenceResultOf(javascriptRuntimeObservationSchema),
|
||||
@@ -44,8 +44,8 @@ export const JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS = [
|
||||
{
|
||||
title: "Observe one approved Node runtime",
|
||||
input: {
|
||||
inspector_endpoint: endpoint,
|
||||
allowed_file_roots: [root],
|
||||
discovery_evidence_id:
|
||||
"EVIDENCE_ID_FROM_LIST_JAVASCRIPT_RUNTIME_TARGETS",
|
||||
target_id: "TARGET_ID_FROM_LIST_JAVASCRIPT_RUNTIME_TARGETS",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -84,15 +84,14 @@ describe("guided prompt contracts", () => {
|
||||
).toBeLessThan(
|
||||
rendered.get("audit_residual_unknowns")?.indexOf("`update_unknown`") ?? 0,
|
||||
);
|
||||
expect(rendered.get("prepare_bounded_process_capture")).not.toContain(
|
||||
"`binary_session`",
|
||||
);
|
||||
expect(
|
||||
rendered
|
||||
.get("prepare_bounded_process_capture")
|
||||
?.indexOf("`binary_session`"),
|
||||
).toBeLessThan(
|
||||
rendered
|
||||
.get("prepare_bounded_process_capture")
|
||||
?.indexOf("`capture_process_scenario`") ?? 0,
|
||||
);
|
||||
?.indexOf("`capture_process_scenario`"),
|
||||
).toBeGreaterThanOrEqual(0);
|
||||
expect(
|
||||
rendered
|
||||
.get("compare_application_versions")
|
||||
|
||||
@@ -116,18 +116,14 @@ export const PROMPT_CONTRACTS = [
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
tools: [
|
||||
"binary_session",
|
||||
"open_binary",
|
||||
"analyze_javascript_application",
|
||||
],
|
||||
tools: ["open_binary", "analyze_javascript_application"],
|
||||
instruction:
|
||||
"For an approved JavaScript/Electron artifact, reconstruct its application graph without execution when useful; open target_path only when no matching target is active.",
|
||||
},
|
||||
{
|
||||
tools: ["list_documents", "set_current_document", "binary_overview"],
|
||||
tools: ["list_documents", "set_current_document"],
|
||||
instruction:
|
||||
"Select an explicitly discovered document, then establish bounded binary, language, segment, and procedure context.",
|
||||
"Select a document only when needed for the requested analysis.",
|
||||
},
|
||||
{
|
||||
tools: ["search_strings", "search_procedures", "procedure_address"],
|
||||
@@ -294,7 +290,7 @@ export const PROMPT_CONTRACTS = [
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
tools: ["binary_session", "open_binary", "binary_overview"],
|
||||
tools: ["open_binary"],
|
||||
instruction:
|
||||
"Confirm the target only when needed; open or analyze it if it is not already active and the request requires it.",
|
||||
},
|
||||
@@ -395,11 +391,6 @@ export const PROMPT_CONTRACTS = [
|
||||
),
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
tools: ["binary_session"],
|
||||
instruction:
|
||||
"Inspect capture capability, declared effects, and limits before proposing execution. An unavailable capability is not permission to widen policy.",
|
||||
},
|
||||
{
|
||||
tools: ["get_evidence_bundle"],
|
||||
instruction:
|
||||
|
||||
@@ -66,11 +66,15 @@ export const inspectElectronPageInputSchema = z
|
||||
}));
|
||||
export const inspectElectronPageToolInputSchema = z.union([
|
||||
z.strictObject({
|
||||
...inspectElectronPageFacts,
|
||||
discovery_evidence_id: z.string().min(1),
|
||||
target_id: z.string().trim().min(1).max(256),
|
||||
observation_ms: z.number().int().min(0).max(10_000).default(100),
|
||||
include_script_sources: z.literal(false).default(false),
|
||||
}),
|
||||
z.strictObject({
|
||||
...inspectElectronPageFacts,
|
||||
discovery_evidence_id: z.string().min(1),
|
||||
target_id: z.string().trim().min(1).max(256),
|
||||
observation_ms: z.number().int().min(0).max(10_000).default(100),
|
||||
include_script_sources: z.literal(true),
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -82,18 +82,23 @@ export const DEFAULT_JAVASCRIPT_RUNTIME_OBSERVATION_LIMITS = {
|
||||
};
|
||||
|
||||
/** Input for one bounded, attach-only V8 Inspector observation. */
|
||||
export const observeJavaScriptRuntimeToolInputSchema = z
|
||||
export const observeJavaScriptRuntimeToolInputSchema = z.strictObject({
|
||||
discovery_evidence_id: z.string().min(1),
|
||||
target_id: z.string().trim().min(1).max(256),
|
||||
runtime_kind: javascriptRuntimeKindSchema.optional(),
|
||||
observation_ms: z.number().int().min(0).max(10_000).default(100),
|
||||
});
|
||||
|
||||
/** Input after discovery Evidence supplies the authorized target scope. */
|
||||
export const observeJavaScriptRuntimeInputSchema = z
|
||||
.strictObject({
|
||||
...runtimeScope,
|
||||
target_id: z.string().trim().min(1).max(256),
|
||||
runtime_kind: javascriptRuntimeKindSchema.optional(),
|
||||
observation_ms: z.number().int().min(0).max(10_000).default(100),
|
||||
})
|
||||
.superRefine(requireRuntimeScope);
|
||||
|
||||
/** Internal parsed input with REA's fixed collection bounds applied. */
|
||||
export const observeJavaScriptRuntimeInputSchema =
|
||||
observeJavaScriptRuntimeToolInputSchema.transform((input) => ({
|
||||
.superRefine(requireRuntimeScope)
|
||||
.transform((input) => ({
|
||||
...input,
|
||||
limits: DEFAULT_JAVASCRIPT_RUNTIME_OBSERVATION_LIMITS,
|
||||
}));
|
||||
|
||||
@@ -55,10 +55,10 @@ import {
|
||||
} from "./sessionToolPolicies.js";
|
||||
|
||||
const TARGET_FREE_INSTRUCTIONS =
|
||||
"Start with binary_session to see the active target and available providers. For an archive or application package, call open_binary(path), then inspect_artifact for the complete graph and findings; use extract_artifact to materialize its regular contents when needed. For standalone JavaScript or ASAR analysis, use analyze_javascript_application. For managed PE/CLI files, use inspect_managed_artifact. For native binaries, open the target and start with binary_overview. For a live page or runtime, discover targets with list_browser_targets, list_electron_targets, or list_javascript_runtime_targets before inspection; scenario capture tools are for controlled interactions.";
|
||||
"Choose tools from the target and question. For an archive or application package, open_binary(path) binds the active target; use inspect_artifact when its graph and findings help, and extract_artifact when materialized files are needed. For standalone JavaScript or ASAR analysis, use analyze_javascript_application. For managed PE/CLI files, use inspect_managed_artifact. For native binaries, open the target and use focused search, procedure, or function tools directly; binary_overview is available when metadata or inventory context is useful. For a live page or runtime, discover targets with list_browser_targets, list_electron_targets, or list_javascript_runtime_targets when needed; scenario capture tools are for controlled interactions.";
|
||||
|
||||
const ACTIVE_TARGET_INSTRUCTIONS =
|
||||
"REA analyzes the active reverse-engineering target. For native binaries, call binary_overview for target metadata and inventory counts, then search_procedures or list_procedures to choose a function and analyze_function with its returned name or address for a complete dossier. Use inspect_native_api when you need to reconstruct a native API boundary, and focused procedure tools when you need only one facet such as assembly, callers, or references.";
|
||||
"REA analyzes the active reverse-engineering target. For native binaries, use search_procedures or list_procedures to choose a function and analyze_function with its returned name or address for a complete dossier. Call binary_overview when target metadata or inventory counts are useful. Use inspect_native_api when you need to reconstruct a native API boundary, and focused procedure tools when you need only one facet such as assembly, callers, or references.";
|
||||
|
||||
export interface CreateServerOptions {
|
||||
readonly logger?: Logger;
|
||||
@@ -336,10 +336,18 @@ const registerObservationTools = ({
|
||||
...common,
|
||||
electron: options.electronObservation,
|
||||
electronActive: options.electronActiveObservation,
|
||||
evidenceLookup:
|
||||
session === undefined
|
||||
? undefined
|
||||
: (evidenceId) => session.evidenceById(evidenceId),
|
||||
});
|
||||
registerJavaScriptRuntimeObservationTools(server, {
|
||||
...common,
|
||||
runtime: options.javascriptRuntimeObservation,
|
||||
evidenceLookup:
|
||||
session === undefined
|
||||
? undefined
|
||||
: (evidenceId) => session.evidenceById(evidenceId),
|
||||
});
|
||||
registerApplicationTools(server, {
|
||||
logger,
|
||||
|
||||
@@ -15,9 +15,16 @@ import type { ProgressReporter } from "../application/ProgressReporter.js";
|
||||
import { ELECTRON_TOOL_CONTRACTS } from "../contracts/electronToolContracts.js";
|
||||
import type { ToolContract } from "../contracts/toolContracts.js";
|
||||
import type { AnalysisError } from "../domain/errors.js";
|
||||
import { AnalysisInputError } from "../domain/errors.js";
|
||||
import { resolveEvidenceReferences } from "../application/EvidenceReferenceResolver.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import { inspectElectronPageInputSchema } from "../domain/electronObservation.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import {
|
||||
electronTargetListSchema,
|
||||
inspectElectronPageInputSchema,
|
||||
inspectElectronPageToolInputSchema,
|
||||
listElectronTargetsInputSchema,
|
||||
} from "../domain/electronObservation.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { mcpProgressReporter } from "./mcpProgress.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
@@ -30,6 +37,9 @@ interface ElectronToolRegistration {
|
||||
readonly electronActive: ElectronActiveObservationPort | undefined;
|
||||
readonly permissionAuthority: PermissionAuthority | undefined;
|
||||
readonly recordEvidence: BinarySessionPort["recordEvidence"] | undefined;
|
||||
readonly evidenceLookup:
|
||||
| ((evidenceId: string) => Evidence | undefined)
|
||||
| undefined;
|
||||
}
|
||||
|
||||
interface ElectronToolContext {
|
||||
@@ -78,16 +88,61 @@ export const registerElectronTools = (
|
||||
options,
|
||||
inspectContract,
|
||||
{ input, context },
|
||||
(parsed, { signal, progress }) =>
|
||||
inspectElectronPage(
|
||||
async (parsed, { signal, progress }) => {
|
||||
const request = inspectElectronPageToolInputSchema.parse(parsed);
|
||||
const source = resolveEvidenceReferences(
|
||||
options.evidenceLookup,
|
||||
[request.discovery_evidence_id],
|
||||
[
|
||||
{
|
||||
operation: "list_electron_targets",
|
||||
predicate: "rea.electron-target-list/v1",
|
||||
},
|
||||
],
|
||||
);
|
||||
if (!source.ok) return err(source.error);
|
||||
const discovery = source.value[0];
|
||||
if (discovery === undefined) return err(invalidDiscoveryEvidence());
|
||||
const scope = listElectronTargetsInputSchema.safeParse(
|
||||
discovery.parameters,
|
||||
);
|
||||
const listed = electronTargetListSchema.safeParse(
|
||||
discovery.normalized_result,
|
||||
);
|
||||
if (!scope.success || !listed.success)
|
||||
return err(invalidDiscoveryEvidence());
|
||||
if (
|
||||
!listed.data.targets.some(
|
||||
({ target_id }) => target_id === request.target_id,
|
||||
)
|
||||
)
|
||||
return {
|
||||
ok: false,
|
||||
error: new AnalysisInputError(
|
||||
"inspect_electron_page",
|
||||
undefined,
|
||||
[
|
||||
{
|
||||
path: ["target_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"Choose a target returned by the referenced discovery Evidence",
|
||||
},
|
||||
],
|
||||
),
|
||||
};
|
||||
return inspectElectronPage(
|
||||
options.electron,
|
||||
options.permissionAuthority,
|
||||
inspectElectronPageInputSchema.parse(parsed),
|
||||
{
|
||||
signal,
|
||||
progress,
|
||||
},
|
||||
),
|
||||
inspectElectronPageInputSchema.parse({
|
||||
...scope.data,
|
||||
target_id: request.target_id,
|
||||
observation_ms: request.observation_ms,
|
||||
include_script_sources: request.include_script_sources,
|
||||
}),
|
||||
{ signal, progress },
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
server.registerTool(
|
||||
@@ -133,6 +188,16 @@ export const registerElectronTools = (
|
||||
);
|
||||
};
|
||||
|
||||
const invalidDiscoveryEvidence = () =>
|
||||
new AnalysisInputError("inspect_electron_page", undefined, [
|
||||
{
|
||||
path: ["discovery_evidence_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"The referenced Evidence is not a usable Electron target discovery",
|
||||
},
|
||||
]);
|
||||
|
||||
const runElectronTool = async <Input>(
|
||||
options: ElectronToolRegistration,
|
||||
contract: ToolContract,
|
||||
|
||||
@@ -7,12 +7,19 @@ import {
|
||||
observeJavaScriptRuntime,
|
||||
} from "../application/JavaScriptRuntimeObservationService.js";
|
||||
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
|
||||
import { resolveEvidenceReferences } from "../application/EvidenceReferenceResolver.js";
|
||||
import { JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS } from "../contracts/javascriptRuntimeObservationToolContracts.js";
|
||||
import type { ToolContract } from "../contracts/toolContracts.js";
|
||||
import type { AnalysisError } from "../domain/errors.js";
|
||||
import { AnalysisInputError } from "../domain/errors.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import { observeJavaScriptRuntimeInputSchema } from "../domain/javascriptRuntimeObservation.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import {
|
||||
javascriptRuntimeTargetListSchema,
|
||||
listJavaScriptRuntimeTargetsInputSchema,
|
||||
observeJavaScriptRuntimeInputSchema,
|
||||
observeJavaScriptRuntimeToolInputSchema,
|
||||
} from "../domain/javascriptRuntimeObservation.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
@@ -23,6 +30,9 @@ interface RuntimeToolRegistration {
|
||||
readonly runtime: JavaScriptRuntimeObservationPort | undefined;
|
||||
readonly permissionAuthority: PermissionAuthority | undefined;
|
||||
readonly recordEvidence: BinarySessionPort["recordEvidence"] | undefined;
|
||||
readonly evidenceLookup:
|
||||
| ((evidenceId: string) => Evidence | undefined)
|
||||
| undefined;
|
||||
}
|
||||
|
||||
/** Register passive Inspector tools even when policy keeps them unavailable. */
|
||||
@@ -57,17 +67,72 @@ export const registerJavaScriptRuntimeObservationTools = (
|
||||
options,
|
||||
observeContract,
|
||||
{ input, context },
|
||||
(parsed, signal) =>
|
||||
observeJavaScriptRuntime(
|
||||
async (parsed, signal) => {
|
||||
const request = observeJavaScriptRuntimeToolInputSchema.parse(parsed);
|
||||
const source = resolveEvidenceReferences(
|
||||
options.evidenceLookup,
|
||||
[request.discovery_evidence_id],
|
||||
[
|
||||
{
|
||||
operation: "list_javascript_runtime_targets",
|
||||
predicate: "rea.javascript-runtime-target-list/v1",
|
||||
},
|
||||
],
|
||||
);
|
||||
if (!source.ok) return err(source.error);
|
||||
const discovery = source.value[0];
|
||||
if (discovery === undefined) return err(invalidDiscoveryEvidence());
|
||||
const scope = listJavaScriptRuntimeTargetsInputSchema.safeParse(
|
||||
discovery.parameters,
|
||||
);
|
||||
const listed = javascriptRuntimeTargetListSchema.safeParse(
|
||||
discovery.normalized_result,
|
||||
);
|
||||
if (!scope.success || !listed.success)
|
||||
return err(invalidDiscoveryEvidence());
|
||||
if (
|
||||
!listed.data.targets.some(
|
||||
({ target_id }) => target_id === request.target_id,
|
||||
)
|
||||
)
|
||||
return err(
|
||||
new AnalysisInputError("observe_javascript_runtime", undefined, [
|
||||
{
|
||||
path: ["target_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"Choose a target returned by the referenced discovery Evidence",
|
||||
},
|
||||
]),
|
||||
);
|
||||
return observeJavaScriptRuntime(
|
||||
options.runtime,
|
||||
options.permissionAuthority,
|
||||
observeJavaScriptRuntimeInputSchema.parse(parsed),
|
||||
observeJavaScriptRuntimeInputSchema.parse({
|
||||
...scope.data,
|
||||
target_id: request.target_id,
|
||||
...(request.runtime_kind === undefined
|
||||
? {}
|
||||
: { runtime_kind: request.runtime_kind }),
|
||||
observation_ms: request.observation_ms,
|
||||
}),
|
||||
{ signal },
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const invalidDiscoveryEvidence = () =>
|
||||
new AnalysisInputError("observe_javascript_runtime", undefined, [
|
||||
{
|
||||
path: ["discovery_evidence_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"The referenced Evidence is not a usable Inspector target discovery",
|
||||
},
|
||||
]);
|
||||
|
||||
const runRuntimeTool = async <Input>(
|
||||
options: RuntimeToolRegistration,
|
||||
contract: ToolContract,
|
||||
|
||||
Reference in New Issue
Block a user