Skip to content

fix: make Revit pairing work on .NET 8+ (Revit 2025+) - #91

Open
protocolx30 wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
protocolx30:fix/pairing-dotnet8-dpapi
Open

protocolx30 wants to merge 1 commit into
mcp-servers-for-revit:masterfrom
protocolx30:fix/pairing-dotnet8-dpapi

Conversation

@protocolx30

Copy link
Copy Markdown

Summary

On Revit 2025+ (IronPython running on .NET 8 or later — Revit 2027.3 reports .NET 10), MCP > Bridge > Connect never pairs. revit_mcp/pairing.py imports System.Security.Cryptography.ProtectedData, which is not part of the .NET 8+ runtime and is not shipped by Revit 2027 or pyRevit. The import raises an uncaught exception, pyRevit shows its error window, and no pairing.dpapi file is written — so every MCP request is denied.

Changes (revit_mcp/pairing.py only)

  • DPAPI fallback: if ProtectedData cannot be loaded under IronPython, fall through to the existing crypt32 ctypes path. It uses the same CurrentUser scope with no entropy, so it produces the same blob the CPython MCP server already reads.
  • Fail closed, never crash: get_token() and validate_request() now catch unexpected exceptions, deny the request, and append the traceback (never the token) to %LOCALAPPDATA%\RevitMCPPython\pairing_error.log. An exception escaping a pyRevit route can take Revit down (see below), so failing closed is safer than raising.

Testing

  • Revit 2027.3 + pyRevit 6.5.0: Connect now writes the pairing; get_revit_status and list_levels return model data through the MCP server (stdio).
  • CPython (uv, Python 3.13): pair → authorization_headers() → validate_request() accepts the valid token, rejects a wrong token, and rejects requests carrying an Origin header.

Reviewer notes: pyRevit 6.5 issues outside this repo

While testing I hit two problems in pyRevit's own Routes server (pyrevitlib/pyrevit/routes/server/server.py). They are not fixed by this PR, but they block the bridge on stock pyRevit 6.5:

  1. Request headers are never passed to handlers. _prepare_request() builds base.Request(...) without calling add_header, so request.headers is always {} and authorize_request() always returns 401. Working around it locally meant copying self.headers onto the request.
  2. Revit crashes after a pyRevit Reload when anything on a Routes worker thread writes to sys.stderr (for example log_message or an unhandled thread exception). pyRevit then creates its output window on a non-STA thread and .NET terminates Revit (InvalidOperationException: The calling thread must be STA). Working around it locally meant routing log_message/handle_error to a file.

It may be worth documenting a minimum pyRevit version, or a known-issues note, until these are fixed upstream in pyRevit.

🤖 Generated with Claude Code

On Revit 2025+ (IronPython on .NET 8/10), System.Security.Cryptography.
ProtectedData is not available, so MCP > Bridge > Connect raised an
uncaught import error and no pairing secret was ever written.

- Fall back to the existing crypt32 ctypes DPAPI path when ProtectedData
  cannot be loaded; it produces the same CurrentUser blob the CPython
  MCP server already reads.
- Catch unexpected errors in get_token/validate_request so they deny the
  request instead of escaping into pyRevit's route thread, and record
  the traceback (never the token) in pairing_error.log.

Tested on Revit 2027.3 with pyRevit 6.5: Connect pairs, and
get_revit_status / list_levels succeed. Note pyRevit 6.5 itself also
needs request headers passed through to route handlers for the bearer
check to succeed; that is a separate pyRevit-side issue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@protocolx30

Copy link
Copy Markdown
Author

Correction to the "pyRevit 6.5 issues" section of the description: both are already reported and fixed upstream in pyRevit.

So on pyRevit 7.x neither problem needs a local workaround. A note in the README recommending pyRevit 7.0.1 or later, or 7.1.0 for both fixes, might save other users the same debugging.

This PR is still needed on pyRevit 7: ProtectedData is missing from .NET 8+ regardless of the pyRevit version, so Connect fails on Revit 2025+ without the fallback.

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