Skip to content

feat(server): add port 0 binding and broker registration #60

Open

Description

@CalvinAllen

Parent Epic

Part of #18 - STDIO Transport Support (Broker + Shim Architecture)

Description

Modify Server.exe to support broker mode: bind to an OS-assigned port (port 0) and register with the broker on startup, deregister on shutdown, and re-register when the solution changes.

Implementation

Modify: src/CodingWithCalvin.MCPServer.Server/Program.cs

New CLI Option

var brokerPortOption = new Option<int>(
    name: "--broker-port",
    getDefaultValue: () => 0,
    description: "Broker port to register with (0 = standalone mode, no broker)");

Broker Mode Logic (--broker-port > 0)

  1. Bind http://localhost:0 instead of configured port (OS assigns available port)
  2. Split app.RunAsync() into app.StartAsync() + app.WaitForShutdownAsync() to get the actual port
  3. Read actual port from IServerAddressesFeature after start
  4. Fetch solution info via RPC: rpcClient.GetSolutionInfoAsync()
  5. POST InstanceRegistration to http://localhost:{brokerPort}/api/instances
  6. On ApplicationStopping: DELETE http://localhost:{brokerPort}/api/instances/{instanceId} (best-effort, short timeout)

Standalone Mode (--broker-port == 0)

Existing behavior, completely unchanged. Uses --port value as before.

Modify: src/CodingWithCalvin.MCPServer.Server/RpcClient.cs

Implement the new IServerRpc.OnSolutionChangedAsync(...):

  • Store broker URL and instance ID as fields
  • On call: re-POST registration to broker with updated solution info (upsert by instanceId)

Registration Payload

{
    "instanceId": "generated-guid",
    "solutionPath": "C:/repos/MyApp/MyApp.sln",
    "solutionDirectory": "C:/repos/MyApp",
    "displayName": "Visual Studio MCP",
    "vsPid": 12345,
    "serverPort": 52341,
    "serverHost": "localhost"
}
  • instanceId: new GUID generated at Server.exe startup
  • solutionPath/solutionDirectory: from GetSolutionInfoAsync() (nullable if no solution open)
  • displayName: from --name CLI arg
  • vsPid: parsed from pipe name (vsmcp-{pid})
  • serverPort: actual bound port from Kestrel

Dependencies

  • Requires Phase 1 (shared models + RPC contract)
  • Requires Phase 2 (broker with registration endpoints)

Acceptance Criteria

  • --broker-port CLI option added
  • Standalone mode (--broker-port 0) works exactly as before
  • Broker mode binds port 0 and reads actual assigned port
  • Registration POST sent to broker on startup
  • Deregistration DELETE sent to broker on shutdown
  • OnSolutionChangedAsync re-registers with updated solution info
  • Graceful handling when broker is unreachable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions