-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathmcp-agent-install.test.ts
More file actions
194 lines (170 loc) · 6.95 KB
/
Copy pathmcp-agent-install.test.ts
File metadata and controls
194 lines (170 loc) · 6.95 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
import { describe, expect, it } from 'vitest';
import {
AGENT_SLUGS,
applyJsonInstall,
isAgentSlug,
planAgentInstall,
removeJsonInstall,
type JsonInstallPlan,
type McpLaunchSpec,
} from '../src/mcp-agent-install.js';
const SPEC: McpLaunchSpec = {
command: '/usr/local/bin/node',
args: ['/opt/open-design/cli.js', 'mcp', '--daemon-url', 'http://127.0.0.1:7456'],
env: { OD_DATA_DIR: '/home/u/.open-design' },
};
const SPEC_NO_ENV: McpLaunchSpec = { ...SPEC, env: {} };
const ctx = (platform: NodeJS.Platform = 'linux') => ({
home: '/home/u',
platform,
serverName: 'open-design',
});
describe('agent slug guard', () => {
it('accepts every documented slug and rejects others', () => {
for (const s of AGENT_SLUGS) expect(isAgentSlug(s)).toBe(true);
expect(isAgentSlug('not-an-agent')).toBe(false);
expect(AGENT_SLUGS).toHaveLength(15);
expect(isAgentSlug('kiro')).toBe(true);
});
});
describe('CLI-driven agents', () => {
it('claude registers via `claude mcp add --scope user` with env flags', () => {
const plan = planAgentInstall('claude', SPEC, ctx());
expect(plan.kind).toBe('cli');
if (plan.kind !== 'cli') throw new Error('expected cli');
expect(plan.bin).toBe('claude');
expect(plan.addArgv).toEqual([
'mcp', 'add', '--scope', 'user',
'open-design',
'-e', 'OD_DATA_DIR=/home/u/.open-design',
'--',
SPEC.command, ...SPEC.args,
]);
expect(plan.removeArgv).toEqual(['mcp', 'remove', '--scope', 'user', 'open-design']);
});
it('codex uses --env and a -- separator', () => {
const plan = planAgentInstall('codex', SPEC, ctx());
if (plan.kind !== 'cli') throw new Error('expected cli');
expect(plan.addArgv).toContain('--env');
expect(plan.addArgv).toContain('OD_DATA_DIR=/home/u/.open-design');
expect(plan.addArgv.slice(-SPEC.args.length - 2)).toEqual(['--', SPEC.command, ...SPEC.args]);
});
});
describe('JSON-config agents', () => {
it('cursor merges a stdio entry under mcpServers', () => {
const plan = planAgentInstall('cursor', SPEC, ctx());
if (plan.kind !== 'json') throw new Error('expected json');
expect(plan.configPath).toBe('/home/u/.cursor/mcp.json');
expect(plan.keyPath).toEqual(['mcpServers']);
expect(plan.entry).toEqual({
command: SPEC.command,
args: SPEC.args,
type: 'stdio',
env: SPEC.env,
});
});
it('opencode folds command+args into one array under `mcp` and uses `environment`', () => {
const plan = planAgentInstall('opencode', SPEC, ctx());
if (plan.kind !== 'json') throw new Error('expected json');
expect(plan.keyPath).toEqual(['mcp']);
expect(plan.entry).toEqual({
type: 'local',
command: [SPEC.command, ...SPEC.args],
enabled: true,
environment: SPEC.env,
});
});
it('openclaw nests under mcp.servers', () => {
const plan = planAgentInstall('openclaw', SPEC_NO_ENV, ctx());
if (plan.kind !== 'json') throw new Error('expected json');
expect(plan.keyPath).toEqual(['mcp', 'servers']);
expect(plan.entry).toEqual({ command: SPEC.command, args: SPEC.args });
});
it('kiro merges a stdio entry into the user MCP settings file', () => {
const plan = planAgentInstall('kiro', SPEC, ctx());
if (plan.kind !== 'json') throw new Error('expected json');
expect(plan.configPath).toBe('/home/u/.kiro/settings/mcp.json');
expect(plan.keyPath).toEqual(['mcpServers']);
expect(plan.serverKey).toBe('open-design');
expect(plan.entry).toEqual({
command: SPEC.command,
args: SPEC.args,
env: SPEC.env,
});
});
it('omits env entirely when the launch spec has no env', () => {
const plan = planAgentInstall('cursor', SPEC_NO_ENV, ctx());
if (plan.kind !== 'json') throw new Error('expected json');
expect(plan.entry).not.toHaveProperty('env');
});
it('cline path is OS-specific', () => {
const mac = planAgentInstall('cline', SPEC, ctx('darwin'));
const linux = planAgentInstall('cline', SPEC, ctx('linux'));
if (mac.kind !== 'json' || linux.kind !== 'json') throw new Error('expected json');
expect(mac.configPath).toContain('Library/Application Support/Code/User');
expect(linux.configPath).toContain('.config/Code/User');
expect(mac.configPath).toContain('saoudrizwan.claude-dev');
});
});
describe('manual (unverified) agents', () => {
it('pi / vibe / hermes never produce a writable plan', () => {
for (const slug of ['pi', 'vibe', 'hermes'] as const) {
const plan = planAgentInstall(slug, SPEC, ctx());
expect(plan.kind).toBe('manual');
if (plan.kind !== 'manual') throw new Error('expected manual');
expect(plan.snippet.length).toBeGreaterThan(0);
expect(plan.reason.length).toBeGreaterThan(0);
}
});
it('vibe snippet is TOML array-of-tables', () => {
const plan = planAgentInstall('vibe', SPEC, ctx());
if (plan.kind !== 'manual') throw new Error('expected manual');
expect(plan.format).toBe('toml');
expect(plan.snippet).toContain('[[mcp_servers]]');
expect(plan.snippet).toContain('transport = "stdio"');
});
});
describe('applyJsonInstall', () => {
const plan = planAgentInstall('cursor', SPEC, ctx()) as JsonInstallPlan;
it('creates the file structure from empty input', () => {
const out = applyJsonInstall(null, plan);
const parsed = JSON.parse(out);
expect(parsed.mcpServers['open-design'].command).toBe(SPEC.command);
expect(out.endsWith('\n')).toBe(true);
});
it('preserves unrelated keys and sibling servers', () => {
const existing = JSON.stringify({
editor: { theme: 'dark' },
mcpServers: { 'other-server': { command: 'foo' } },
});
const out = JSON.parse(applyJsonInstall(existing, plan));
expect(out.editor).toEqual({ theme: 'dark' });
expect(out.mcpServers['other-server']).toEqual({ command: 'foo' });
expect(out.mcpServers['open-design'].type).toBe('stdio');
});
it('is idempotent — re-applying yields the same content', () => {
const once = applyJsonInstall(null, plan);
const twice = applyJsonInstall(once, plan);
expect(twice).toBe(once);
});
it('throws on an unparseable existing config rather than clobbering it', () => {
expect(() => applyJsonInstall('{not json', plan)).toThrow(/not valid JSON/);
});
});
describe('removeJsonInstall', () => {
const plan = planAgentInstall('cursor', SPEC, ctx()) as JsonInstallPlan;
it('removes only the open-design entry', () => {
const existing = applyJsonInstall(
JSON.stringify({ mcpServers: { other: { command: 'x' } } }),
plan,
);
const out = JSON.parse(removeJsonInstall(existing, plan)!);
expect(out.mcpServers).not.toHaveProperty('open-design');
expect(out.mcpServers.other).toEqual({ command: 'x' });
});
it('returns null when there is nothing to remove', () => {
expect(removeJsonInstall(null, plan)).toBeNull();
expect(removeJsonInstall('{}', plan)).toBeNull();
expect(removeJsonInstall(JSON.stringify({ mcpServers: {} }), plan)).toBeNull();
});
});