Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript
18.5 KB
Raw Blame
// biome-ignore-all lint/suspicious/noTemplateCurlyInString: the fixtures are other apps' configs, written with their `${...}` references.
// S20: the user's MCP servers from other apps, found read-only in a fixture home, imported on request.
import { createHash } from "node:crypto";
import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import type { ImportCandidate, ServerPayload } from "@gentle-dot/protocol";
import { afterEach, describe, expect, it } from "vitest";
import { importLocations, scanClientConfigs } from "../src/connector-import.ts";
import { ConnectorManager, ConnectorStore } from "../src/connectors.ts";
import { MemorySecretSource } from "../src/secret-source.ts";
import { tempDir, waitFor } from "./helpers.ts";

const SECRETS = ["ghp_desktopsecret", "sk_cursorsecret", "bar-secret-value", "lin-token-123"];

function write(home: string, path: string, value: unknown) {
	const file = join(home, path);
	mkdirSync(dirname(file), { recursive: true });
	writeFileSync(file, typeof value === "string" ? value : JSON.stringify(value, null, 2));
}

/** One config file per client format, as each app writes it. */
function fixtureHome(): string {
	const home = tempDir();
	const github = {
		command: "npx",
		args: ["-y", "@modelcontextprotocol/server-github"],
		env: { GITHUB_PERSONAL_ACCESS_TOKEN: "ghp_desktopsecret" },
	};
	write(home, "Library/Application Support/Claude/claude_desktop_config.json", {
		mcpServers: {
			filesystem: {
				command: "npx",
				args: ["-y", "@modelcontextprotocol/server-filesystem", "/Users/x/Docs"],
			},
			github,
		},
	});
	write(home, ".config/Claude/claude_desktop_config.json", {
		mcpServers: { memory: { command: "npx", args: ["-y", "@modelcontextprotocol/server-memory"] } },
	});
	write(home, ".claude.json", {
		numStartups: 3,
		mcpServers: {
			sentry: { type: "http", url: "https://mcp.sentry.dev/mcp" },
			legacy: { type: "sse", url: "https://old.example.com/sse" },
		},
		projects: {
			[join(home, "proj")]: {
				mcpServers: {
					local1: { type: "stdio", command: "node", args: ["server.js"], env: { API_KEY: "${API_KEY}" } },
				},
			},
			[join(home, "missing-project")]: {},
		},
	});
	write(home, "proj/.mcp.json", { mcpServers: { projsrv: { command: "uvx", args: ["mcp-server-time"] } } });
	write(home, ".cursor/mcp.json", {
		mcpServers: {
			linear: { url: "https://mcp.linear.app/mcp", headers: { Authorization: "Bearer lin-token-123" } },
			stripe: {
				url: "https://mcp.stripe.com?key=sk_cursorsecret",
				headers: { Authorization: "Bearer sk_cursorsecret" },
			},
			github,
		},
	});
	write(home, "Library/Application Support/Code/User/mcp.json", {
		inputs: [
			{ type: "promptString", id: "perplexity-key", description: "Perplexity API Key", password: true },
		],
		servers: {
			perplexity: {
				type: "stdio",
				command: "npx",
				args: ["-y", "server-perplexity-ask"],
				env: { PERPLEXITY_API_KEY: "${input:perplexity-key}" },
			},
			copilot: {
				type: "http",
				url: "https://api.githubcopilot.com/mcp/",
				headers: { Authorization: "Bearer ${env:GH_TOKEN}" },
			},
		},
	});
	write(home, ".config/Code/User/mcp.json", "{oops");
	write(home, ".codeium/windsurf/mcp_config.json", {
		mcpServers: {
			figma: { serverUrl: "https://mcp.figma.com/mcp" },
			oldsse: { serverUrl: "https://x.example.com/sse" },
		},
	});
	write(home, ".config/opencode/opencode.json", {
		$schema: "https://opencode.ai/config.json",
		mcp: {
			context7: {
				type: "remote",
				url: "https://mcp.context7.com/mcp",
				headers: { CONTEXT7_API_KEY: "{env:CTX7}" },
			},
			everything: {
				type: "local",
				command: ["npx", "-y", "@modelcontextprotocol/server-everything", "--token", "bar-secret-value"],
				environment: { FOO: "bar-secret-value" },
				enabled: true,
			},
		},
	});
	write(home, ".gentle-shell/agent/mcp.json", {
		mcpServers: { codegraph: { command: "codegraph", args: ["serve", "--mcp"], exposure: "codemode" } },
	});
	return home;
}

