UNPKG

@code-yeongyu/senpi

Version:

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

141 lines • 6.78 kB
import { Type } from "typebox"; import { Check } from "typebox/value"; import { authorizeToolDispatch, hasDispatchAuthorizer } from "../permission-system/dispatch.js"; import { getDispatchIdentity } from "../permission-system/dispatch-metadata.js"; import { TimeoutError } from "./errors.js"; import { compileModernSchema, McpToolSchemaError } from "./modern/tool-schema.js"; import { safeTimer } from "./wrap.js"; export function createMcpInvocationResolver(scope) { const identity = (entry) => ({ owner: scope.owner, operation: JSON.stringify([entry.server, entry.tool]), metadata: JSON.stringify([ scope.configuration, entry.server, entry.tool, entry.schema, entry.outputSchema, entry.modern, entry.description, entry.annotations, ]), }); return { identity, async resolve(offered, args, invocation, signal) { const refuse = (kind, message, current) => ({ kind: "refused", error: { kind, message, server: offered.server, tool: offered.tool, currentDefinition: current?.schema, }, }); signal?.throwIfAborted(); if (!scope.isCurrent()) return refuse("unavailable", "MCP session or server configuration was replaced."); const ctx = invocation?.context; if (scope.contextRequired && (ctx === undefined || (scope.sessionManager !== undefined && ctx.sessionManager !== scope.sessionManager && scope.servesSession?.(ctx.sessionManager) !== true))) { return refuse("unavailable", "MCP invocation does not belong to this session."); } const deadline = new AbortController(); const timeout = safeTimer("invocation.catalog.deadline", offered.requestTimeoutMs, () => deadline.abort(new TimeoutError("MCP metadata was not ready before the request timeout.", { phase: "catalog", serverName: offered.server, })), scope.sink); const readySignal = signal === undefined ? deadline.signal : AbortSignal.any([signal, deadline.signal]); const ready = scope.ready(); let abort = () => { }; const interrupted = new Promise((_resolve, reject) => { abort = () => reject(readySignal.reason); readySignal.addEventListener("abort", abort, { once: true }); if (readySignal.aborted) abort(); }); try { await Promise.race([ready, interrupted]); } catch (error) { readySignal.throwIfAborted(); if (!scope.isCurrent()) return refuse("unavailable", "MCP session or server configuration was replaced."); throw error; } finally { clearTimeout(timeout); readySignal.removeEventListener("abort", abort); } signal?.throwIfAborted(); if (!scope.isCurrent()) return refuse("unavailable", "MCP session or server configuration was replaced."); const current = scope.current(offered.tool); if (current === undefined) return refuse("unavailable", "MCP tool is no longer available."); // Validate the current raw schema, never the coercing preflight schema. let validate; try { validate = current.modern ? compileModernSchema(current.schema) : (value) => Check(Type.Unsafe(current.schema), value); } catch (error) { if (!(error instanceof McpToolSchemaError)) throw error; return refuse("schema_changed", `${error.code}: ${error.message}`, current); } if (!validate(args)) { return refuse("schema_changed", "Arguments do not match the current MCP tool schema.", current); } const preparedIdentity = identity(current); let permissionFence; let operationFence; if (ctx !== undefined && invocation !== undefined && (scope.contextRequired || ctx.sessionManager !== undefined)) { operationFence = () => { const registered = ctx.tools.find((tool) => tool.name === invocation.toolName); const registeredIdentity = registered === undefined ? undefined : getDispatchIdentity(registered.parameters, invocation.input); return (registeredIdentity?.owner === preparedIdentity.owner && registeredIdentity.operation === preparedIdentity.operation); }; if (!operationFence()) { return refuse("unavailable", "MCP invocation name no longer identifies this operation.", current); } if (hasDispatchAuthorizer(ctx.sessionManager)) { try { permissionFence = await authorizeToolDispatch(ctx.sessionManager, { ...invocation, identity: preparedIdentity }, ctx, signal); } catch (error) { signal?.throwIfAborted(); return refuse("permission_denied", error instanceof Error ? error.message : String(error), current); } } else if (ctx.loadedExtensionPaths === undefined || ctx.loadedExtensionPaths.some((path) => path.includes("permission-system"))) { return refuse("permission_denied", "Permission authorizer is unavailable.", current); } } return { kind: "ready", entry: current, isCurrent: () => { if (signal?.aborted || !scope.isCurrent()) return false; const latest = scope.current(offered.tool); return (latest !== undefined && identity(latest).metadata === preparedIdentity.metadata && validate(args) && (operationFence === undefined || operationFence()) && (permissionFence === undefined || permissionFence())); }, }; }, }; } //# sourceMappingURL=invocation.js.map