@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.
@@ -0,0 +1,376 @@
1
+ import { expect, test } from "bun:test";
2
+ import * as path from "node:path";
3
+ import {
4
+ DEFAULT_MCP_TIMEOUTS,
5
+ type McpConfigDiagnostic,
6
+ type McpConfigOrigin,
7
+ type McpConfigSnapshot,
8
+ type McpConfigSource,
9
+ resolveMcpConfig,
10
+ } from "@wincode/mcp";
11
+
12
+ const workspace = "/workspace";
13
+ const configVariable = (name: string): string => ["$", "{", name, "}"].join("");
14
+ const projectOrigin: McpConfigOrigin = {
15
+ path: `${workspace}/.wincode/mcp.json`,
16
+ scope: "project",
17
+ };
18
+ const userOrigin: McpConfigOrigin = {
19
+ path: "/home/user/.wincode/mcp.json",
20
+ scope: "global",
21
+ };
22
+
23
+ const source = (
24
+ origin: McpConfigOrigin,
25
+ document: Readonly<Record<string, unknown>>
26
+ ): McpConfigSource => ({ ...origin, document });
27
+
28
+ const createSnapshot = (
29
+ sources: readonly McpConfigSource[],
30
+ diagnostics: readonly McpConfigDiagnostic[] = []
31
+ ): McpConfigSnapshot => ({
32
+ diagnostics,
33
+ document: Object.freeze(
34
+ Object.assign(
35
+ Object.create(null),
36
+ ...sources.map(({ document }) => document)
37
+ )
38
+ ),
39
+ sourceFor: () => sources.at(-1),
40
+ sources,
41
+ });
42
+
43
+ const resolveSources = (
44
+ sources: readonly McpConfigSource[],
45
+ env: Readonly<Record<string, string | undefined>> = {},
46
+ workingDirectory = workspace
47
+ ) =>
48
+ resolveMcpConfig({
49
+ env,
50
+ snapshot: createSnapshot(sources),
51
+ workspace: workingDirectory,
52
+ });
53
+
54
+ const resolveAsUser = (
55
+ document: Readonly<Record<string, unknown>>,
56
+ env: Readonly<Record<string, string | undefined>> = {},
57
+ workingDirectory = workspace
58
+ ) => resolveSources([source(userOrigin, document)], env, workingDirectory);
59
+
60
+ const resolveAsProject = (
61
+ document: Readonly<Record<string, unknown>>,
62
+ env: Readonly<Record<string, string | undefined>> = {}
63
+ ) => resolveSources([source(projectOrigin, document)], env);
64
+
65
+ test("Pi-shaped stdio and HTTP entries resolve defaults, environment values, and exposure", async () => {
66
+ const result = await resolveAsUser(
67
+ {
68
+ mcpServers: {
69
+ local: {
70
+ command: "bun",
71
+ args: ["run", "server.ts"],
72
+ cwd: "servers",
73
+ env: { TOKEN: configVariable("MCP_TOKEN") },
74
+ description: "Local development server",
75
+ timeout: 12,
76
+ exposure: "hidden",
77
+ toolExposure: { public: "direct", "delete_*": "hidden" },
78
+ },
79
+ remote: {
80
+ url: "https://mcp.example.test/tools?q=1",
81
+ headers: { Authorization: `Bearer ${configVariable("MCP_TOKEN")}` },
82
+ },
83
+ },
84
+ },
85
+ { MCP_TOKEN: "resolved-token" }
86
+ );
87
+
88
+ expect(result.diagnostics).toEqual([]);
89
+ expect(result.servers.local).toEqual({
90
+ command: ["bun", "run", "server.ts"],
91
+ cwd: path.resolve(workspace, "servers"),
92
+ description: "Local development server",
93
+ disabled: false,
94
+ environment: { TOKEN: "resolved-token" },
95
+ exposure: "hidden",
96
+ name: "local",
97
+ timeout: { ...DEFAULT_MCP_TIMEOUTS, execution: 12_000 },
98
+ toolExposure: { public: "direct", "delete_*": "hidden" },
99
+ type: "local",
100
+ });
101
+ expect(result.servers.remote).toMatchObject({
102
+ disabled: false,
103
+ exposure: "direct",
104
+ headers: { Authorization: "Bearer resolved-token" },
105
+ name: "remote",
106
+ type: "remote",
107
+ url: "https://mcp.example.test/tools?q=1",
108
+ });
109
+ });
110
+
111
+ test("Pi variable escapes and leading shell commands resolve only in env and headers", async () => {
112
+ const result = await resolveAsUser(
113
+ {
114
+ mcpServers: {
115
+ local: {
116
+ command: "$MCP_EXECUTABLE",
117
+ args: ["$MCP_ARGUMENT", "$$LITERAL", "$!literal"],
118
+ env: {
119
+ TOKEN: "!printf 'Bearer %s' \"$MCP_TOKEN\"",
120
+ },
121
+ },
122
+ remote: {
123
+ url: "https://$MCP_HOST/mcp",
124
+ headers: {
125
+ Authorization: "!printf 'Bearer %s' \"$MCP_TOKEN\"",
126
+ },
127
+ },
128
+ },
129
+ },
130
+ {
131
+ MCP_ARGUMENT: "server.ts",
132
+ MCP_EXECUTABLE: "bun",
133
+ MCP_HOST: "mcp.example.test",
134
+ MCP_TOKEN: "command-token",
135
+ },
136
+ process.cwd()
137
+ );
138
+
139
+ expect(result.diagnostics).toEqual([]);
140
+ expect(result.servers.local).toMatchObject({
141
+ command: ["bun", "server.ts", "$LITERAL", "!literal"],
142
+ environment: { TOKEN: "Bearer command-token" },
143
+ });
144
+ expect(result.servers.remote).toMatchObject({
145
+ headers: { Authorization: "Bearer command-token" },
146
+ url: "https://mcp.example.test/mcp",
147
+ });
148
+ });
149
+
150
+ test("failed and oversized Pi header commands are diagnosed without blocking their server", async () => {
151
+ const result = await resolveAsUser(
152
+ {
153
+ mcpServers: {
154
+ remote: {
155
+ url: "https://mcp.example.test/mcp",
156
+ headers: {
157
+ Authorization: "!exit 1",
158
+ "X-Oversized": "!printf '%070000d' 0",
159
+ },
160
+ },
161
+ },
162
+ },
163
+ {},
164
+ process.cwd()
165
+ );
166
+
167
+ expect(result.servers.remote).toBeDefined();
168
+ expect(result.servers.remote).not.toHaveProperty("headers.Authorization");
169
+ expect(result.servers.remote).not.toHaveProperty("headers.X-Oversized");
170
+ expect(
171
+ result.diagnostics.filter(({ code }) => code === "command-failed")
172
+ ).toHaveLength(2);
173
+ });
174
+
175
+ test("project server definitions replace user endpoints instead of inheriting credentials", async () => {
176
+ const result = await resolveSources([
177
+ source(userOrigin, {
178
+ mcpServers: {
179
+ shared: {
180
+ url: "https://user.example.test/mcp",
181
+ headers: { Authorization: "Bearer user-token" },
182
+ },
183
+ },
184
+ }),
185
+ source(projectOrigin, {
186
+ mcpServers: {
187
+ shared: {
188
+ url: "https://project.example.test/mcp",
189
+ headers: { "X-Project": "trusted" },
190
+ },
191
+ },
192
+ }),
193
+ ]);
194
+
195
+ expect(result.diagnostics).toEqual([]);
196
+ expect(result.servers.shared).toMatchObject({
197
+ headers: { "X-Project": "trusted" },
198
+ url: "https://project.example.test/mcp",
199
+ });
200
+ expect(result.servers.shared).not.toHaveProperty("headers.Authorization");
201
+ });
202
+
203
+ test("project enablement and exposure overlays retain user server details", async () => {
204
+ const result = await resolveSources([
205
+ source(userOrigin, {
206
+ mcpServers: {
207
+ shared: {
208
+ url: "https://user.example.test/mcp",
209
+ headers: { Authorization: "Bearer user-token" },
210
+ exposure: "direct",
211
+ },
212
+ },
213
+ }),
214
+ source(projectOrigin, {
215
+ mcpServers: { shared: { enabled: false, exposure: "hidden" } },
216
+ }),
217
+ ]);
218
+
219
+ expect(result.diagnostics).toEqual([]);
220
+ expect(result.servers.shared).toMatchObject({
221
+ disabled: true,
222
+ exposure: "hidden",
223
+ headers: { Authorization: "Bearer user-token" },
224
+ url: "https://user.example.test/mcp",
225
+ });
226
+ });
227
+
228
+ test("trusted project configuration may supply local commands and remote headers", async () => {
229
+ const result = await resolveAsProject(
230
+ {
231
+ mcpServers: {
232
+ local: {
233
+ command: "bun",
234
+ args: ["run", "server.ts"],
235
+ env: { TOKEN: configVariable("PROJECT_TOKEN") },
236
+ },
237
+ remote: {
238
+ url: "https://project.example.test/mcp",
239
+ headers: {
240
+ Authorization: `Bearer ${configVariable("PROJECT_TOKEN")}`,
241
+ },
242
+ },
243
+ },
244
+ },
245
+ { PROJECT_TOKEN: "project-token" }
246
+ );
247
+
248
+ expect(result.diagnostics).toEqual([]);
249
+ expect(result.servers.local).toMatchObject({
250
+ command: ["bun", "run", "server.ts"],
251
+ environment: { TOKEN: "project-token" },
252
+ type: "local",
253
+ });
254
+ expect(result.servers.remote).toMatchObject({
255
+ headers: { Authorization: "Bearer project-token" },
256
+ type: "remote",
257
+ });
258
+ });
259
+
260
+ test("project OAuth client secrets are rejected instead of entering project configuration", async () => {
261
+ const result = await resolveAsProject({
262
+ mcpServers: {
263
+ remote: {
264
+ url: "https://project.example.test/mcp",
265
+ oauth: {
266
+ clientId: "client",
267
+ clientSecret: configVariable("CLIENT_SECRET"),
268
+ },
269
+ },
270
+ },
271
+ });
272
+
273
+ expect(result.servers.remote).toBeUndefined();
274
+ expect(result.diagnostics).toContainEqual(
275
+ expect.objectContaining({
276
+ code: "unsupported-auth",
277
+ message:
278
+ "MCP configuration cannot provide OAuth client secrets; use the host secret store.",
279
+ serverName: "remote",
280
+ })
281
+ );
282
+ });
283
+
284
+ test("user OAuth client secrets are rejected before reaching MCP transport configuration", async () => {
285
+ const result = await resolveAsUser({
286
+ mcpServers: {
287
+ remote: {
288
+ url: "https://user.example.test/mcp",
289
+ oauth: {
290
+ clientId: "client",
291
+ clientSecret: "must-not-appear-in-resolved-config",
292
+ },
293
+ },
294
+ },
295
+ });
296
+
297
+ expect(result.servers.remote).toBeUndefined();
298
+ expect(result.diagnostics).toContainEqual(
299
+ expect.objectContaining({
300
+ code: "unsupported-auth",
301
+ message:
302
+ "MCP configuration cannot provide OAuth client secrets; use the host secret store.",
303
+ serverName: "remote",
304
+ })
305
+ );
306
+ expect(JSON.stringify(result.diagnostics)).not.toContain(
307
+ "must-not-appear-in-resolved-config"
308
+ );
309
+ });
310
+
311
+ test("unsupported exposure and transport entries are diagnosed without blocking valid servers", async () => {
312
+ const result = await resolveAsUser({
313
+ mcpServers: {
314
+ valid: { command: "bun", args: ["run", "server.ts"] },
315
+ unsupportedExposure: {
316
+ command: "bun",
317
+ exposure: "codemode",
318
+ },
319
+ unsupportedTransport: {
320
+ type: "sse",
321
+ url: "https://example.test/sse",
322
+ },
323
+ },
324
+ });
325
+
326
+ expect(result.servers.valid).toBeDefined();
327
+ expect(result.servers.unsupportedExposure).toBeUndefined();
328
+ expect(result.servers.unsupportedTransport).toBeUndefined();
329
+ expect(result.diagnostics).toEqual(
330
+ expect.arrayContaining([
331
+ expect.objectContaining({
332
+ code: "unsupported-exposure",
333
+ serverName: "unsupportedExposure",
334
+ }),
335
+ expect.objectContaining({
336
+ code: "unsupported-transport",
337
+ serverName: "unsupportedTransport",
338
+ }),
339
+ ])
340
+ );
341
+ });
342
+
343
+ test("the retired Wincode mcp map is inert in general settings", async () => {
344
+ const result = await resolveAsUser({
345
+ mcp: {
346
+ legacy: {
347
+ type: "local",
348
+ command: ["bun", "run", "legacy.ts"],
349
+ },
350
+ },
351
+ });
352
+
353
+ expect(result.servers).toEqual({});
354
+ expect(result.diagnostics).toEqual([]);
355
+ });
356
+
357
+ test("missing environment values diagnose the server without leaking configuration values", async () => {
358
+ const result = await resolveAsUser({
359
+ mcpServers: {
360
+ remote: {
361
+ url: "https://example.test/mcp",
362
+ headers: { Authorization: `Bearer ${configVariable("MISSING_TOKEN")}` },
363
+ },
364
+ },
365
+ });
366
+
367
+ expect(result.servers.remote).toBeUndefined();
368
+ expect(result.diagnostics).toContainEqual(
369
+ expect.objectContaining({
370
+ code: "missing-env",
371
+ message: "Missing environment variable MISSING_TOKEN for server remote",
372
+ serverName: "remote",
373
+ })
374
+ );
375
+ expect(JSON.stringify(result)).not.toContain("Bearer");
376
+ });
@@ -0,0 +1,186 @@
1
+ import { expect, test } from "bun:test";
2
+ import {
3
+ createMcpManagement,
4
+ type McpConfigSnapshot,
5
+ type McpConfigSource,
6
+ type McpSecretStore,
7
+ } from "@wincode/mcp";
8
+
9
+ const source = (
10
+ scope: "global" | "project",
11
+ path: string,
12
+ document: Readonly<Record<string, unknown>>
13
+ ): McpConfigSource => ({ scope, path, document });
14
+
15
+ const snapshot = (sources: readonly McpConfigSource[]): McpConfigSnapshot => ({
16
+ diagnostics: [],
17
+ document: {},
18
+ sourceFor: () => undefined,
19
+ sources,
20
+ });
21
+
22
+ test("MCP management writes the selected transport in the canonical shape at its selected scope", async () => {
23
+ const writes: unknown[] = [];
24
+ const management = createMcpManagement({
25
+ authorizeWrite: () => undefined,
26
+ config: {
27
+ getSnapshot: async () =>
28
+ snapshot([
29
+ source("global", "/home/user/.wincode/mcp.json", {}),
30
+ source("project", "/workspace/.wincode/mcp.json", {}),
31
+ ]),
32
+ setValue: async (scope, path, value) => {
33
+ writes.push({ path, scope, value });
34
+ },
35
+ },
36
+ env: {},
37
+ workspace: "/workspace",
38
+ });
39
+
40
+ await management.add({
41
+ args: ["run", "docs.ts"],
42
+ command: "bun",
43
+ name: "docs",
44
+ scope: "project",
45
+ });
46
+
47
+ expect(writes).toEqual([
48
+ {
49
+ path: ["mcpServers", "docs"],
50
+ scope: "project",
51
+ value: { args: ["run", "docs.ts"], command: "bun" },
52
+ },
53
+ ]);
54
+ });
55
+
56
+ test("MCP management rejects ambiguous transports before persistence", async () => {
57
+ let writes = 0;
58
+ const management = createMcpManagement({
59
+ authorizeWrite: () => undefined,
60
+ config: {
61
+ getSnapshot: async () => snapshot([]),
62
+ setValue: async () => {
63
+ writes += 1;
64
+ },
65
+ },
66
+ env: {},
67
+ workspace: "/workspace",
68
+ });
69
+
70
+ await expect(
71
+ management.add({
72
+ command: "bun",
73
+ name: "broken",
74
+ scope: "global",
75
+ url: "https://mcp.example.test",
76
+ })
77
+ ).rejects.toThrow("Provide exactly one");
78
+ expect(writes).toBe(0);
79
+ });
80
+
81
+ test("MCP management delegates project write authorization to the host", async () => {
82
+ let reads = 0;
83
+ let writes = 0;
84
+ const management = createMcpManagement({
85
+ authorizeWrite: (scope) => {
86
+ if (scope === "project") {
87
+ throw new Error("Project is not trusted.");
88
+ }
89
+ },
90
+ config: {
91
+ getSnapshot: async () => {
92
+ reads += 1;
93
+ return snapshot([]);
94
+ },
95
+ setValue: async () => {
96
+ writes += 1;
97
+ },
98
+ },
99
+ env: {},
100
+ workspace: "/workspace",
101
+ });
102
+
103
+ await expect(
104
+ management.add({
105
+ name: "project_docs",
106
+ scope: "project",
107
+ url: "https://mcp.example.test",
108
+ })
109
+ ).rejects.toThrow("Project is not trusted.");
110
+ expect(reads).toBe(0);
111
+ expect(writes).toBe(0);
112
+ });
113
+
114
+ test("MCP management list preserves config-resolution diagnostics for callers", async () => {
115
+ const management = createMcpManagement({
116
+ authorizeWrite: () => undefined,
117
+ config: {
118
+ getSnapshot: async () =>
119
+ snapshot([
120
+ source("global", "/home/user/.wincode/mcp.json", {
121
+ mcpServers: { broken: { url: "not-a-url" } },
122
+ }),
123
+ ]),
124
+ setValue: async () => undefined,
125
+ },
126
+ env: {},
127
+ workspace: "/workspace",
128
+ });
129
+
130
+ const result = await management.list();
131
+
132
+ expect(result.servers).toEqual({});
133
+ expect(result.diagnostics.map(({ code }) => code)).toContain("invalid-url");
134
+ });
135
+
136
+ test("MCP management login and logout resolve OAuth servers through injected capabilities", async () => {
137
+ const secrets: McpSecretStore = {
138
+ delete: async () => undefined,
139
+ get: async () => undefined,
140
+ set: async () => undefined,
141
+ };
142
+ const calls: string[] = [];
143
+ const management = createMcpManagement({
144
+ authorizeWrite: () => undefined,
145
+ config: {
146
+ getSnapshot: async () =>
147
+ snapshot([
148
+ source("global", "/home/user/.wincode/mcp.json", {
149
+ mcpServers: {
150
+ docs: {
151
+ auth: "oauth",
152
+ oauth: { clientId: "configured-client" },
153
+ url: "https://mcp.example.test",
154
+ },
155
+ },
156
+ }),
157
+ ]),
158
+ setValue: async () => undefined,
159
+ },
160
+ env: {},
161
+ oauth: {
162
+ login: async (input) => {
163
+ expect(input.serverName).toBe("docs");
164
+ expect(input.serverUrl).toBe("https://mcp.example.test/");
165
+ expect(input.oauth).toEqual({ clientId: "configured-client" });
166
+ expect(input.secrets).toBe(secrets);
167
+ calls.push("login");
168
+ },
169
+ logout: async (store, name) => {
170
+ expect(store).toBe(secrets);
171
+ expect(name).toBe("docs");
172
+ calls.push("logout");
173
+ },
174
+ },
175
+ workspace: "/workspace",
176
+ });
177
+
178
+ await management.login({
179
+ name: "docs",
180
+ openAuthorizationUrl: async () => undefined,
181
+ secrets,
182
+ });
183
+ await management.logout({ name: "docs", secrets });
184
+
185
+ expect(calls).toEqual(["login", "logout"]);
186
+ });
@@ -0,0 +1,17 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { isJsonValue } from "@wincode/mcp";
3
+
4
+ const nestedValue = (depth: number): unknown => {
5
+ let value: unknown = "leaf";
6
+ for (let index = 0; index < depth; index += 1) {
7
+ value = { child: value };
8
+ }
9
+ return value;
10
+ };
11
+
12
+ describe("MCP JSON manifest policy", () => {
13
+ test("rejects values beyond the MCP nesting limit", () => {
14
+ expect(isJsonValue(nestedValue(64))).toBe(true);
15
+ expect(isJsonValue(nestedValue(65))).toBe(false);
16
+ });
17
+ });
@@ -0,0 +1,106 @@
1
+ import { expect, test } from "bun:test";
2
+ import type {
3
+ OAuthClientInformationContext,
4
+ OAuthClientProvider,
5
+ StoredOAuthTokens,
6
+ } from "@modelcontextprotocol/client";
7
+ import {
8
+ createMcpOAuthProvider,
9
+ logoutMcpServer,
10
+ type McpSecretStore,
11
+ } from "@wincode/mcp";
12
+
13
+ const createSecretStore = (): Readonly<{
14
+ secrets: McpSecretStore;
15
+ values: Map<string, string>;
16
+ }> => {
17
+ const values = new Map<string, string>();
18
+ return {
19
+ secrets: {
20
+ delete: async (key) => {
21
+ values.delete(key);
22
+ },
23
+ get: async (key) => values.get(key),
24
+ set: async (key, value) => {
25
+ values.set(key, value);
26
+ },
27
+ },
28
+ values,
29
+ };
30
+ };
31
+
32
+ const issuer = "https://auth.example.test";
33
+ const context: OAuthClientInformationContext = { issuer };
34
+
35
+ const tokens = {
36
+ access_token: "access-token",
37
+ expires_in: 3600,
38
+ issuer,
39
+ refresh_token: "refresh-token",
40
+ token_type: "Bearer",
41
+ } satisfies StoredOAuthTokens;
42
+
43
+ test("MCP OAuth credentials are isolated by server and persisted through the injected secret store", async () => {
44
+ const { secrets, values } = createSecretStore();
45
+ const provider = createMcpOAuthProvider({
46
+ oauth: { clientId: "configured-client" },
47
+ redirectUrl: "http://127.0.0.1:45831/oauth/callback",
48
+ secrets,
49
+ serverName: "docs",
50
+ });
51
+ const state = await provider.state?.();
52
+ if (state === undefined) {
53
+ throw new Error("Expected an OAuth state value.");
54
+ }
55
+ await provider.saveCodeVerifier("pkce-verifier");
56
+ await provider.saveTokens(tokens, context);
57
+
58
+ expect(provider.clientMetadata.redirect_uris).toEqual([
59
+ "http://127.0.0.1:45831/oauth/callback",
60
+ ]);
61
+ expect(await provider.tokens(context)).toEqual(tokens);
62
+ expect(await provider.codeVerifier()).toBe("pkce-verifier");
63
+ expect(values.get("mcp.oauth.docs.flow.state")).toBe(state);
64
+ await expect(
65
+ createMcpOAuthProvider({ secrets, serverName: "other" }).tokens(context)
66
+ ).resolves.toBeUndefined();
67
+
68
+ await logoutMcpServer(secrets, "docs");
69
+ expect(await provider.tokens(context)).toBeUndefined();
70
+ expect(values.has("mcp.oauth.docs.active-issuer")).toBe(false);
71
+ });
72
+
73
+ test("MCP OAuth providers supply redirect metadata and keep client IDs user scoped", async () => {
74
+ const { secrets } = createSecretStore();
75
+ const provider: OAuthClientProvider = createMcpOAuthProvider({
76
+ oauth: { clientId: "saved-client-id" },
77
+ redirectUrl: "http://127.0.0.1:45832/oauth/callback",
78
+ secrets,
79
+ serverName: "calendar",
80
+ });
81
+
82
+ expect(provider.clientMetadata).toMatchObject({
83
+ client_name: "Wincode",
84
+ redirect_uris: ["http://127.0.0.1:45832/oauth/callback"],
85
+ token_endpoint_auth_method: "none",
86
+ });
87
+ await expect(provider.clientInformation(context)).resolves.toMatchObject({
88
+ client_id: "saved-client-id",
89
+ });
90
+ await expect(provider.clientInformation(context)).resolves.not.toHaveProperty(
91
+ "client_secret"
92
+ );
93
+ });
94
+
95
+ test("MCP OAuth providers reject client secrets from configuration", () => {
96
+ const { secrets } = createSecretStore();
97
+ expect(() =>
98
+ createMcpOAuthProvider({
99
+ oauth: { clientId: "configured-client", clientSecret: "raw-secret" },
100
+ secrets,
101
+ serverName: "calendar",
102
+ })
103
+ ).toThrow(
104
+ "MCP OAuth client secrets must be stored through the host secret store."
105
+ );
106
+ });