/** Every file below `home` with its hash and modification time. */
function snapshot(home: string): Record<string, string> {
	const out: Record<string, string> = {};
	const walk = (dir: string) => {
		for (const name of readdirSync(dir)) {
			const path = join(dir, name);
			const stat = statSync(path);
			if (stat.isDirectory()) walk(path);
			else out[path] = `${createHash("sha256").update(readFileSync(path)).digest("hex")}:${stat.mtimeMs}`;
		}
	};
	walk(home);
	return out;
}

describe("scanning other apps' MCP configs", () => {
	it("reads every client format and maps it to the engine's schema", () => {
		const home = fixtureHome();
		const found = scanClientConfigs(home);
		const byName = (name: string) => found.find((s) => s.name === name);
		expect(found.map((s) => `${s.source}:${s.name}`).sort()).toEqual(
			[
				"Claude Desktop:filesystem",
				"Claude Desktop:github",
				"Claude Desktop:memory",
				"Claude Code:sentry",
				"Claude Code:legacy",
				"Claude Code:local1",
				"Claude Code:projsrv",
				"Cursor:linear",
				"Cursor:stripe",
				"Cursor:github",
				"VS Code:perplexity",
				"VS Code:copilot",
				"Windsurf:figma",
				"Windsurf:oldsse",
				"OpenCode:context7",
				"OpenCode:everything",
				"Gentle Shell:codegraph",
			].sort(),
		);
		expect(byName("filesystem")?.server).toEqual({
			command: "npx",
			args: ["-y", "@modelcontextprotocol/server-filesystem", "/Users/x/Docs"],
		});
		expect(byName("sentry")?.server).toEqual({ url: "https://mcp.sentry.dev/mcp" });
		for (const name of ["legacy", "oldsse"]) {
			expect(byName(name)?.server, name).toBeUndefined();
			expect(byName(name)).toMatchObject({ transport: "sse", reason: expect.stringMatching(/SSE/) });
		}
		expect(byName("figma")?.server).toEqual({ url: "https://mcp.figma.com/mcp" });
		// Claude Code's `${VAR}` and VS Code's `${env:VAR}` / OpenCode's `{env:VAR}` read the environment.
		expect(byName("local1")?.server).toMatchObject({ env: { API_KEY: "${API_KEY}" } });
		expect(byName("copilot")?.server).toMatchObject({ headers: { Authorization: "Bearer ${GH_TOKEN}" } });
		expect(byName("context7")?.server).toMatchObject({ headers: { CONTEXT7_API_KEY: "${CTX7}" } });
		expect(byName("everything")?.server).toMatchObject({
			command: "npx",
			args: ["-y", "@modelcontextprotocol/server-everything", "--token", "bar-secret-value"],
			env: { FOO: "bar-secret-value" },
		});
		// VS Code inputs become values the user types in the app.
		expect(byName("perplexity")?.server).toMatchObject({
			env: { PERPLEXITY_API_KEY: "${input:perplexity-key}" },
		});
		expect(byName("perplexity")?.fields).toEqual([
			{ key: "perplexity-key", label: "Perplexity API Key", secret: true },
		]);
		// Gentle Shell entries keep their server, not the other app's exposure.
		expect(byName("codegraph")?.server).toEqual({ command: "codegraph", args: ["serve", "--mcp"] });
	});

	it("finds Claude Desktop and VS Code where Windows keeps them (AppData\\Roaming)", () => {
		const home = tempDir();
		write(home, "AppData/Roaming/Claude/claude_desktop_config.json", {
			mcpServers: { files: { command: "npx", args: ["-y", "files-server"] } },
		});
		write(home, "AppData/Roaming/Code/User/mcp.json", {
			servers: { docs: { type: "http", url: "https://docs.example.com/mcp" } },
		});
		const found = scanClientConfigs(home).map((server) => `${server.source}:${server.name}`);
		expect(found).toEqual(["Claude Desktop:files", "VS Code:docs"]);
	});

	it("finds Claude Desktop where its Windows (MSIX) package keeps it, and only Claude's package", () => {
		const home = tempDir();
		const config = "LocalCache/Roaming/Claude/claude_desktop_config.json";
		write(home, `AppData/Local/Packages/Claude_pzs8sxrjxfjjc/${config}`, {
			mcpServers: { files: { command: "npx", args: ["-y", "files-server"] } },
		});
		write(home, `AppData/Local/Packages/ClaudeHelper_123/${config}`, {
			mcpServers: { helper: { command: "helper" } },
		});
		write(home, `AppData/Local/Packages/Other_456/${config}`, {
			mcpServers: { other: { command: "other" } },
		});
		write(home, "AppData/Local/Packages/Claude_broken", "not a folder");
		const found = scanClientConfigs(home).map((server) => `${server.source}:${server.name}`);
		expect(found).toEqual(["Claude Desktop:files"]);
	});

	it("reads AppData where Windows says it is, not below the home folder", () => {
		const home = tempDir();
		const roaming = tempDir();
		const local = tempDir();
		write(home, "AppData/Roaming/Claude/claude_desktop_config.json", {
			mcpServers: { stale: { command: "old" } },
		});
		write(roaming, "Claude/claude_desktop_config.json", { mcpServers: { files: { command: "files" } } });
		write(roaming, "Code/User/mcp.json", {
			servers: { docs: { type: "http", url: "https://docs.example.com/mcp" } },
		});
		write(local, "Packages/Claude_pzs8sxrjxfjjc/LocalCache/Roaming/Claude/claude_desktop_config.json", {
			mcpServers: { notes: { command: "notes" } },
		});
		const found = scanClientConfigs(home, { appData: roaming, localAppData: local }).map(
			(server) => `${server.source}:${server.name}`,
		);
		expect(found).toEqual(["Claude Desktop:files", "Claude Desktop:notes", "VS Code:docs"]);
	});

	it("takes AppData from the environment for the user's home, and from a test home otherwise", () => {
		const roaming = join(tempDir(), "Roaming");
		const local = join(tempDir(), "Local");
		const env = { APPDATA: roaming, LOCALAPPDATA: local };
		const home = tempDir();
		expect(importLocations(undefined, env, home)).toEqual({ home, appData: roaming, localAppData: local });
		const fixture = tempDir();
		expect(importLocations(fixture, env, home)).toEqual({
			home: fixture,
			appData: join(fixture, "AppData", "Roaming"),
			localAppData: join(fixture, "AppData", "Local"),
		});
		expect(importLocations(undefined, { APPDATA: "relative", LOCALAPPDATA: "" }, home)).toEqual({
			home,
			appData: join(home, "AppData", "Roaming"),
			localAppData: join(home, "AppData", "Local"),
		});
	});

	it("finds nothing in an empty home and skips files it cannot read", () => {
		expect(scanClientConfigs(tempDir())).toEqual([]);
	});
});

