Skip to content

Streaming responses for extensions and trusted functions, and the opt-in MCP streaming transport (#659, #626) - #796

Merged
jimhoyd merged 3 commits into
mainfrom
claude/sleepy-shannon-xvazgl
Sep 26, 2026
Merged

jimhoyd merged 3 commits into
mainfrom
claude/sleepy-shannon-xvazgl

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Problem and change

Every URLCode response was a single buffered {status, headers, body}, so nothing could send progress or keep a connection open. The MCP extension could not implement the optional Streamable HTTP pieces: POST replies as SSE, the GET server stream, and session resumption. This PR adds a generic core capability and then the MCP use of it.

Core (feat(core)), a generic streaming response.

  • API:
    • HandlerResult.stream?: AsyncIterable<string | Uint8Array>, exclusive with body/contentLength.
    • Extensions opt in with RuntimeExtension.streams: true. Trusted function routes opt in with route YAML stream: true; it requires function and is refused with sandbox: true.
    • ExtensionRequest.signal and the function context.signal abort on client disconnect, on a limit, or at shutdown.
  • Targets: node is native. vercel is delegated (extensions only; Vercel controls delivery). aws, cloudflare and static refuse a declared stream before serving, and capabilities.ts reports the new streaming capability in those terms.
  • Wire behavior:
    • Status and decorated headers go out with the first chunk (yield '' to flush), chunked, with backpressure.
    • HEAD never pulls the producer.
    • An error before the first chunk takes the normal error path. Any non-complete end truncates the response without the chunked terminator, so clients can tell.
    • A stream from an undeclared route is refused with 502 and a stream_refused log record.
  • Limits (operator only, never route YAML): --max-streams 32, --stream-idle-timeout-ms 30000, --stream-max-duration-ms 300000, --stream-max-bytes 16 MiB, plus createVercelHandler({streams}). A stream leaves maxInFlightRequests once its handler returns.
  • Policies: cache and compression bypass streams explicitly.
  • Lifecycle: shutdown drains streams within --close-timeout-ms, and a retired dev-reload runtime waits for its streams.
  • Observability: request is logged at headers and stream at the end, with bytes, duration and end reason.
  • Docs and tests: new RIM-STREAM-001 card; SPECIFICATION "Streamed responses", EXTENSIONS, CAPABILITIES, OPERATIONS, HTTP, MONITORING/OBSERVABILITY and YAML reference updates; a new recipes/streaming-progress; test/streaming.test.ts (23 tests on real sockets).

MCP (feat(mcp)), an opt-in streaming transport, off by default.

  • Enabling it: mcp({ streaming: true | {maxSessions, sessionIdleTimeoutMs, keepAliveMs, replayMaxEvents, replayMaxBytes} }), with defaults 1000 / 30 min / 15 s / 64 / 64 KiB. Invalid options throw at creation.
  • Streaming off: byte-for-byte the previous behavior on every target, including aws; the 34 existing tests pass unchanged. GET and DELETE answer 405.
  • Streaming on:
    • POST tools/call with a progressToken and an SSE Accept header streams notifications/progress, then the result. notifications/cancelled is handled.
    • initialize issues a random Mcp-Session-Id bound to the creating principal. Unknown or foreign ids get 404, a missing id gets 400, DELETE answers 204, and eviction is bounded by LRU.
    • GET /mcp opens one SSE server stream per session, with keep-alive comments and integer event ids. Last-Event-ID replays from a bounded per-session buffer; a second GET replaces the first.
  • Where it runs: aws is refused by core. Vercel is refused by mcp itself, because sessions live in one process's memory. Sessions do not survive a restart: the client gets a 404 and re-initializes.
  • Tests: packages/mcp/test/streaming.test.ts has 15 real-socket tests, including a real disconnect, then reconnect with Last-Event-ID receiving exactly the missed events in order, and restart gives 404.

Package budgets:

Closes #659. Closes #626.

Verification

All runs used Node 26.10.0.

  • Before rebasing onto feat(core): studio opens the browser; review report shows changed files, a verdict, route marks and findings #792:
    • core commit alone: npm run check, npm run audit:packages and npm run verify passed (exit 0);
    • core + MCP: the same full sequence passed (exit 0).
  • After rebasing onto main 64b181b:
    • typecheck, lint and check pass;
    • audit:packages passes after the budget raise;
    • streaming, studio, project-report, cli and recipes tests: 77/77; packages/mcp: 49/49;
    • npm run test:package: 795236 packed / 3184374 unpacked bytes, 471 files; the packed install and starter/cookbook checks passed.

Compatibility and security

  • Additive. Buffered responses are unchanged, and nothing streams unless a route or extension declares it. MCP streaming is off unless the operator enables it.

  • sandbox: true isolation is unchanged; a sandboxed route cannot stream.

  • MCP sessions:

    • On an MCP mount without authentication, the session id acts as a bearer secret.
    • Anyone who can reach an unprotected mount can evict sessions by opening new ones. Evicted clients re-initialize. This is documented in the package SECURITY.md.
    • A client disconnect on a POST progress stream cancels the tool call. The spec says a disconnect SHOULD NOT be treated as cancellation; POST streams here carry no event ids and cannot be resumed, so the result could not be recovered anyway. This is a deliberate choice.
  • Not verified: HTTP/2, real Vercel delivery, and buffering proxies (documented: use proxy_buffering off / X-Accel-Buffering: no). This is real-socket testing only, not a security review or a deployment/soak test.

  • Not implemented: server-initiated messages from real tools (the plumbing is there, but no tool sends one yet, and listChanged stays false), the 2025-11-25 SSE polling behavior, and persisted sessions.

  • Changes match the documented portable contract; docs/examples are updated where needed.

  • Relevant tests and package checks pass, or limitations are explained above.

  • No credentials, customer data or private project material are included.

  • Runtime/starter changes remain aligned where applicable.

🤖 Generated with Claude Code

https://claude.ai/code/session_014jmv9dMggduiprzSsNd7iP


Generated by Claude Code

…nctions on node/vercel, refused elsewhere (#659)

HandlerResult gains `stream` (AsyncIterable<string | Uint8Array>), exclusive
with `body`. A trusted function route opts in with `stream: true` (its Response
body is handed on unread); an extension registration opts in with
`streams: true`. Any other streamed result is the generic 502, logged as
`stream_refused`. `stream: true` needs `function` and is refused with
`sandbox: true`; the sandbox stays buffered and unchanged.

The self-hosted server (native) and the Vercel adapter (delegated) write
streams through a shared StreamHost: runtime decoration, no Content-Length,
chunked, head committed on the first chunk, backpressure, HEAD never pulls,
cancellation through iterator return() and an AbortSignal handed to
extensions (request.signal) and trusted functions (context.signal), and
operator limits separate from short-request admission (--max-streams,
--stream-idle-timeout-ms, --stream-max-duration-ms, --stream-max-bytes).
Shutdown ends streams at the close deadline; a retired dev-reload runtime
closes after its last stream. AWS, Cloudflare and static refuse streaming
before serving through the new `streaming` capability, and prepareExtensions
refuses a `streams: true` registration outside node/vercel. Cache and
compression bypass streams explicitly. Each ended stream logs a `stream`
record with bytes, duration and end reason.

Adds RIM-STREAM-001, specification/extension/operations/capability docs, the
streaming-progress recipe and real-socket tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014jmv9dMggduiprzSsNd7iP
… stream and Last-Event-ID replay (#659, #626)

The optional parts of the MCP Streamable HTTP transport, enabled only by the
operator: mcp({ streaming: true }) or bounded options (maxSessions 1000,
sessionIdleTimeoutMs 1800000, keepAliveMs 15000, replayMaxEvents 64,
replayMaxBytes 65536). Off by default: the registration then does not declare
streams and every response is unchanged on every target, aws included.

On, the registration declares streams: true (core refuses aws before
activation; activation refuses vercel, whose instances do not share the
in-memory session table):
- initialize issues a 32-byte random base64url Mcp-Session-Id; later requests
  without it answer 400, an unknown/ended/expired/evicted one 404, DELETE ends
  it. A session is bound to the creating principal; another principal's use
  answers 404. The table is bounded (LRU eviction, idle expiry) and lives in
  memory, so a restart means 404 and re-initialize.
- a tools/call with _meta.progressToken and an SSE Accept answers
  notifications/progress then the response as SSE; tools report through
  context.progress, and every handler gets context.signal (client disconnect,
  notifications/cancelled, session end, stream limits).
- GET opens the session's server stream: keep-alive comments, consecutive
  event ids, a bounded replay buffer and Last-Event-ID replay; a newer GET
  replaces the open one. Nothing sends server-initiated messages yet; the
  plumbing is exercised by a test-only trigger.

Real-socket tests in packages/mcp/test/streaming.test.ts. README, SECURITY,
llms, CHANGELOG, EXTENSIONS/FRAMEWORK/OPERATIONS docs and urlcode.json updated;
the mcp unpacked-size budget is raised to 160 KiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014jmv9dMggduiprzSsNd7iP
@jimhoyd
jimhoyd merged commit df0952d into main Sep 26, 2026
30 checks passed
@jimhoyd
jimhoyd deleted the claude/sleepy-shannon-xvazgl branch September 26, 2026 17:03
jimhoyd added a commit that referenced this pull request Sep 26, 2026
Resolves runtime.ts/server.ts/extensions.ts and the RIM table. One rule now
covers both features: an admitted stream holds its runtime's extensions, so a
handoff release waits for open streams as well as buffered requests, and a dev
reload of a project declaring extensions first ends open streams with the new
end reason `reload` (clients resume, e.g. MCP Last-Event-ID) rather than being
refused for as long as a client stays connected. A project declaring no
extensions has nothing to hand over and keeps #796's behavior: streams run on
the retired runtime. handle() keeps its request counted until a stream is
admitted, so no release or close runs between dispatch settling and tracking.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants