Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: keep chat available during connector discovery outages

Krishna Dobhalc5c8287f2902
diff --git a/server/connector-proxy.test.ts b/server/connector-proxy.test.ts
index e77fa40a56..2aeac67890 100644
--- a/server/connector-proxy.test.ts
+++ b/server/connector-proxy.test.ts
@@ -406,17 +406,82 @@ describe("connector MCP bridge", () => {
expect(reply.result.tools).toEqual([{ name: "COMPOSIO_MANAGE_CONNECTIONS" }]);
});
- it("returns a JSON-RPC error, not a tools result, when a non-call relay fails", async () => {
+ it("keeps the MCP server available when connected apps are not configured", async () => {
const lines = start({});
child!.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 3, method: "tools/list", params: {} })}\n`);
const reply = await nextJson(lines);
expect(reply).toEqual({
jsonrpc: "2.0",
id: 3,
- error: { code: -32000, message: "connected apps are unavailable" },
+ result: { tools: [] },
});
});
+ it.each(["http", "rpc", "malformed", "disconnected"])("survives a cold-start %s outage and discovers tools after recovery", async (failure) => {
+ let offline = true;
+ let listings = 0;
+ let executions = 0;
+ const upstream = await listen((request, response) => {
+ let body = "";
+ request.on("data", (chunk) => { body += chunk; });
+ request.on("end", () => {
+ const rpc = JSON.parse(body);
+ if (rpc.method === "tools/call") {
+ executions += 1;
+ response.writeHead(500);
+ response.end();
+ return;
+ }
+ if (rpc.method !== "tools/list") {
+ response.end(JSON.stringify({ jsonrpc: "2.0", id: rpc.id, result: {} }));
+ return;
+ }
+ listings += 1;
+ if (offline) {
+ if (failure === "disconnected") return request.socket.destroy();
+ response.writeHead(failure === "http" ? 500 : 200);
+ response.end(failure === "rpc"
+ ? JSON.stringify({ jsonrpc: "2.0", id: rpc.id, error: { code: -32603, message: "Internal error" } })
+ : "unavailable");
+ return;
+ }
+ response.end(JSON.stringify({ jsonrpc: "2.0", id: rpc.id, result: { tools: [{ name: "GMAIL_FETCH_EMAILS" }] } }));
+ });
+ });
+ const lines = start({ OMB_CONNECTOR_UPSTREAM_URL: upstream });
+ const ask = async (id: number, method: string, params = {}) => {
+ child!.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
+ return nextJson(lines);
+ };
+ expect((await ask(1, "initialize")).result.serverInfo.name).toBe("openmausbot-connectors");
+ expect(await ask(2, "tools/list")).toEqual({ jsonrpc: "2.0", id: 2, result: { tools: [] } });
+ expect(listings).toBe(2);
+ const failedCall = await ask(3, "tools/call", { name: "GMAIL_FETCH_EMAILS", arguments: {} });
+ expect(failedCall.result.isError).toBe(true);
+ expect(executions).toBe(1);
+ offline = false;
+ expect((await ask(4, "tools/list")).result.tools).toEqual([{ name: "GMAIL_FETCH_EMAILS" }]);
+ });
+
+ it("bounds discovery when the connector service stops responding", async () => {
+ let listings = 0;
+ const upstream = await listen((request) => {
+ listings += 1;
+ request.resume();
+ });
+ const lines = start({ OMB_CONNECTOR_UPSTREAM_URL: upstream });
+ child!.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 5, method: "tools/list" })}\n`);
+ expect(await nextJson(lines)).toEqual({ jsonrpc: "2.0", id: 5, result: { tools: [] } });
+ expect(listings).toBe(2);
+ });
+
+ it("keeps locally defined app tools available during a discovery outage", async () => {
+ const lines = start({ OMB_CONNECTOR_UPSTREAM_URL: "", OMB_CONNECTOR_APP_TOOLS: JSON.stringify(["gmail.search_emails"]) });
+ child!.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 6, method: "tools/list" })}\n`);
+ const reply = await nextJson(lines);
+ expect(reply.result.tools.map((tool: { name: string }) => tool.name)).toEqual(["gmail_search_emails"]);
+ });
+
it("filters a JSON tools/list response down to the bot's grants", async () => {
const upstream = await listen((request, response) => {
request.resume();
diff --git a/server/connector-proxy.ts b/server/connector-proxy.ts
index 103d23cbb0..def3e6a922 100644
--- a/server/connector-proxy.ts
+++ b/server/connector-proxy.ts
@@ -37,6 +37,7 @@ const MAX_RESPONSE_BYTES = 20 * 1024 * 1024;
const INITIALIZE_RELAY_TIMEOUT_MS = 1_000;
const RELAY_TIMEOUT_MS = 10 * 60_000;
const TOOLS_LIST_RETRY_MS = 500;
+const TOOLS_LIST_RELAY_TIMEOUT_MS = 5_000;
function parsedHeaders(): Record<string, string> {
try {
@@ -255,6 +256,19 @@ function fittedList(frame: Json): Json {
return { ...frame, result: capToolListResult(fitToolListResult(frame.result, SCHEMA_PROFILE, log), TOOL_LIMIT, log) };
}
+async function listTools(message: Json): Promise<Json> {
+ for (let attempt = 0; attempt < 2; attempt += 1) {
+ if (attempt) await delay(TOOLS_LIST_RETRY_MS);
+ try {
+ const response = await relay(message, TOOLS_LIST_RELAY_TIMEOUT_MS);
+ if (response && !response.error && Array.isArray((response.result as Json | undefined)?.tools)) return response;
+ } catch {
+ }
+ }
+ process.stderr.write("connector-proxy: Connected apps are temporarily unavailable; start a new session to retry discovery.\n");
+ return { jsonrpc: "2.0", id: message.id, result: { tools: [] } };
+}
+
async function handle(received: Json): Promise<void> {
let message = received;
const id = message.id;
@@ -315,9 +329,8 @@ async function handle(received: Json): Promise<void> {
}
}
try {
- // The engine lists tools once per session, so one failed listing would leave the whole session without them.
const response = method === "tools/list"
- ? await relay(message).catch(() => delay(TOOLS_LIST_RETRY_MS).then(() => relay(message)))
+ ? await listTools(message)
: await relay(message);
if (response && id !== undefined) {
const name = String((message.params as Json | undefined)?.name ?? "");