const managers: ConnectorManager[] = [];
afterEach(() => {
	for (const manager of managers.splice(0)) manager.cancelAll();
});

function setup(home: string) {
	const dataDir = tempDir();
	const agentHome = join(dataDir, "agent");
	const logs: string[] = [];
	const secrets = new MemorySecretSource();
	const store = new ConnectorStore({
		dataDir,
		agentHome,
		secrets,
		env: { PATH: "/usr/bin", GH_TOKEN: "gh-from-env" },
	});
	const manager = new ConnectorManager({
		store,
		importHome: home,
		log: (line) => logs.push(line),
	});
	managers.push(manager);
	const owner = {};
	const sent: ServerPayload[] = [];
	const emit = (payload: ServerPayload) => sent.push(payload);
	return { agentHome, dataDir, store, manager, logs, owner, sent, emit, secrets };
}

const pick = (found: ImportCandidate[], name: string) => {
	const candidate = found.find((c) => c.name === name);
	if (!candidate) throw new Error(`${name} was not found`);
	return candidate;
};

describe("importing MCP servers", () => {
	it("lists what it found with the source app and names only, never values", () => {
		const home = fixtureHome();
		const { manager, store } = setup(home);
		store.update((state) => {
			state.connectors.notion = { enabled: true, mode: "read_only" };
		});
		const found = manager.scan();
		const text = JSON.stringify(found);
		for (const secret of SECRETS) expect(text).not.toContain(secret);
		expect(pick(found, "github")).toEqual({
			id: expect.any(String),
			name: "github",
			sources: ["Claude Desktop", "Cursor"],
			transport: "stdio",
			summary: "npx -y @modelcontextprotocol/server-github",
			envNames: ["GITHUB_PERSONAL_ACCESS_TOKEN"],
			headerNames: [],
			inputs: [],
			importable: true,
		});
		expect(pick(found, "stripe")).toMatchObject({
			transport: "http",
			summary: "https://mcp.stripe.com/?key=•••",
			headerNames: ["Authorization"],
		});
		expect(pick(found, "everything").summary).toBe(
			"npx -y @modelcontextprotocol/server-everything --token •••",
		);
		expect(pick(found, "perplexity")).toMatchObject({
			inputs: ["Perplexity API Key"],
			envNames: ["PERPLEXITY_API_KEY"],
		});
		expect(pick(found, "legacy")).toMatchObject({
			transport: "sse",
			importable: false,
			reason: expect.stringMatching(/SSE/),
		});
		// The catalog has Linear with a curated list, so its import is offered as a duplicate.
		expect(pick(found, "linear")).toMatchObject({ importable: false, duplicateOf: "linear" });
		expect(found.filter((c) => c.name === "github")).toHaveLength(1);
	});

	it("copies the chosen servers, their values into the app's secure store, read only and hidden, without touching the sources", async () => {
		const home = fixtureHome();
		const before = snapshot(home);
		const { manager, store, agentHome, dataDir, logs, owner, emit, sent, secrets } = setup(home);
		const found = manager.scan();
		const chosen = ["github", "stripe", "copilot", "everything", "perplexity", "legacy", "linear"].map(
			(name) => pick(found, name).id,
		);
		expect((await manager.importServers(chosen)).sort()).toEqual([
			"copilot",
			"everything",
			"github",
			"perplexity",
			"stripe",
		]);
		// Read only with no curated list: the proxy shows none of their tools.
		for (const id of ["copilot", "everything", "github", "stripe"])
			expect(store.proxyView(id), id).toMatchObject({ mode: "read_only", readOnlyTools: [] });
		expect((await store.credentials("github")).env).toEqual({
			GITHUB_PERSONAL_ACCESS_TOKEN: "ghp_desktopsecret",
		});
		expect((await store.credentials("stripe")).headers).toEqual({ Authorization: "Bearer sk_cursorsecret" });
		// An environment reference stays one, resolved only when the proxy connects.
		expect(secrets.values.get("connector/copilot/header/Authorization")).toBe("Bearer ${GH_TOKEN}");
		expect((await store.credentials("copilot")).headers).toEqual({ Authorization: "Bearer gh-from-env" });
		// A server that still needs a typed value waits for it.
		expect(store.proxyView("perplexity")).toBeUndefined();
		const list = manager.list();
		expect(list.find((c) => c.id === "perplexity")).toMatchObject({
			status: "needs_setup",
			mode: "read_only",
		});
		expect(list.find((c) => c.id === "github")).toMatchObject({
			status: "connected",
			enabled: true,
			mode: "read_only",
			custom: {
				origin: "Imported from Claude Desktop",
				summary: "npx -y @modelcontextprotocol/server-github",
			},
		});
		expect(store.policy().connectors?.github?.readOnlyTools).toEqual([]);
		// The import itself is never shown or logged with its values.
		for (const secret of SECRETS) {
			expect(JSON.stringify(list)).not.toContain(secret);
			expect(logs.join("\n")).not.toContain(secret);
		}
		const record = readFileSync(join(dataDir, "connectors.json"), "utf8");
		for (const secret of ["ghp_desktopsecret", "Bearer sk_cursorsecret"])
			expect(record).not.toContain(secret);
		const saved = JSON.parse(record).connectors;
		expect(saved.everything.custom.server.env).toEqual({
			FOO: { secretRef: "connector/everything/env/FOO" },
		});
		expect(secrets.values.get("connector/everything/env/FOO")).toBe("bar-secret-value");
		// A credential inside the address or the command line stays there (a documented limit; masked when shown).
		expect(saved.stripe.custom.server.url).toBe("https://mcp.stripe.com?key=sk_cursorsecret");
		expect(saved.everything.custom.server.args).toContain("bar-secret-value");
		expect(JSON.parse(record).connectors.github.custom.server.env).toEqual({
			GITHUB_PERSONAL_ACCESS_TOKEN: { secretRef: "connector/github/env/GITHUB_PERSONAL_ACCESS_TOKEN" },
		});
		// The VS Code input is typed in the app.
		expect(manager.setup(owner, "perplexity", emit)).toBeUndefined();
		const prompt = await waitFor(() => sent.flatMap((m) => (m.type === "auth_prompt" ? [m.prompt] : []))[0]);
		expect(prompt).toMatchObject({ kind: "secret", message: expect.stringContaining("Perplexity API Key") });
		manager.reply(owner, prompt.flowId, { value: "pplx-typed" });
		await waitFor(() => sent.find((m) => m.type === "auth_done"));
		// The proxy hands the server what was typed, and what it reads from the environment.
		expect((await store.credentials("perplexity")).env).toEqual({ PERPLEXITY_API_KEY: "pplx-typed" });
		expect(readFileSync(join(agentHome, "mcp.json"), "utf8")).not.toContain("pplx-typed");
		expect(JSON.stringify(sent)).not.toContain("pplx-typed");
		// Importing again adds nothing: they are already connectors.
		const again = manager.scan();
		expect(pick(again, "github")).toMatchObject({ importable: false, duplicateOf: "github" });
		expect(await manager.importServers([pick(again, "github").id, "unknown:id"])).toEqual([]);
		expect(snapshot(home)).toEqual(before);
	});

	it("imports nothing before a scan", async () => {
		const { manager, store } = setup(fixtureHome());
		expect(await manager.importServers(["Cursor:github"])).toEqual([]);
		expect(store.state()).toEqual({ connectors: {} });
	});
});

