@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
74 lines • 4.03 kB
JavaScript
import { redactResolvedReferenceData, redactResolvedReferenceValues } from "../../../../config-reference-redaction.js";
import { ToolExecError } from "../errors.js";
import { ensureMcpToolCallConnection, withMcpRetriableFailedSendRetry, withMcpSessionExpiryRetry } from "../health.js";
import { runMcpConnectionLifecycleCall } from "../idle.js";
import { beginScopedMcpCall, mcpRequestOptions } from "../request-origin.js";
import { McpMrtrError } from "../modern/errors.js";
import { callMcpToolRequest } from "../tool-client.js";
export async function callMcpTool(entry, args, signal, onUpdate, label, invocation) {
try {
return await runMcpConnectionLifecycleCall(entry.connection, () => withMcpSessionExpiryRetry(entry.connection, async () => {
if (entry.invocation === undefined) {
await ensureMcpToolCallConnection(entry.connection, entry.ensureFresh);
await entry.ensureConnected?.();
}
const prepare = async () => {
for (;;) {
const prepared = await entry.invocation?.resolve(entry, args, invocation, signal);
if (prepared === undefined || prepared.kind === "refused" || prepared.isCurrent()) {
return prepared;
}
}
};
// Readiness and its bounded renewal are not failed remote sends.
// Only a call attempt below enters the existing failed-send retry.
let next = await prepare();
return await withMcpRetriableFailedSendRetry(entry.connection, async () => {
for (;;) {
const prepared = next ?? (await prepare());
next = undefined;
if (prepared?.kind === "refused")
return prepared;
if (prepared !== undefined && !prepared.isCurrent())
continue;
const current = prepared?.entry ?? entry;
const origin = current.requestOrigin?.(invocation?.context);
// A call of one of several scopes keeps a legacy form it causes in its own scope's UI (S16c).
const endScopedCall = origin === undefined ? undefined : beginScopedMcpCall(current.connection.client, origin, signal);
try {
const result = await callMcpToolRequest(current.connection.client, { name: current.tool, arguments: args }, current.outputSchema, mcpRequestOptions(origin, {
onprogress: (progress) => {
onUpdate?.(redactResolvedReferenceData({
content: [{ type: "text", text: formatProgress(label, progress) }],
details: { progress, server: entry.server, tool: entry.tool },
}));
},
signal,
timeout: current.requestTimeoutMs,
}));
return { kind: "called", entry: current, result };
}
finally {
endScopedCall?.();
}
}
});
}));
}
catch (error) {
if (error instanceof McpMrtrError)
throw error;
const label = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
throw new ToolExecError(redactResolvedReferenceValues(`ToolExecError: ${label}`), {
cause: error,
phase: "call",
serverName: entry.server,
});
}
}
function formatProgress(label, progress) {
const total = progress.total === undefined ? "" : `/${progress.total}`;
const message = progress.message === undefined ? "" : ` ${progress.message}`;
return `${label} progress ${progress.progress}${total}${message}`.trim();
}
//# sourceMappingURL=call.js.map