@useavalon/caelence-agent 0.2.2-canary.2 → 0.2.2-canary.4
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/desktop/src/App.tsx +44 -12
- package/desktop/src/McpPanel.tsx +268 -0
- package/desktop/src/api.ts +42 -0
- package/desktop/src/app.css +69 -0
- package/package.json +1 -1
- package/src/cli/slash-dispatch.test.ts +1 -0
- package/src/core/approval.test.ts +42 -0
- package/src/core/approval.ts +23 -8
- package/src/core/mode.ts +1 -0
- package/src/core/run-turn.ts +3 -2
- package/src/desktop/bridge.ts +34 -0
- package/src/mcp/user-servers.test.ts +138 -0
- package/src/mcp/user-servers.ts +427 -0
- package/src/runtime.ts +60 -11
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
addUserMcpServer,
|
|
7
|
+
isHttpMcpUrl,
|
|
8
|
+
listPublicMcps,
|
|
9
|
+
listUserMcpServers,
|
|
10
|
+
parseAddUserMcp,
|
|
11
|
+
parseMcpServersJson,
|
|
12
|
+
removeUserMcpServer,
|
|
13
|
+
splitMcpArgs,
|
|
14
|
+
userMcpPath,
|
|
15
|
+
userMcpToolPrefix,
|
|
16
|
+
} from "./user-servers.ts";
|
|
17
|
+
|
|
18
|
+
describe("user MCP servers", () => {
|
|
19
|
+
test("splits quoted command args", () => {
|
|
20
|
+
expect(splitMcpArgs(`npx -y "@ag-grid/mcp-server"`)).toEqual([
|
|
21
|
+
"npx",
|
|
22
|
+
"-y",
|
|
23
|
+
"@ag-grid/mcp-server",
|
|
24
|
+
]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("accepts http MCP urls only", () => {
|
|
28
|
+
expect(isHttpMcpUrl("https://mcp.example.com/mcp")).toBe(true);
|
|
29
|
+
expect(isHttpMcpUrl("http://127.0.0.1:3100/mcp")).toBe(true);
|
|
30
|
+
expect(isHttpMcpUrl("file:///tmp/mcp")).toBe(false);
|
|
31
|
+
expect(isHttpMcpUrl("javascript:alert(1)")).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("namespaces user tools away from hosted connectors", () => {
|
|
35
|
+
expect(userMcpToolPrefix("ag-grid")).toBe("mcp_ag_grid");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("parses a pasted local command into argv", () => {
|
|
39
|
+
const parsed = parseAddUserMcp({ command: "npx -y @ag-grid/mcp-server", label: "AG Grid" }, []);
|
|
40
|
+
expect(parsed).toEqual({
|
|
41
|
+
ok: {
|
|
42
|
+
id: "ag-grid",
|
|
43
|
+
label: "AG Grid",
|
|
44
|
+
kind: "stdio",
|
|
45
|
+
command: "npx",
|
|
46
|
+
args: ["-y", "@ag-grid/mcp-server"],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("round-trips a local server and hides tokens on the public list", async () => {
|
|
52
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-mcp-"));
|
|
53
|
+
try {
|
|
54
|
+
const added = addUserMcpServer(cwd, {
|
|
55
|
+
kind: "http",
|
|
56
|
+
url: "https://mcp.example.com/mcp",
|
|
57
|
+
token: "secret-token",
|
|
58
|
+
label: "Example",
|
|
59
|
+
});
|
|
60
|
+
expect("ok" in added).toBe(true);
|
|
61
|
+
const stored = listUserMcpServers(cwd);
|
|
62
|
+
expect(stored[0]?.token).toBe("secret-token");
|
|
63
|
+
const listed = listPublicMcps(cwd, [{ command: "uvx", args: ["demo-mcp"] }]);
|
|
64
|
+
expect(listed.map((item) => item.id)).toEqual(["example", "config-0"]);
|
|
65
|
+
expect(listed[0]?.detail).toBe("https://mcp.example.com/mcp · token set");
|
|
66
|
+
expect(JSON.stringify(listed)).not.toContain("secret-token");
|
|
67
|
+
expect(listed[1]?.removable).toBe(false);
|
|
68
|
+
const raw = await readFile(userMcpPath(cwd), "utf8");
|
|
69
|
+
expect(raw).toContain("secret-token");
|
|
70
|
+
expect(removeUserMcpServer(cwd, "example")).toBe(true);
|
|
71
|
+
expect(listUserMcpServers(cwd)).toEqual([]);
|
|
72
|
+
expect(removeUserMcpServer(cwd, "config-0")).toBe(false);
|
|
73
|
+
} finally {
|
|
74
|
+
await rm(cwd, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("parses a pasted mcpServers JSON block", () => {
|
|
79
|
+
const parsed = parseMcpServersJson(`{
|
|
80
|
+
"mcpServers": {
|
|
81
|
+
"fetch": {
|
|
82
|
+
"command": "uvx",
|
|
83
|
+
"args": ["mcp-server-fetch"],
|
|
84
|
+
"env": {},
|
|
85
|
+
"disabled": true,
|
|
86
|
+
"autoApprove": []
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}`);
|
|
90
|
+
expect(parsed).toEqual({
|
|
91
|
+
ok: [
|
|
92
|
+
{
|
|
93
|
+
kind: "stdio",
|
|
94
|
+
label: "fetch",
|
|
95
|
+
command: "uvx",
|
|
96
|
+
args: ["mcp-server-fetch"],
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("adds a pasted mcpServers JSON block", async () => {
|
|
103
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-mcp-json-"));
|
|
104
|
+
try {
|
|
105
|
+
const added = addUserMcpServer(cwd, {
|
|
106
|
+
kind: "stdio",
|
|
107
|
+
command: `{
|
|
108
|
+
"mcpServers": {
|
|
109
|
+
"fetch": {
|
|
110
|
+
"command": "uvx",
|
|
111
|
+
"args": ["mcp-server-fetch"]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}`,
|
|
115
|
+
});
|
|
116
|
+
expect(added).toMatchObject({
|
|
117
|
+
ok: {
|
|
118
|
+
label: "fetch",
|
|
119
|
+
kind: "stdio",
|
|
120
|
+
command: "uvx",
|
|
121
|
+
args: ["mcp-server-fetch"],
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
expect(listUserMcpServers(cwd)[0]?.command).toBe("uvx");
|
|
125
|
+
} finally {
|
|
126
|
+
await rm(cwd, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("rejects a duplicate command already in harness.config.ts", () => {
|
|
131
|
+
const parsed = parseAddUserMcp(
|
|
132
|
+
{ command: "uvx", args: ["demo-mcp"] },
|
|
133
|
+
[],
|
|
134
|
+
[{ command: "uvx", args: ["demo-mcp"] }],
|
|
135
|
+
);
|
|
136
|
+
expect(parsed).toEqual({ error: "That MCP is already in harness.config.ts." });
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import type { ExtraMcpServerConfig } from "../config.ts";
|
|
4
|
+
|
|
5
|
+
export type UserMcpKind = "stdio" | "http";
|
|
6
|
+
|
|
7
|
+
export interface UserMcpServer {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
kind: UserMcpKind;
|
|
11
|
+
command?: string;
|
|
12
|
+
args?: string[];
|
|
13
|
+
env?: Record<string, string>;
|
|
14
|
+
url?: string;
|
|
15
|
+
token?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PublicUserMcp {
|
|
19
|
+
id: string;
|
|
20
|
+
label: string;
|
|
21
|
+
kind: UserMcpKind;
|
|
22
|
+
detail: string;
|
|
23
|
+
origin: "user" | "config";
|
|
24
|
+
removable: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AddUserMcpInput {
|
|
28
|
+
label?: string;
|
|
29
|
+
kind?: string;
|
|
30
|
+
command?: string;
|
|
31
|
+
args?: string | string[];
|
|
32
|
+
url?: string;
|
|
33
|
+
token?: string;
|
|
34
|
+
env?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface StoreFile {
|
|
38
|
+
servers: UserMcpServer[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function userMcpPath(cwd: string): string {
|
|
42
|
+
return resolve(cwd, ".harness", "mcp.json");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function userMcpToolPrefix(id: string): string {
|
|
46
|
+
const slug = id.replace(/[^a-zA-Z0-9]+/g, "_").replace(/^_+|_+$/g, "");
|
|
47
|
+
return `mcp_${slug || "server"}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function splitMcpArgs(raw: string): string[] {
|
|
51
|
+
const out: string[] = [];
|
|
52
|
+
const re = /"([^"]*)"|'([^']*)'|[^\s]+/g;
|
|
53
|
+
for (const match of raw.matchAll(re)) {
|
|
54
|
+
const part = match[1] ?? match[2] ?? match[0];
|
|
55
|
+
if (part) out.push(part);
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function isHttpMcpUrl(value: string): boolean {
|
|
61
|
+
try {
|
|
62
|
+
const url = new URL(value);
|
|
63
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function looksLikeJsonObject(raw: string): boolean {
|
|
70
|
+
const trimmed = raw.trim();
|
|
71
|
+
return trimmed.startsWith("{") && trimmed.endsWith("}");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function tokenFromHeaders(value: unknown): string | undefined {
|
|
75
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
76
|
+
const headers = value as Record<string, unknown>;
|
|
77
|
+
const auth = headers.Authorization ?? headers.authorization;
|
|
78
|
+
if (typeof auth !== "string") return undefined;
|
|
79
|
+
return auth.replace(/^Bearer\s+/i, "").trim() || undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function mcpEntryToInput(name: string, value: unknown): AddUserMcpInput | { error: string } {
|
|
83
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
84
|
+
return { error: "MCP server config is not valid." };
|
|
85
|
+
}
|
|
86
|
+
const row = value as Record<string, unknown>;
|
|
87
|
+
const label =
|
|
88
|
+
name.trim() || (typeof row.name === "string" ? row.name.trim() : undefined) || undefined;
|
|
89
|
+
const url =
|
|
90
|
+
typeof row.url === "string"
|
|
91
|
+
? row.url.trim()
|
|
92
|
+
: typeof row.serverUrl === "string"
|
|
93
|
+
? row.serverUrl.trim()
|
|
94
|
+
: "";
|
|
95
|
+
if (url) {
|
|
96
|
+
const token =
|
|
97
|
+
(typeof row.token === "string" ? row.token.trim() : "") || tokenFromHeaders(row.headers);
|
|
98
|
+
return { kind: "http", ...(label ? { label } : {}), url, ...(token ? { token } : {}) };
|
|
99
|
+
}
|
|
100
|
+
const command = typeof row.command === "string" ? row.command.trim() : "";
|
|
101
|
+
if (!command) {
|
|
102
|
+
return { error: `${label || "MCP"} needs a command or URL.` };
|
|
103
|
+
}
|
|
104
|
+
const args = Array.isArray(row.args)
|
|
105
|
+
? row.args.filter((part): part is string => typeof part === "string")
|
|
106
|
+
: [];
|
|
107
|
+
const env = asEnv(row.env);
|
|
108
|
+
return {
|
|
109
|
+
kind: "stdio",
|
|
110
|
+
...(label ? { label } : {}),
|
|
111
|
+
command,
|
|
112
|
+
...(args.length > 0 ? { args } : {}),
|
|
113
|
+
...(env ? { env } : {}),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function mcpServerMap(value: unknown): Record<string, unknown> | undefined {
|
|
118
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
119
|
+
return value as Record<string, unknown>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseMcpServersJson(raw: string): { ok: AddUserMcpInput[] } | { error: string } {
|
|
123
|
+
let parsed: unknown;
|
|
124
|
+
try {
|
|
125
|
+
parsed = JSON.parse(raw) as unknown;
|
|
126
|
+
} catch {
|
|
127
|
+
return { error: "MCP JSON is not valid." };
|
|
128
|
+
}
|
|
129
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
130
|
+
return { error: "MCP JSON needs a command, URL, or mcpServers map." };
|
|
131
|
+
}
|
|
132
|
+
const row = parsed as Record<string, unknown>;
|
|
133
|
+
const grouped = mcpServerMap(row.mcpServers) ?? mcpServerMap(row.servers);
|
|
134
|
+
const entries = grouped
|
|
135
|
+
? Object.entries(grouped)
|
|
136
|
+
: typeof row.command === "string" ||
|
|
137
|
+
typeof row.url === "string" ||
|
|
138
|
+
typeof row.serverUrl === "string"
|
|
139
|
+
? [["", parsed] as [string, unknown]]
|
|
140
|
+
: [];
|
|
141
|
+
if (entries.length === 0) {
|
|
142
|
+
return { error: "MCP JSON needs a command, URL, or mcpServers map." };
|
|
143
|
+
}
|
|
144
|
+
const out: AddUserMcpInput[] = [];
|
|
145
|
+
for (const [name, value] of entries) {
|
|
146
|
+
const input = mcpEntryToInput(name, value);
|
|
147
|
+
if ("error" in input) return input;
|
|
148
|
+
out.push(input);
|
|
149
|
+
}
|
|
150
|
+
return { ok: out };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function expandMcpAddInput(
|
|
154
|
+
raw: AddUserMcpInput,
|
|
155
|
+
): { ok: AddUserMcpInput[] } | { error: string } {
|
|
156
|
+
const command = typeof raw.command === "string" ? raw.command.trim() : "";
|
|
157
|
+
if (!looksLikeJsonObject(command)) return { ok: [raw] };
|
|
158
|
+
const parsed = parseMcpServersJson(command);
|
|
159
|
+
if ("error" in parsed) return parsed;
|
|
160
|
+
if (raw.label?.trim() && parsed.ok.length === 1) {
|
|
161
|
+
const first = parsed.ok[0];
|
|
162
|
+
if (first && !first.label) return { ok: [{ ...first, label: raw.label.trim() }] };
|
|
163
|
+
}
|
|
164
|
+
return parsed;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readStore(cwd: string): StoreFile {
|
|
168
|
+
const path = userMcpPath(cwd);
|
|
169
|
+
if (!existsSync(path)) return { servers: [] };
|
|
170
|
+
try {
|
|
171
|
+
const parsed = JSON.parse(readFileSync(path, "utf8")) as StoreFile;
|
|
172
|
+
if (!parsed || !Array.isArray(parsed.servers)) return { servers: [] };
|
|
173
|
+
return { servers: parsed.servers.filter(isUserMcpServer) };
|
|
174
|
+
} catch {
|
|
175
|
+
return { servers: [] };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function isUserMcpServer(value: unknown): value is UserMcpServer {
|
|
180
|
+
if (!value || typeof value !== "object") return false;
|
|
181
|
+
const row = value as Record<string, unknown>;
|
|
182
|
+
if (typeof row.id !== "string" || !row.id.trim()) return false;
|
|
183
|
+
if (typeof row.label !== "string" || !row.label.trim()) return false;
|
|
184
|
+
if (row.kind === "stdio") {
|
|
185
|
+
return typeof row.command === "string" && row.command.trim().length > 0;
|
|
186
|
+
}
|
|
187
|
+
if (row.kind === "http") {
|
|
188
|
+
return typeof row.url === "string" && isHttpMcpUrl(row.url.trim());
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function writeStore(cwd: string, store: StoreFile): void {
|
|
194
|
+
const path = userMcpPath(cwd);
|
|
195
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
196
|
+
writeFileSync(path, `${JSON.stringify(store, null, "\t")}\n`, { encoding: "utf8", mode: 0o600 });
|
|
197
|
+
try {
|
|
198
|
+
chmodSync(path, 0o600);
|
|
199
|
+
} catch {
|
|
200
|
+
// best-effort on filesystems that ignore mode
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function listUserMcpServers(cwd: string): UserMcpServer[] {
|
|
205
|
+
return readStore(cwd).servers;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function slugMcpId(label: string, existing: string[]): string {
|
|
209
|
+
const base =
|
|
210
|
+
label
|
|
211
|
+
.trim()
|
|
212
|
+
.toLowerCase()
|
|
213
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
214
|
+
.replace(/^-+|-+$/g, "")
|
|
215
|
+
.slice(0, 40) || "mcp";
|
|
216
|
+
const taken = new Set(existing);
|
|
217
|
+
if (!taken.has(base) && !base.startsWith("config-")) return base;
|
|
218
|
+
let n = 2;
|
|
219
|
+
while (taken.has(`${base}-${n}`)) n += 1;
|
|
220
|
+
return `${base}-${n}`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function asStringList(value: string | string[] | undefined): string[] {
|
|
224
|
+
if (Array.isArray(value)) {
|
|
225
|
+
return value.map((part) => part.trim()).filter(Boolean);
|
|
226
|
+
}
|
|
227
|
+
if (typeof value === "string") return splitMcpArgs(value);
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function asEnv(value: unknown): Record<string, string> | undefined {
|
|
232
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
233
|
+
const out: Record<string, string> = {};
|
|
234
|
+
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
|
|
235
|
+
if (!key.trim() || typeof entry !== "string") continue;
|
|
236
|
+
out[key] = entry;
|
|
237
|
+
}
|
|
238
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function defaultLabel(kind: UserMcpKind, command: string, args: string[], url: string): string {
|
|
242
|
+
if (kind === "http") {
|
|
243
|
+
try {
|
|
244
|
+
return new URL(url).hostname || "MCP";
|
|
245
|
+
} catch {
|
|
246
|
+
return "MCP";
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const pkg = args.find((part) => part.startsWith("@") || part.toLowerCase().includes("mcp"));
|
|
250
|
+
return pkg ?? command;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function stdioFingerprint(command: string, args: string[]): string {
|
|
254
|
+
return `stdio:${command}\0${args.join("\0")}`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function httpFingerprint(url: string): string {
|
|
258
|
+
return `http:${url}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function configFingerprints(configMcp: ExtraMcpServerConfig[]): Set<string> {
|
|
262
|
+
return new Set(configMcp.map((mcp) => stdioFingerprint(mcp.command, mcp.args ?? [])));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function parseMcpKind(raw: string | undefined): UserMcpKind | null {
|
|
266
|
+
if (!raw || raw === "stdio") return "stdio";
|
|
267
|
+
if (raw === "http") return "http";
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function parseAddUserMcp(
|
|
272
|
+
raw: AddUserMcpInput,
|
|
273
|
+
existing: UserMcpServer[],
|
|
274
|
+
configMcp: ExtraMcpServerConfig[] = [],
|
|
275
|
+
): { ok: UserMcpServer } | { error: string } {
|
|
276
|
+
const kind = parseMcpKind(raw.kind);
|
|
277
|
+
if (!kind) return { error: "kind must be stdio or http." };
|
|
278
|
+
|
|
279
|
+
if (kind === "http") {
|
|
280
|
+
const url = typeof raw.url === "string" ? raw.url.trim() : "";
|
|
281
|
+
if (!url) return { error: "URL is required." };
|
|
282
|
+
if (!isHttpMcpUrl(url)) return { error: "URL must be http or https." };
|
|
283
|
+
const token = typeof raw.token === "string" ? raw.token.trim() : "";
|
|
284
|
+
const print = httpFingerprint(url);
|
|
285
|
+
if (
|
|
286
|
+
existing.some(
|
|
287
|
+
(server) => server.kind === "http" && httpFingerprint(server.url ?? "") === print,
|
|
288
|
+
)
|
|
289
|
+
) {
|
|
290
|
+
return { error: "That MCP is already added." };
|
|
291
|
+
}
|
|
292
|
+
const label =
|
|
293
|
+
(typeof raw.label === "string" ? raw.label.trim() : "") || defaultLabel("http", "", [], url);
|
|
294
|
+
const id = slugMcpId(
|
|
295
|
+
label,
|
|
296
|
+
existing.map((server) => server.id),
|
|
297
|
+
);
|
|
298
|
+
return {
|
|
299
|
+
ok: {
|
|
300
|
+
id,
|
|
301
|
+
label,
|
|
302
|
+
kind: "http",
|
|
303
|
+
url,
|
|
304
|
+
...(token ? { token } : {}),
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const givenCommand = typeof raw.command === "string" ? raw.command.trim() : "";
|
|
310
|
+
if (!givenCommand) return { error: "Command is required." };
|
|
311
|
+
let command = givenCommand;
|
|
312
|
+
let args = asStringList(raw.args);
|
|
313
|
+
if (args.length === 0 && command.includes(" ")) {
|
|
314
|
+
const parts = splitMcpArgs(command);
|
|
315
|
+
command = parts[0] ?? "";
|
|
316
|
+
args = parts.slice(1);
|
|
317
|
+
}
|
|
318
|
+
if (!command) return { error: "Command is required." };
|
|
319
|
+
if (command.includes("\0") || args.some((part) => part.includes("\0"))) {
|
|
320
|
+
return { error: "Command is not valid." };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const print = stdioFingerprint(command, args);
|
|
324
|
+
if (configFingerprints(configMcp).has(print)) {
|
|
325
|
+
return { error: "That MCP is already in harness.config.ts." };
|
|
326
|
+
}
|
|
327
|
+
if (
|
|
328
|
+
existing.some(
|
|
329
|
+
(server) =>
|
|
330
|
+
server.kind === "stdio" &&
|
|
331
|
+
stdioFingerprint(server.command ?? "", server.args ?? []) === print,
|
|
332
|
+
)
|
|
333
|
+
) {
|
|
334
|
+
return { error: "That MCP is already added." };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const env = asEnv(raw.env);
|
|
338
|
+
const label =
|
|
339
|
+
(typeof raw.label === "string" ? raw.label.trim() : "") ||
|
|
340
|
+
defaultLabel("stdio", command, args, "");
|
|
341
|
+
const id = slugMcpId(
|
|
342
|
+
label,
|
|
343
|
+
existing.map((server) => server.id),
|
|
344
|
+
);
|
|
345
|
+
return {
|
|
346
|
+
ok: {
|
|
347
|
+
id,
|
|
348
|
+
label,
|
|
349
|
+
kind: "stdio",
|
|
350
|
+
command,
|
|
351
|
+
...(args.length > 0 ? { args } : {}),
|
|
352
|
+
...(env ? { env } : {}),
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function addUserMcpServer(
|
|
358
|
+
cwd: string,
|
|
359
|
+
raw: AddUserMcpInput,
|
|
360
|
+
configMcp: ExtraMcpServerConfig[] = [],
|
|
361
|
+
): { ok: UserMcpServer } | { error: string } {
|
|
362
|
+
const expanded = expandMcpAddInput(raw);
|
|
363
|
+
if ("error" in expanded) return expanded;
|
|
364
|
+
const store = readStore(cwd);
|
|
365
|
+
let existing = store.servers;
|
|
366
|
+
const added: UserMcpServer[] = [];
|
|
367
|
+
let lastError: string | undefined;
|
|
368
|
+
for (const input of expanded.ok) {
|
|
369
|
+
const parsed = parseAddUserMcp(input, existing, configMcp);
|
|
370
|
+
if ("error" in parsed) {
|
|
371
|
+
lastError = parsed.error;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
existing = [...existing, parsed.ok];
|
|
375
|
+
added.push(parsed.ok);
|
|
376
|
+
}
|
|
377
|
+
if (added.length === 0) return { error: lastError ?? "No MCP servers to add." };
|
|
378
|
+
writeStore(cwd, { servers: existing });
|
|
379
|
+
const last = added[added.length - 1];
|
|
380
|
+
if (!last) return { error: lastError ?? "No MCP servers to add." };
|
|
381
|
+
return { ok: last };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export function removeUserMcpServer(cwd: string, id: string): boolean {
|
|
385
|
+
const key = id.trim();
|
|
386
|
+
if (!key || key.startsWith("config-")) return false;
|
|
387
|
+
const store = readStore(cwd);
|
|
388
|
+
const next = store.servers.filter((server) => server.id !== key);
|
|
389
|
+
if (next.length === store.servers.length) return false;
|
|
390
|
+
writeStore(cwd, { servers: next });
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function publicDetail(server: UserMcpServer): string {
|
|
395
|
+
if (server.kind === "http") {
|
|
396
|
+
const url = server.url ?? "";
|
|
397
|
+
return server.token ? `${url} · token set` : url;
|
|
398
|
+
}
|
|
399
|
+
return [server.command, ...(server.args ?? [])].filter(Boolean).join(" ");
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function toPublicUserMcp(server: UserMcpServer): PublicUserMcp {
|
|
403
|
+
return {
|
|
404
|
+
id: server.id,
|
|
405
|
+
label: server.label,
|
|
406
|
+
kind: server.kind,
|
|
407
|
+
detail: publicDetail(server),
|
|
408
|
+
origin: "user",
|
|
409
|
+
removable: true,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function listPublicMcps(
|
|
414
|
+
cwd: string,
|
|
415
|
+
configMcp: ExtraMcpServerConfig[] = [],
|
|
416
|
+
): PublicUserMcp[] {
|
|
417
|
+
const user = listUserMcpServers(cwd).map(toPublicUserMcp);
|
|
418
|
+
const config = configMcp.map((mcp, index) => ({
|
|
419
|
+
id: `config-${index}`,
|
|
420
|
+
label: mcp.command,
|
|
421
|
+
kind: "stdio" as const,
|
|
422
|
+
detail: [mcp.command, ...(mcp.args ?? [])].filter(Boolean).join(" "),
|
|
423
|
+
origin: "config" as const,
|
|
424
|
+
removable: false,
|
|
425
|
+
}));
|
|
426
|
+
return [...user, ...config];
|
|
427
|
+
}
|
package/src/runtime.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type ApprovalRequest,
|
|
7
7
|
createExecApprovalGate,
|
|
8
8
|
type ExecApprovalPolicy,
|
|
9
|
+
isFileWriteTool,
|
|
9
10
|
} from "./core/approval.ts";
|
|
10
11
|
import type { AgentEventEmitter, MainModelProvider } from "./core/events.ts";
|
|
11
12
|
import { createHookRunner } from "./core/hooks.ts";
|
|
@@ -39,6 +40,7 @@ import { addUsage, type TokenUsage } from "./core/usage.ts";
|
|
|
39
40
|
import { type ChatFn, createOpenRouterChat } from "./evals/runner.ts";
|
|
40
41
|
import { buildRemoteMcpTools } from "./integrations/remote-mcp.ts";
|
|
41
42
|
import { linkedMcpSources } from "./integrations/store.ts";
|
|
43
|
+
import { listUserMcpServers, userMcpToolPrefix } from "./mcp/user-servers.ts";
|
|
42
44
|
import { createObservability } from "./observability/create.ts";
|
|
43
45
|
import { noopObservability } from "./observability/noop.ts";
|
|
44
46
|
import { harnessPromptRecords, syncPrompts } from "./observability/prompts.ts";
|
|
@@ -104,6 +106,7 @@ export interface HarnessRuntime {
|
|
|
104
106
|
compact(): Promise<boolean>;
|
|
105
107
|
reloadSkills(): Skill[];
|
|
106
108
|
reloadIntegrations(): Promise<void>;
|
|
109
|
+
reloadExtraMcp(): Promise<void>;
|
|
107
110
|
integrationIds(): string[];
|
|
108
111
|
authorSkill(name: string, brief: string): Promise<{ rel: string } | { error: string }>;
|
|
109
112
|
findSkills(query: string): Promise<string>;
|
|
@@ -173,8 +176,9 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
173
176
|
const todos = createMemoryTodoStore();
|
|
174
177
|
const hooks = createHookRunner({ cwd, hooks: config.hooks });
|
|
175
178
|
void hooks.sessionStart();
|
|
176
|
-
const closers: Array<() => void> = [];
|
|
177
179
|
const extra: McpToolDefinition[] = [...(options.extraTools ?? [])];
|
|
180
|
+
let extraMcpTools: McpToolDefinition[] = [];
|
|
181
|
+
let extraMcpClosers: Array<() => void> = [];
|
|
178
182
|
let integrationTools: McpToolDefinition[] = [];
|
|
179
183
|
const shouldLoadIntegrations = options.loadIntegrations ?? !options.provider;
|
|
180
184
|
const reloadIntegrations = async (): Promise<void> => {
|
|
@@ -184,16 +188,53 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
184
188
|
}
|
|
185
189
|
integrationTools = await buildRemoteMcpTools({ sources: linkedMcpSources() });
|
|
186
190
|
};
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
191
|
+
const reloadExtraMcp = async (): Promise<void> => {
|
|
192
|
+
const nextTools: McpToolDefinition[] = [];
|
|
193
|
+
const nextClosers: Array<() => void> = [];
|
|
194
|
+
for (const mcp of config.mcp) {
|
|
195
|
+
try {
|
|
196
|
+
const loaded = await loadStdioMcpTools({ ...mcp, cwd });
|
|
197
|
+
nextTools.push(...loaded.tools);
|
|
198
|
+
nextClosers.push(loaded.close);
|
|
199
|
+
} catch {
|
|
200
|
+
// fail-soft: missing extra MCP must not block the REPL
|
|
201
|
+
}
|
|
195
202
|
}
|
|
196
|
-
|
|
203
|
+
for (const server of listUserMcpServers(cwd)) {
|
|
204
|
+
try {
|
|
205
|
+
if (server.kind === "stdio" && server.command) {
|
|
206
|
+
const loaded = await loadStdioMcpTools({
|
|
207
|
+
command: server.command,
|
|
208
|
+
args: server.args,
|
|
209
|
+
env: server.env,
|
|
210
|
+
cwd,
|
|
211
|
+
namePrefix: userMcpToolPrefix(server.id),
|
|
212
|
+
});
|
|
213
|
+
nextTools.push(...loaded.tools);
|
|
214
|
+
nextClosers.push(loaded.close);
|
|
215
|
+
} else if (server.kind === "http" && server.url) {
|
|
216
|
+
const tools = await buildRemoteMcpTools({
|
|
217
|
+
sources: [
|
|
218
|
+
{
|
|
219
|
+
connectorId: userMcpToolPrefix(server.id),
|
|
220
|
+
label: server.label,
|
|
221
|
+
mcpUrl: server.url,
|
|
222
|
+
accessToken: server.token ?? "",
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
});
|
|
226
|
+
nextTools.push(...tools);
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
// fail-soft: a bad user MCP must not block the REPL
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const close of extraMcpClosers) close();
|
|
233
|
+
extraMcpClosers = nextClosers;
|
|
234
|
+
extraMcpTools = nextTools;
|
|
235
|
+
};
|
|
236
|
+
await reloadIntegrations();
|
|
237
|
+
await reloadExtraMcp();
|
|
197
238
|
|
|
198
239
|
const or = resolveOpenRouter(config);
|
|
199
240
|
let apiKey = or.apiKey;
|
|
@@ -227,6 +268,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
227
268
|
...createGitTools(cwd),
|
|
228
269
|
createTodoTool(todos),
|
|
229
270
|
...extra,
|
|
271
|
+
...extraMcpTools,
|
|
230
272
|
...integrationTools,
|
|
231
273
|
createReadSkillTool(skills),
|
|
232
274
|
]);
|
|
@@ -295,6 +337,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
295
337
|
},
|
|
296
338
|
allowRiskyAlways(toolName) {
|
|
297
339
|
alwaysAllow.add(toolName);
|
|
340
|
+
if (isFileWriteTool(toolName)) {
|
|
341
|
+
alwaysAllow.add("write_file");
|
|
342
|
+
alwaysAllow.add("edit_file");
|
|
343
|
+
}
|
|
298
344
|
},
|
|
299
345
|
async compact() {
|
|
300
346
|
if (!runtime.activeSessionId) return false;
|
|
@@ -311,6 +357,9 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
311
357
|
async reloadIntegrations() {
|
|
312
358
|
await reloadIntegrations();
|
|
313
359
|
},
|
|
360
|
+
async reloadExtraMcp() {
|
|
361
|
+
await reloadExtraMcp();
|
|
362
|
+
},
|
|
314
363
|
integrationIds() {
|
|
315
364
|
return linkedMcpSources().map((source) => source.connectorId);
|
|
316
365
|
},
|
|
@@ -483,7 +532,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
483
532
|
return session;
|
|
484
533
|
},
|
|
485
534
|
close() {
|
|
486
|
-
for (const close of
|
|
535
|
+
for (const close of extraMcpClosers) close();
|
|
487
536
|
void observability.flush();
|
|
488
537
|
},
|
|
489
538
|
};
|