Commit Graph

1005 Commits

Author SHA1 Message Date
Max Isbey 116a95fc37 Re-point fd at the wire when a divert dup2 fails
A Windows dup2 (UCRT before Windows 11) closes its target before
duplicating, so an OSError from the divert can leave the standard
descriptor closed rather than still carrying the wire. Run the same
dup2(private_fd, fd) restore the exit path uses before serving in
place: an idempotent no-op when fd is already on the wire, and a
re-materialization from the private duplicate when the target was
closed. Adds a regression variant that closes the target as UCRT does.

Also scopes the real-host logging note to the default stderr handler.
2026-07-24 16:19:49 +00:00
Max Isbey 885c6aa9a4 Scope the stray-output docs to flushed writes
The diversion catches output that is actually flushed to stdout while
serving. A print() in a block-buffered process usually stays unflushed
in sys.stdout's user-space buffer, and the interpreter's exit flush
drains it onto the restored protocol stream, so the docs now say so
instead of implying every stray print is diverted. The logging advice
is unchanged: its handler flushes each record.

No-Verification-Needed: docs-only wording change
2026-07-24 16:19:49 +00:00
Max Isbey 57271c28bf Scope the stderr-closed isolation test to POSIX
The wire duplicate is allocated above the standard range atomically only
via F_DUPFD; Windows has no atomic minfd dup, so with fd 2 closed the
duplicate can land in the hole and the transport degrades to serving in
place. That degradation is safe, but it leaves fd 0 on the planted pipe,
and the test's blocking read of it then never returns on Windows.

No-Verification-Needed: test-only platform scoping
2026-07-24 16:19:49 +00:00
Max Isbey ad9c115f29 Correct the watchdog poll mask note and use the public get_osfhandle
The null device reports POLLIN (plus POLLOUT under the default event
mask), never POLLERR; the migration note taught the wrong mask. The
Windows handle rebind now uses msvcrt.get_osfhandle, the documented
public API, instead of pywin32's underscore-private equivalent, which
also drops the win32file import. The migration entry also gains the
one API-visible change: a second concurrent stdio_server() raises
RuntimeError.
2026-07-24 16:19:48 +00:00
Max Isbey 0f9695fe2c Never close a buffer the transport does not own
The transport's text layers now detach instead of closing on garbage
collection. In the in-place paths the wrapped buffer is the sys
stream's own, and closing it destroyed sys.stdout for the rest of the
process (the issue #1933 class, which the claimed path had already
fixed incidentally via the private descriptor).

Also strengthens the fd 0 watchdog migration note (the null device
reports permanently readable, so any-event watchers misfire at startup
rather than merely never firing) and restores the note about child
output volume flowing into the client's stderr channel.
2026-07-24 16:19:48 +00:00
Max Isbey 6990fa63d7 Open the errlog capture as UTF-8 in the noisy-child test
Matches the sibling errlog tests; the platform locale default could
mojibake a failing child's stderr diagnostics on Windows.

No-Verification-Needed: test-only encoding fix
2026-07-24 16:19:48 +00:00
Max Isbey cf0ba4fcf9 Exclude POSIX-only fcntl lines from the Windows coverage gate
No-Verification-Needed: coverage pragmas only
2026-07-24 16:19:48 +00:00
Max Isbey a45283b735 Reshape the stream claim into an explicit state machine
The claim registry maps each standard descriptor to at most one owning
transport; the wire duplicate is allocated where it cannot land in the
standard range (F_DUPFD_CLOEXEC above fd 2 on POSIX) and recorded on
the claim before the descriptor is moved; release restores with a
single dup2 and deregisters only on success. Every failure, modeled or
not, lands on the safe side: the claim is retained and later
transports are refused rather than handed a diverted descriptor.

Deleted by the same design: the roll-forward path (its premise, a
reliably reportable dup2 outcome, does not exist on Windows), and the
restore-time flush (user flush() side effects can destroy the wire;
unflushed stray output now drains after the session instead). The
design was validated through three adversarial verification passes;
the write-up with invariants and accepted residues is on the PR.
2026-07-24 16:19:48 +00:00
Max Isbey 8a076d0006 Roll forward when a failed claim cannot be rolled back
If a mid-claim OSError is followed by a rollback failure, fd is stuck
on the diversion but the private duplicate still holds the wire, so
the transport now keeps the claim and serves from the duplicate like a
completed claim; the restore at exit gets another chance. Previously
the claim was released while fd stayed diverted, letting a later
stdio_server() claim the diversion as its wire.
2026-07-24 16:19:48 +00:00
Max Isbey 6a6c544fec Keep a still-diverted fd claimed when its restore fails
A failed exit-time restore is still swallowed so it never masks what
ended the transport, but the fd now stays in the claimed set: a later
stdio_server() in that process is refused instead of claiming the
diversion and serving the null device as its wire.
2026-07-24 16:19:48 +00:00
Max Isbey 87dda9759d Scope the fd 0 watchdog migration example to POSIX
os.getppid() never changes on Windows, and the polled-fd pattern being
migrated is POSIX-only to begin with.

