forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle-mcp.test.ts
More file actions
285 lines (260 loc) · 9.53 KB
/
Copy pathbundle-mcp.test.ts
File metadata and controls
285 lines (260 loc) · 9.53 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { isRecord } from "../utils.js";
import { loadEnabledBundleLspConfig } from "./bundle-lsp.js";
import { loadEnabledBundleMcpConfig } from "./bundle-mcp.js";
import {
createEnabledPluginEntries,
createBundleMcpTempHarness,
createBundleProbePlugin,
withBundleHomeEnv,
writeClaudeBundleManifest,
} from "./bundle-mcp.test-support.js";
function getServerArgs(value: unknown): unknown[] | undefined {
return isRecord(value) && Array.isArray(value.args) ? value.args : undefined;
}
function normalizePathForAssertion(value: string | undefined): string | undefined {
if (!value) {
return value;
}
return path.normalize(value).replace(/\\/g, "/");
}
async function expectResolvedPathEqual(actual: unknown, expected: string): Promise<void> {
expect(typeof actual).toBe("string");
if (typeof actual !== "string") {
return;
}
expect(normalizePathForAssertion(await fs.realpath(actual))).toBe(
normalizePathForAssertion(await fs.realpath(expected)),
);
}
function expectNoDiagnostics(diagnostics: unknown[]) {
expect(diagnostics).toStrictEqual([]);
}
const tempHarness = createBundleMcpTempHarness();
afterEach(async () => {
await tempHarness.cleanup();
});
function createEnabledBundleConfig(pluginIds: string[]): OpenClawConfig {
return {
plugins: {
entries: createEnabledPluginEntries(pluginIds),
},
};
}
async function expectInlineBundleMcpServer(params: {
loadedServer: unknown;
pluginRoot: string;
commandRelativePath: string;
argRelativePaths: readonly string[];
}) {
const loadedArgs = getServerArgs(params.loadedServer);
const loadedCommand = isRecord(params.loadedServer) ? params.loadedServer.command : undefined;
const loadedCwd = isRecord(params.loadedServer) ? params.loadedServer.cwd : undefined;
const loadedEnv =
isRecord(params.loadedServer) && isRecord(params.loadedServer.env)
? params.loadedServer.env
: {};
await expectResolvedPathEqual(loadedCwd, params.pluginRoot);
expect(typeof loadedCommand).toBe("string");
expect(loadedArgs).toHaveLength(params.argRelativePaths.length);
expect(typeof loadedEnv.PLUGIN_ROOT).toBe("string");
if (typeof loadedCommand !== "string" || typeof loadedCwd !== "string") {
throw new Error("expected inline bundled MCP server to expose command and cwd");
}
expect(normalizePathForAssertion(path.relative(loadedCwd, loadedCommand))).toBe(
normalizePathForAssertion(params.commandRelativePath),
);
expect(
loadedArgs?.map((entry) =>
typeof entry === "string"
? normalizePathForAssertion(path.relative(loadedCwd, entry))
: entry,
),
).toEqual([...params.argRelativePaths]);
await expectResolvedPathEqual(loadedEnv.PLUGIN_ROOT, params.pluginRoot);
}
describe("loadEnabledBundleMcpConfig", () => {
it("loads enabled Claude bundle MCP config and absolutizes relative args", async () => {
await withBundleHomeEnv(
tempHarness,
"openclaw-bundle-mcp",
async ({ homeDir, workspaceDir }) => {
const { pluginRoot, serverPath } = await createBundleProbePlugin(homeDir);
const config: OpenClawConfig = {
plugins: {
entries: {
"bundle-probe": { enabled: true },
},
},
};
const loaded = loadEnabledBundleMcpConfig({
workspaceDir,
cfg: config,
});
const resolvedServerPath = await fs.realpath(serverPath);
const loadedServer = loaded.config.mcpServers.bundleProbe;
const loadedArgs = getServerArgs(loadedServer);
const loadedServerPath = typeof loadedArgs?.[0] === "string" ? loadedArgs[0] : undefined;
const resolvedPluginRoot = await fs.realpath(pluginRoot);
expectNoDiagnostics(loaded.diagnostics);
expect(isRecord(loadedServer) ? loadedServer.command : undefined).toBe("node");
expect(loadedArgs).toHaveLength(1);
if (!loadedServerPath) {
throw new Error("expected bundled MCP args to include the server path");
}
expect(normalizePathForAssertion(await fs.realpath(loadedServerPath))).toBe(
normalizePathForAssertion(resolvedServerPath),
);
await expectResolvedPathEqual(loadedServer.cwd, resolvedPluginRoot);
},
);
});
it("merges inline bundle MCP servers and skips disabled bundles", async () => {
await withBundleHomeEnv(
tempHarness,
"openclaw-bundle-inline",
async ({ homeDir, workspaceDir }) => {
await writeClaudeBundleManifest({
homeDir,
pluginId: "inline-enabled",
manifest: {
name: "inline-enabled",
mcpServers: {
enabledProbe: {
command: "node",
args: ["./enabled.mjs"],
},
},
},
});
await writeClaudeBundleManifest({
homeDir,
pluginId: "inline-disabled",
manifest: {
name: "inline-disabled",
mcpServers: {
disabledProbe: {
command: "node",
args: ["./disabled.mjs"],
},
},
},
});
const loaded = loadEnabledBundleMcpConfig({
workspaceDir,
cfg: {
plugins: {
entries: {
...createEnabledPluginEntries(["inline-enabled"]),
"inline-disabled": { enabled: false },
},
},
},
});
const enabledProbe = loaded.config.mcpServers.enabledProbe;
const enabledArgs = getServerArgs(enabledProbe);
expect(isRecord(enabledProbe) ? enabledProbe.command : undefined).toBe("node");
expect(enabledArgs).toHaveLength(1);
expect(typeof enabledArgs?.[0]).toBe("string");
if (typeof enabledArgs?.[0] !== "string") {
throw new Error("expected inline MCP enabledProbe args to include enabled.mjs");
}
expect(enabledArgs[0]).toContain("enabled.mjs");
expect(loaded.config.mcpServers.disabledProbe).toBeUndefined();
},
);
});
it("resolves inline Claude MCP paths from the plugin root and expands CLAUDE_PLUGIN_ROOT", async () => {
await withBundleHomeEnv(
tempHarness,
"openclaw-bundle-inline-placeholder",
async ({ homeDir, workspaceDir }) => {
const pluginRoot = await writeClaudeBundleManifest({
homeDir,
pluginId: "inline-claude",
manifest: {
name: "inline-claude",
mcpServers: {
inlineProbe: {
command: "${CLAUDE_PLUGIN_ROOT}/bin/server.sh",
args: ["${CLAUDE_PLUGIN_ROOT}/servers/probe.mjs", "./local-probe.mjs"],
cwd: "${CLAUDE_PLUGIN_ROOT}",
env: {
PLUGIN_ROOT: "${CLAUDE_PLUGIN_ROOT}",
},
},
},
},
});
const loaded = loadEnabledBundleMcpConfig({
workspaceDir,
cfg: createEnabledBundleConfig(["inline-claude"]),
});
const loadedServer = loaded.config.mcpServers.inlineProbe;
expectNoDiagnostics(loaded.diagnostics);
await expectInlineBundleMcpServer({
loadedServer,
pluginRoot,
commandRelativePath: path.join("bin", "server.sh"),
argRelativePaths: [
normalizePathForAssertion(path.join("servers", "probe.mjs"))!,
normalizePathForAssertion("local-probe.mjs")!,
],
});
},
);
});
it("reports malformed file-backed MCP configs instead of silently dropping servers", async () => {
await withBundleHomeEnv(
tempHarness,
"openclaw-bundle-malformed-mcp",
async ({ homeDir, workspaceDir }) => {
const pluginRoot = await writeClaudeBundleManifest({
homeDir,
pluginId: "malformed-mcp",
manifest: {
name: "malformed-mcp",
mcpServers: ".mcp.json",
},
});
await fs.writeFile(path.join(pluginRoot, ".mcp.json"), "{", "utf-8");
const loaded = loadEnabledBundleMcpConfig({
workspaceDir,
cfg: createEnabledBundleConfig(["malformed-mcp"]),
});
expect(loaded.config.mcpServers).toStrictEqual({});
expect(loaded.diagnostics).toHaveLength(1);
expect(loaded.diagnostics[0]?.pluginId).toBe("malformed-mcp");
expect(loaded.diagnostics[0]?.message).toContain("unable to read .mcp.json");
},
);
});
it("reports malformed file-backed LSP configs instead of silently dropping servers", async () => {
await withBundleHomeEnv(
tempHarness,
"openclaw-bundle-malformed-lsp",
async ({ homeDir, workspaceDir }) => {
const pluginRoot = await writeClaudeBundleManifest({
homeDir,
pluginId: "malformed-lsp",
manifest: {
name: "malformed-lsp",
lspServers: ".lsp.json",
},
});
await fs.writeFile(path.join(pluginRoot, ".lsp.json"), "{", "utf-8");
const loaded = loadEnabledBundleLspConfig({
workspaceDir,
cfg: createEnabledBundleConfig(["malformed-lsp"]),
});
expect(loaded.config.lspServers).toStrictEqual({});
expect(loaded.diagnostics).toHaveLength(1);
expect(loaded.diagnostics[0]?.pluginId).toBe("malformed-lsp");
expect(loaded.diagnostics[0]?.message).toContain("unable to read .lsp.json");
},
);
});
});