@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
84 lines • 4.3 kB
JavaScript
// MCP prompts → slash commands (todo 40).
//
// Every prompt a connected server lists becomes a `/mcp:<server>:<prompt>`
// senpi command. Invoking it collects the prompt's declared arguments through
// ctx.ui.input (required arguments re-prompt once, then abort with a notice;
// optional arguments may be left empty), calls prompts/get, and injects the
// flattened returned messages into the input editor (senpi command semantics:
// the user reviews and submits). prompts_list_changed rides the existing
// tools-changed refresh, which re-runs registration; command registration is
// last-wins, and prompts removed server-side keep a stale command until the
// next session — invoking one surfaces the server error as a notice.
import { createMcpLogger } from "./log.js";
import { mcpRequestOptions } from "./request-origin.js";
import { ensureMcpToolCallConnection } from "./health.js";
const registeredCommandNames = new Set();
/** Test seam: forget which commands were registered (module-level last-wins). */
export function resetMcpPromptCommandsForTests() {
registeredCommandNames.clear();
}
export function registerMcpPromptCommands(serviceOrPi, piOrServers, serversArg) {
const service = serversArg === undefined ? undefined : serviceOrPi;
const pi = (serversArg === undefined ? serviceOrPi : piOrServers);
const servers = (serversArg === undefined ? piOrServers : serversArg);
const added = [];
for (const server of servers) {
for (const prompt of server.prompts) {
const commandName = `mcp:${server.server}:${prompt.name}`;
if (service?.isMcpPromptCommandRegistered(commandName) ?? registeredCommandNames.has(commandName))
continue;
if (service)
service.markMcpPromptCommandRegistered(commandName);
else
registeredCommandNames.add(commandName);
added.push(commandName);
pi.registerCommand(commandName, {
description: prompt.description ?? `MCP prompt ${prompt.name} from ${server.server}`,
handler: async (_args, ctx) => {
try {
server.bindPresentation?.(ctx.ui);
const collected = await collectPromptArguments(prompt.arguments ?? [], ctx);
if (collected === undefined)
return;
if (server.requestOrigin)
await ensureMcpToolCallConnection(server.connection, server.ensureFresh);
const result = await server.connection.client.getPrompt({ arguments: collected, name: prompt.name }, mcpRequestOptions(server.requestOrigin?.(), { timeout: server.requestTimeoutMs }));
ctx.ui.setEditorText(flattenPromptMessages(result.messages));
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
createMcpLogger(server.server).warn(`prompt ${prompt.name} failed: ${message}`);
ctx.ui.notify(`MCP prompt ${prompt.name} failed: ${message}`, "error");
}
},
});
}
}
return added;
}
async function collectPromptArguments(declared, ctx) {
const collected = {};
for (const argument of declared) {
const title = `${argument.name}${argument.required ? "" : " (optional)"}`;
const value = await ctx.ui.input(title, argument.description);
if (value !== undefined && value.length > 0) {
collected[argument.name] = value;
continue;
}
if (argument.required) {
ctx.ui.notify(`MCP prompt cancelled: required argument '${argument.name}' not provided.`, "warning");
return undefined;
}
}
return collected;
}
function flattenPromptMessages(messages) {
return messages
.map((message) => {
const content = message.content;
const text = typeof content?.text === "string" ? content.text : JSON.stringify(message.content);
return messages.length > 1 ? `${message.role}: ${text}` : text;
})
.join("\n\n");
}
//# sourceMappingURL=prompts.js.map