Repository navigation
fix: pass HTTP headers to pyRevit routes so pairing auth works - #77
Open
lexievanb-del wants to merge 1 commit into
Open
lexievanb-del wants to merge 1 commit into
lexievanb-del wants to merge 1 commit into
Conversation
pyRevit builds route requests without HTTP headers, so the bearer check in authorize_request always failed and every route returned 401. startup.py runs in its own IronPython engine and pyRevit starts the routes server afterwards, so a background thread waits for the live server and patches its handler class to copy headers onto requests. Header-copy failures are logged to bridge.log (names and errors only, never values) and leave the request unauthenticated, so pyRevit's own routes keep working. 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.
What
Every
revit_mcproute returns 401 "Bridge is disconnected or pairing is invalid.", even after MCP > Bridge > Connect. This makes the route handlers receive the HTTP headers, so the pairedAuthorization: Bearer …header reachesauthorize_request.Why
pyRevit's
HttpRequestHandler._prepare_requestbuildsroutes.Requestfrom path, method, body and params only. It never copies HTTP headers, sorequest.headersis always{}andvalidate_requestalways fails. This holds in pyRevit 6.4.0 and on current pyRevitdevelop. The unit tests didn't catch it because they build requests with headers already attached.How
revit_mcp/security.py:install_header_passthrough(handler_class, log=None)wraps_prepare_requestand copies each header via pyRevit's ownRequest.add_header. It is idempotent. If copying fails, it logs the error and returns the request unchanged, so the MCP route denies it and pyRevit's own routes keep working.startup.py: pyRevit runs extension startup scripts in a clean IronPython engine and starts the routes server after them. Patchingpyrevit.routes.server.server.HttpRequestHandlerfromstartup.pytherefore patches a copy the live server never uses. A first attempt that did this still returned 401. Instead, a daemon thread waits up to 2 minutes forroutes.get_active_server(), which is shared throughenvvarslike the routes, and patchesactive_server.server.RequestHandlerClass. It makes no Revit API calls.%LOCALAPPDATA%\RevitMCPPython\bridge.log, next to the pairing file. Entries hold header names and errors only, never header values, so the token is never logged.Testing
uv run --frozen --extra test python -m pytest tests/unit -q: 107 passed, including the newtest_pyrevit_requests_receive_http_headers. It covers 401 before patching, success after, idempotency, and the fail-safe path.bridge.logshowsheader passthrough installed on pyrevit.routes.server.server.HttpRequestHandler.get_revit_statuspasses auth and returns 503 "No active Revit document", as expected.get_revit_statusreports healthy andlist_levelsreturns its levels./routes/statusstill returns 200.execute_revit_code, which have in-Revit review dialogs.Reviewer notes
_prepare_request,RequestHandlerClass). If pyRevit ever passes headers itself, the wrapper only re-adds the same keys. Raising the missing headers with pyRevit upstream is worth considering.0.0.0.0:48884by default, and/routes/statusis unauthenticated.🤖 Generated with Claude Code