You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit bc62edf
Browse filesBrowse the repository at this point in the historyBrowse files
fix(mcp): bound and narrow the first-load diagnostics
Corrects measurement defects in the first-load instrumentation and scopes
it as a bounded experiment for iaser-ai#335.
- Request inspection reads a tee of the request alongside the handler, up
to 64 KiB actually read, so a missing or wrong Content-Length can no
longer make it read or wait for the whole body. The beacon reads at most
8 KiB the same way and accepts 120 reports a minute per instance.
- JSON-RPC errors are decoded from JSON and SSE bodies up to 64 KiB rather
than matched by pattern. The stream outcome is `drained`, `cancelled` or
`failed` on this side; it does not claim delivery to the host.
- Card diagnostics run only in a diagnostic card. The early script reads an
`ansari-diagnostics` head address and reports by itself at load, pagehide
and timeout, so a bundle that never runs is still reported. The closed
card's early script does nothing and writes no console line.
- `view.prepared` records the served card's address, size, diagnostic state
and connect origins; reports are logged as client-authored.
- Trials record the deployment release and the first attempt and Retry as
separate windows. The timeline flags mixed clients, overlapping trials
and other releases as ambiguous, and words findings as observations.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: apps/mcp/README.md
+44-33Lines changed: 44 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,53 +95,64 @@ The Worker takes the variables in the table below as Wrangler `vars` or secrets,
95
95
|`MCP_ALLOWED_ORIGINS`| Comma-separated allowed browser origin hostnames. A configured list replaces defaults; an explicitly empty list permits no Origin header value. Requests without Origin remain supported. |
96
96
|`MCP_RESPONSE_MODE`|`json` for modern MCP final-answer-only responses; otherwise SSE, which supports client-requested progress notifications. Older-host compatibility responses may use SSE. |
97
97
|`ANSARI_RELEASE`| Optional safe release identifier, embedded in served HTML and health/log records. |
98
-
|`MCP_VIEW_DIAGNOSTICS_ORIGIN`| Set only on a deployment being diagnosed: its own public origin, such as `https://ansari-mcp-development.up.railway.app`. The card may then connect to that one origin and reports its start-up milestones to `POST /diag/view`; this changes the card's bytesand so its address. Unset, the card, its address and its closed permissions are unchanged and the route does not exist. A value that is not a bare `https` origin (`http` only for localhost) stops startup. See [First-load diagnosis](#first-load-diagnosis). |
98
+
|`MCP_VIEW_DIAGNOSTICS_ORIGIN`| Set only on a deployment being diagnosed: its own public origin, such as `https://ansari-mcp-development.up.railway.app`. The card may then connect to that one origin and reports its start-up milestones to `POST /diag/view`; this changes the card's bytes, address and permissions. Unset, the card, its address and its closed permissions are unchanged and the route does not exist. A value that is not a bare `https` origin (`http` only for localhost) stops startup. See [First-load diagnosis](#first-load-diagnosis). |
99
99
100
100
`POST /mcp` serves stateless MCP requests through Hono and the official MCP request handler. Modern requests need no initialization; older hosts can use the handler’s stateless compatibility path. No MCP session IDs, session map, expiry timer or replay store are retained. `GET /mcp` and `DELETE /mcp` return 405. `/health` exposes the release. The HTML is supplied by the hosting target (read from `dist/` at Node start, bundled into the Worker), revision-stamped and fingerprinted once before the first request is served: the release is stamped first, so equal final bytes have equal identities and a release change legitimately changes the hash. Each request creates its own lightweight MCP server using that prepared view; resource bytes and identity stay fixed until restart. No demo, font server, alternative renderer or historical resource is served. Tool calls have a fresh 60-second budget; timeout yields a safe error and leaves subsequent calls usable. The same lifetime signal aborts the upstream fetch and response-body read when the request ends or the budget expires.
101
101
102
102
Closing a request server releases its budget even if it was never connected to a transport. On SIGINT or SIGTERM the listener stops accepting requests, closes MCP handlers and HTTP connections, and exits; unfinished calls are interrupted, including older-host SSE calls. Fetch cancellation does not guarantee that the remote backend has stopped processing. Caller-disconnect propagation depends on the host transport; the local Workers runtime does not establish immediate upstream cancellation from a client disconnect. Actual fetch cancellation at the request deadline is covered on both hosting targets.
103
103
104
104
## First-load diagnosis
105
105
106
-
Issue #335: in Claude web and ChatGPT web the host's own error replaced the card on first load, and one Retry
107
-
recovered it. The card's own failure texts never appeared, so the host gave up before the card showed anything. These
108
-
records tell the possible causes apart.
109
-
110
-
**Server, always on.** Each `/mcp` exchange writes an `mcp.request` line when it arrives and an `mcp.response` line
111
-
when it ends, joined by `rid`. The request line names the JSON-RPC method; for a card read, the address read and
112
-
whether it is the one served now (`uriCurrent`); for the opening handshake, the client's name, version and protocol;
113
-
for a tool call, the tool name. The response line has the HTTP status, any JSON-RPC error code (a stale card read is
114
-
error `-32602` inside an HTTP 200), bytes sent, time to headers and total time, and whether the response was
115
-
`complete`, `cancelled` by the client or `failed`. Handler errors are logged by name and code. No question, answer or
116
-
other parameter value is logged.
117
-
118
-
**Card, always on.** A small script runs before the bundle and records when it started, any error, unhandled
119
-
rejection or content-policy violation, and when the host's messages arrive (first message, tool input, tool result,
120
-
cancellation, teardown request), by method name only. The stack's hooks add when fonts loaded, when the host context
121
-
was delivered, how starting ended and when the first answer or problem was rendered. The card writes each report as an
122
-
`[ansari-view]` console line in its frame. One violation is expected on every load: zod's `new Function('')`
123
-
capability probe under the card's `script-src`, which it catches.
124
-
125
-
**Card beacon, diagnosed deployment only.** With `MCP_VIEW_DIAGNOSTICS_ORIGIN` set, the card also sends each report
126
-
to `POST /diag/view` when it starts or fails, renders its first answer or problem, is put away, or has not started
127
-
after 15 seconds. The server accepts only the fixed report shape (milestones, enumerated failures, error names) and
128
-
logs it as `view.report`, with the host sandbox's origin name.
129
-
130
-
**Trials.** Run one trial per new conversation, with the deployment live for hours (`aged`) and right after a
131
-
redeploy (`fresh`):
106
+
A bounded experiment for issue #335, not a permanent observability design. In Claude web and ChatGPT web the host's
107
+
own error replaced the card on first load and one Retry recovered it; the card's own failure texts never appeared, so
108
+
the host gave up before the card showed anything. The aim is to locate the first missing step between the card read,
109
+
the card's start, the host handshake, tool-result arrival and the first rendered answer, where that step can be
110
+
observed at all. After the trials, this is removed or narrowed to what proved useful.
111
+
112
+
**Server records, always on.** Each `/mcp` exchange writes `mcp.request` and `mcp.response` lines joined by `rid`. The
113
+
request is inspected from a copy, alongside the handler and up to 64 KiB actually read, so inspection never holds up
114
+
or alters the exchange; a larger body is recorded as `oversize`. The request line names the JSON-RPC method; for a card
115
+
read, the address read and whether it is the one this instance serves (`uriCurrent`); for the handshake, the client's
116
+
name, version and protocol; for a tool call, the tool name. The response line has the HTTP status, bytes, time to
117
+
headers and total time, and `stream`: whether this side's response stream was `drained`, `cancelled` or `failed`. That
118
+
is not proof the host received or rendered it. Responses up to 64 KiB are decoded as JSON-RPC for an error code; a
119
+
stale card read is `-32602` inside an HTTP 200. At start, `view.prepared` records the served card's address, size,
120
+
whether it is a diagnostic card and its connect origins. Handler errors are logged by name and code. These records hold
121
+
no question, answer or other parameter value.
122
+
123
+
**Card reports, diagnostic card only.** With `MCP_VIEW_DIAGNOSTICS_ORIGIN` set, the served card carries an
124
+
`ansari-diagnostics` address in its head, may connect to that one origin, and changes bytes, address and permissions.
125
+
Compare its trials with the closed card's rather than assuming they behave the same. An early script, which runs
126
+
before the bundle is parsed, records errors, unhandled rejections, content-policy violations and when the host's
127
+
messages arrive (first message, tool input, tool result, cancellation, teardown request), by method name only. The
128
+
stack's hooks add font loading, host-context delivery, how starting ended and the first rendered answer or problem. The
129
+
early script reports by itself at `load`, at `pagehide` and after 15 s without a start, so a bundle that never runs is
130
+
still reported; the bundle adds reports when the card is ready, fails, or renders its first answer or problem. Each
131
+
report is an `[ansari-view]` console line and a beacon to `POST /diag/view`. The route accepts only the fixed report
132
+
shape, at most 8 KiB and 120 reports a minute per instance, and logs `view.report` with `clientAuthored: true`: the
133
+
shape limits what is logged, but anyone who can reach the route can send a report. Report fields are milestones,
134
+
enumerated failures and error names; the stack still prints a callback's own error to the console, as before. One
135
+
violation is expected on every load: zod's `new Function('')` capability probe under the card's `script-src`, which it
136
+
catches. The closed card's early script does nothing.
137
+
138
+
**Trials.** Use the deployment for nothing else while a trial runs. Run one trial per new conversation, with the
139
+
deployment live for hours (`aged`) and right after a redeploy (`fresh`), and with the diagnostic and the closed card.
140
+
The trial script records the deployment's release and the first attempt and Retry as separate windows:
132
141
133
142
```sh
134
-
bash scripts/first-load-trial.sh claude aged
135
-
bash scripts/first-load-trial.sh chatgpt fresh
136
-
# Railway: deployment and edge HTTP logs for the trial window, then one timeline per trial.
143
+
bash scripts/first-load-trial.sh https://ansari-mcp-development.up.railway.app claude aged diagnostic
144
+
# Railway: deployment and edge HTTP logs covering the trials, then one timeline per trial.
if(card==='diagnostic')out.push(`${cards.size} card instance(s) reported in this window${cards.size===0 ? ' (none observed: a card may not have run, or its report did not arrive)' : ''}`);
61
68
for(const[id,last]ofcards){
62
69
constm=last.marks??{};
63
-
if(m.teardownRequested!==undefined&&m.ready===undefined)out.push(`card ${id}: the host asked to tear it down before it was ready`);
70
+
if(m.main===undefined)out.push(`card ${id}: the bundle had not evaluated by its last report (${last.reason})`);
71
+
if(m.teardownRequested!==undefined&&m.ready===undefined)out.push(`card ${id}: the host asked for teardown before the card was ready, as of its last report`);
if(m.ready!==undefined&&m.script!==undefined&&m.ready-m.script>3_000)out.push(`card ${id}: ${Math.round(m.ready-m.script)}ms from script start to ready`);
66
-
if(m.hostMessage===undefined)out.push(`card ${id}: no message from the host ever arrived`);
74
+
if(m.hostMessage===undefined)out.push(`card ${id}: no host message observed by its last report (${last.reason})`);
67
75
}
68
76
returnout;
69
77
}
70
78
71
-
for(consttrialoflines(trialsFile)){
79
+
/** Signs that a window holds traffic other than this trial's, so its records cannot be attributed with confidence. */
if(trial.release&&trial.release!=='unknown'&&releases.size&&![...releases].every(release=>release===trial.release))out.push(`records from releases ${[...releases].join(', ')}, trial recorded ${trial.release}`);
0 commit comments