feat(session): add dynamic binary lifecycle

This commit is contained in:
morluto
2026-07-12 06:34:18 +08:00
parent c2c3d989eb
commit f32e718cc7
16 changed files with 942 additions and 28 deletions
+156
View File
@@ -0,0 +1,156 @@
import type { BinaryTarget } from "../domain/binaryTarget.js";
import { parseBinaryTarget } from "../domain/binaryTarget.js";
import {
HopperCancelledError,
NoBinaryOpenError,
type HopperError,
} from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
import type { JsonValue } from "../hopper/protocol.js";
import type { HopperToolPort } from "./HopperToolPort.js";
/** A closeable Hopper port created for exactly one parsed target. */
export interface BinaryClient extends HopperToolPort {
close(): Promise<void>;
}
/** Production seam for creating a target-scoped Hopper client. */
export type BinaryClientFactory = (target: BinaryTarget) => BinaryClient;
/** Serializes target transitions while allowing calls within the active session. */
export class BinarySession implements HopperToolPort {
#active:
| { readonly target: BinaryTarget; readonly client: BinaryClient }
| undefined;
#transition: Promise<void> = Promise.resolve();
readonly #calls = new Set<Promise<unknown>>();
constructor(readonly createClient: BinaryClientFactory) {}
/** Open or switch to a target, retaining the old session when the new one fails. */
open(
path: string,
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<BinaryTarget, HopperError>> {
return this.#serialize(async () => {
if (isAborted(options.signal)) return err(new HopperCancelledError());
const parsed = await parseBinaryTarget(path);
if (!parsed.ok) return parsed;
if (isAborted(options.signal)) return err(new HopperCancelledError());
if (this.#active?.target.path === parsed.value.path)
return ok(parsed.value);
await this.#drainCalls();
const previous = this.#active;
this.#active = undefined;
await previous?.client.close();
const client = this.createClient(parsed.value);
const started = await client.callTool("health", {}, options);
if (!started.ok) {
await client.close();
if (previous !== undefined) await this.#restore(previous.target);
return started;
}
if (isAborted(options.signal)) {
await client.close();
if (previous !== undefined) await this.#restore(previous.target);
return err(new HopperCancelledError());
}
this.#active = { target: parsed.value, client };
return ok(parsed.value);
});
}
/** Close the active target, if any. */
close(): Promise<Result<null, HopperError>> {
return this.#serialize(async () => {
const previous = this.#active;
this.#active = undefined;
await this.#drainCalls();
await previous?.client.close();
return ok(null);
});
}
/** Describe the active binary session. */
status(): JsonValue {
const target = this.#active?.target;
return target === undefined
? { open: false }
: {
open: true,
path: target.path,
format: target.format,
kind: target.kind,
};
}
/** Invoke a Hopper tool against the active target. */
async callTool(
name: string,
arguments_: Readonly<Record<string, JsonValue>>,
options?: { readonly signal?: AbortSignal },
): Promise<Result<JsonValue, HopperError>> {
const transitioned = await this.#waitForTransition(options?.signal);
if (!transitioned.ok) return transitioned;
const active = this.#active;
if (active === undefined) return err(new NoBinaryOpenError());
const call = active.client.callTool(name, arguments_, options);
this.#calls.add(call);
try {
return await call;
} finally {
this.#calls.delete(call);
}
}
#serialize<T>(operation: () => Promise<T>): Promise<T> {
const result = this.#transition.then(operation, operation);
this.#transition = result.then(
() => undefined,
() => undefined,
);
return result;
}
async #drainCalls(): Promise<void> {
await Promise.allSettled([...this.#calls]);
}
async #restore(target: BinaryTarget): Promise<void> {
const client = this.createClient(target);
const started = await client.callTool("health", {});
if (started.ok) this.#active = { target, client };
else await client.close();
}
async #waitForTransition(
signal?: AbortSignal,
): Promise<Result<undefined, HopperCancelledError>> {
if (signal?.aborted === true) return err(new HopperCancelledError());
if (signal === undefined) {
await this.#transition;
return ok(undefined);
}
return new Promise((resolve) => {
const onAbort = (): void => {
resolve(err(new HopperCancelledError()));
};
signal.addEventListener("abort", onAbort, { once: true });
this.#transition.then(
() => {
signal.removeEventListener("abort", onAbort);
resolve(
signal.aborted ? err(new HopperCancelledError()) : ok(undefined),
);
},
() => {
signal.removeEventListener("abort", onAbort);
resolve(
signal.aborted ? err(new HopperCancelledError()) : ok(undefined),
);
},
);
});
}
}
const isAborted = (signal: AbortSignal | undefined): boolean =>
signal?.aborted === true;
+3 -3
View File
@@ -8,14 +8,14 @@ const DEFAULT_HOPPER_LAUNCHER_PATH =
export interface AppConfig {
readonly hopperLauncherPath: string;
readonly hopperTargetPath: string;
readonly hopperTargetPath: string | undefined;
readonly hopperTargetKind: "executable" | "database";
readonly hopperLoaderArgs: readonly string[];
}
const environmentSchema = z.object({
HOPPER_LAUNCHER_PATH: z.string().min(1).optional(),
HOPPER_TARGET_PATH: z.string().min(1),
HOPPER_TARGET_PATH: z.string().min(1).optional(),
HOPPER_TARGET_KIND: z.enum(["executable", "database"]).default("executable"),
HOPPER_LOADER_ARGS_JSON: z.string().optional(),
});
@@ -27,7 +27,7 @@ export const parseConfig = (
const parsedEnvironment = environmentSchema.safeParse(environment);
if (!parsedEnvironment.success) {
return err(
new ConfigurationError("HOPPER_TARGET_PATH is required", {
new ConfigurationError("Invalid Hopper environment configuration", {
cause: parsedEnvironment.error,
}),
);
+19 -1
View File
@@ -13,7 +13,7 @@ const caseSensitive = z
.describe("Whether to match case");
/** The architectural owner of a tool implementation. */
export type ToolKind = "official-proxy" | "enhanced";
export type ToolKind = "official-proxy" | "enhanced" | "session";
/** Stable caller-visible contract retained across the Python-to-TypeScript migration. */
export interface ToolContract {
@@ -35,6 +35,12 @@ const enhanced = (
inputSchema: z.ZodObject,
): ToolContract => ({ name, description, kind: "enhanced", inputSchema });
const session = (
name: string,
description: string,
inputSchema: z.ZodObject,
): ToolContract => ({ name, description, kind: "session", inputSchema });
/** All 31 tools forwarded unchanged to Hopper's official MCP server. */
export const OFFICIAL_TOOL_CONTRACTS = [
official(
@@ -218,8 +224,20 @@ export const ENHANCED_TOOL_CONTRACTS = [
),
] as const satisfies readonly ToolContract[];
/** Binary lifecycle tools available before Hopper is launched. */
export const SESSION_TOOL_CONTRACTS = [
session(
"open_binary",
"Open or switch the active binary",
z.object({ path: z.string().min(1) }),
),
session("close_binary", "Close the active binary", z.object({})),
session("binary_session", "Describe the active binary session", z.object({})),
] as const satisfies readonly ToolContract[];
/** Complete caller-visible tool inventory. */
export const TOOL_CONTRACTS = [
...OFFICIAL_TOOL_CONTRACTS,
...ENHANCED_TOOL_CONTRACTS,
...SESSION_TOOL_CONTRACTS,
] as const;
+217
View File
@@ -0,0 +1,217 @@
import { constants } from "node:fs";
import { access, open, realpath } from "node:fs/promises";
import { isAbsolute, resolve } from "node:path";
import { BinaryTargetError } from "./errors.js";
import { err, ok, type Result } from "./result.js";
/** CPU families understood by the supported executable loaders. */
export type BinaryArchitecture = "x86" | "x86_64" | "arm" | "arm64";
/** A parsed binary target and the Hopper launcher arguments it requires. */
export interface BinaryTarget {
readonly path: string;
readonly kind: "executable" | "database";
readonly format: "hopper" | "mach-o" | "elf" | "pe";
readonly architecture?: BinaryArchitecture;
readonly availableArchitectures?: readonly BinaryArchitecture[];
readonly loaderArgs: readonly string[];
}
/** Resolve, read, and classify a local binary path, including its architecture metadata. */
export const parseBinaryTarget = async (
input: string,
cwd = process.cwd(),
hostArchitecture: NodeJS.Architecture = process.arch,
): Promise<Result<BinaryTarget, BinaryTargetError>> => {
const candidate = isAbsolute(input) ? input : resolve(cwd, input);
try {
await access(candidate, constants.R_OK);
const path = await realpath(candidate);
if (path.toLowerCase().endsWith(".hop"))
return ok({ path, kind: "database", format: "hopper", loaderArgs: [] });
const handle = await open(path, "r");
let bytes: Buffer;
try {
bytes = Buffer.alloc(4096);
const read = await handle.read(bytes, 0, bytes.length, 0);
bytes = bytes.subarray(0, read.bytesRead);
} finally {
await handle.close();
}
const detected = parseExecutableHeader(bytes, hostArchitecture);
if (!detected.ok) return err(new BinaryTargetError(path, detected.error));
return ok({ path, kind: "executable", ...detected.value });
} catch (cause: unknown) {
return err(
new BinaryTargetError(candidate, "path is not readable", { cause }),
);
}
};
type ExecutableMetadata = Pick<
BinaryTarget,
"format" | "architecture" | "availableArchitectures" | "loaderArgs"
>;
/** Parse supported executable headers without performing I/O. */
export const parseExecutableHeader = (
bytes: Buffer,
hostArchitecture: NodeJS.Architecture,
): Result<ExecutableMetadata, string> => {
if (
bytes.length >= 4 &&
bytes.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46]))
)
return parseElf(bytes);
if (bytes.length >= 2 && bytes[0] === 0x4d && bytes[1] === 0x5a)
return parsePe(bytes);
if (bytes.length < 8) return err("truncated or unsupported binary header");
const magic = bytes.readUInt32BE(0);
if ([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(magic))
return parseThinMachO(bytes, magic);
if ([0xcafebabe, 0xbebafeca, 0xcafebabf, 0xbfbafeca].includes(magic))
return parseFatMachO(bytes, magic, hostArchitecture);
return err("unsupported binary format");
};
const parseThinMachO = (
bytes: Buffer,
magic: number,
): Result<ExecutableMetadata, string> => {
if (bytes.length < 8) return err("truncated Mach-O header");
const little = magic === 0xcefaedfe || magic === 0xcffaedfe;
const architecture = machArchitecture(
little ? bytes.readUInt32LE(4) : bytes.readUInt32BE(4),
);
if (architecture === undefined) return err("unsupported Mach-O architecture");
return ok({
format: "mach-o",
architecture,
availableArchitectures: [architecture],
loaderArgs: ["-l", "Mach-O"],
});
};
const parseFatMachO = (
bytes: Buffer,
magic: number,
host: NodeJS.Architecture,
): Result<ExecutableMetadata, string> => {
const little = magic === 0xbebafeca || magic === 0xbfbafeca;
const is64 = magic === 0xcafebabf || magic === 0xbfbafeca;
if (bytes.length < 8) return err("truncated FAT header");
const read = (offset: number): number =>
little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
const count = read(4);
const entrySize = is64 ? 32 : 20;
if (count === 0 || count > 128 || bytes.length < 8 + count * entrySize)
return err("truncated or invalid FAT architecture table");
const architectures: BinaryArchitecture[] = [];
for (let index = 0; index < count; index += 1) {
const architecture = machArchitecture(read(8 + index * entrySize));
if (architecture !== undefined && !architectures.includes(architecture))
architectures.push(architecture);
}
const preferred =
host === "arm64"
? "arm64"
: host === "x64"
? "x86_64"
: host === "ia32"
? "x86"
: host === "arm"
? "arm"
: undefined;
if (preferred === undefined || !architectures.includes(preferred))
return err(`FAT binary has no host-compatible ${host} architecture`);
const flag =
preferred === "arm64"
? "--aarch64"
: preferred === "x86_64"
? "--x86_64"
: preferred === "arm"
? "--arm"
: "--x86";
return ok({
format: "mach-o",
architecture: preferred,
availableArchitectures: architectures,
loaderArgs: ["-l", "FAT", flag, "-l", "Mach-O"],
});
};
const parseElf = (bytes: Buffer): Result<ExecutableMetadata, string> => {
if (bytes.length < 20) return err("truncated ELF header");
if (bytes[4] !== 1 && bytes[4] !== 2) return err("unsupported ELF class");
const little = bytes[5] === 1;
if (!little && bytes[5] !== 2) return err("unsupported ELF endianness");
const machine = little ? bytes.readUInt16LE(18) : bytes.readUInt16BE(18);
const architecture = elfArchitecture(machine);
if (architecture === undefined) return err("unsupported ELF architecture");
return ok({
format: "elf",
architecture,
availableArchitectures: [architecture],
loaderArgs: ["-l", "ELF"],
});
};
const parsePe = (bytes: Buffer): Result<ExecutableMetadata, string> => {
if (bytes.length < 64) return err("truncated PE DOS header");
const offset = bytes.readUInt32LE(0x3c);
if (
offset > bytes.length - 6 ||
bytes.toString("binary", offset, offset + 4) !== "PE\u0000\u0000"
)
return err("invalid or truncated PE header");
const architecture = peArchitecture(bytes.readUInt16LE(offset + 4));
if (architecture === undefined) return err("unsupported PE architecture");
return ok({
format: "pe",
architecture,
availableArchitectures: [architecture],
loaderArgs: ["-l", "PE"],
});
};
const machArchitecture = (cpu: number): BinaryArchitecture | undefined => {
switch (cpu) {
case 7:
return "x86";
case 0x01000007:
return "x86_64";
case 12:
return "arm";
case 0x0100000c:
return "arm64";
}
return undefined;
};
const elfArchitecture = (machine: number): BinaryArchitecture | undefined => {
switch (machine) {
case 3:
return "x86";
case 62:
return "x86_64";
case 40:
return "arm";
case 183:
return "arm64";
}
return undefined;
};
const peArchitecture = (machine: number): BinaryArchitecture | undefined => {
switch (machine) {
case 0x14c:
return "x86";
case 0x8664:
return "x86_64";
case 0x1c0:
return "arm";
case 0xaa64:
return "arm64";
}
return undefined;
};
+22
View File
@@ -71,3 +71,25 @@ export class HopperStartError extends HopperError {
export class ConfigurationError extends HopperError {
readonly _tag = "ConfigurationError";
}
/** No binary session exists for an analysis request. */
export class NoBinaryOpenError extends HopperError {
readonly _tag = "NoBinaryOpenError";
constructor() {
super(
"No binary is open. Call open_binary with a readable local path first.",
);
}
}
/** A supplied target path could not be safely opened as a supported binary. */
export class BinaryTargetError extends HopperError {
readonly _tag = "BinaryTargetError";
constructor(
readonly path: string,
reason: string,
options?: ErrorOptions,
) {
super(`Cannot open binary: ${reason}: ${path}`, options);
}
}
+10
View File
@@ -0,0 +1,10 @@
/** Provisional product identifiers kept together for release-time renaming. */
export const PRODUCT_IDENTITY = {
displayName: "Better Binary",
packageName: "better-binary-mcp",
cliBinary: "better-binary",
mcpBinary: "better-binary-mcp",
mcpServerKey: "better-binary",
skillName: "better-binary-analysis",
configFileName: "better-binary.json",
} as const;
+28 -13
View File
@@ -2,6 +2,7 @@
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import { realpathSync } from "node:fs";
import {
serveStdio,
@@ -9,6 +10,7 @@ import {
} from "@modelcontextprotocol/server/stdio";
import { parseConfig } from "./config.js";
import { BinarySession } from "./application/BinarySession.js";
import { HopperApplicationLauncher } from "./hopper/BridgeLauncher.js";
import { HopperClient } from "./hopper/HopperClient.js";
import { createServer } from "./server/createServer.js";
@@ -24,24 +26,37 @@ export const run = async (): Promise<number> => {
const bridgeScriptPath = fileURLToPath(
new URL("../bridge/hopper_bridge.py", import.meta.url),
);
const hopper = new HopperClient({
launcher: new HopperApplicationLauncher({
launcherPath: config.value.hopperLauncherPath,
targetPath: config.value.hopperTargetPath,
targetKind: config.value.hopperTargetKind,
loaderArgs: config.value.hopperLoaderArgs,
bridgeScriptPath,
}),
});
const session = new BinarySession(
(target) =>
new HopperClient({
launcher: new HopperApplicationLauncher({
launcherPath: config.value.hopperLauncherPath,
targetPath: target.path,
targetKind: target.kind,
loaderArgs:
config.value.hopperLoaderArgs.length > 0
? config.value.hopperLoaderArgs
: target.loaderArgs,
bridgeScriptPath,
}),
}),
);
if (config.value.hopperTargetPath !== undefined) {
const opened = await session.open(config.value.hopperTargetPath);
if (!opened.ok) {
process.stderr.write(`${opened.error._tag}: ${opened.error.message}\n`);
return 1;
}
}
let handle: StdioServerHandle;
try {
handle = serveStdio(() => createServer(hopper), {
handle = serveStdio(() => createServer(session, session), {
onerror: () => {
process.stderr.write("MCP stdio transport error\n");
},
});
} catch {
await hopper.close();
await session.close();
process.stderr.write("Failed to start MCP stdio transport\n");
return 1;
}
@@ -50,7 +65,7 @@ export const run = async (): Promise<number> => {
const shutdown = (): Promise<void> => {
shutdownPromise ??= (async () => {
await handle.close();
await hopper.close();
await session.close();
})();
return shutdownPromise;
};
@@ -71,7 +86,7 @@ export const run = async (): Promise<number> => {
const entryPath = process.argv[1];
if (
entryPath !== undefined &&
pathToFileURL(entryPath).href === import.meta.url
pathToFileURL(realpathSync(entryPath)).href === import.meta.url
) {
run()
.then((exitCode) => {
+10 -3
View File
@@ -1,20 +1,27 @@
import { McpServer } from "@modelcontextprotocol/server";
import type { HopperToolPort } from "../application/HopperToolPort.js";
import type { BinarySession } from "../application/BinarySession.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { registerEnhancedTools } from "./registerEnhancedTools.js";
import { registerOfficialTools } from "./registerOfficialTools.js";
import { registerSessionTools } from "./registerSessionTools.js";
/** Construct one MCP server instance without acquiring subprocess resources. */
export const createServer = (hopper: HopperToolPort): McpServer => {
export const createServer = (
hopper: HopperToolPort,
session?: BinarySession,
): McpServer => {
const server = new McpServer(
{ name: "betterBinaryMCP", version: "0.1.0" },
{ name: PRODUCT_IDENTITY.mcpServerKey, version: "0.1.0" },
{
capabilities: { tools: {} },
instructions:
"Reverse-engineering tools for Hopper Disassembler. Start with binary_overview.",
"Reverse-engineering tools for Hopper Disassembler. Open a target with open_binary, then start with binary_overview.",
},
);
registerOfficialTools(server, hopper);
registerEnhancedTools(server, hopper);
if (session !== undefined) registerSessionTools(server, session);
return server;
};
+54
View File
@@ -0,0 +1,54 @@
import type { McpServer } from "@modelcontextprotocol/server";
import { z } from "zod";
import type { BinarySession } from "../application/BinarySession.js";
import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
import { toCallToolResult } from "./toolResult.js";
/** Register binary lifecycle management tools. */
export const registerSessionTools = (
server: McpServer,
session: BinarySession,
): void => {
const [openContract, closeContract, statusContract] = SESSION_TOOL_CONTRACTS;
server.registerTool(
openContract.name,
{
description: openContract.description,
inputSchema: openContract.inputSchema,
},
async (input, context) => {
const parsed = z.object({ path: z.string().min(1) }).parse(input);
const opened = await session.open(parsed.path, {
signal: context.mcpReq.signal,
});
return opened.ok
? toCallToolResult({
ok: true,
value: {
path: opened.value.path,
format: opened.value.format,
kind: opened.value.kind,
loaderArgs: [...opened.value.loaderArgs],
},
})
: toCallToolResult(opened);
},
);
server.registerTool(
closeContract.name,
{
description: closeContract.description,
inputSchema: closeContract.inputSchema,
},
async () => toCallToolResult(await session.close()),
);
server.registerTool(
statusContract.name,
{
description: statusContract.description,
inputSchema: statusContract.inputSchema,
},
() => toCallToolResult({ ok: true, value: session.status() }),
);
};