No-Verification-Needed: docs-only change
2026-07-24 16:19:48 +00:00
Max Isbey 3e321f6de0 Add migration note for code that watches fd 0 directly
No-Verification-Needed: docs-only change
2026-07-24 16:19:48 +00:00
Max Isbey 0ba79d4fc2 Hold the stream claim for in-place fallbacks too
A transport that falls back to serving the real stdin or stdout in
place (incomplete descriptor table, or a claim that failed with
OSError) now keeps its fd in the claimed set for its lifetime, so a
second concurrent stdio_server() is refused in every shape rather than
only after a successful diversion. The sentinel now means a transport
owns the stream, not that the fd is currently diverted.
2026-07-24 16:19:48 +00:00
Max Isbey 987b69128d Replace dashes with plain punctuation in prose added by this branch
No-Verification-Needed: docstring and docs prose only
2026-07-24 16:19:48 +00:00
Max Isbey 2e609e18a3 Refuse abnormal processes instead of repairing them in stdio isolation
The stdin/stdout claim now engages only in a normal process: the sys
stream backed by its real descriptor and fds 0-2 all open, which makes
it impossible for the private wire duplicates to land in the standard
range. Anything else - replaced streams, an incomplete descriptor
table, a failed dup - is served in place exactly as v1 was, and a
second concurrent stdio_server() raises RuntimeError instead of
contending for the streams.

This replaces the previous hardening (the dup-above-standard-range
loop, stderr-merge detection, and nested transports serving into the
diversion) with guards, and removes the migration entry: no working
code changes behavior, so there is nothing to migrate. Comments and
docstrings trimmed throughout the diff.
2026-07-24 16:19:48 +00:00
Max Isbey 2b0f3ace9f Isolate the stdio server's stdout from handler code and subprocesses
While serving on the process's real stdout, stdio_server now moves the
protocol pipe to a private descriptor and points fd 1 - and, on
Windows, the standard output handle - at stderr, restoring it when the
transport exits. A stray print() in handler code or a child process
writing to its inherited stdout lands in the client's log instead of
corrupting the JSON-RPC stream. The null device stands in when stderr
is unusable or, on POSIX, is detected as merged into stdout (2>&1).

The stdin claim generalizes into the shared _claim_fd mechanism: one
lock-guarded sentinel table covers both descriptors, private wire
duplicates are forced above the standard descriptor range so a process
started with a standard descriptor closed cannot hand the wire out as
its "stderr", and a failed claim degrades to serving the sys stream's
buffer in place exactly as v1 did.

Docs now describe the guarded behavior with its remaining gaps (output
flushed before serving begins, injected streams, merged stderr on
Windows), and the transport:stdio:stream-purity divergence narrows
accordingly.
2026-07-24 16:19:48 +00:00
Max Isbey fbe9841788 Isolate the stdio server's stdin from handler subprocesses
While serving on the process's real stdin, stdio_server() now reads the
protocol from a private duplicate of fd 0 and points fd 0 (and, on
Windows, the standard input handle) at the null device, restoring both
on exit. Children spawned by handler code then inherit the null device
instead of the protocol pipe.

A child that inherited the pipe could consume protocol bytes on any
platform, and on Windows a Python child hangs inside interpreter
startup behind the transport's pending read (CPython gh-78961) until
the next request arrives, so any tool that ran a subprocess without
stdin=DEVNULL appeared to hang until timeout.

Isolation engages only when sys.stdin is backed by the real fd 0, at
most once per process, and degrades to reading stdin in place when the
descriptor table cannot be rearranged.

