MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / runMcpHttpBridge

Function runMcpHttpBridge

apps/cli/src/main.ts:1359–1439  ·  view source on GitHub ↗
(input: {
  readonly manifest: ExecutorLocalServerManifest;
  readonly elicitationMode: "browser" | "model";
})

Source from the content-addressed store, hash-verified

1357 * when stdin closes or the daemon connection drops; close is best-effort.
1358 */
1359const runMcpHttpBridge = async (input: {
1360 readonly manifest: ExecutorLocalServerManifest;
1361 readonly elicitationMode: "browser" | "model";
1362}): Promise<void> => {
1363 const stdio = new StdioServerTransport();
1364 const authorization = getExecutorServerAuthorizationHeader(input.manifest.connection);
1365 const http = new StreamableHTTPClientTransport(
1366 mcpUrlForActiveLocalServer(input.manifest.connection, input.elicitationMode),
1367 authorization ? { requestInit: { headers: { Authorization: authorization } } } : undefined,
1368 );
1369
1370 let finished = false;
1371 let closing = false;
1372 let closePromise: Promise<void> | null = null;
1373 let resolveExit: () => void = () => {};
1374 const waitForExit = new Promise<void>((resolve) => {
1375 resolveExit = resolve;
1376 });
1377
1378 const finish = () => {
1379 if (finished) return;
1380 finished = true;
1381 process.off("SIGINT", shutdown);
1382 process.off("SIGTERM", shutdown);
1383 process.stdin.off("end", shutdown);
1384 process.stdin.off("close", shutdown);
1385 resolveExit();
1386 };
1387
1388 const closeBoth = (): Promise<void> => {
1389 if (!closePromise) {
1390 closing = true;
1391 closePromise = Promise.allSettled([stdio.close(), http.close()]).then(() => undefined);
1392 }
1393 return closePromise;
1394 };
1395
1396 function shutdown() {
1397 finish();
1398 void closeBoth();
1399 }
1400
1401 const isAbortDuringClose = (error: Error): boolean =>
1402 error.name === "AbortError" || error.message.toLowerCase().includes("aborted");
1403
1404 const reportError = (context: string, cause: unknown) => {
1405 const error = toError(cause);
1406 if (closing && isAbortDuringClose(error)) return;
1407 console.error(`Executor MCP bridge ${context}: ${error.message}`);
1408 };
1409
1410 const forwardMessage =
1411 (send: (message: JSONRPCMessage) => Promise<void>, context: string) =>
1412 (message: JSONRPCMessage) => {
1413 void send(message).then(undefined, (cause: unknown) => {
1414 reportError(context, cause);
1415 shutdown();
1416 });

Callers 1

runStdioMcpSessionFunction · 0.85

Calls 8

reportErrorFunction · 0.85
forwardMessageFunction · 0.85
closeBothFunction · 0.85
finishFunction · 0.70
sendMethod · 0.65
startMethod · 0.65

Tested by

no test coverage detected