-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathverify-package-mcp.mjs
More file actions
231 lines (219 loc) · 7.2 KB
/
Copy pathverify-package-mcp.mjs
File metadata and controls
231 lines (219 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { MCP_STARTUP_POLICY } from "../dist/mcpStartupPolicy.js";
import * as prompts from "./verify-package-prompts.mjs";
import { json, verifyCompleteToolCatalog } from "./lib/verify-package-core.mjs";
const execute = promisify(execFile);
const verifyMcpToolsAndPrompts = async (client, mcpOptions) => {
await verifyCompleteToolCatalog(client, mcpOptions);
await prompts.verifyPromptCatalog(client, mcpOptions, prompts.names);
await prompts.verifyPromptCompletion(client, mcpOptions, false);
};
const verifyMcpReplay = async (client, mcpOptions, investigationReplay) => {
const replay = await client.callTool(
{
name: "find_changed_behavior",
arguments: { investigation_run: investigationReplay.arguments },
},
mcpOptions,
);
const replayEvidence = json(prompts.mcpText(replay));
const replayWorkspace = json(
await readFile(investigationReplay.workspacePath, "utf8"),
);
if (
replay.isError === true ||
replayEvidence.evidence_id !== investigationReplay.evidenceId ||
replayWorkspace.revision !== investigationReplay.revision
)
throw new Error(
"packaged MCP did not replay with workspace-only authority",
);
};
const verifyMcpTargetFree = async (client, mcpOptions) => {
const status = await client.callTool(
{ name: "binary_session", arguments: { detail: "full" } },
mcpOptions,
);
const currentDocument =
status.structuredContent?.result?.tool_availability?.find(
({ name }) => name === "current_document",
);
if (
currentDocument?.available !== false ||
currentDocument.reason !== "target_required"
)
throw new Error(
"packaged target-free MCP omitted target-bound availability metadata",
);
};
const verifyMcpUnknownProvider = async (client, mcpOptions) => {
const unknownProvider = await client.callTool(
{
name: "open_binary",
arguments: {
path: process.execPath,
provider_id: "missing-provider",
},
},
mcpOptions,
);
if (
unknownProvider.isError !== true ||
unknownProvider.structuredContent?.error?.details?.selection_reason !==
"unknown_provider"
)
throw new Error("packaged MCP accepted an unknown analysis provider");
};
const verifyMcpOpenAndBind = async (client, mcpOptions) => {
const opened = await client.callTool(
{
name: "open_binary",
arguments: { path: process.execPath, provider_id: "hopper" },
},
mcpOptions,
);
if (opened.isError === true)
throw new Error("packaged MCP could not open a binary");
const providerStatusEnvelope = json(
prompts.mcpText(
await client.callTool(
{ name: "binary_session", arguments: { detail: "full" } },
mcpOptions,
),
),
);
const providerStatus = providerStatusEnvelope.result;
if (
providerStatus.analysis_provider_binding?.provider?.id !== "hopper" ||
providerStatus.analysis_provider_binding?.selection_source !== "request"
)
throw new Error("packaged MCP omitted its explicit Hopper binding");
};
const verifyMcpLinuxToolAvailability = async (client, mcpOptions) => {
const current = await client.callTool(
{ name: "current_document", arguments: {} },
mcpOptions,
);
if (current.isError !== true)
throw new Error("packaged Linux MCP executed an unavailable Hopper tool");
};
const verifyMcpNonLinuxCurrentDocument = async (
client,
mcpOptions,
current,
) => {
if (json(prompts.mcpText(current)).result !== "fixture")
throw new Error("packaged MCP bridge call failed");
const batch = await client.callTool(
{
name: "batch_decompile",
arguments: { addresses: ["0x1000"] },
},
mcpOptions,
);
const batchResult = json(prompts.mcpText(batch)).result;
if (
batch.isError === true ||
batchResult?.total !== 1 ||
batchResult?.succeeded !== 1 ||
batchResult?.failed !== 0 ||
batchResult?.items?.[0]?.status !== "ok" ||
batchResult?.items?.[0]?.pseudocode !== "return 0;"
)
throw new Error("packaged MCP structured batch result failed");
};
const verifyMcpBinaryLifecycle = async (client, mcpOptions) => {
await verifyMcpOpenAndBind(client, mcpOptions);
await prompts.verifyPromptCompletion(
client,
mcpOptions,
process.platform !== "linux",
);
if (process.platform === "linux") {
await verifyMcpLinuxToolAvailability(client, mcpOptions);
} else {
const current = await client.callTool(
{ name: "current_document", arguments: {} },
mcpOptions,
);
await verifyMcpNonLinuxCurrentDocument(client, mcpOptions, current);
}
const closed = await client.callTool(
{ name: "close_binary", arguments: {} },
mcpOptions,
);
if (closed.isError === true)
throw new Error("packaged MCP could not close its binary");
await prompts.verifyPromptCompletion(client, mcpOptions, false);
};
const verifyMcpEvidenceBundle = async (client, mcpOptions, evidenceRoot) => {
const mcpBundlePath = join(evidenceRoot, "mcp.json");
const mcpExport = await client.callTool(
{ name: "export_evidence_bundle", arguments: { path: mcpBundlePath } },
mcpOptions,
);
if (mcpExport.isError === true)
throw new Error("packaged MCP evidence export failed");
const mcpImport = await client.callTool(
{ name: "import_evidence_bundle", arguments: { path: mcpBundlePath } },
mcpOptions,
);
if (mcpImport.isError === true)
throw new Error("packaged MCP evidence import failed");
};
/** Connect to the packaged MCP server and exercise the target-free catalog. */
export async function verifyPackageMcp({
cli,
environment,
evidenceRoot,
investigationReplay,
}) {
const diagnosed = json(
(
await execute(cli, ["mcp", "doctor", "--json"], {
env: environment,
timeout: MCP_STARTUP_POLICY.doctorDeadlineMs,
})
).stdout,
);
if (
diagnosed.healthy !== true ||
diagnosed.inventory?.tools?.observed !==
diagnosed.inventory?.tools?.expected
)
throw new Error("packaged production MCP doctor failed");
const transport = new StdioClientTransport({
command: cli,
args: ["mcp"],
env: {
...environment,
REA_INVESTIGATION_INPUT_ROOTS_JSON: JSON.stringify([]),
},
stderr: "pipe",
});
let mcpStderr = "";
transport.stderr?.on("data", (chunk) => {
mcpStderr += chunk.toString();
});
const client = new Client({ name: "package-smoke", version: "1.0.0" });
try {
await client.connect(transport);
const mcpOptions = { timeout: 15_000 };
await verifyMcpToolsAndPrompts(client, mcpOptions);
await verifyMcpReplay(client, mcpOptions, investigationReplay);
await verifyMcpTargetFree(client, mcpOptions);
await verifyMcpUnknownProvider(client, mcpOptions);
await verifyMcpBinaryLifecycle(client, mcpOptions);
await verifyMcpEvidenceBundle(client, mcpOptions, evidenceRoot);
} catch (cause) {
throw new Error(`packaged MCP smoke failed: ${mcpStderr}`, { cause });
} finally {
await client.close();
await transport.close();
}
}