@useavalon/caelence-agent 0.2.2-canary.3 → 0.2.2-canary.5
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 +160 -45
- package/desktop/src/ComposerAttach.tsx +2 -26
- package/desktop/src/FilesPanel.tsx +68 -0
- package/desktop/src/McpPanel.tsx +268 -0
- package/desktop/src/api.ts +48 -1
- package/desktop/src/app.css +69 -0
- package/desktop/src/composer-files.test.ts +2 -0
- package/desktop/src/composer-files.ts +10 -0
- package/desktop/src/user-display.test.ts +42 -0
- package/desktop/src/user-display.ts +20 -0
- package/package.json +1 -1
- package/src/cli/slash-dispatch.test.ts +1 -0
- package/src/cli/tui.tsx +40 -5
- package/src/core/approval.test.ts +22 -0
- package/src/core/approval.ts +20 -0
- package/src/desktop/bridge.ts +60 -6
- package/src/desktop/upload-message.test.ts +53 -0
- package/src/desktop/upload-message.ts +74 -0
- package/src/desktop/uploads.test.ts +34 -0
- package/src/desktop/uploads.ts +178 -13
- package/src/index.ts +1 -0
- package/src/init.test.ts +21 -0
- package/src/mcp/user-servers.test.ts +138 -0
- package/src/mcp/user-servers.ts +427 -0
- package/src/runtime.ts +61 -12
|
@@ -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
|
@@ -37,9 +37,11 @@ import {
|
|
|
37
37
|
createConstructAgent,
|
|
38
38
|
} from "./core/strands.ts";
|
|
39
39
|
import { addUsage, type TokenUsage } from "./core/usage.ts";
|
|
40
|
+
import { listUploadOriginals, matchUploadEdit } from "./desktop/uploads.ts";
|
|
40
41
|
import { type ChatFn, createOpenRouterChat } from "./evals/runner.ts";
|
|
41
42
|
import { buildRemoteMcpTools } from "./integrations/remote-mcp.ts";
|
|
42
43
|
import { linkedMcpSources } from "./integrations/store.ts";
|
|
44
|
+
import { listUserMcpServers, userMcpToolPrefix } from "./mcp/user-servers.ts";
|
|
43
45
|
import { createObservability } from "./observability/create.ts";
|
|
44
46
|
import { noopObservability } from "./observability/noop.ts";
|
|
45
47
|
import { harnessPromptRecords, syncPrompts } from "./observability/prompts.ts";
|
|
@@ -105,6 +107,7 @@ export interface HarnessRuntime {
|
|
|
105
107
|
compact(): Promise<boolean>;
|
|
106
108
|
reloadSkills(): Skill[];
|
|
107
109
|
reloadIntegrations(): Promise<void>;
|
|
110
|
+
reloadExtraMcp(): Promise<void>;
|
|
108
111
|
integrationIds(): string[];
|
|
109
112
|
authorSkill(name: string, brief: string): Promise<{ rel: string } | { error: string }>;
|
|
110
113
|
findSkills(query: string): Promise<string>;
|
|
@@ -170,12 +173,16 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
170
173
|
});
|
|
171
174
|
let systemPrompt = composePrompt(mode);
|
|
172
175
|
const store = createFileSessionStore(cwd);
|
|
173
|
-
const local = createLocalTools({
|
|
176
|
+
const local = createLocalTools({
|
|
177
|
+
cwd,
|
|
178
|
+
extraWriteAbsolutes: () => listUploadOriginals(cwd),
|
|
179
|
+
});
|
|
174
180
|
const todos = createMemoryTodoStore();
|
|
175
181
|
const hooks = createHookRunner({ cwd, hooks: config.hooks });
|
|
176
182
|
void hooks.sessionStart();
|
|
177
|
-
const closers: Array<() => void> = [];
|
|
178
183
|
const extra: McpToolDefinition[] = [...(options.extraTools ?? [])];
|
|
184
|
+
let extraMcpTools: McpToolDefinition[] = [];
|
|
185
|
+
let extraMcpClosers: Array<() => void> = [];
|
|
179
186
|
let integrationTools: McpToolDefinition[] = [];
|
|
180
187
|
const shouldLoadIntegrations = options.loadIntegrations ?? !options.provider;
|
|
181
188
|
const reloadIntegrations = async (): Promise<void> => {
|
|
@@ -185,16 +192,53 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
185
192
|
}
|
|
186
193
|
integrationTools = await buildRemoteMcpTools({ sources: linkedMcpSources() });
|
|
187
194
|
};
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
const reloadExtraMcp = async (): Promise<void> => {
|
|
196
|
+
const nextTools: McpToolDefinition[] = [];
|
|
197
|
+
const nextClosers: Array<() => void> = [];
|
|
198
|
+
for (const mcp of config.mcp) {
|
|
199
|
+
try {
|
|
200
|
+
const loaded = await loadStdioMcpTools({ ...mcp, cwd });
|
|
201
|
+
nextTools.push(...loaded.tools);
|
|
202
|
+
nextClosers.push(loaded.close);
|
|
203
|
+
} catch {
|
|
204
|
+
// fail-soft: missing extra MCP must not block the REPL
|
|
205
|
+
}
|
|
196
206
|
}
|
|
197
|
-
|
|
207
|
+
for (const server of listUserMcpServers(cwd)) {
|
|
208
|
+
try {
|
|
209
|
+
if (server.kind === "stdio" && server.command) {
|
|
210
|
+
const loaded = await loadStdioMcpTools({
|
|
211
|
+
command: server.command,
|
|
212
|
+
args: server.args,
|
|
213
|
+
env: server.env,
|
|
214
|
+
cwd,
|
|
215
|
+
namePrefix: userMcpToolPrefix(server.id),
|
|
216
|
+
});
|
|
217
|
+
nextTools.push(...loaded.tools);
|
|
218
|
+
nextClosers.push(loaded.close);
|
|
219
|
+
} else if (server.kind === "http" && server.url) {
|
|
220
|
+
const tools = await buildRemoteMcpTools({
|
|
221
|
+
sources: [
|
|
222
|
+
{
|
|
223
|
+
connectorId: userMcpToolPrefix(server.id),
|
|
224
|
+
label: server.label,
|
|
225
|
+
mcpUrl: server.url,
|
|
226
|
+
accessToken: server.token ?? "",
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
nextTools.push(...tools);
|
|
231
|
+
}
|
|
232
|
+
} catch {
|
|
233
|
+
// fail-soft: a bad user MCP must not block the REPL
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
for (const close of extraMcpClosers) close();
|
|
237
|
+
extraMcpClosers = nextClosers;
|
|
238
|
+
extraMcpTools = nextTools;
|
|
239
|
+
};
|
|
240
|
+
await reloadIntegrations();
|
|
241
|
+
await reloadExtraMcp();
|
|
198
242
|
|
|
199
243
|
const or = resolveOpenRouter(config);
|
|
200
244
|
let apiKey = or.apiKey;
|
|
@@ -228,6 +272,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
228
272
|
...createGitTools(cwd),
|
|
229
273
|
createTodoTool(todos),
|
|
230
274
|
...extra,
|
|
275
|
+
...extraMcpTools,
|
|
231
276
|
...integrationTools,
|
|
232
277
|
createReadSkillTool(skills),
|
|
233
278
|
]);
|
|
@@ -316,6 +361,9 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
316
361
|
async reloadIntegrations() {
|
|
317
362
|
await reloadIntegrations();
|
|
318
363
|
},
|
|
364
|
+
async reloadExtraMcp() {
|
|
365
|
+
await reloadExtraMcp();
|
|
366
|
+
},
|
|
319
367
|
integrationIds() {
|
|
320
368
|
return linkedMcpSources().map((source) => source.connectorId);
|
|
321
369
|
},
|
|
@@ -403,6 +451,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
403
451
|
policy,
|
|
404
452
|
alwaysAllow,
|
|
405
453
|
emit,
|
|
454
|
+
resolveUploadEdit: (path) => matchUploadEdit(cwd, path),
|
|
406
455
|
ask: async (req) => {
|
|
407
456
|
if (turnOptions?.approvalAsk) return turnOptions.approvalAsk(req);
|
|
408
457
|
if (policy === "prompt") return false;
|
|
@@ -488,7 +537,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
488
537
|
return session;
|
|
489
538
|
},
|
|
490
539
|
close() {
|
|
491
|
-
for (const close of
|
|
540
|
+
for (const close of extraMcpClosers) close();
|
|
492
541
|
void observability.flush();
|
|
493
542
|
},
|
|
494
543
|
};
|