Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6
7import { readClaudeMcpServers, resolveClaudeMcpConfigFilePath } from "./ClaudeMcpConfig.ts";
8
9const writeClaudeConfig = Effect.fn(function* (homeDir: string, contents: string) {
10 const fs = yield* FileSystem.FileSystem;
11 const path = yield* Path.Path;
12 yield* fs.makeDirectory(homeDir, { recursive: true });
13 yield* fs.writeFileString(path.join(homeDir, ".claude.json"), contents);
14});
15
16it.layer(NodeServices.layer)("readClaudeMcpServers", (it) => {
17 it.effect("reads declared servers from the instance config directory", () =>
18 Effect.gen(function* () {
19 const fs = yield* FileSystem.FileSystem;
20 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-mcp-" });
21 yield* writeClaudeConfig(
22 tempDir,
23 `{
24 "mcpServers": {
25 "codegraph": { "type": "stdio", "command": "codegraph", "args": ["serve", "--mcp"] },
26 "remote": { "type": "http", "url": "https://example.com/mcp" }
27 }
28 }`,
29 );
30
31 const { definitions: servers } = yield* readClaudeMcpServers({ homePath: tempDir }, {});
32
33 assert.deepEqual(
34 servers.map((server) => server.name),
35 ["codegraph", "remote"],
36 );
37 assert.deepEqual(servers[0]?.definition, {
38 type: "stdio",
39 command: "codegraph",
40 args: ["serve", "--mcp"],
41 });
42 }).pipe(Effect.scoped),
43 );
44
45 it.effect("reports an incomplete read for a malformed config", () =>
46 Effect.gen(function* () {
47 const fs = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-mcp-" });
50
51 // An absent config is a complete read of "no servers"...
52 const missing = yield* readClaudeMcpServers({ homePath: path.join(tempDir, "absent") }, {});
53 assert.deepEqual(missing.definitions, []);
54 assert.equal(missing.complete, true);
55
56 // ...but a file that exists and cannot be parsed leaves the list unknown,
57 // which is what stops the adapter from taking over MCP resolution.
58 yield* writeClaudeConfig(tempDir, "{ not json");
59 const malformed = yield* readClaudeMcpServers({ homePath: tempDir }, {});
60 assert.deepEqual(malformed.definitions, []);
61 assert.equal(malformed.complete, false);
62 }).pipe(Effect.scoped),
63 );
64
65 it.effect("falls back to CLAUDE_CONFIG_DIR when the instance sets no home", () =>
66 Effect.gen(function* () {
67 const fs = yield* FileSystem.FileSystem;
68 const path = yield* Path.Path;
69 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-mcp-" });
70 yield* writeClaudeConfig(tempDir, '{ "mcpServers": { "alpaca": { "command": "uvx" } } }');
71
72 const configPath = yield* resolveClaudeMcpConfigFilePath(
73 { homePath: "" },
74 { CLAUDE_CONFIG_DIR: tempDir },
75 );
76 assert.equal(configPath, path.join(tempDir, ".claude.json"));
77
78 const { definitions: servers } = yield* readClaudeMcpServers(
79 { homePath: "" },
80 { CLAUDE_CONFIG_DIR: tempDir },
81 );
82 assert.deepEqual(
83 servers.map((server) => server.name),
84 ["alpaca"],
85 );
86 }).pipe(Effect.scoped),
87 );
88
89 it.effect("resolves a relative CLAUDE_CONFIG_DIR against the workspace cwd", () =>
90 Effect.gen(function* () {
91 const fs = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-mcp-" });
94 const workspace = path.join(tempDir, "workspace");
95 yield* writeClaudeConfig(
96 path.join(workspace, "nested-home"),
97 '{ "mcpServers": { "codegraph": { "command": "codegraph" } } }',
98 );
99
100 const { definitions: servers } = yield* readClaudeMcpServers(
101 { homePath: "" },
102 { CLAUDE_CONFIG_DIR: "nested-home" },
103 workspace,
104 );
105
106 assert.deepEqual(
107 servers.map((server) => server.name),
108 ["codegraph"],
109 );
110 }).pipe(Effect.scoped),
111 );
112
113 it.effect("adds local servers and only approved project servers", () =>
114 Effect.gen(function* () {
115 const fs = yield* FileSystem.FileSystem;
116 const path = yield* Path.Path;
117 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-mcp-" });
118 const workspace = path.join(tempDir, "workspace");
119 yield* fs.makeDirectory(workspace, { recursive: true });
120 yield* fs.writeFileString(
121 path.join(workspace, ".mcp.json"),
122 [
123 "{",
124 ' "mcpServers": {',
125 ' "approved": { "command": "approved-server" },',
126 ' "unapproved": { "command": "unapproved-server" },',
127 ' "rejected": { "command": "rejected-server" }',
128 " }",
129 "}",
130 ].join("\n"),
131 );
132 yield* writeClaudeConfig(
133 tempDir,
134 [
135 "{",
136 ' "mcpServers": { "codegraph": { "command": "codegraph" } },',
137 ' "projects": {',
138 ` "${workspace.replaceAll("\\", "\\\\")}": {`,
139 ' "mcpServers": { "scratch": { "command": "scratch-server" } },',
140 ' "enabledMcpjsonServers": ["approved"],',
141 ' "disabledMcpjsonServers": ["rejected"]',
142 " }",
143 " }",
144 "}",
145 ].join("\n"),
146 );
147
148 const { definitions: servers } = yield* readClaudeMcpServers(
149 { homePath: tempDir },
150 {},
151 workspace,
152 );
153
154 assert.deepEqual(servers.map((server) => `${server.name}:${server.scope}`).sort(), [
155 "approved:project",
156 "codegraph:user",
157 "scratch:local",
158 ]);
159 }).pipe(Effect.scoped),
160 );
161});