@wincode/mcp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -2
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +34 -4
- package/src/client.ts +370 -0
- package/src/config/resolve.ts +722 -0
- package/src/config/schema.ts +96 -0
- package/src/config.ts +31 -0
- package/src/identifiers.ts +6 -0
- package/src/index.ts +10 -0
- package/src/management.ts +257 -0
- package/src/manifest.ts +102 -0
- package/src/oauth.ts +347 -0
- package/src/plugin/config.ts +40 -0
- package/src/plugin/index.ts +501 -0
- package/src/registry.ts +1455 -0
- package/src/result.ts +504 -0
- package/src/sanitize.ts +32 -0
- package/src/tool-identity.ts +24 -0
- package/src/utf8-byte-length.ts +4 -0
- package/test/client.test.ts +642 -0
- package/test/config-resolve.test.ts +376 -0
- package/test/management.test.ts +186 -0
- package/test/manifest.test.ts +17 -0
- package/test/oauth.test.ts +106 -0
- package/test/plugin-config.test.ts +117 -0
- package/test/result.test.ts +291 -0
- package/test/sanitize.test.ts +103 -0
- package/test/tool-identity.test.ts +27 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import type {
|
|
3
|
+
PluginConfigDiagnostic,
|
|
4
|
+
PluginConfigFileStore,
|
|
5
|
+
PluginConfigSnapshot,
|
|
6
|
+
PluginMcpServerRegistration,
|
|
7
|
+
} from "@wincode/coding-agent";
|
|
8
|
+
import { createMcpConfigLoader } from "../src/plugin/config";
|
|
9
|
+
|
|
10
|
+
const diagnostic: PluginConfigDiagnostic = {
|
|
11
|
+
code: "parse-error",
|
|
12
|
+
message: "Ignored a malformed lower-priority config file.",
|
|
13
|
+
path: "/home/user/.wincode/mcp.json",
|
|
14
|
+
scope: "global",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const snapshot: PluginConfigSnapshot = {
|
|
18
|
+
diagnostics: [diagnostic],
|
|
19
|
+
document: {
|
|
20
|
+
mcpServers: {
|
|
21
|
+
wiki: {
|
|
22
|
+
url: "https://mcp.example.test/",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
sourceFor: () => ({ path: "/home/user/.wincode/mcp.json", scope: "global" }),
|
|
27
|
+
sources: [
|
|
28
|
+
{
|
|
29
|
+
document: {
|
|
30
|
+
mcpServers: { wiki: { url: "https://mcp.example.test/" } },
|
|
31
|
+
},
|
|
32
|
+
path: "/home/user/.wincode/mcp.json",
|
|
33
|
+
scope: "global",
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
test("Extension-owned MCP Servers enter the managed catalog without overriding file config", async () => {
|
|
39
|
+
const configFiles: PluginConfigFileStore = {
|
|
40
|
+
getSnapshot: async () => snapshot,
|
|
41
|
+
refreshSnapshot: async () => snapshot,
|
|
42
|
+
setValue: async () => snapshot,
|
|
43
|
+
};
|
|
44
|
+
const registrations: readonly PluginMcpServerRegistration[] = [
|
|
45
|
+
{
|
|
46
|
+
config: { command: "bun", args: ["run", "extension-mcp.ts"] },
|
|
47
|
+
name: "extension_docs",
|
|
48
|
+
ownerPluginId: "docs_plugin",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
config: { command: "bun", args: ["run", "replacement.ts"] },
|
|
52
|
+
name: "wiki",
|
|
53
|
+
ownerPluginId: "other_plugin",
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
const load = createMcpConfigLoader(configFiles, () => registrations);
|
|
57
|
+
const result = await load({
|
|
58
|
+
env: {},
|
|
59
|
+
refresh: false,
|
|
60
|
+
workspace: "/workspace",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
expect(result.servers.extension_docs).toMatchObject({
|
|
64
|
+
type: "local",
|
|
65
|
+
command: ["bun", "run", "extension-mcp.ts"],
|
|
66
|
+
});
|
|
67
|
+
expect(result.servers.wiki).toMatchObject({
|
|
68
|
+
type: "remote",
|
|
69
|
+
url: "https://mcp.example.test/",
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("MCP reads the host-owned mcp namespace and refreshes only when asked", async () => {
|
|
74
|
+
let reads = 0;
|
|
75
|
+
let refreshes = 0;
|
|
76
|
+
const configFiles: PluginConfigFileStore = {
|
|
77
|
+
getSnapshot: async (namespace) => {
|
|
78
|
+
expect(namespace).toBe("mcp");
|
|
79
|
+
reads += 1;
|
|
80
|
+
return snapshot;
|
|
81
|
+
},
|
|
82
|
+
refreshSnapshot: async (namespace) => {
|
|
83
|
+
expect(namespace).toBe("mcp");
|
|
84
|
+
refreshes += 1;
|
|
85
|
+
return snapshot;
|
|
86
|
+
},
|
|
87
|
+
setValue: async () => snapshot,
|
|
88
|
+
};
|
|
89
|
+
const load = createMcpConfigLoader(configFiles);
|
|
90
|
+
|
|
91
|
+
const initial = await load({
|
|
92
|
+
env: {},
|
|
93
|
+
refresh: false,
|
|
94
|
+
workspace: "/workspace",
|
|
95
|
+
});
|
|
96
|
+
const refreshed = await load({
|
|
97
|
+
env: {},
|
|
98
|
+
refresh: true,
|
|
99
|
+
workspace: "/workspace",
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(initial.servers.wiki).toMatchObject({
|
|
103
|
+
type: "remote",
|
|
104
|
+
url: "https://mcp.example.test/",
|
|
105
|
+
});
|
|
106
|
+
expect(refreshed.servers.wiki).toEqual(initial.servers.wiki);
|
|
107
|
+
expect({ reads, refreshes }).toEqual({ reads: 1, refreshes: 1 });
|
|
108
|
+
expect(initial.diagnostics).toEqual(
|
|
109
|
+
expect.arrayContaining([
|
|
110
|
+
expect.objectContaining({
|
|
111
|
+
code: "parse-error",
|
|
112
|
+
message: diagnostic.message,
|
|
113
|
+
path: diagnostic.path,
|
|
114
|
+
}),
|
|
115
|
+
])
|
|
116
|
+
);
|
|
117
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { agentIdSchema } from "@wincode/agent-core";
|
|
5
|
+
import {
|
|
6
|
+
createMcpToolExecutor,
|
|
7
|
+
MAX_MCP_RESULT_BYTES,
|
|
8
|
+
type McpCatalogSnapshot,
|
|
9
|
+
normalizeMcpResourceContents,
|
|
10
|
+
normalizeMcpResult,
|
|
11
|
+
normalizeMcpResultWithArtifacts,
|
|
12
|
+
toMcpSnapshotId,
|
|
13
|
+
} from "@wincode/mcp";
|
|
14
|
+
import { isObjectLike, isString, isUndefined } from "@wincode/utils";
|
|
15
|
+
|
|
16
|
+
const savedArtifactPattern = /saved to (.+)\]/u;
|
|
17
|
+
const beginningExcerptPattern = /^BEGIN /u;
|
|
18
|
+
const endingExcerptPattern = / END$/u;
|
|
19
|
+
|
|
20
|
+
const agentId = (value: string) => agentIdSchema.parse(value);
|
|
21
|
+
|
|
22
|
+
describe("MCP result normalization", () => {
|
|
23
|
+
test("preserves small images alongside safe text and structured values", () => {
|
|
24
|
+
const result = normalizeMcpResult({
|
|
25
|
+
isError: true,
|
|
26
|
+
content: [
|
|
27
|
+
{ type: "text", text: "ok" },
|
|
28
|
+
{ type: "image", data: "base64-secret", mimeType: "image/png" },
|
|
29
|
+
{ type: "resource_link", uri: "https://mcp.deepwiki.com/mcp?case=x" },
|
|
30
|
+
],
|
|
31
|
+
structuredContent: { n: 1 },
|
|
32
|
+
});
|
|
33
|
+
expect(result.isError).toBe(true);
|
|
34
|
+
expect(result.content[1]).toMatchObject({
|
|
35
|
+
type: "image",
|
|
36
|
+
data: "base64-secret",
|
|
37
|
+
mimeType: "image/png",
|
|
38
|
+
});
|
|
39
|
+
expect(result.structuredContent).toEqual({ n: 1 });
|
|
40
|
+
});
|
|
41
|
+
test("saves binary tool output privately while preserving small images", async () => {
|
|
42
|
+
const result = await normalizeMcpResultWithArtifacts({
|
|
43
|
+
content: [
|
|
44
|
+
{ type: "image", data: "aGVsbG8=", mimeType: "image/png" },
|
|
45
|
+
{ type: "audio", data: "AQID", mimeType: "audio/wav" },
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
expect(result.content[0]).toMatchObject({
|
|
49
|
+
type: "image",
|
|
50
|
+
data: "aGVsbG8=",
|
|
51
|
+
mimeType: "image/png",
|
|
52
|
+
});
|
|
53
|
+
const binaryText = result.content.find(
|
|
54
|
+
(item) => isObjectLike(item) && "text" in item && isString(item.text)
|
|
55
|
+
);
|
|
56
|
+
if (
|
|
57
|
+
!(
|
|
58
|
+
isObjectLike(binaryText) &&
|
|
59
|
+
"text" in binaryText &&
|
|
60
|
+
isString(binaryText.text)
|
|
61
|
+
)
|
|
62
|
+
) {
|
|
63
|
+
throw new Error("Expected a private binary artifact reference.");
|
|
64
|
+
}
|
|
65
|
+
const match = savedArtifactPattern.exec(binaryText.text);
|
|
66
|
+
if (match?.[1] === undefined) {
|
|
67
|
+
throw new Error("Expected the artifact path in the result.");
|
|
68
|
+
}
|
|
69
|
+
const filePath = match[1];
|
|
70
|
+
try {
|
|
71
|
+
expect([...(await Bun.file(filePath).bytes())]).toEqual([1, 2, 3]);
|
|
72
|
+
} finally {
|
|
73
|
+
await fs.rm(path.dirname(filePath), { force: true, recursive: true });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("stores oversized tool output and keeps a bounded preview", async () => {
|
|
78
|
+
const fullText = `MCP-BEGIN ${"x".repeat(MAX_MCP_RESULT_BYTES * 2)} MCP-END`;
|
|
79
|
+
const result = await normalizeMcpResultWithArtifacts({
|
|
80
|
+
content: [{ type: "text", text: fullText }],
|
|
81
|
+
});
|
|
82
|
+
expect(result.truncated).toBe(true);
|
|
83
|
+
const artifactReference = result.content.find(
|
|
84
|
+
(item) =>
|
|
85
|
+
isObjectLike(item) &&
|
|
86
|
+
"text" in item &&
|
|
87
|
+
isString(item.text) &&
|
|
88
|
+
item.text.includes("Complete MCP output saved to")
|
|
89
|
+
);
|
|
90
|
+
if (
|
|
91
|
+
!(
|
|
92
|
+
isObjectLike(artifactReference) &&
|
|
93
|
+
"text" in artifactReference &&
|
|
94
|
+
isString(artifactReference.text)
|
|
95
|
+
)
|
|
96
|
+
) {
|
|
97
|
+
throw new Error("Expected the complete output artifact reference.");
|
|
98
|
+
}
|
|
99
|
+
const match = savedArtifactPattern.exec(artifactReference.text);
|
|
100
|
+
if (match?.[1] === undefined) {
|
|
101
|
+
throw new Error("Expected the artifact path in the result.");
|
|
102
|
+
}
|
|
103
|
+
const filePath = match[1];
|
|
104
|
+
try {
|
|
105
|
+
const saved = JSON.parse(await Bun.file(filePath).text()) as {
|
|
106
|
+
content: { text: string }[];
|
|
107
|
+
};
|
|
108
|
+
expect(saved.content[0]?.text).toBe(fullText);
|
|
109
|
+
expect(JSON.stringify(result)).toContain("MCP-BEGIN");
|
|
110
|
+
expect(JSON.stringify(result)).toContain("MCP-END");
|
|
111
|
+
expect(JSON.stringify(result)).toContain("middle omitted");
|
|
112
|
+
expect(
|
|
113
|
+
new TextEncoder().encode(JSON.stringify(result)).byteLength
|
|
114
|
+
).toBeLessThanOrEqual(MAX_MCP_RESULT_BYTES);
|
|
115
|
+
} finally {
|
|
116
|
+
await fs.rm(path.dirname(filePath), { force: true, recursive: true });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("preserves resource images and saves other binary resources", async () => {
|
|
121
|
+
const contents = await normalizeMcpResourceContents([
|
|
122
|
+
{ uri: "file:///image", mimeType: "image/png", blob: "aGVsbG8=" },
|
|
123
|
+
{
|
|
124
|
+
uri: "file:///binary",
|
|
125
|
+
mimeType: "application/octet-stream",
|
|
126
|
+
blob: "AQID",
|
|
127
|
+
},
|
|
128
|
+
]);
|
|
129
|
+
expect(contents[0]).toMatchObject({
|
|
130
|
+
type: "image",
|
|
131
|
+
uri: "file:///image",
|
|
132
|
+
data: "aGVsbG8=",
|
|
133
|
+
});
|
|
134
|
+
const artifact = contents[1];
|
|
135
|
+
if (
|
|
136
|
+
!(isObjectLike(artifact) && "text" in artifact && isString(artifact.text))
|
|
137
|
+
) {
|
|
138
|
+
throw new Error("Expected a binary resource artifact reference.");
|
|
139
|
+
}
|
|
140
|
+
const match = savedArtifactPattern.exec(artifact.text);
|
|
141
|
+
if (match?.[1] === undefined) {
|
|
142
|
+
throw new Error("Expected the binary artifact path.");
|
|
143
|
+
}
|
|
144
|
+
const filePath = match[1];
|
|
145
|
+
try {
|
|
146
|
+
expect([...(await Bun.file(filePath).bytes())]).toEqual([1, 2, 3]);
|
|
147
|
+
} finally {
|
|
148
|
+
await fs.rm(path.dirname(filePath), { force: true, recursive: true });
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("adapts normalized failures while forwarding cancellation", async () => {
|
|
153
|
+
const signal = new AbortController().signal;
|
|
154
|
+
let receivedSignal: AbortSignal | undefined;
|
|
155
|
+
const snapshot: McpCatalogSnapshot = {
|
|
156
|
+
agent: agentId("build"),
|
|
157
|
+
id: toMcpSnapshotId("snapshot-1"),
|
|
158
|
+
manifest: [],
|
|
159
|
+
tools: new Map(),
|
|
160
|
+
};
|
|
161
|
+
const executor = createMcpToolExecutor(
|
|
162
|
+
async (_snapshot, _toolName, _input, nextSignal) => {
|
|
163
|
+
receivedSignal = nextSignal;
|
|
164
|
+
return {
|
|
165
|
+
content: [{ text: "denied", type: "text" }],
|
|
166
|
+
isError: true,
|
|
167
|
+
truncated: false,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
if (isUndefined(executor)) {
|
|
172
|
+
throw new Error("Expected an MCP tool executor.");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
await expect(executor(snapshot, "mcp_demo", {}, signal)).resolves.toEqual({
|
|
176
|
+
errorText: "MCP tool call failed.",
|
|
177
|
+
type: "failure",
|
|
178
|
+
});
|
|
179
|
+
expect(receivedSignal).toBe(signal);
|
|
180
|
+
});
|
|
181
|
+
test("bounds a huge resource link URI without scanning into the tail", () => {
|
|
182
|
+
const tail = "resource-link-tail";
|
|
183
|
+
const uri = `${"x".repeat(20 * 1024 * 1024)}${tail}`;
|
|
184
|
+
const result = normalizeMcpResult({
|
|
185
|
+
content: [{ type: "resource_link", uri }],
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const resourceLink = result.content[0];
|
|
189
|
+
expect(resourceLink).toMatchObject({
|
|
190
|
+
type: "resource_link",
|
|
191
|
+
uri: "x".repeat(2048),
|
|
192
|
+
});
|
|
193
|
+
expect(JSON.stringify(resourceLink)).not.toContain(tail);
|
|
194
|
+
});
|
|
195
|
+
test("truncates UTF-8 output within bound", () => {
|
|
196
|
+
const result = normalizeMcpResult({
|
|
197
|
+
content: [{ type: "text", text: "😀".repeat(100_000) }],
|
|
198
|
+
});
|
|
199
|
+
expect(result.truncated).toBe(true);
|
|
200
|
+
expect(
|
|
201
|
+
new TextEncoder().encode(JSON.stringify(result)).byteLength
|
|
202
|
+
).toBeLessThanOrEqual(MAX_MCP_RESULT_BYTES);
|
|
203
|
+
});
|
|
204
|
+
test("does not return unpaired surrogates when truncating U+1FFFF", () => {
|
|
205
|
+
const result = normalizeMcpResult({
|
|
206
|
+
content: [{ type: "text", text: "\u{1ffff}".repeat(100_000) }],
|
|
207
|
+
});
|
|
208
|
+
const serialized = JSON.stringify(result);
|
|
209
|
+
for (let index = 0; index < serialized.length; index += 1) {
|
|
210
|
+
const codeUnit = serialized.charCodeAt(index);
|
|
211
|
+
if (codeUnit >= 0xd8_00 && codeUnit <= 0xdb_ff) {
|
|
212
|
+
expect(serialized.charCodeAt(index + 1)).toBeGreaterThanOrEqual(
|
|
213
|
+
0xdc_00
|
|
214
|
+
);
|
|
215
|
+
expect(serialized.charCodeAt(index + 1)).toBeLessThanOrEqual(0xdf_ff);
|
|
216
|
+
index += 1;
|
|
217
|
+
} else {
|
|
218
|
+
expect(codeUnit < 0xdc_00 || codeUnit > 0xdf_ff).toBe(true);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
test("does not end with a replacement after a prefix cuts before a pair", () => {
|
|
223
|
+
const result = normalizeMcpResult({
|
|
224
|
+
content: [{ type: "text", text: `a${"😀".repeat(100_000)}` }],
|
|
225
|
+
});
|
|
226
|
+
const text = result.content[0];
|
|
227
|
+
expect(text).toMatchObject({ type: "text" });
|
|
228
|
+
if (isObjectLike(text) && "text" in text && isString(text.text)) {
|
|
229
|
+
expect(text.text.endsWith("\ufffd")).toBe(false);
|
|
230
|
+
const lastCodeUnit = text.text.charCodeAt(text.text.length - 1);
|
|
231
|
+
if (lastCodeUnit >= 0xdc_00 && lastCodeUnit <= 0xdf_ff) {
|
|
232
|
+
const precedingCodeUnit = text.text.charCodeAt(text.text.length - 2);
|
|
233
|
+
expect(precedingCodeUnit).toBeGreaterThanOrEqual(0xd8_00);
|
|
234
|
+
expect(precedingCodeUnit).toBeLessThanOrEqual(0xdb_ff);
|
|
235
|
+
} else {
|
|
236
|
+
expect(lastCodeUnit).not.toBeGreaterThanOrEqual(0xd8_00);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
test("retains text after malformed surrogates in oversized output", () => {
|
|
241
|
+
const result = normalizeMcpResult({
|
|
242
|
+
content: [{ type: "text", text: `\ud800${"x".repeat(300_000)}` }],
|
|
243
|
+
});
|
|
244
|
+
const text = result.content[0];
|
|
245
|
+
expect(text).toMatchObject({ type: "text" });
|
|
246
|
+
if (isObjectLike(text) && "text" in text && isString(text.text)) {
|
|
247
|
+
expect(text.text.startsWith("\ufffdx")).toBe(true);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const midStringResult = normalizeMcpResult({
|
|
251
|
+
content: [{ type: "text", text: `x\udc00${"x".repeat(300_000)}` }],
|
|
252
|
+
});
|
|
253
|
+
const midText = midStringResult.content[0];
|
|
254
|
+
if (isObjectLike(midText) && "text" in midText && isString(midText.text)) {
|
|
255
|
+
expect(midText.text.startsWith("x\ufffdx")).toBe(true);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
test("preserves embedded resource text beyond metadata bounds", () => {
|
|
259
|
+
const text = "resource text ".repeat(300);
|
|
260
|
+
const result = normalizeMcpResult({
|
|
261
|
+
content: [{ type: "resource", resource: { uri: "file://x", text } }],
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(result.truncated).toBe(false);
|
|
265
|
+
expect(result.content[0]).toMatchObject({ type: "resource", text });
|
|
266
|
+
});
|
|
267
|
+
test("truncates huge embedded resource text at the global cap", () => {
|
|
268
|
+
const result = normalizeMcpResult({
|
|
269
|
+
content: [
|
|
270
|
+
{
|
|
271
|
+
type: "resource",
|
|
272
|
+
resource: {
|
|
273
|
+
uri: "file://x",
|
|
274
|
+
text: `BEGIN ${"😀".repeat(100_000)} END`,
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
expect(result.truncated).toBe(true);
|
|
281
|
+
expect(
|
|
282
|
+
new TextEncoder().encode(JSON.stringify(result)).byteLength
|
|
283
|
+
).toBeLessThanOrEqual(MAX_MCP_RESULT_BYTES);
|
|
284
|
+
const resource = result.content[0];
|
|
285
|
+
if (isObjectLike(resource) && "text" in resource) {
|
|
286
|
+
expect(resource.text).toMatch(beginningExcerptPattern);
|
|
287
|
+
expect(resource.text).toMatch(endingExcerptPattern);
|
|
288
|
+
expect(resource.text).toContain("middle omitted");
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
collectSecrets,
|
|
4
|
+
type ResolvedMcpServerConfig,
|
|
5
|
+
sanitizeMessage,
|
|
6
|
+
} from "@wincode/mcp";
|
|
7
|
+
|
|
8
|
+
const localConfig = (): ResolvedMcpServerConfig => ({
|
|
9
|
+
name: "demo",
|
|
10
|
+
type: "local",
|
|
11
|
+
command: ["bun", "x", "demo"],
|
|
12
|
+
disabled: false,
|
|
13
|
+
timeout: { startup: 30_000, catalog: 30_000, execution: 43_200_000 },
|
|
14
|
+
environment: { API_KEY: "env-super-secret" },
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const remoteConfig = (): ResolvedMcpServerConfig => ({
|
|
18
|
+
name: "remote-demo",
|
|
19
|
+
type: "remote",
|
|
20
|
+
url: "https://mcp.deepwiki.com/mcp?case=redaction",
|
|
21
|
+
disabled: false,
|
|
22
|
+
timeout: { startup: 30_000, catalog: 30_000, execution: 43_200_000 },
|
|
23
|
+
headers: { Authorization: "Bearer super-secret-token" },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("mcp sanitize", () => {
|
|
27
|
+
test("collectSecrets returns environment values for local servers", () => {
|
|
28
|
+
expect(collectSecrets(localConfig())).toEqual([
|
|
29
|
+
"x",
|
|
30
|
+
"demo",
|
|
31
|
+
"env-super-secret",
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("collectSecrets returns headers and url for remote servers", () => {
|
|
36
|
+
expect(collectSecrets(remoteConfig())).toEqual([
|
|
37
|
+
"Bearer super-secret-token",
|
|
38
|
+
"https://mcp.deepwiki.com/mcp?case=redaction",
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("sanitizeMessage redacts local environment secrets", () => {
|
|
43
|
+
const message = sanitizeMessage(
|
|
44
|
+
localConfig(),
|
|
45
|
+
new Error("boom env-super-secret"),
|
|
46
|
+
"fallback"
|
|
47
|
+
);
|
|
48
|
+
expect(message).toBe("boom [redacted]");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("sanitizeMessage redacts remote headers and url", () => {
|
|
52
|
+
const message = sanitizeMessage(
|
|
53
|
+
remoteConfig(),
|
|
54
|
+
new Error(
|
|
55
|
+
"auth failed at https://mcp.deepwiki.com/mcp?case=redaction with Bearer super-secret-token"
|
|
56
|
+
),
|
|
57
|
+
"fallback"
|
|
58
|
+
);
|
|
59
|
+
expect(message).not.toContain("super-secret-token");
|
|
60
|
+
expect(message).not.toContain("mcp.deepwiki.com");
|
|
61
|
+
expect(message).not.toContain("Bearer");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("sanitizeMessage redacts arbitrary URLs from client errors", () => {
|
|
65
|
+
const message = sanitizeMessage(
|
|
66
|
+
localConfig(),
|
|
67
|
+
new Error("request failed at https://debug.example/path?token=secret"),
|
|
68
|
+
"fallback"
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
expect(message).not.toContain("debug.example");
|
|
72
|
+
expect(message).not.toContain("token=secret");
|
|
73
|
+
expect(message).toContain("[redacted]");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("sanitizeMessage uses the fallback when config is undefined", () => {
|
|
77
|
+
expect(sanitizeMessage(undefined, new Error("raw error"), "fallback")).toBe(
|
|
78
|
+
"fallback"
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("sanitizeMessage strips controls, generic secrets, and oversized tails", () => {
|
|
83
|
+
const message = sanitizeMessage(
|
|
84
|
+
localConfig(),
|
|
85
|
+
new Error(
|
|
86
|
+
`boom\nAuthorization: Bearer generic-secret ${"x".repeat(4096)}TAIL`
|
|
87
|
+
),
|
|
88
|
+
"fallback"
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(message).toContain("boom [redacted]");
|
|
92
|
+
expect(message).not.toContain("generic-secret");
|
|
93
|
+
expect(message).not.toContain("boom\n");
|
|
94
|
+
expect(message).not.toContain("TAIL");
|
|
95
|
+
expect(message.length).toBeLessThanOrEqual(2048);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("sanitizeMessage falls back to the given fallback for non-errors", () => {
|
|
99
|
+
expect(
|
|
100
|
+
sanitizeMessage(localConfig(), "not an error", "unknown MCP error")
|
|
101
|
+
).toBe("unknown MCP error");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { qualifyMcpToolName } from "@wincode/mcp";
|
|
3
|
+
|
|
4
|
+
const IDENTITY_PATTERN = /^mcp_[A-Za-z0-9_-]+$/;
|
|
5
|
+
const IDENTITY_WITH_HASH_PATTERN =
|
|
6
|
+
/^mcp_[A-Za-z0-9_-]+_[A-Za-z0-9_-]+_[0-9a-f]{8}$/;
|
|
7
|
+
|
|
8
|
+
describe("MCP tool identity", () => {
|
|
9
|
+
test("is stable, bounded, sanitized, and distinct", async () => {
|
|
10
|
+
const one = await qualifyMcpToolName("weird server/é", "tool!name");
|
|
11
|
+
expect(one).toMatch(IDENTITY_PATTERN);
|
|
12
|
+
expect(one.length).toBeLessThanOrEqual(64);
|
|
13
|
+
expect(one).toBe(await qualifyMcpToolName("weird server/é", "tool!name"));
|
|
14
|
+
expect(one).not.toBe(
|
|
15
|
+
await qualifyMcpToolName("weird server/é", "tool?name")
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("bounds long identities and preserves hash suffix", async () => {
|
|
20
|
+
const identity = await qualifyMcpToolName(
|
|
21
|
+
"server-".repeat(100),
|
|
22
|
+
"tool-".repeat(100)
|
|
23
|
+
);
|
|
24
|
+
expect(identity.length).toBeLessThanOrEqual(64);
|
|
25
|
+
expect(identity).toMatch(IDENTITY_WITH_HASH_PATTERN);
|
|
26
|
+
});
|
|
27
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@wincode/config/tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"declarationMap": true,
|
|
6
|
+
"sourceMap": true,
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"paths": {
|
|
9
|
+
"@/*": ["../coding-agent/*"]
|
|
10
|
+
},
|
|
11
|
+
"composite": true,
|
|
12
|
+
"strictNullChecks": true,
|
|
13
|
+
"types": ["bun"]
|
|
14
|
+
}
|
|
15
|
+
}
|