@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
193 lines • 9.36 kB
JavaScript
import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
import { resolveAuthMode } from "./auth/context.js";
import { updateMcpServerConfig } from "./config-edit.js";
import { collectAllPages } from "./expose/pagination.js";
import { McpManagerView } from "./manager-view.js";
import { buildMcpStatusRows, formatMcpStatus } from "./status.js";
const EXPOSURES = ["auto", "direct", "search", "proxy"];
export async function showMcpManager(ctx, pi, service, run) {
const authAction = await ctx.ui.custom((tui, theme, keybindings, done) => {
const view = new McpManagerView(tui, theme, keybindings);
void manage(view, ctx, pi, service, run).then(done, (error) => {
done(undefined);
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
});
return view;
});
// The existing auth flow retains notices in the transcript; do not hide it behind a custom view.
if (authAction)
await run(authAction.command, authAction.name, ctx);
}
async function manage(view, ctx, pi, service, run) {
const subscribe = (listener) => {
let disconnect = [];
const bind = () => {
for (const stop of disconnect)
stop();
disconnect = service.getServerSnapshots().flatMap((snapshot) => {
const connection = service.getConnection(snapshot.name);
return connection ? [connection.onStateChange(listener)] : [];
});
};
bind();
const unregister = service.onMcpRegistrationChanged(() => {
bind();
listener();
});
return () => {
unregister();
for (const stop of disconnect)
stop();
};
};
for (;;) {
const name = await view.menu(async () => {
const rows = await buildMcpStatusRows(service.getServerSnapshots(), (server) => service.getServerExposureStatus(server));
return {
title: "MCP servers",
items: rows.map(({ snapshot, exposure }) => {
const resources = service.getMcpResourceServers(pi).find((server) => server.server === snapshot.name);
return {
value: snapshot.name,
label: snapshot.name,
description: [
snapshot.configState === "enabled" ? snapshot.lifecycleState : snapshot.configState,
`${exposure.toolCount ?? "?"} tools`,
...(resources ? [`${resources.resources.length} resources`] : []),
exposure.mode ?? service.getAuthTarget(snapshot.name)?.config.exposure ?? "auto",
snapshot.source ?? "unknown",
].join(" · "),
};
}),
empty: "No MCP servers configured. Add one with /mcp add <name> <command...|url>.",
confirmLabel: "manage",
cancelLabel: "close",
};
}, subscribe);
if (name === undefined)
return undefined;
let message;
for (;;) {
const action = await view.menu(() => {
const snapshot = service.getServerSnapshots().find((server) => server.name === name);
const config = service.getAuthTarget(name)?.config;
return {
title: `MCP server ${name}`,
details: [snapshot?.lastError, message].filter(Boolean).join("\n") || undefined,
items: snapshot ? serverActions(snapshot, config, service.getServerAuthStatus(name) === "oAuth") : [],
empty: "This server is no longer configured.",
confirmLabel: "select",
cancelLabel: "back",
};
}, subscribe);
if (action === undefined)
break;
const snapshot = service.getServerSnapshots().find((server) => server.name === name);
if (!snapshot)
break;
if (action === "auth" || action === "logout")
return { command: action, name };
message = undefined;
try {
if (action === "status") {
const rows = await buildMcpStatusRows([snapshot], (server) => service.getServerExposureStatus(server));
await showText(view, `Details of ${name}`, formatMcpStatus("", rows).trim());
}
else if (action === "logs") {
await showText(view, `Logs of ${name}`, service.getLogLines(name, 20).join("\n") || "(empty)");
}
else if (action === "tools") {
const connection = service.getConnection(name);
if (connection?.state !== "connected")
continue;
const tools = await collectAllPages((cursor) => connection.client.listTools(cursor === undefined ? {} : { cursor }, { timeout: 2000 }));
await view.menu(() => ({
title: `Tools of ${name}`,
items: tools.items.map((tool) => ({
value: tool.name,
label: tool.name,
description: tool.description,
})),
empty: "The server offers no tools.",
confirmLabel: "back",
cancelLabel: "back",
}));
}
else if (action === "enable" || action === "disable" || action === "exposure") {
let exposure;
if (action === "exposure") {
const choice = await view.menu(() => ({
title: `Exposure of ${name}`,
items: EXPOSURES.map((value) => ({ value, label: value })),
selected: service.getAuthTarget(name)?.config.exposure,
confirmLabel: "save",
cancelLabel: "back",
}));
exposure = EXPOSURES.find((value) => value === choice);
if (exposure === undefined)
continue;
}
if (!isWritable(snapshot) || !snapshot.sourcePath)
continue;
const patch = exposure === undefined ? { enabled: action === "enable" } : { exposure };
if (!updateMcpServerConfig(snapshot.sourcePath, name, patch)) {
message = "This server is no longer in its configuration file.";
continue;
}
view.status(`MCP server ${name}`, "Applying configuration...");
await service.attachSession({ type: "session_start", reason: "reload" }, ctx, pi);
message = "Configuration saved.";
}
else if (action === "test" || action === "reconnect") {
view.status(`MCP server ${name}`, action === "test" ? "Testing..." : "Reconnecting...");
const notices = [];
await run(action, name, ctx, (text) => notices.push(text));
message = notices.join("\n");
}
}
catch (error) {
message = error instanceof Error ? error.message : String(error);
}
}
}
}
function isWritable(snapshot) {
return snapshot.configState !== "untrusted" && (snapshot.source === "global" || snapshot.source === "project");
}
function serverActions(snapshot, config, loggedIn) {
const items = [
{ value: "status", label: "Details" },
{ value: "logs", label: "Logs" },
];
if (snapshot.configState === "enabled") {
if (snapshot.lifecycleState === "connected")
items.push({ value: "tools", label: "Tools" });
items.push({ value: "test", label: "Test connection" }, { value: "reconnect", label: "Reconnect" });
if (config && resolveAuthMode(config) === "oauth") {
items.push(loggedIn
? { value: "logout", label: "Sign out", description: "deletes stored credentials" }
: { value: "auth", label: "Sign in", description: "closes this view and opens authorization" });
}
}
if (isWritable(snapshot)) {
items.push({
value: snapshot.configState === "disabled" ? "enable" : "disable",
label: snapshot.configState === "disabled" ? "Enable" : "Disable",
description: `saved to ${snapshot.source} mcp.json`,
});
items.push({ value: "exposure", label: "Exposure", description: config?.exposure });
}
return items;
}
async function showText(view, title, text) {
await view.menu(() => ({
title,
items: wrapTextWithAnsi(text, Math.max(1, view.columns - 4)).map((label, index) => ({
value: String(index),
label,
})),
confirmLabel: "back",
cancelLabel: "back",
}));
}
//# sourceMappingURL=manager.js.map