describe("values the engine resolves in an import", () => {
	it("notes a value that runs a command or reads an environment variable, without showing the command", () => {
		const home = tempDir();
		write(home, ".gentle-shell/agent/mcp.json", {
			mcpServers: {
				vault: {
					command: "vault-mcp",
					env: {
						VAULT_TOKEN: "!security find-generic-password -w -s vault",
						PLAIN: "a$$b",
						REGION: "$AWS_REGION",
					},
				},
				remote: {
					url: "https://mcp.example.com/mcp",
					headers: { Authorization: "Bearer ${GH_TOKEN}", "X-Team": "literal-team" },
					oauth: { clientSecret: "!op read op://vault/item/secret" },
				},
				plain: { command: "plain-mcp", env: { MODE: "fast", ESCAPED: "$$HOME and $!bang" } },
			},
		});
		const { manager } = setup(home);
		const found = manager.scan();
		expect(pick(found, "vault").notes).toEqual([
			"VAULT_TOKEN: Runs a command on your computer to get this value.",
			"REGION: Reads the environment variable AWS_REGION.",
		]);
		expect(pick(found, "remote").notes).toEqual([
			"Authorization: Reads the environment variable GH_TOKEN.",
			"Client Secret: Runs a command on your computer to get this value.",
		]);
		expect(pick(found, "plain").notes).toBeUndefined();
		const text = JSON.stringify(found);
		for (const command of ["security find-generic-password", "op read"]) expect(text).not.toContain(command);
		// Other apps' references become the engine's, so they get the same note.
		const other = tempDir();
		write(other, ".cursor/mcp.json", {
			mcpServers: {
				copilot: {
					url: "https://api.githubcopilot.com/mcp/",
					headers: { Authorization: "Bearer ${env:GH_TOKEN}" },
				},
			},
		});
		expect(pick(setup(other).manager.scan(), "copilot").notes).toEqual([
			"Authorization: Reads the environment variable GH_TOKEN.",
		]);
	});
});