A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
// biome-ignore-all lint/suspicious/noTemplateCurlyInString: `${input:<key>}` and `${NAME}` are the placeholders connectors use.
// The connector files with the MCP proxy (S25.4): the engine's mcp.json lists only the daemon's
// proxy addresses, the real servers stay in the daemon's memory and their secrets in the app's
// secure store (S25.5), and the approval guard lets proxied tools through (the proxy asks) while it
// keeps protecting the files, the old sign-in files included.
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import { ConnectorStore, type CustomConnector, renderMcpJson } from "../src/connectors.ts";
import { decide, parsePolicy } from "../src/extensions/approval-guard.ts";
import { MemorySecretSource } from "../src/secret-source.ts";
import { tempDir } from "./helpers.ts";
const NOTION_KEY = "mcp__notion|https://mcp.notion.com/mcp";
const KEY = "proxy-key-for-this-launch-0123456789abcdef";
const PROXY = { url: "http://127.0.0.1:4999/mcp", key: KEY };
/** Every secret the files below hold; none may reach the engine's mcp.json. */
const SECRETS = {
discordToken: "discord-bot-token-value-$weird!",
slackSecret: "slack-client-secret-value",
headerToken: "custom-header-token-value",
envToken: "custom-env-token-value",
oauthAccess: "oauth-access-token-value",
oauthRefresh: "oauth-refresh-token-value",
};
async function engineMcp() {
const require = createRequire(import.meta.url);
const file = (require.resolve.paths("@earendil-works/pi-coding-agent") ?? [])
.map((dir) => join(dir, "@earendil-works", "pi-coding-agent", "dist", "core", "mcp-servers.js"))
.find((path) => existsSync(path));
if (!file) throw new Error("the engine package is missing");
return (await import(pathToFileURL(file).href)) as {
validateMcpServerConfig(name: string, raw: unknown): unknown;
};
}
const stores: ConnectorStore[] = [];
afterEach(() => {
for (const s of stores.splice(0)) s.close();
});
function setup(options: { env?: NodeJS.ProcessEnv } = {}) {
const dataDir = tempDir();
const agentHome = join(dataDir, "agent");
const workspace = join(dataDir, "workspace");
mkdirSync(agentHome);
mkdirSync(workspace);
const secrets = new MemorySecretSource();
const make = () => {
const s = new ConnectorStore({
dataDir,
agentHome,
workspace,
secrets,
guardPath: "/app/guard.ts",
...(options.env ? { env: options.env } : {}),
});
stores.push(s);
return s;
};
return { dataDir, agentHome, workspace, make, secrets };
}
const httpCustom: CustomConnector = {
name: "Tracker",
description: "",
origin: "Imported from Cursor",
server: { url: "https://tracker.example.com/mcp", headers: { Authorization: "Bearer ${input:TRACKER}" } },
fields: [{ key: "TRACKER", label: "TRACKER", secret: true }],
oauth: false,
};
const stdioCustom: CustomConnector = {
name: "Files",
description: "",
origin: "Drafted by the assistant",
server: { command: "npx", args: ["-y", "[email protected]"], env: { FILES_TOKEN: "${input:FILES_TOKEN}" } },
fields: [{ key: "FILES_TOKEN", label: "FILES_TOKEN", secret: true }],
oauth: false,
};
function fill(s: ConnectorStore) {
s.update((state) => {
state.connectors.notion = { enabled: true, mode: "read_only" };
state.connectors.linear = { enabled: false, mode: "read_write" };
state.connectors.discord = { enabled: true, mode: "read_write", values: { token: SECRETS.discordToken } };
state.connectors.slack = {
enabled: true,
mode: "read_only",
values: { client_id: "slack-client-id", client_secret: SECRETS.slackSecret },
};
state.connectors.tracker = {
enabled: true,
mode: "read_only",
values: { TRACKER: SECRETS.headerToken },
custom: httpCustom,
};
state.connectors.files = {
enabled: true,
mode: "read_write",
values: { FILES_TOKEN: SECRETS.envToken },
custom: stdioCustom,
};
});
}
describe("the engine's mcp.json with the proxy", () => {
it("lists every connector as a proxy address with this launch's key, and nothing else", async () => {
const { validateMcpServerConfig } = await engineMcp();
const { make, agentHome, dataDir } = setup();
const s = make();
s.setProxy(PROXY);
fill(s);
await s.saveSignIn("notion", {
serverUrl: "https://mcp.notion.com/mcp",
tokens: {
access_token: SECRETS.oauthAccess,
token_type: "Bearer",
refresh_token: SECRETS.oauthRefresh,
},
});
const text = readFileSync(join(agentHome, "mcp.json"), "utf8");
const servers = JSON.parse(text).mcpServers as Record<string, Record<string, unknown>>;
expect(Object.keys(servers)).toEqual(["notion", "linear", "discord", "slack", "tracker", "files"]);
const entry = (id: string) => ({
url: `http://127.0.0.1:4999/mcp/${id}`,
headers: { Authorization: `Bearer ${KEY}` },
exposure: "direct",
timeout: 300,
});
expect(servers.notion).toEqual(entry("notion"));
expect(servers.linear).toEqual({ ...entry("linear"), enabled: false });
expect(servers.files).toEqual(entry("files"));
for (const [name, config] of Object.entries(servers))
expect(typeof validateMcpServerConfig(name, config)).toBe("object");
for (const secret of Object.values(SECRETS)) expect(text).not.toContain(secret);
for (const upstream of [
"notion.com",
"linear.app",
"slack.com",
"tracker.example.com",
"npx",
"discord-mcp",
])
expect(text).not.toContain(upstream);
for (const name of ["DISCORD_TOKEN", "FILES_TOKEN", "env", "oauth", "toolExposure", "command"])
expect(text).not.toContain(name);
// The real servers are only in the daemon's memory, and their secrets only in the app's store.
expect(existsSync(s.signinHome)).toBe(false);
const record = readFileSync(join(dataDir, "connectors.json"), "utf8");
for (const secret of Object.values(SECRETS)) expect(record).not.toContain(secret);
expect(JSON.parse(record).connectors.slack.values.client_id).toBe("slack-client-id");
expect(statSync(join(dataDir, "connectors.json")).mode & 0o777).toBe(0o600);
});
it("leaves the connectors out until the proxy listens, keeps the computer helper as it is, and changes the key per launch", () => {
const { make, agentHome } = setup();
const s = make();
s.update((state) => {
state.connectors.notion = { enabled: true, mode: "read_only" };
});
const read = () => JSON.parse(readFileSync(join(agentHome, "mcp.json"), "utf8")).mcpServers;
expect(read()).toEqual({});
s.setComputer({ url: "http://127.0.0.1:5555/mcp", token: "helper-key" });
expect(s.setProxy(PROXY)).toBe(true);
expect(s.setProxy(PROXY)).toBe(false);
expect(read().computer).toEqual({
url: "http://127.0.0.1:5555/mcp",
headers: { Authorization: "Bearer helper-key" },
exposure: "direct",
timeout: 300,
});
expect(read().notion.headers.Authorization).toBe(`Bearer ${KEY}`);
s.setProxy({ ...PROXY, key: "next-launch-key-0123456789abcdef0123" });
expect(read().notion.headers.Authorization).toBe("Bearer next-launch-key-0123456789abcdef0123");
expect(JSON.parse(renderMcpJson({ connectors: {} }, undefined, PROXY))).toEqual({ mcpServers: {} });
});
});
describe("the proxy's view of a connector and its credentials", () => {
it("gives the proxy each connector's live state without secrets, and a new revision when its values change", () => {
const { make } = setup();
const s = make();
fill(s);
expect(s.proxyView("notion")).toMatchObject({
id: "notion",
name: "Notion",
enabled: true,
mode: "read_only",
server: { url: "https://mcp.notion.com/mcp" },
});
expect(s.proxyView("notion")?.readOnlyTools).toContain("notion-search");
expect(s.proxyView("linear")).toMatchObject({ enabled: false });
expect(s.proxyView("files")).toMatchObject({
mode: "read_write",
readOnlyTools: [],
server: { command: "npx", args: ["-y", "[email protected]"] },
});
for (const id of ["discord", "slack", "tracker", "files"]) {
const text = JSON.stringify(s.proxyView(id));
for (const secret of Object.values(SECRETS)) expect(text).not.toContain(secret);
}
expect(s.proxyView("atlassian")).toBeUndefined();
const before = s.proxyView("files")?.revision;
s.update((state) => {
const files = state.connectors.files;
if (files) files.mode = "read_only";
});
expect(s.proxyView("files")?.revision).toBe(before);
s.update((state) => {
const files = state.connectors.files;
if (files) files.values = { FILES_TOKEN: "another" };
});
expect(s.proxyView("files")?.revision).not.toBe(before);
});
it("injects the typed values as they were typed, the environment references, and the stored sign-in", async () => {
const { make } = setup({ env: { PATH: "/usr/bin", TEAM_ID: "team-7" } });
const s = make();
fill(s);
s.update((state) => {
state.connectors.team = {
enabled: true,
mode: "read_only",
custom: {
name: "Team",
description: "",
origin: "Imported from Claude Code",
server: {
url: "https://team.example.com/mcp",
headers: { "X-Team": "${TEAM_ID}", "X-Price": "$$5" },
},
fields: [],
oauth: false,
},
};
});
expect((await s.credentials("discord")).env).toEqual({ DISCORD_TOKEN: SECRETS.discordToken });
expect((await s.credentials("files")).env).toEqual({ FILES_TOKEN: SECRETS.envToken });
const tracker = await s.credentials("tracker");
expect(tracker.headers).toEqual({ Authorization: `Bearer ${SECRETS.headerToken}` });
// A server with its own Authorization header does not sign in.
expect(tracker.authProvider).toBeUndefined();
expect((await s.credentials("team")).headers).toEqual({ "X-Team": "team-7", "X-Price": "$5" });
const notion = await s.credentials("notion");
expect(await notion.authProvider?.token()).toBeUndefined();
// A planted sign-in file is never read; only a sign-in the daemon stored is used.
mkdirSync(s.signinHome, { recursive: true });
writeFileSync(s.authFile, JSON.stringify({ [NOTION_KEY]: { tokens: { access_token: "planted" } } }));
expect(await notion.authProvider?.token()).toBeUndefined();
await s.saveSignIn("notion", {
serverUrl: "https://mcp.notion.com/mcp",
tokens: { access_token: SECRETS.oauthAccess, token_type: "Bearer" },
});
expect(await notion.authProvider?.token()).toBe(SECRETS.oauthAccess);
});
});
describe("the old sign-in files", () => {
it("protects the sign-in home and its files from the file tools and shell commands like the other credential files", () => {
const { make, agentHome, dataDir } = setup();
const s = make();
fill(s);
const policy = s.policy();
const signin = join(dataDir, "connector-signin");
expect(policy.protectedPaths).toEqual(
expect.arrayContaining([
join(agentHome, "mcp.json"),
join(agentHome, "mcp-auth.json"),
signin,
join(signin, "mcp.json"),
join(signin, "mcp-auth.json"),
]),
);
mkdirSync(signin, { recursive: true });
writeFileSync(s.authFile, "{}");
const cwd = join(dataDir, "workspace");
for (const call of [
{ toolName: "read", input: { path: join(signin, "mcp-auth.json") } },
{ toolName: "read", input: { path: "../connector-signin/mcp.json" } },
{ toolName: "ls", input: { path: signin } },
{ toolName: "grep", input: { pattern: "token", path: dataDir } },
{ toolName: "write", input: { path: join(signin, "mcp.json"), content: "{}" } },
{ toolName: "bash", input: { command: "cat ../connector-signin/mcp-auth.json" } },
{ toolName: "bash", input: { command: "ls ../connector-signin" } },
])
expect(decide({ ...call, cwd }, policy).action).toBe("block");
});
it("are never written again: the daemon keeps no sign-in home while it runs", async () => {
const { make, secrets } = setup();
const s = make();
s.watch();
fill(s);
await s.settled();
await s.saveSignIn("notion", {
serverUrl: "https://mcp.notion.com/mcp",
tokens: { access_token: SECRETS.oauthAccess, token_type: "Bearer" },
});
expect(s.enforce()).toBe(false);
await new Promise((resolve) => setTimeout(resolve, 100));
expect(existsSync(s.signinHome)).toBe(false);
expect(secrets.values.get("connector/discord/value/token")).toBe(SECRETS.discordToken);
expect(secrets.values.get("connector/files/value/FILES_TOKEN")).toBe(SECRETS.envToken);
});
});
describe("the approval guard with proxied connectors", () => {
it("lets the tools of a proxied connector through (the proxy asks), and still blocks one that is turned off", () => {
const { make, dataDir } = setup();
const s = make();
fill(s);
const policy = parsePolicy(JSON.stringify(s.policy()));
if (!policy) throw new Error("the policy did not parse");
expect(policy.proxied).toEqual(["notion", "discord", "slack", "tracker", "files"]);
const cwd = join(dataDir, "workspace");
const call = (toolName: string) => decide({ toolName, input: { body: "hi" }, cwd }, policy).action;
expect(call("mcp__notion__notion_create_pages")).toBe("pass");
expect(call("mcp__files__delete_everything")).toBe("pass");
expect(call("mcp__linear__create_issue")).toBe("block");
expect(call("mcp__unknown__x")).toBe("block");
expect(parsePolicy(JSON.stringify({ ...s.policy(), proxied: [7] }))).toBeUndefined();
});
});