@kya-os/cli
Version:
CLI for MCP-I setup and management
46 lines • 1.62 kB
JavaScript
import { existsSync, mkdirSync, readFileSync } from "fs";
import { join } from "path";
import { writeFileAtomicSafe } from "./fs-safe.js";
const AGENT_METADATA_FILE = "agent.json";
function agentMetadataPath(cwd) {
return join(cwd, ".mcpi", AGENT_METADATA_FILE);
}
export function readAgentMetadata(cwd) {
const path = agentMetadataPath(cwd);
if (!existsSync(path)) {
return null;
}
try {
const parsed = JSON.parse(readFileSync(path, "utf-8"));
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
return parsed;
}
return null;
}
catch {
return null;
}
}
/**
* Merge the given fields into .mcpi/agent.json, creating the directory and
* file when needed. Existing fields are preserved unless overwritten by a
* defined value in `meta`.
*/
export function writeAgentMetadata(cwd, meta) {
const dir = join(cwd, ".mcpi");
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
const existing = readAgentMetadata(cwd) ?? {};
const merged = { ...existing };
for (const [key, value] of Object.entries(meta)) {
// Empty strings are skipped alongside undefined: legacy API responses
// coerce missing fields to "", which must not clobber good values.
if (value !== undefined && value !== "") {
merged[key] = value;
}
}
// Symlink-safe: agent.json lives in a possibly-untrusted working tree.
writeFileAtomicSafe(agentMetadataPath(cwd), JSON.stringify(merged, null, 2) + "\n");
}
//# sourceMappingURL=agent-metadata.js.map