From 743df3ee0669d3f98ec158d3e6d09d16c4b5f5c9 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:49:12 +0700 Subject: [PATCH 1/9] fix: allow longer timeout for staging MCP health probes --- workers/control-api/worker.mjs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/workers/control-api/worker.mjs b/workers/control-api/worker.mjs index 069c6b2..2a8f44d 100644 --- a/workers/control-api/worker.mjs +++ b/workers/control-api/worker.mjs @@ -232,7 +232,7 @@ async function probeMcpServer() { }), cache: "no-store", redirect: "error", - signal: AbortSignal.timeout(4000) + signal: AbortSignal.timeout(12000) }); if (!initializeResponse.ok) { return { state: "unavailable", description: "MCP initialize returned HTTP " + initializeResponse.status + ".", checkedAt, latencyMs: Date.now() - startedAt }; @@ -248,7 +248,7 @@ async function probeMcpServer() { body: JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }), cache: "no-store", redirect: "error", - signal: AbortSignal.timeout(4000) + signal: AbortSignal.timeout(12000) }); if (notificationResponse.status !== 202) { return { state: "unknown", description: "MCP initialized notification was not accepted.", checkedAt, latencyMs: Date.now() - startedAt }; @@ -260,7 +260,7 @@ async function probeMcpServer() { body: JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list" }), cache: "no-store", redirect: "error", - signal: AbortSignal.timeout(4000) + signal: AbortSignal.timeout(12000) }); if (!toolsResponse.ok) { return { state: "unavailable", description: "MCP tools/list returned HTTP " + toolsResponse.status + ".", checkedAt, latencyMs: Date.now() - startedAt }; @@ -277,7 +277,8 @@ async function probeMcpServer() { latencyMs: Date.now() - startedAt, toolCount: toolNames.length }; - } catch { + } catch (error) { + console.warn(JSON.stringify({ event: "control_api.mcp_probe_failed", errorType: error?.name || "Error" })); return { state: "unavailable", description: "MCP staging endpoint could not complete the protocol handshake.", From 9d4fcd87d5a096a0c0a3bb358346f5f968ec0124 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:51:57 +0700 Subject: [PATCH 2/9] debug: identify failing phase in staging MCP probe --- workers/control-api/worker.mjs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/workers/control-api/worker.mjs b/workers/control-api/worker.mjs index 2a8f44d..7505630 100644 --- a/workers/control-api/worker.mjs +++ b/workers/control-api/worker.mjs @@ -216,6 +216,7 @@ async function probeCliAuth() { async function probeMcpServer() { const startedAt = Date.now(); const checkedAt = new Date().toISOString(); + let phase = "initialize"; try { const initializeResponse = await fetch(MCP_ENDPOINT, { method: "POST", @@ -231,41 +232,45 @@ async function probeMcpServer() { } }), cache: "no-store", - redirect: "error", + redirect: "follow", signal: AbortSignal.timeout(12000) }); if (!initializeResponse.ok) { return { state: "unavailable", description: "MCP initialize returned HTTP " + initializeResponse.status + ".", checkedAt, latencyMs: Date.now() - startedAt }; } const initialized = await initializeResponse.json(); + phase = "validate-initialize"; if (initialized?.result?.protocolVersion !== "2025-03-26" || initialized?.result?.serverInfo?.name !== "ocodejs-control-center") { return { state: "unknown", description: "MCP initialize response did not match the expected protocol contract.", checkedAt, latencyMs: Date.now() - startedAt }; } + phase = "initialized-notification"; const notificationResponse = await fetch(MCP_ENDPOINT, { method: "POST", headers: { Accept: "application/json, text/event-stream", "Content-Type": "application/json", "MCP-Protocol-Version": "2025-03-26" }, body: JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }), cache: "no-store", - redirect: "error", + redirect: "follow", signal: AbortSignal.timeout(12000) }); if (notificationResponse.status !== 202) { return { state: "unknown", description: "MCP initialized notification was not accepted.", checkedAt, latencyMs: Date.now() - startedAt }; } + phase = "tools-list"; const toolsResponse = await fetch(MCP_ENDPOINT, { method: "POST", headers: { Accept: "application/json, text/event-stream", "Content-Type": "application/json", "MCP-Protocol-Version": "2025-03-26" }, body: JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list" }), cache: "no-store", - redirect: "error", + redirect: "follow", signal: AbortSignal.timeout(12000) }); if (!toolsResponse.ok) { return { state: "unavailable", description: "MCP tools/list returned HTTP " + toolsResponse.status + ".", checkedAt, latencyMs: Date.now() - startedAt }; } const listed = await toolsResponse.json(); + phase = "validate-tools-list"; const toolNames = (listed?.result?.tools || []).map((tool) => tool.name).sort(); if (JSON.stringify(toolNames) !== JSON.stringify(["get_agent_definition", "get_control_api_health", "get_provider_definition", "list_agents", "list_providers", "list_services"])) { return { state: "unknown", description: "MCP tools/list did not return the expected read-only tool allowlist.", checkedAt, latencyMs: Date.now() - startedAt }; @@ -281,7 +286,7 @@ async function probeMcpServer() { console.warn(JSON.stringify({ event: "control_api.mcp_probe_failed", errorType: error?.name || "Error" })); return { state: "unavailable", - description: "MCP staging endpoint could not complete the protocol handshake.", + description: "MCP staging probe failed during " + phase + " (" + (error?.name || "Error") + ").", checkedAt, latencyMs: Date.now() - startedAt }; From 084b17c31e86de9a579c06231495b0c3cfdc9daa Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:54:45 +0700 Subject: [PATCH 3/9] fix: probe MCP through an internal Cloudflare service binding --- workers/control-api/worker.mjs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/workers/control-api/worker.mjs b/workers/control-api/worker.mjs index 7505630..8be90bb 100644 --- a/workers/control-api/worker.mjs +++ b/workers/control-api/worker.mjs @@ -213,12 +213,15 @@ async function probeCliAuth() { } } -async function probeMcpServer() { +async function probeMcpServer(env = {}) { const startedAt = Date.now(); const checkedAt = new Date().toISOString(); let phase = "initialize"; + const mcpFetch = env.MCP_SERVER && typeof env.MCP_SERVER.fetch === "function" + ? (init) => env.MCP_SERVER.fetch(new Request("https://mcp-server/mcp", init)) + : (init) => fetch(MCP_ENDPOINT, init); try { - const initializeResponse = await fetch(MCP_ENDPOINT, { + const initializeResponse = await mcpFetch({ method: "POST", headers: { Accept: "application/json, text/event-stream", "Content-Type": "application/json" }, body: JSON.stringify({ @@ -245,7 +248,7 @@ async function probeMcpServer() { } phase = "initialized-notification"; - const notificationResponse = await fetch(MCP_ENDPOINT, { + const notificationResponse = await mcpFetch({ method: "POST", headers: { Accept: "application/json, text/event-stream", "Content-Type": "application/json", "MCP-Protocol-Version": "2025-03-26" }, body: JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }), @@ -258,7 +261,7 @@ async function probeMcpServer() { } phase = "tools-list"; - const toolsResponse = await fetch(MCP_ENDPOINT, { + const toolsResponse = await mcpFetch({ method: "POST", headers: { Accept: "application/json, text/event-stream", "Content-Type": "application/json", "MCP-Protocol-Version": "2025-03-26" }, body: JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list" }), @@ -293,12 +296,12 @@ async function probeMcpServer() { } } -async function serviceSnapshot(service, checkedAt) { +async function serviceSnapshot(service, checkedAt, env = {}) { if (service.id === "cli-auth") { return { ...service, ...(await probeCliAuth()) }; } if (service.id === "mcp-server") { - return { ...service, ...(await probeMcpServer()) }; + return { ...service, ...(await probeMcpServer(env)) }; } return { ...service, @@ -389,7 +392,7 @@ async function handleRequest(request, env, url, headers, id) { if (url.pathname === `${API_PREFIX}/services`) { const checkedAt = new Date().toISOString(); - const services = await Promise.all(SERVICES.map((service) => serviceSnapshot(service, checkedAt))); + const services = await Promise.all(SERVICES.map((service) => serviceSnapshot(service, checkedAt, env))); return json({ services, count: services.length, @@ -415,7 +418,7 @@ async function handleRequest(request, env, url, headers, id) { } return json({ - service: await serviceSnapshot(service, new Date().toISOString()), + service: await serviceSnapshot(service, new Date().toISOString(), env), requestId: id }, 200, headers); } @@ -490,7 +493,7 @@ async function handleRequest(request, env, url, headers, id) { } if (url.pathname === API_PREFIX + "/mcp/connections") { - const health = await probeMcpServer(); + const health = await probeMcpServer(env); const connections = MCP_CONNECTIONS.map((connection) => ({ ...connection, ...health From 7ba050a8524375bc7dcf9e33e1ec4685c164fb6f Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:54:53 +0700 Subject: [PATCH 4/9] test: cover internal MCP service binding health probe --- workers/control-api/worker.test.mjs | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/workers/control-api/worker.test.mjs b/workers/control-api/worker.test.mjs index d32b25b..fdab8b8 100644 --- a/workers/control-api/worker.test.mjs +++ b/workers/control-api/worker.test.mjs @@ -441,3 +441,45 @@ test("runtime readiness explicitly keeps provider inference and agent execution assert.ok(body.checkedAt); assert.ok(body.requestId); }); + + +test("MCP probe uses the internal Cloudflare service binding when configured", async () => { + const methods = []; + const tools = [ + "get_control_api_health", + "get_provider_definition", + "get_agent_definition", + "list_agents", + "list_providers", + "list_services" + ]; + const MCP_SERVER = { + fetch: async (request) => { + assert.equal(new URL(request.url).pathname, "/mcp"); + const message = await request.json(); + methods.push(message.method); + if (message.method === "initialize") { + return new Response(JSON.stringify({ jsonrpc: "2.0", id: message.id, result: { + protocolVersion: "2025-03-26", + serverInfo: { name: "ocodejs-control-center" } + }}), { status: 200, headers: { "Content-Type": "application/json" } }); + } + if (message.method === "notifications/initialized") return new Response(null, { status: 202 }); + if (message.method === "tools/list") { + return new Response(JSON.stringify({ jsonrpc: "2.0", id: message.id, result: { + tools: tools.map((name) => ({ name })) + }}), { status: 200, headers: { "Content-Type": "application/json" } }); + } + throw new Error("Unexpected MCP method: " + message.method); + } + }; + const response = await worker.fetch( + new Request("https://api-staging.example.workers.dev/api/v1/services/mcp-server"), + { ...env, MCP_SERVER } + ); + const body = await response.json(); + assert.equal(response.status, 200); + assert.equal(body.service.state, "healthy"); + assert.equal(body.service.toolCount, 6); + assert.deepEqual(methods, ["initialize", "notifications/initialized", "tools/list"]); +}); From f424f6caeba510aa8ae748f4b3cf7a1657217d7a Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:54:59 +0700 Subject: [PATCH 5/9] docs: document internal MCP staging service binding --- workers/control-api/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/workers/control-api/README.md b/workers/control-api/README.md index b22b3a3..9b441ea 100644 --- a/workers/control-api/README.md +++ b/workers/control-api/README.md @@ -58,3 +58,14 @@ No production routes are attached. This Worker is isolated from existing OCodeJS ## Structured request telemetry Every request emits one JSON event to Cloudflare Workers Logs with `event`, `requestId`, HTTP method, normalized URL path, response status, duration in milliseconds and timestamp. The query string, request body, cookies, authorization headers and upstream payloads are deliberately excluded. Responses also return `X-Request-Id` and `Server-Timing` for correlation. This is log-based observability. The current branch additionally supports an optional Workers Analytics Engine binding named `OBSERVABILITY`, which writes bounded request dimensions and duration when attached. The binding is not configured on staging by this code change; without it, the Worker continues to operate using structured logs only. No public metrics query, alert evaluator, durable audit record store or role-based explorer is implemented yet. + + +## Cloudflare staging service binding + +The Control API staging Worker should use an internal Cloudflare service binding to the MCP Worker in the same Cloudflare account. Configure this binding only on the staging Worker: + +- Binding name: `MCP_SERVER` +- Binding type: `service` +- Service: `ocodejs-mcp-staging` + +The API uses this binding for health probes when present and falls back to the public `MCP_ENDPOINT` URL otherwise. The public endpoint remains in the service catalog for clients. Do not copy this binding into Production without a separate review. From 7fbbb716dbe264930149d379e7f07aa19bedce25 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:03:30 +0700 Subject: [PATCH 6/9] feat: probe Auth backend through internal staging service binding --- workers/control-api/worker.mjs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/workers/control-api/worker.mjs b/workers/control-api/worker.mjs index 8be90bb..c66e2c9 100644 --- a/workers/control-api/worker.mjs +++ b/workers/control-api/worker.mjs @@ -166,11 +166,15 @@ function errorResponse(code, message, status, id, headers) { }, status, headers); } -async function probeCliAuth() { +async function probeCliAuth(env = {}) { const startedAt = Date.now(); const checkedAt = new Date().toISOString(); + const viaServiceBinding = env.CLI_AUTH && typeof env.CLI_AUTH.fetch === "function"; + const authFetch = viaServiceBinding + ? (init) => env.CLI_AUTH.fetch(new Request("https://auth-internal/health", init)) + : (init) => fetch(CLI_AUTH_HEALTH_URL, init); try { - const response = await fetch(CLI_AUTH_HEALTH_URL, { + const response = await authFetch({ method: "GET", headers: { Accept: "application/json" }, cache: "no-store", @@ -198,7 +202,9 @@ async function probeCliAuth() { return { state: configured ? "healthy" : "degraded", description: configured - ? "Health endpoint responds and required GitHub OAuth/PAT bindings are present; end-to-end login is not verified." + ? (viaServiceBinding + ? "Internal Auth Worker health responds and required GitHub OAuth/PAT bindings are present; public auth hostname and end-to-end login remain unverified." + : "Health endpoint responds and required GitHub OAuth/PAT bindings are present; end-to-end login is not verified.") : "Health endpoint responds, but one or more GitHub OAuth/PAT bindings are missing.", checkedAt, latencyMs: Date.now() - startedAt @@ -298,7 +304,7 @@ async function probeMcpServer(env = {}) { async function serviceSnapshot(service, checkedAt, env = {}) { if (service.id === "cli-auth") { - return { ...service, ...(await probeCliAuth()) }; + return { ...service, ...(await probeCliAuth(env)) }; } if (service.id === "mcp-server") { return { ...service, ...(await probeMcpServer(env)) }; From 62086ee1fd02d479caf67f489c1a735fcc4c91b6 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:03:38 +0700 Subject: [PATCH 7/9] test: cover internal CLI Auth service binding health probe --- workers/control-api/worker.test.mjs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/workers/control-api/worker.test.mjs b/workers/control-api/worker.test.mjs index fdab8b8..6bac462 100644 --- a/workers/control-api/worker.test.mjs +++ b/workers/control-api/worker.test.mjs @@ -483,3 +483,26 @@ test("MCP probe uses the internal Cloudflare service binding when configured", a assert.equal(body.service.toolCount, 6); assert.deepEqual(methods, ["initialize", "notifications/initialized", "tools/list"]); }); + + +test("CLI Auth probe uses the internal Cloudflare service binding when configured", async () => { + const CLI_AUTH = { + fetch: async (request) => { + assert.equal(new URL(request.url).pathname, "/health"); + return new Response(JSON.stringify({ + ok: true, + service: "ocodejs-auth-worker", + patConfigured: true, + oauthConfigured: true + }), { status: 200, headers: { "Content-Type": "application/json" } }); + } + }; + const response = await worker.fetch( + new Request("https://api-staging.example.workers.dev/api/v1/services/cli-auth"), + { ...env, CLI_AUTH } + ); + const body = await response.json(); + assert.equal(response.status, 200); + assert.equal(body.service.state, "healthy"); + assert.match(body.service.description, /Internal Auth Worker health responds/); +}); From 8ba2097911de3730c0ccb62a2e01eda475ba682a Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:03:42 +0700 Subject: [PATCH 8/9] docs: document internal Auth staging service binding --- workers/control-api/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/workers/control-api/README.md b/workers/control-api/README.md index 9b441ea..58a17e3 100644 --- a/workers/control-api/README.md +++ b/workers/control-api/README.md @@ -69,3 +69,11 @@ The Control API staging Worker should use an internal Cloudflare service binding - Service: `ocodejs-mcp-staging` The API uses this binding for health probes when present and falls back to the public `MCP_ENDPOINT` URL otherwise. The public endpoint remains in the service catalog for clients. Do not copy this binding into Production without a separate review. + +The same staging Worker can also use an internal binding for the configured Auth backend in the Huynh Cloudflare account: + +- Binding name: `CLI_AUTH` +- Binding type: `service` +- Service: `ocodejs-auth` + +This reports the internal backend health only. It does not prove that the public `auth.ocodejs.dev` hostname routes to this Worker or that an OAuth login succeeds. Keep the binding staging-only until the canonical public Auth Worker/domain ownership is resolved. From 70480b9356d861ee5e85e1957f24d3f3ff27b7d4 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:05:14 +0700 Subject: [PATCH 9/9] fix: preserve canonical Auth hostname in internal service binding request --- workers/control-api/worker.mjs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/control-api/worker.mjs b/workers/control-api/worker.mjs index c66e2c9..c2335ad 100644 --- a/workers/control-api/worker.mjs +++ b/workers/control-api/worker.mjs @@ -171,7 +171,7 @@ async function probeCliAuth(env = {}) { const checkedAt = new Date().toISOString(); const viaServiceBinding = env.CLI_AUTH && typeof env.CLI_AUTH.fetch === "function"; const authFetch = viaServiceBinding - ? (init) => env.CLI_AUTH.fetch(new Request("https://auth-internal/health", init)) + ? (init) => env.CLI_AUTH.fetch(new Request(CLI_AUTH_HEALTH_URL, init)) : (init) => fetch(CLI_AUTH_HEALTH_URL, init); try { const response = await authFetch({ @@ -209,10 +209,11 @@ async function probeCliAuth(env = {}) { checkedAt, latencyMs: Date.now() - startedAt }; - } catch { + } catch (error) { + console.warn(JSON.stringify({ event: "control_api.cli_auth_probe_failed", errorType: error?.name || "Error" })); return { state: "unavailable", - description: "CLI Auth health endpoint could not be reached.", + description: "CLI Auth health endpoint could not be reached (" + (error?.name || "Error") + ").", checkedAt, latencyMs: Date.now() - startedAt };