Repository navigation
fix: forward HTTP headers to pyRevit Routes handlers (pairing always 401 on pyRevit 6.5) - #83
Open
khalidfathy wants to merge 1 commit into
Conversation
pyRevit 6.5.x builds Routes requests without copying HTTP headers, so request.headers is always empty and the pairing check rejects every request with 401, even after a successful Connect. Patch the live Routes server's RequestHandlerClass to copy incoming headers onto each request. pyRevit runs extension startup scripts in a separate engine before it creates the server, so the patch is applied on Revit's first idle after session load. The request-path wrapper never logs (output from the server thread crashes Revit) and fails closed. Auth is unchanged: the paired bearer token is still required and requests with an Origin header are still refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On pyRevit 6.5.5 (Revit 2024), every bridge request returns
401 Bridge is disconnected or pairing is invalid., even right after MCP > Bridge > Connect succeeds and the client sends the correct bearer token.Cause: pyRevit's
HttpRequestHandler._prepare_request(pyrevitlib/pyrevit/routes/server/server.py) buildsbase.Requestwithout copying the incoming HTTP headers, sorequest.headersis always{}.security.authorize_request→pairing.validate_requesttherefore never seesAuthorization, and the extension can't authenticate any request on this pyRevit version.Fix
revit_mcp/header_passthrough.pywraps the Routes handler's_prepare_requestto copy the incoming headers onto the request viaRequest.add_header. It only fills headers when pyRevit left them empty, so it becomes a no-op once pyRevit forwards headers itself.startup.pyin its own engine before it creates the Routes server, and shares the live server through env vars. Patchingpyrevit.routes.server.server.HttpRequestHandlerfromstartup.pypatches a different class object than the one serving requests. Sostartup.pysubscribes toUIApplication.Idlingonce, patchesget_active_server().server.RequestHandlerClass, and unsubscribes. It gives up after a bounded number of idles if Routes is disabled.InputManager..ctorInvalidOperationException) during testing.Security
The authentication logic is unchanged. Requests still need the paired DPAPI bearer token, and requests with an
Originheader are still refused, which now actually takes effect becauseOriginis forwarded too.Testing
tests/unit/test_header_passthrough.py: forwarding,Originpass-through, idempotency, not overwriting existing headers, a missing handler, and the deferred idle patch against a server that starts later. The full unit suite passes (112 tests,pytest+pytest-asyncio)./status/→active,healthyget_revit_status,get_revit_model_infoandlist_levelsover MCP stdio → ok, and Revit stayed stableNotes for reviewers
ThreadedHttpServer.shutdowncloses the socket underserve_forever, and the resulting traceback is printed from a non-UI thread. It happens with or without this change; restarting Revit avoids it. It may be worth a README note.startup.pychanges need a full Revit restart (not Reload) to take effect.🤖 Generated with Claude Code