UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

112 lines • 6.24 kB
// Tier-C proxy mode (todo 38): per-server OPT-IN (`exposure:"proxy"`). // // The whole server catalog hides behind ONE always-active `mcp_<server>` // gateway tool with three ops: search (BM25 over this server's catalog), // describe (schema + description for one tool), call (tool + args as a JSON // STRING). The JSON-string args deliberately trade provider strict-mode // validation for a ~200-token footprint (SPEC §5: -27.3pp GSM8K structured // output under proxy) — which is why auto NEVER selects this mode. Errors are // returned as guiding text (never thrown for user mistakes) so the model can // self-correct; unknown tool names get nearest-match suggestions. import { Text } from "@earendil-works/pi-tui"; import { Type } from "typebox"; import { registerDispatchIdentity } from "../../permission-system/dispatch-metadata.js"; import { buildBm25Index } from "../../tool-search/engine/bm25.js"; import { deriveMcpRegistrationId } from "../../tool-search/engine/marker.js"; import { executeMcpCatalogEntry, mapMcpCatalogNames } from "./register.js"; const ParamsSchema = Type.Object({ op: Type.Union([Type.Literal("search"), Type.Literal("describe"), Type.Literal("call")], { description: "search: rank tools by capability; describe: full schema for one tool; call: invoke a tool.", }), query: Type.Optional(Type.String({ description: "search: capability description to rank tools by." })), tool: Type.Optional(Type.String({ description: "describe/call: the server-side tool name." })), args: Type.Optional(Type.String({ description: 'call: tool arguments as a JSON object STRING, e.g. "{\\"path\\": \\"a.txt\\"}".', })), }); export function createMcpProxyTool(server, entries) { const named = mapMcpCatalogNames(entries); const byTool = new Map(named.map(({ entry }) => [entry.tool, entry])); const index = buildBm25Index(named.map(({ entry, name }) => ({ name, label: entry.tool, aliases: [entry.tool], description: entry.description, keywords: [], source: "mcp", group: server, ownerLabel: server, registrationId: deriveMcpRegistrationId(server, entry.tool), }))); const name = named[0]?.name.split("_").slice(0, 2).join("_") ?? `mcp_${server}`; const parameters = Type.Object(ParamsSchema.properties); registerDispatchIdentity(parameters, (input) => { if (input.op !== "call" || typeof input.tool !== "string") return undefined; const entry = byTool.get(input.tool); return entry?.invocation?.identity(entry); }); return { name, label: `MCP proxy for ${server}`, description: `Gateway to the '${server}' MCP server (${entries.length} tools). Use op:"search" with a query to find tools, op:"describe" with a tool name for its schema, then op:"call" with tool + args (args is a JSON object STRING: no strict validation, so match the described schema exactly).`, promptSnippet: `Proxy for the ${server} MCP server: search -> describe -> call (args as a JSON string).`, parameters, executionMode: "parallel", async execute(toolCallId, params, signal, onUpdate, context) { if (params.op === "search") { const matches = index.search(params.query ?? "", 10, {}); const body = matches.length === 0 ? `No '${server}' tools matched. Try broader keywords or op:"describe" with an exact tool name.` : matches .map((match) => `- ${match.doc.label} - ${match.doc.description ?? "(no description)"}`) .join("\n"); return textResult(server, `Tools on '${server}':\n${body}`); } const entry = params.tool === undefined ? undefined : byTool.get(params.tool); if (entry === undefined) { return textResult(server, unknownToolText(server, params.tool, index)); } if (params.op === "describe") { return textResult(server, `${entry.tool}: ${entry.description ?? "(no description)"}\nInput schema (JSON Schema):\n${JSON.stringify(entry.schema ?? {}, null, 2)}`); } let args; try { const parsed = JSON.parse(params.args ?? "{}"); if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { throw new Error("args must encode a JSON object"); } args = parsed; } catch (error) { return textResult(server, `Invalid args for '${entry.tool}': ${error instanceof Error ? error.message : String(error)}. Pass args as a JSON object STRING matching the schema from op:"describe", e.g. {"op":"call","tool":"${entry.tool}","args":"{\\"key\\": \\"value\\"}"}.`); } return await executeMcpCatalogEntry(entry, args, signal, onUpdate, { toolCallId, toolName: name, input: params, context, }); }, renderCall(args, theme) { const detail = args.op === "search" ? (args.query ?? "") : (args.tool ?? ""); return new Text(theme.fg("toolTitle", theme.bold(`${name} ${args.op} ${detail}`.trim())), 0, 0); }, renderResult(result, options, theme) { const title = options.isPartial ? `${name}: running` : `${name}: ${result.details?.preview ?? "done"}`; return new Text(theme.fg("toolOutput", title), 0, 0); }, }; } function textResult(server, text) { return { content: [{ type: "text", text }], details: { preview: firstLine(text), server, tool: "proxy" } }; } function unknownToolText(server, tool, index) { const nearest = tool === undefined ? [] : index.search(tool, 3, {}); const hint = nearest.length === 0 ? "" : ` Nearest matches: ${nearest.map((match) => match.doc.label).join(", ")}.`; return `Unknown tool '${tool ?? "(missing)"}' on '${server}'.${hint} Use op:"search" to list tools.`; } function firstLine(text) { return text.split("\n", 1)[0].slice(0, 80); } //# sourceMappingURL=proxy.js.map