A local-first multi-agent app development platform, built on PenguinHarness
/**
* Agent config routes (reads/writes system_config.yaml and AGENTS.md):
* GET|PUT /api/projects/:p/agents/:a/config, plus POST …/config/reset to adopt the
* current default config (keeps only name/description/version — the config-side
* analogue of a skill update), POST …/config/kernel-update to smart-merge up to the
* current defaults generation (customizations kept), and POST …/config/mcp-test to
* probe one MCP Server entry's reachability. Members can read and write (unrestricted).
*/
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Hono } from "hono";
import { McpToolProvider, resolveMCPServer } from "@adelie/core";
import type { MCPServerConfig } from "@adelie/core";
import type {
AgentConfigResponse,
AgentConfigUpdateRequest,
AgentKernelUpdateResponse,
McpServerTestResponse,
} from "../../api/types.js";
import type { AppEnv } from "../../auth/middleware.js";
import { badRequest, optionalString, readJson, requireValidId } from "../validate.js";
import type { SessionManager } from "../../runtime/session-manager.js";
import type { AgentConfig } from "../../mechanisms/agents.js";
import type { Access } from "../../mechanisms/projects.js";
/** What this route group reaches — bound by its module (src/modules). */
export interface AgentConfigRouteDeps {
agentConfigService: AgentConfig;
manager: SessionManager;
access: Access;
}
/** Ceiling on a single mcp-test probe's connect budget (an entry may configure minutes). */
const MCP_TEST_TIMEOUT_CAP_MS = 30_000;
export function agentConfigRoutes(deps: AgentConfigRouteDeps): Hono<AppEnv> {
const app = new Hono<AppEnv>();
app.get("/", async (c) => {
// Id validation happens before any path construction: prevents agentId path traversal for cross-Project privilege escalation.
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.access.requireProjectAccess(c.var.user.userId, projectId);
const view = await deps.agentConfigService.getConfig(projectId, agentId);
return c.json({
...view,
activeSessionCount: deps.manager.activeCountForAgent(projectId, agentId),
} satisfies AgentConfigResponse);
});
app.put("/", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.access.requireProjectAccess(c.var.user.userId, projectId);
const body = await readJson(c);
const req: AgentConfigUpdateRequest = {};
const agentsMd = optionalString(body, "agentsMd", { label: "agentsMd" });
if (agentsMd !== undefined) req.agentsMd = agentsMd;
if (body.config !== undefined) {
if (body.config === null || typeof body.config !== "object" || Array.isArray(body.config)) {
throw badRequest("config must be an object.");
}
req.config = body.config as AgentConfigUpdateRequest["config"];
}
// Fine-grained validation (numeric ranges / enums) is done inside agent-config-service.
await deps.agentConfigService.updateConfig(projectId, agentId, req);
// Agent State normally needs no invalidation — core re-reads it into the next model
// context itself, the hook switch included. The switch is still the exception: its
// saved feedback promises the next turn, and a conversation that is running would
// otherwise keep its hooks until its next compaction.
if (req.config?.hooks !== undefined) {
deps.manager.invalidateAgentRuntimes(projectId, agentId);
}
const view = await deps.agentConfigService.getConfig(projectId, agentId);
return c.json({
...view,
activeSessionCount: deps.manager.activeCountForAgent(projectId, agentId),
} satisfies AgentConfigResponse);
});
// Probe one MCP Server entry: connect + discover tools with the entry's own
// connectTimeoutMs, then close. Runs server-side on purpose — stdio servers spawn on
// this host, exactly where Sessions run them, and browser-origin HTTP probes would
// stumble over CORS. A malformed entry is a 400 (same resolver as PUT validation); an
// unreachable server is a normal `{ ok: false }` result carrying the collected warning
// (connect error, timeout, stderr tail). Nothing is written to the Agent State.
app.post("/mcp-test", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.access.requireProjectAccess(c.var.user.userId, projectId);
const body = await readJson(c);
if (typeof body.name !== "string" || body.name.length === 0) {
throw badRequest("name must be a non-empty string.");
}
if (body.config === null || typeof body.config !== "object" || Array.isArray(body.config)) {
throw badRequest("config must be an object.");
}
const entry = { name: body.name, config: body.config } as MCPServerConfig;
let resolved;
try {
resolved = resolveMCPServer(entry);
} catch (err) {
throw badRequest(err instanceof Error ? err.message : String(err));
}
// A probe must not hold a request open arbitrarily long (the bulk test walks every
// server sequentially): the entry's own connect budget applies, capped for testing.
if (resolved.connectTimeoutMs > MCP_TEST_TIMEOUT_CAP_MS) {
entry.config = { ...entry.config, connectTimeoutMs: MCP_TEST_TIMEOUT_CAP_MS };
}
const warnings: string[] = [];
// A fresh empty directory stands in for the Session Workspace (a new session's
// temporary workspace is exactly that), so a stdio server that resolves relative
// paths behaves like it will at runtime — not like the server process's own cwd.
const workspaceDir = await mkdtemp(join(tmpdir(), "penguin-mcp-test-"));
const provider = new McpToolProvider([entry], {
workspaceDir,
warn: (m) => warnings.push(m),
});
const startedAt = Date.now();
try {
const tools = await provider.listTools();
const latencyMs = Date.now() - startedAt;
// Verdict comes from the connect outcome itself, not from the warning count:
// benign warnings (a tool listed twice / an LLM-unusable tool name) must not
// report a healthy server as failed.
const outcome = provider.connectResults()[0];
if (outcome === undefined || outcome.status !== "completed") {
return c.json({
ok: false,
error:
outcome?.error_message ??
(warnings.length > 0 ? warnings.join("; ") : "connect failed"),
latencyMs,
} satisfies McpServerTestResponse);
}
return c.json({
ok: true,
tools: tools.map((t) => t.name),
latencyMs,
} satisfies McpServerTestResponse);
} finally {
await provider.close();
void rm(workspaceDir, { recursive: true, force: true }).catch(() => undefined);
}
});
// Smart-merge the config up to the current defaults generation (see
// AgentConfigService.kernelUpdate): non-destructive sibling of /reset, same member-level
// authorization as /reset and PUT. Responds with the merge report (advanced / kept tabs +
// the new stamp); the client re-GETs the config for the fresh values.
app.post("/kernel-update", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.access.requireProjectAccess(c.var.user.userId, projectId);
const result = await deps.agentConfigService.kernelUpdate(projectId, agentId);
return c.json(result satisfies AgentKernelUpdateResponse);
});
// Overwrite system_config.yaml with the current defaults (see AgentConfigService.resetConfig
// for the exact semantics); same authorization as PUT, and responds like GET/PUT with the
// fresh config so the client can refresh in place.
app.post("/reset", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.access.requireProjectAccess(c.var.user.userId, projectId);
await deps.agentConfigService.resetConfig(projectId, agentId);
// The defaults carry no `hooks` section, so a reset can switch hooks back on; cached
// runtimes are rebuilt for the same reason a switch write rebuilds them (see the PUT
// above).
deps.manager.invalidateAgentRuntimes(projectId, agentId);
const view = await deps.agentConfigService.getConfig(projectId, agentId);
return c.json({
...view,
activeSessionCount: deps.manager.activeCountForAgent(projectId, agentId),
} satisfies AgentConfigResponse);
});
return app;
}