Repository navigation
fix: use internal Auth and MCP service bindings in staging #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
743df3e
9d4fcd8
084b17c
7ba050a
f424f6c
7fbbb71
62086ee
8ba2097
70480b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(CLI_AUTH_HEALTH_URL, 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,26 +202,33 @@ 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 | ||
| }; | ||
| } 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 | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| 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({ | ||
|
|
@@ -231,41 +242,45 @@ async function probeMcpServer() { | |
| } | ||
| }), | ||
| cache: "no-store", | ||
| redirect: "error", | ||
| signal: AbortSignal.timeout(4000) | ||
| redirect: "follow", | ||
| signal: AbortSignal.timeout(12000) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Staging validation aborts early When the sequential MCP handshake takes longer than the staging checker’s 10-second Bound the total probe duration so it completes within the staging checker’s 10-second request budget. Also at Prompt for AI agentsIn `workers/control-api/worker.mjs` at line 235:
**Staging validation aborts early**
When the sequential MCP handshake takes longer than the staging checker’s 10-second `/services` budget, `probeMcpServer` can spend up to 36 seconds on its three sequential requests, so `check-control-api-staging.mjs` aborts before the probe returns and reports the staging result as unverified or failed.
Bound the total probe duration so it completes within the staging checker’s 10-second request budget.
Also at `workers/control-api/worker.mjs:251`, `workers/control-api/worker.mjs:263`. |
||
| }); | ||
| 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 }; | ||
| } | ||
|
|
||
| const notificationResponse = await fetch(MCP_ENDPOINT, { | ||
| phase = "initialized-notification"; | ||
| 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" }), | ||
| cache: "no-store", | ||
| redirect: "error", | ||
| signal: AbortSignal.timeout(4000) | ||
| 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 }; | ||
| } | ||
|
|
||
| const toolsResponse = await fetch(MCP_ENDPOINT, { | ||
| phase = "tools-list"; | ||
| 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" }), | ||
| cache: "no-store", | ||
| redirect: "error", | ||
| signal: AbortSignal.timeout(4000) | ||
| 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 }; | ||
|
|
@@ -277,22 +292,23 @@ 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" })); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Failed handshakes lack diagnostics When the endpoint returns an HTTP error or an invalid handshake response, the HTTP-status and protocol-mismatch branches return Emit the failure warning on the HTTP-status and protocol-mismatch return branches as well as in the Prompt for AI agentsIn `workers/control-api/worker.mjs` at line 281:
**Failed handshakes lack diagnostics**
When the endpoint returns an HTTP error or an invalid handshake response, the HTTP-status and protocol-mismatch branches return `unavailable` or `unknown` without throwing, so they bypass the `catch` that logs `control_api.mcp_probe_failed`; callers see a failed probe without the failure diagnostic.
Emit the failure warning on the HTTP-status and protocol-mismatch return branches as well as in the `catch`. |
||
| 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 | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async function serviceSnapshot(service, checkedAt) { | ||
| 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()) }; | ||
| return { ...service, ...(await probeMcpServer(env)) }; | ||
| } | ||
| return { | ||
| ...service, | ||
|
|
@@ -383,7 +399,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, | ||
|
|
@@ -409,7 +425,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); | ||
| } | ||
|
|
@@ -484,7 +500,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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Public probes hold capacity longer
When anonymous clients repeatedly call the public probe routes while the MCP endpoint responds slowly but within each request timeout,
handleRequestcallsprobeMcpServeron public MCP connection routes without authentication or throttling. Its three serial fetches can now keep each Worker request and its upstream requests open for up to 36 seconds, allowing repeated callers to exhaust capacity during a slow MCP response.Apply a rate limit or cache probe results, and enforce an overall deadline for the handshake.
Also at
workers/control-api/worker.mjs:252,workers/control-api/worker.mjs:263.Prompt for AI agents