Skip to content

Open-source Revit MCP server: let Claude and other AI assistants read and edit BIM models, with human approval on every change.

feat(addin): launch the panel hub process automatically on startup

A. Sam Mohammad0a6c5cd6d8b6
diff --git a/packages/revit-bridge-addin/src/Bridge/App.cs b/packages/revit-bridge-addin/src/Bridge/App.cs
index 3fe0064..127de32 100644
--- a/packages/revit-bridge-addin/src/Bridge/App.cs
+++ b/packages/revit-bridge-addin/src/Bridge/App.cs
@@ -13,6 +13,7 @@ namespace RevitBridge.Bridge
private BridgeServer? _server;
private CommandQueue? _queue;
private ExternalEvent? _externalEvent;
+ private PanelHubLauncher? _hubLauncher;
public static string? RevitVersion { get; private set; }
public static string? ActiveDocumentName { get; private set; }
@@ -52,6 +53,17 @@ namespace RevitBridge.Bridge
Log.Error(ex, "Bridge started, but the AEC Model Bridge ribbon could not be created");
}
+ try
+ {
+ // Fire-and-forget: checks/launches in the background, never blocks startup.
+ _hubLauncher = new PanelHubLauncher();
+ _hubLauncher.EnsureRunning();
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Bridge started, but the panel hub could not be launched");
+ }
+
application.ControlledApplication.DocumentChanged += (sender, args) =>
{
ActiveDocumentName = args.GetDocument()?.Title;
@@ -303,6 +315,7 @@ namespace RevitBridge.Bridge
{
try
{
+ _hubLauncher?.StopIfLaunched();
_server?.Stop();
_externalEvent?.Dispose();
Log.Information("AEC Model Bridge stopped");
diff --git a/packages/revit-bridge-addin/src/Bridge/PanelHubLauncher.cs b/packages/revit-bridge-addin/src/Bridge/PanelHubLauncher.cs
new file mode 100644
index 0000000..e7e23fb
--- /dev/null
+++ b/packages/revit-bridge-addin/src/Bridge/PanelHubLauncher.cs
@@ -0,0 +1,205 @@
+using System;
+using System.Diagnostics;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+using Serilog;
+
+namespace RevitBridge.Bridge;
+
+/// <summary>
+/// Ensures the Python MCP hub's panel HTTP shim (packages/mcp-server-revit's
+/// panel_server.py) is reachable on 127.0.0.1 before the dockable panel needs it.
+///
+/// HubClient.cs assumes something is already listening on the panel port — this
+/// class is what makes that true on a machine where nobody has manually started
+/// `aec-model-bridge-panel-server`. It never manages a hub it didn't start itself:
+/// if something is already answering /health (a developer's own instance, or a
+/// future bundled-runtime launcher), this leaves it alone entirely, both at
+/// startup and at shutdown.
+///
+/// Python provisioning is not yet bundled with the installer (see
+/// docs/product/DECISIONS_AND_RISKS.md D-010 — still open); this is a best-effort
+/// launch matching the interpreter conventions docs/install.md already documents
+/// (a project .venv, or a bare `python`/`py` on PATH), not a substitute for that
+/// installer work.
+/// </summary>
+public sealed class PanelHubLauncher
+{
+ private const int HealthCheckTimeoutMs = 800;
+ private const int PostLaunchPollIntervalMs = 500;
+ private const int PostLaunchMaxAttempts = 10;
+
+ private static readonly HttpClient HealthClient = new HttpClient { Timeout = TimeSpan.FromMilliseconds(HealthCheckTimeoutMs) };
+
+ private Process? _launchedProcess;
+
+ private static int Port
+ {
+ get
+ {
+ var raw = Environment.GetEnvironmentVariable("MCP_PANEL_HTTP_PORT");
+ return int.TryParse(raw, out var port) ? port : 8787;
+ }
+ }
+
+ /// <summary>
+ /// Checks whether the panel hub is reachable and launches it in the background
+ /// if not. Returns immediately; the check/launch/poll sequence runs on a
+ /// background task so it never blocks Revit's OnStartup.
+ /// </summary>
+ public void EnsureRunning()
+ {
+ _ = Task.Run(EnsureRunningAsync);
+ }
+
+ private async Task EnsureRunningAsync()
+ {
+ if (await IsHealthyAsync().ConfigureAwait(false))
+ {
+ Log.Information("Panel hub already reachable on 127.0.0.1:{Port} — not launching a new one", Port);
+ return;
+ }
+
+ var interpreter = ResolvePythonInterpreter();
+ if (interpreter == null)
+ {
+ Log.Warning(
+ "Panel hub is not reachable on 127.0.0.1:{Port} and no Python interpreter could be found to " +
+ "launch it. Set AEC_MODEL_BRIDGE_PYTHON to a python.exe, or start it manually with " +
+ "'aec-model-bridge-panel-server' / 'python -m revit_mcp_server.panel_server' — see docs/install.md. " +
+ "The panel will show a connection error until the hub is reachable.",
+ Port);
+ return;
+ }
+
+ try
+ {
+ var startInfo = new ProcessStartInfo
+ {
+ FileName = interpreter,
+ Arguments = "-m revit_mcp_server.panel_server",
+ UseShellExecute = false,
+ CreateNoWindow = true,
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ };
+
+ _launchedProcess = Process.Start(startInfo);
+ Log.Information("Launched panel hub via '{Interpreter} -m revit_mcp_server.panel_server' (pid {Pid})",
+ interpreter, _launchedProcess?.Id);
+ }
+ catch (Exception ex)
+ {
+ Log.Warning(ex, "Failed to launch the panel hub with interpreter '{Interpreter}'", interpreter);
+ return;
+ }
+
+ for (var attempt = 0; attempt < PostLaunchMaxAttempts; attempt++)
+ {
+ await Task.Delay(PostLaunchPollIntervalMs).ConfigureAwait(false);
+ if (await IsHealthyAsync().ConfigureAwait(false))
+ {
+ Log.Information("Panel hub is up on 127.0.0.1:{Port}", Port);
+ return;
+ }
+ }
+
+ Log.Warning(
+ "Launched the panel hub but it never became reachable on 127.0.0.1:{Port} after {Attempts} attempts. " +
+ "Check %LOCALAPPDATA%\\AECModelBridge or the process's own logs for why it failed to start.",
+ Port, PostLaunchMaxAttempts);
+ }
+
+ private static async Task<bool> IsHealthyAsync()
+ {
+ try
+ {
+ using var response = await HealthClient.GetAsync($"http://127.0.0.1:{Port}/health").ConfigureAwait(false);
+ return response.IsSuccessStatusCode;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Finds a Python interpreter capable of running revit_mcp_server, preferring
+ /// an explicit override, then falling back to whatever's on PATH. Does not
+ /// search for a project .venv, since the add-in has no reliable way to know
+ /// where the hub's source checkout lives once installed — that's exactly the
+ /// gap the bundled-runtime installer (D-010) is meant to close.
+ /// </summary>
+ private static string? ResolvePythonInterpreter()
+ {
+ var overridePath = Environment.GetEnvironmentVariable("AEC_MODEL_BRIDGE_PYTHON");
+ if (!string.IsNullOrWhiteSpace(overridePath))
+ {
+ return overridePath;
+ }
+
+ foreach (var candidate in new[] { "python", "py" })
+ {
+ if (CanRun(candidate))
+ {
+ return candidate;
+ }
+ }
+
+ return null;
+ }
+
+ private static bool CanRun(string command)
+ {
+ try
+ {
+ using var process = Process.Start(new ProcessStartInfo
+ {
+ FileName = command,
+ Arguments = "--version",
+ UseShellExecute = false,
+ CreateNoWindow = true,
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ });
+ process?.WaitForExit(2000);
+ return process is { HasExited: true, ExitCode: 0 };
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Kills the hub process only if this launcher started it — a hub that was
+ /// already running before Revit started (a developer's own instance) is left
+ /// untouched, matching the same restraint EnsureRunning applies at startup.
+ /// </summary>
+ public void StopIfLaunched()
+ {
+ if (_launchedProcess == null)
+ {
+ return;
+ }
+
+ try
+ {
+ if (!_launchedProcess.HasExited)
+ {
+ _launchedProcess.Kill();
+ Log.Information("Stopped the panel hub process this launcher started (pid {Pid})", _launchedProcess.Id);
+ }
+ }
+ catch (Exception ex)
+ {
+ Log.Warning(ex, "Failed to stop the panel hub process cleanly");
+ }
+ finally
+ {
+ _launchedProcess.Dispose();
+ _launchedProcess = null;
+ }
+ }
+}