Skip to content

fix: pass HTTP headers to pyRevit routes so pairing auth works - #77

Open
lexievanb-del wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
lexievanb-del:fix/route-auth-headers
Open

lexievanb-del wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
lexievanb-del:fix/route-auth-headers

Conversation

@lexievanb-del

Copy link
Copy Markdown

What

Every revit_mcp route 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 paired Authorization: Bearer … header reaches authorize_request.

Why

pyRevit's HttpRequestHandler._prepare_request builds routes.Request from path, method, body and params only. It never copies HTTP headers, so request.headers is always {} and validate_request always fails. This holds in pyRevit 6.4.0 and on current pyRevit develop. 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_request and copies each header via pyRevit's own Request.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. Patching pyrevit.routes.server.server.HttpRequestHandler from startup.py therefore 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 for routes.get_active_server(), which is shared through envvars like the routes, and patches active_server.server.RequestHandlerClass. It makes no Revit API calls.
  • Diagnostics go to %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 new test_pyrevit_requests_receive_http_headers. It covers 401 before patching, success after, idempotency, and the fail-safe path.
  • Revit 2026.5 + pyRevit 6.4.0, stdio client (Claude Code):
    • After a pyRevit reload, bridge.log shows header passthrough installed on pyrevit.routes.server.server.HttpRequestHandler.
    • With no document open, get_revit_status passes auth and returns 503 "No active Revit document", as expected.
    • On a test model, get_revit_status reports healthy and list_levels returns its levels.
    • pyRevit's own /routes/status still returns 200.
  • Not exercised: model-changing tools and execute_revit_code, which have in-Revit review dialogs.

Reviewer notes

  • Requests that arrive during the first seconds after startup, before the thread patches the server, still get 401. This fails closed.
  • This patches pyRevit internals (_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.
  • Unrelated but noticed: pyRevit Routes binds 0.0.0.0:48884 by default, and /routes/status is unauthenticated.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant