Skip to content

fix: forward HTTP headers to pyRevit Routes handlers (pairing always 401 on pyRevit 6.5) - #83

Open
khalidfathy wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
khalidfathy:fix/routes-header-passthrough
Open

khalidfathy wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
khalidfathy:fix/routes-header-passthrough

Conversation

@khalidfathy

Copy link
Copy Markdown

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) builds base.Request without copying the incoming HTTP headers, so request.headers is always {}. security.authorize_request → pairing.validate_request therefore never sees Authorization, and the extension can't authenticate any request on this pyRevit version.

Fix

  • New revit_mcp/header_passthrough.py wraps the Routes handler's _prepare_request to copy the incoming headers onto the request via Request.add_header. It only fills headers when pyRevit left them empty, so it becomes a no-op once pyRevit forwards headers itself.
  • Why it's deferred: pyRevit runs extension startup.py in its own engine before it creates the Routes server, and shares the live server through env vars. Patching pyrevit.routes.server.server.HttpRequestHandler from startup.py patches a different class object than the one serving requests. So startup.py subscribes to UIApplication.Idling once, patches get_active_server().server.RequestHandlerClass, and unsubscribes. It gives up after a bounded number of idles if Routes is disabled.
  • The wrapper runs on the HTTP server thread and never logs or prints. pyRevit redirects output to a WPF window, and creating that window off the UI thread terminates Revit; I hit this crash (InputManager..ctor InvalidOperationException) during testing.
  • It fails closed: on any error the headers stay empty and the request gets 401.

Security

The authentication logic is unchanged. Requests still need the paired DPAPI bearer token, and requests with an Origin header are still refused, which now actually takes effect because Origin is forwarded too.

Testing

  • tests/unit/test_header_passthrough.py: forwarding, Origin pass-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).
  • Manual check on Revit 2024 / pyRevit 6.5.5, after a full Revit restart:
    • paired /status/ → active, healthy
    • no token → 401
    • wrong token → 401
    • get_revit_status, get_revit_model_info and list_levels over MCP stdio → ok, and Revit stayed stable

Notes for reviewers

  • Separate, pre-existing issue: using pyRevit → Reload while Routes is running can crash Revit. ThreadedHttpServer.shutdown closes the socket under serve_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.py changes need a full Revit restart (not Reload) to take effect.

🤖 Generated with Claude Code

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>
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