Fixes #671.
2026-07-24 16:19:48 +00:00
Max 00a70148bc Serve the 2026-07-28 protocol over stdio: decide the era from the opening request (#3152)
CI / checks (push) Failing after 1s
Deploy Docs / deploy-docs (push) Has been cancelled
Conformance Tests / server-conformance (push) Has been cancelled
Conformance Tests / client-conformance (push) Has been cancelled
GitHub Actions Security Analysis / zizmor (push) Has been cancelled
CI / all-green (push) Has been cancelled
2026-07-24 13:46:37 +01:00
Max 837ef904f8 Align with spec #3002: optional clientInfo, serverInfo in result _meta (#3143)
Deploy Docs / deploy-docs (push) Has been cancelled
CI / checks (push) Failing after 24m23s
CI / all-green (push) Has been cancelled
Conformance Tests / server-conformance (push) Has been cancelled
Conformance Tests / client-conformance (push) Has been cancelled
GitHub Actions Security Analysis / zizmor (push) Has been cancelled
2026-07-23 12:00:36 +01:00
Max 3a6f2996cd docs: load media examples from disk instead of inline base64 (#3108) 2026-07-16 20:41:05 +01:00
Max ebcc4dc3fb Pin pymdown-extensions back to 11.0 (#3106) 2026-07-16 20:25:33 +01:00
Max 497f7afa61 docs: make API reference rendering independent of page order (#3107) 2026-07-16 18:29:44 +01:00
Andre.Kalberer e464f72c12 docs: document Windows stdio subprocess stdin handling (#3079) 2026-07-16 11:25:47 +01:00
Marcelo Trylesinski 03aaebd3aa Add Streamable HTTP request body limits (#3095) 2026-07-16 08:33:32 +02:00
Marcelo Trylesinski 2713b53b12 Replace httpx and httpx-sse with httpx2 (#2972)
CI / checks (push) Failing after 1s
CI / all-green (push) Has been cancelled
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
v2.0.0b2
2026-07-14 17:05:08 +01:00
Otis Cui 1216c53693 fix: reject trailing newline in tool-name and URI-template varname validation (#3076)
Python's $ with re.match also matches just before a single trailing newline, so tool-name validation accepted "name\n" and UriTemplate.parse accepted varnames like "foo\n". Switch both checks to re.fullmatch.

Closes #3084
2026-07-10 12:56:54 +00:00
Marcelo Trylesinski 4fc8882c02 docs: replace MkDocs with Zensical (#3073)
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
2026-07-10 12:48:46 +01:00
Max 74a242ae7f ci: pick the docs-preview toolchain from the PR checkout (#3081) 2026-07-09 13:57:14 +01:00
Max 148278e07f Gate the test matrix and retry setup-uv's flaky manifest fetch (#3080) 2026-07-09 12:32:53 +01:00
Max 9bdc03d54e Add the client-side subscriptions/listen driver (#3047) 2026-07-07 14:26:09 +01:00
Max 6d2e908f2b docs: pin mkdocs<2 and silence the mkdocs-material advisory banner in CI (#3072) 2026-07-07 13:45:30 +01:00
Max 867bba6263 Share one event loop per test module to stop Windows socketpair churn (#3070) 2026-07-07 13:19:04 +01:00
Max d287c9868f Extend resolver DI to sampling and roots requests (#3049) 2026-07-06 18:25:57 +01:00
Max 53117cb3a9 Make client-side cancellation work over the 2026 transports (#3046) 2026-07-02 19:21:04 +01:00
Max bf4402725d docs: restructure the migration guide around topical groups with a navigation layer (#3058) 2026-07-02 18:54:21 +01:00
Max 2359b40285 docs: modernize the site theme (#3057) 2026-07-02 16:17:33 +01:00
Max e4d95e0d44 docs: add a "What's new in v2" page (#3054) 2026-07-02 15:01:30 +01:00
Max 220d362112 docs: restructure into topical sections and add the four most-asked-for pages (#3044) 2026-07-01 21:06:04 +01:00
Max 080f2a869d Harden the dual-era stream loop's era-lock and rejection semantics (#3040) 2026-07-01 17:07:12 +01:00
Max d39c68df23 De-flake conformance CI: solo re-verification, spawn-storm reduction, result artifacts (#3043) 2026-07-01 16:59:06 +01:00
Max 0da9092037 Point pre-release install pins at 2.0.0b1 (#3039)
CI / checks (push) Failing after 0s
CI / all-green (push) Has been cancelled
v2.0.0b1
2026-07-01 00:14:52 +01:00
Max e50fb5be19 Serve the 2026-07-28 era over stdio and other stream-pair transports (#3038) 2026-07-01 00:11:56 +01:00
Max dcf8a6a0b5 Document pydantic.ValidationError in client Raises sections (#3036) 2026-07-01 00:03:17 +01:00
Max 410cc0db31 Add v2 feedback issue template (#3037) 2026-07-01 00:03:11 +01:00
Max ca10dade2c Serve subscriptions/listen with a pluggable event bus (SEP-2575) (#3035) 2026-06-30 23:01:04 +01:00
Max 48ef569f7e Validate Mcp-Param-* headers server-side on the 2026-07-28 HTTP path (SEP-2243) (#3033) 2026-06-30 21:39:32 +01:00
Max 4df609119f Add a client extension API (#3034) 2026-06-30 21:31:02 +01:00
Max 7322ca56f4 Require integrity protection for MRTR requestState (#3032) 2026-06-30 21:30:32 +01:00
Den Delimarsky 985652491a Add Cloudflare Pages docs preview with /preview-docs slash command (#3028) 2026-06-30 16:47:21 +01:00
Max 0b200ef03b Surface skipped conformance scenarios as baselined known failures (#3030) 2026-06-30 13:07:05 +01:00