/** * pi-mcp-adapter detection. * * The market writes mcp.json regardless, but connecting servers requires * pi-mcp-adapter. We detect its presence by checking the installed npm module * inside the agent directory, and surface an install action when missing. */ import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { getAgentDir } from "./config.ts"; /** Where pi installs npm packages for the agent. */ export function getNpmModulesDir(): string { return join(getAgentDir(), "npm", "node_modules"); } export function getAdapterPackagePath(): string { return join(getNpmModulesDir(), "pi-mcp-adapter", "package.json"); } /** True when pi-mcp-adapter is installed in the agent's npm modules. */ export function isAdapterInstalled(): boolean { return existsSync(getAdapterPackagePath()); } /** Best-effort installed adapter version, or null. */ export function getAdapterVersion(): string | null { try { const { version } = JSON.parse(readFileSync(getAdapterPackagePath(), "utf8")) as { version?: string; }; return typeof version === "string" ? version : null; } catch { return null; } }