openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
49 lines (48 loc) • 1.75 kB
JavaScript
import { Jn as string, Un as record, sr as unknown } from "./schemas-Cy6abvsu.mjs";
import { n as captureMcpClientElicitation } from "./mcp-client-elicitation-Cw8LWMma.mjs";
import { ElicitResultSchema } from "@modelcontextprotocol/core";
import { ProtocolError, ProtocolErrorCode } from "@modelcontextprotocol/client";
//#region src/agents/mcp-client-elicitation.runtime.ts
/** The SDK transport callback does not inherit the calling tool's async scope. */
function bindMcpClientElicitation(e) {
const t = /* @__PURE__ */ new Set();
for (const n of ["elicitation/create", "openai/elicitation/create"]) {
const handle = async (e, r) => {
const i = t.size === 1 ? t.values().next().value : void 0;
if (!i?.handler) throw new ProtocolError(ProtocolErrorCode.InvalidRequest, "MCP elicitation has no unambiguous active requester");
const a = AbortSignal.any([i.signal, r.mcpReq.signal]);
a.throwIfAborted();
const o = i.holdForHumanInput?.();
try {
const o = await i.handler({
method: n,
requestId: r.mcpReq.id,
params: e,
signal: a
});
a.throwIfAborted();
if (!t.has(i)) throw new ProtocolError(ProtocolErrorCode.InvalidRequest, "MCP elicitation requester expired");
return o;
} finally {
o?.();
}
};
if (n === "elicitation/create") e.setRequestHandler(n, async (e, t) => ElicitResultSchema.parse(await handle(e.params, t)));
else e.setRequestHandler(n, { params: record(string(), unknown()) }, handle);
}
return async (e, n, r) => {
const i = {
handler: captureMcpClientElicitation()?.handle,
signal: e,
holdForHumanInput: r
};
t.add(i);
try {
return await n();
} finally {
t.delete(i);
}
};
}
//#endregion
export { bindMcpClientElicitation };