refactor(mcp): simplify workflow and observation scope

This commit is contained in:
morluto
2026-09-29 12:41:28 +08:00
parent 2f604a3c3a
commit 34d00fd323
16 changed files with 239 additions and 81 deletions
+10 -2
View File
@@ -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,
+75 -10
View File
@@ -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,