feat: evolve REA analysis platform

This commit is contained in:
morluto
2026-07-12 19:32:13 +08:00
parent 1f235cb52f
commit 4f18e26ee5
47 changed files with 1841 additions and 334 deletions
+10 -8
View File
@@ -1,7 +1,7 @@
import { McpServer } from "@modelcontextprotocol/server";
import type { HopperToolPort } from "../application/HopperToolPort.js";
import type { BinarySession } from "../application/BinarySession.js";
import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
import type { BinarySessionPort } from "../application/BinarySession.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { registerEnhancedTools } from "./registerEnhancedTools.js";
import { registerOfficialTools } from "./registerOfficialTools.js";
@@ -14,8 +14,8 @@ import { silentLogger, type Logger } from "../logger.js";
* fixed-target seam used by focused tests and embedders.
*/
export const createServer = (
hopper: HopperToolPort,
session?: BinarySession,
analysis: AnalysisOperationPort,
session?: BinarySessionPort,
logger: Logger = silentLogger,
): McpServer => {
const server = new McpServer(
@@ -24,13 +24,15 @@ export const createServer = (
capabilities: { tools: {} },
instructions:
session === undefined
? "Reverse-engineering tools for an active Hopper Disassembler target. Start with binary_overview."
: "Reverse-engineering tools for Hopper Disassembler. Open a target with open_binary, then start with binary_overview.",
? "Reverse-engineering tools for an active analysis target. Start with binary_overview."
: "Reverse-engineering tools for configured analysis providers. Open a target with open_binary, then start with binary_overview.",
},
);
const toolLogger = logger.child({ layer: "server" });
registerOfficialTools(server, hopper, toolLogger);
registerEnhancedTools(server, hopper, toolLogger);
const activeTarget =
session === undefined ? undefined : () => session.activeTarget();
registerOfficialTools(server, analysis, toolLogger, activeTarget);
registerEnhancedTools(server, analysis, toolLogger, activeTarget);
if (session !== undefined) registerSessionTools(server, session, toolLogger);
return server;
};
+40 -11
View File
@@ -1,7 +1,7 @@
import type { McpServer } from "@modelcontextprotocol/server";
import { EnhancedTools } from "../application/EnhancedTools.js";
import type { HopperToolPort } from "../application/HopperToolPort.js";
import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
import { enhancedToolNameSchema } from "../contracts/enhancedInputs.js";
import {
ENHANCED_TOOL_CONTRACTS,
@@ -10,16 +10,21 @@ import {
import { toCallToolResult } from "./toolResult.js";
import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import type { BinaryTarget } from "../domain/binaryTarget.js";
import { createEvidence } from "../domain/evidence.js";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { enhancedInputSchemas } from "../contracts/enhancedInputs.js";
/** Register composed workflows against the same port as direct bridge tools. */
export const registerEnhancedTools = (
server: McpServer,
hopper: HopperToolPort,
analysis: AnalysisOperationPort,
logger: Logger,
activeTarget?: () => BinaryTarget | undefined,
): void => {
const services = new EnhancedTools(hopper);
const services = new EnhancedTools(analysis);
for (const contract of ENHANCED_TOOL_CONTRACTS) {
registerEnhancedTool(server, services, contract, logger);
registerEnhancedTool(server, services, contract, { logger, activeTarget });
}
};
@@ -27,7 +32,10 @@ const registerEnhancedTool = (
server: McpServer,
services: EnhancedTools,
contract: ToolContract,
logger: Logger,
registration: {
readonly logger: Logger;
readonly activeTarget: (() => BinaryTarget | undefined) | undefined;
},
): void => {
const name = enhancedToolNameSchema.parse(contract.name);
server.registerTool(
@@ -38,12 +46,33 @@ const registerEnhancedTool = (
outputSchema: contract.outputSchema,
annotations: contract.annotations,
},
async (input, context) =>
toCallToolResult(
await logToolExecution(logger, name, () =>
services.execute(name, input, context.mcpReq.signal),
),
async (input, context) => {
const parsedInput = jsonValueSchema.parse(
enhancedInputSchemas[name].parse(input),
);
if (
typeof parsedInput !== "object" ||
parsedInput === null ||
Array.isArray(parsedInput)
)
throw new Error("Validated enhanced input was not a JSON object");
const parameters: Record<string, JsonValue> = { ...parsedInput };
const result = await logToolExecution(registration.logger, name, () =>
services.execute(name, input, context.mcpReq.signal),
);
return toCallToolResult(
result.ok
? {
ok: true,
value: createEvidence(registration.activeTarget?.(), {
operation: name,
parameters,
result: result.value,
}),
}
: result,
contract,
),
);
},
);
};
+32 -11
View File
@@ -1,31 +1,37 @@
import type { McpServer } from "@modelcontextprotocol/server";
import type { HopperToolPort } from "../application/HopperToolPort.js";
import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
import {
OFFICIAL_TOOL_CONTRACTS,
type ToolContract,
} from "../contracts/toolContracts.js";
import { jsonValueSchema, type JsonValue } from "../hopper/protocol.js";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { toCallToolResult } from "./toolResult.js";
import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import type { BinaryTarget } from "../domain/binaryTarget.js";
import { createEvidence } from "../domain/evidence.js";
/** Register direct bridge proxies, preserving MCP cancellation and typed errors. */
export const registerOfficialTools = (
server: McpServer,
hopper: HopperToolPort,
analysis: AnalysisOperationPort,
logger: Logger,
activeTarget?: () => BinaryTarget | undefined,
): void => {
for (const contract of OFFICIAL_TOOL_CONTRACTS) {
registerOfficialTool(server, hopper, contract, logger);
registerOfficialTool(server, analysis, contract, { logger, activeTarget });
}
};
const registerOfficialTool = (
server: McpServer,
hopper: HopperToolPort,
analysis: AnalysisOperationPort,
contract: ToolContract,
logger: Logger,
registration: {
readonly logger: Logger;
readonly activeTarget: (() => BinaryTarget | undefined) | undefined;
},
): void => {
server.registerTool(
contract.name,
@@ -37,12 +43,27 @@ const registerOfficialTool = (
},
async (input, context) => {
const arguments_ = projectOfficialArguments(contract, input);
const result = await logToolExecution(logger, contract.name, () =>
hopper.callTool(contract.name, arguments_, {
signal: context.mcpReq.signal,
}),
const result = await logToolExecution(
registration.logger,
contract.name,
() =>
analysis.execute(contract.name, arguments_, {
signal: context.mcpReq.signal,
}),
);
return toCallToolResult(
result.ok
? {
ok: true,
value: createEvidence(registration.activeTarget?.(), {
operation: contract.name,
parameters: arguments_,
result: result.value,
}),
}
: result,
contract,
);
return toCallToolResult(result, contract);
},
);
};
+4 -2
View File
@@ -1,7 +1,7 @@
import type { McpServer } from "@modelcontextprotocol/server";
import { z } from "zod";
import type { BinarySession } from "../application/BinarySession.js";
import type { BinarySessionPort } from "../application/BinarySession.js";
import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
import { toCallToolResult } from "./toolResult.js";
import type { Logger } from "../logger.js";
@@ -10,7 +10,7 @@ import { logToolExecution } from "./toolLogging.js";
/** Register MCP-only target lifecycle operations on a long-lived session. */
export const registerSessionTools = (
server: McpServer,
session: BinarySession,
session: BinarySessionPort,
logger: Logger,
): void => {
const [openContract, closeContract, statusContract] = SESSION_TOOL_CONTRACTS;
@@ -36,6 +36,8 @@ export const registerSessionTools = (
format: opened.value.format,
kind: opened.value.kind,
loaderArgs: [...opened.value.loaderArgs],
sha256: opened.value.sha256,
architecture: opened.value.architecture ?? null,
},
},
openContract,
+5 -9
View File
@@ -1,8 +1,8 @@
import type { CallToolResult } from "@modelcontextprotocol/server";
import type { HopperError } from "../domain/errors.js";
import type { AnalysisError } from "../domain/errors.js";
import type { Result } from "../domain/result.js";
import type { JsonValue } from "../hopper/protocol.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { ToolContract } from "../contracts/toolContracts.js";
/**
@@ -11,7 +11,7 @@ import type { ToolContract } from "../contracts/toolContracts.js";
* output, and other potentially sensitive details stay private.
*/
export const toCallToolResult = (
result: Result<JsonValue, HopperError>,
result: Result<JsonValue, AnalysisError>,
contract: ToolContract,
): CallToolResult =>
result.ok
@@ -30,18 +30,14 @@ const successResult = (
value: JsonValue,
contract: ToolContract,
): CallToolResult => {
const candidate = ["list_procedures", "list_names", "list_strings"].includes(
contract.name,
)
? value
: { result: value };
const candidate = contract.kind === "session" ? { result: value } : value;
const parsed = contract.outputSchema.safeParse(candidate);
if (!parsed.success) {
return {
content: [
{
type: "text",
text: "HopperProtocolError: Hopper returned output that does not match the tool contract",
text: "HopperProtocolError: Analysis output does not match the tool contract",
},
],
isError: true,