# adner/azurecontainerappssandboxes architecture

> Architecture diagram of the GitHub repository adner/azurecontainerappssandboxes, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adner/azurecontainerappssandboxes
- Repository: https://github.com/adner/azurecontainerappssandboxes

## Overview

#### Purpose and entry points
Experimental host-side MCP service for delegating prompts to isolated, ephemeral Azure Container Apps Sandboxes, then streaming and aggregating worker outputs. Start the service with `scripts/run_mcp_server.py`; direct fan-out execution is available through `scripts/run_fanout_demo.py`. The MCP endpoint exposes `run_agents` and `await_results`.

#### MCP host and run boundary
`src/mcp_server/app.py` initializes the host MCP application. `src/mcp_server/registry.py` owns asynchronous run state, worker events/results, and replayable streams. `src/mcp_server/http_routes.py` serves run-oriented HTTP routes consumed by the live app panel. This process is the orchestration and client-facing boundary; it is not production-authenticated.
- `src/mcp_server/app.py`
- `src/mcp_server/registry.py`
- `src/mcp_server/http_routes.py`

#### Fan-out lifecycle
The orchestration library converts delegated prompts into one sandbox worker each, creates workers from a configured warm snapshot, reasserts egress/exposes worker ports, consumes their SSE output, aggregates completion, and cleans up sandboxes regardless of outcome. The ACA CLI and configured Azure subscription/resource group/sandbox group are infrastructure dependencies.
- `src/sandbox_fanout/fanout.py`
- `src/sandbox_fanout/lifecycle.py`
- `src/sandbox_fanout/backend.py`

#### Worker execution plane
A snapshot contains a single-use FastAPI worker agent. Each worker accepts its assigned prompt, invokes OpenAI model tooling, can execute local Python/shell tools, and emits trace, final result, and optional visualization events over SSE. Workers are hardware-isolated ACA Sandboxes and are intended to be deleted after the batch.
- `src/sandbox_agent/server.py`
- `src/sandbox_agent/agent.py`
- `scripts/build_snapshot.sh`

#### Event and result flow
Client request → MCP host creates a registry run → fan-out provisions workers → each worker streams SSE events → fan-out/registry persist and relay events → MCP client polls/awaits results while the UI consumes `/runs/*` streams. Results retain textual answers and constrained, renderer-safe visualization payloads rather than allowing arbitrary UI content.
- `src/sandbox_fanout/sse.py`
- `src/sandbox_fanout/models.py`
- `src/mcp_server/registry.py`

#### MCP App presentation
The bundled MCP App renders a live pane per worker, displays traces, and validates/renders ECharts visualizations. The built panel is committed for host serving; TypeScript source supports UI iteration.
- `src/mcp_server/ui/dist/panel.html`
- `src/mcp_server/ui/src/main.ts`
- `src/mcp_server/ui/src/visualization.ts`

#### Optional Dataverse delegation
Dataverse access is optional and deliberately host-owned: encrypted MSAL/PKCE state remains outside snapshots, while workers receive only short-lived delegated tokens for a batch. Workers use OpenAI’s optional native remote-MCP path to reach the Dataverse MCP server. This is a single operator identity model, not multi-user authorization.
- `src/dataverse_auth/provider.py`
- `src/dataverse_auth/config.py`
- `scripts/auth_dataverse.py`

#### M365 integration and security posture
`FanoutAgent` packages an optional Microsoft 365 Copilot declarative-agent wrapper that calls the host MCP service. Deployment commonly requires a protected tunnel, but the current implementation has no production-grade inbound auth; exposed worker ports and run streams are explicitly unsafe for public or sensitive deployments.
- `FanoutAgent/appPackage/manifest.json`
- `FanoutAgent/appPackage/mcp-tools.json`
- `cloud_auth_spec.md`

Main entry points: [`src/mcp_server/app.py`](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/app.py) (MCP application), [`src/sandbox_agent/server.py`](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_agent/server.py) (Worker API), [`src/sandbox_fanout/fanout.py`](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/fanout.py) (Fanout orchestration), [`src/dataverse_auth/provider.py`](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/dataverse_auth/provider.py) (Delegated token provider)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Host MCP service"]
  node_mcp_entry["MCP server launcher<br/>Python script<br/>[run_mcp_server.py]"]
  node_mcp_app["MCP application<br/>MCP host<br/>[app.py]"]
  node_registry[("Run registry<br/>async state store<br/>[registry.py]")]
  node_http_routes["Run HTTP routes<br/>HTTP API<br/>[http_routes.py]"]
end

subgraph group_fanout["Sandbox fan-out"]
  node_fanout_demo["Fan-out demo<br/>Python script<br/>[run_fanout_demo.py]"]
  node_fanout["Fanout orchestration<br/>orchestration library<br/>[fanout.py]"]
  node_lifecycle["Sandbox lifecycle<br/>ACA provisioning<br/>[lifecycle.py]"]
  node_backend{{"Azure backend<br/>ACA CLI adapter<br/>[backend.py]"}}
  node_sse["Worker SSE consumer<br/>event stream<br/>[sse.py]"]
  node_result_models["Result models<br/>safe payload schema<br/>[models.py]"]
end

subgraph group_worker["Worker execution plane"]
  node_snapshot_build["Worker snapshot build<br/>build script<br/>[build_snapshot.sh]"]
  node_worker_server["Worker API<br/>FastAPI worker<br/>[server.py]"]
  node_worker_agent["Worker agent<br/>OpenAI tool agent<br/>[agent.py]"]
end

subgraph group_ui["Live app panel"]
  node_panel_dist["Served panel<br/>bundled HTML<br/>[panel.html]"]
  node_panel_main["Panel application<br/>TypeScript UI<br/>[main.ts]"]
  node_visualization["Visualization renderer<br/>ECharts validator<br/>[visualization.ts]"]
end

subgraph group_dataverse["Optional Dataverse delegation"]
  node_dataverse_provider["Delegated token provider<br/>MSAL/PKCE auth<br/>[provider.py]"]
  node_dataverse_config["Dataverse auth config<br/>auth configuration<br/>[config.py]"]
  node_dataverse_auth["Dataverse auth bootstrap<br/>operator script<br/>[auth_dataverse.py]"]
end

subgraph group_m365["M365 integration"]
  node_m365_manifest["Copilot agent package<br/>M365 declarative agent<br/>[manifest.json]"]
  node_m365_tools["MCP tool declaration<br/>Copilot tool config<br/>[mcp-tools.json]"]
  node_security["Deployment security posture<br/>security documentation<br/>[cloud_auth_spec.md]"]
end

node_mcp_entry -->|"starts"| node_mcp_app
node_mcp_app -->|"creates and awaits runs"| node_registry
node_mcp_app -->|"delegates prompts"| node_fanout
node_fanout_demo -->|"invokes directly"| node_fanout
node_fanout -->|"provisions and cleans up"| node_lifecycle
node_lifecycle -->|"uses ACA CLI"| node_backend
node_snapshot_build -->|"builds snapshot containing"| node_worker_server
node_lifecycle -->|"creates isolated workers"| node_worker_server
node_worker_server -->|"executes assigned prompt"| node_worker_agent
node_worker_server -->|"streams SSE events"| node_sse
node_sse -->|"persists and relays"| node_registry
node_fanout -->|"aggregates typed results"| node_result_models
node_result_models -->|"stores completion"| node_registry
node_mcp_app -->|"serves"| node_http_routes
node_http_routes -->|"reads run streams"| node_registry
node_panel_dist -->|"bundles"| node_panel_main
node_panel_main -->|"consumes /runs streams"| node_http_routes
node_panel_main -->|"renders safe payloads"| node_visualization
node_dataverse_auth -->|"establishes operator auth"| node_dataverse_provider
node_dataverse_provider -->|"uses"| node_dataverse_config
node_mcp_app -.->|"issues batch delegated tokens"| node_dataverse_provider
node_dataverse_provider -.->|"passes short-lived token"| node_worker_agent
node_m365_manifest -->|"includes tool contract"| node_m365_tools
node_m365_tools -->|"calls host MCP"| node_mcp_app
node_security -.->|"documents inbound-auth risk"| node_mcp_app
node_security -.->|"documents exposed-port risk"| node_worker_server

click node_mcp_entry "https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/run_mcp_server.py"
click node_mcp_app "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/app.py"
click node_registry "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/registry.py"
click node_http_routes "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/http_routes.py"
click node_fanout_demo "https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/run_fanout_demo.py"
click node_fanout "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/fanout.py"
click node_lifecycle "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/lifecycle.py"
click node_backend "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/backend.py"
click node_sse "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/sse.py"
click node_result_models "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/models.py"
click node_snapshot_build "https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/build_snapshot.sh"
click node_worker_server "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_agent/server.py"
click node_worker_agent "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_agent/agent.py"
click node_panel_dist "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/dist/panel.html"
click node_panel_main "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/src/main.ts"
click node_visualization "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/src/visualization.ts"
click node_dataverse_provider "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/dataverse_auth/provider.py"
click node_dataverse_config "https://github.com/adner/azurecontainerappssandboxes/blob/main/src/dataverse_auth/config.py"
click node_dataverse_auth "https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/auth_dataverse.py"
click node_m365_manifest "https://github.com/adner/azurecontainerappssandboxes/blob/main/FanoutAgent/appPackage/manifest.json"
click node_m365_tools "https://github.com/adner/azurecontainerappssandboxes/blob/main/FanoutAgent/appPackage/mcp-tools.json"
click node_security "https://github.com/adner/azurecontainerappssandboxes/blob/main/cloud_auth_spec.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mcp_entry,node_mcp_app,node_registry,node_http_routes toneBlue
class node_fanout_demo,node_fanout,node_lifecycle,node_backend,node_sse,node_result_models toneAmber
class node_snapshot_build,node_worker_server,node_worker_agent toneMint
class node_panel_dist,node_panel_main,node_visualization toneRose
class node_dataverse_provider,node_dataverse_config,node_dataverse_auth toneIndigo
class node_m365_manifest,node_m365_tools,node_security toneTeal
```

## Components

### Host MCP service

Client-facing orchestration boundary.

- [MCP server launcher](https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/run_mcp_server.py): Python script · `scripts/run_mcp_server.py`
- [MCP application](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/app.py): MCP host · Exposes run_agents and await_results. · `src/mcp_server/app.py`
- [Run registry](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/registry.py): async state store · Stores runs, worker events, results, and replayable streams. · `src/mcp_server/registry.py`
- [Run HTTP routes](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/http_routes.py): HTTP API · Serves live-panel run streams. · `src/mcp_server/http_routes.py`

### Sandbox fan-out

Creates, streams, aggregates, and removes workers.

- [Fan-out demo](https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/run_fanout_demo.py): Python script · `scripts/run_fanout_demo.py`
- [Fanout orchestration](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/fanout.py): orchestration library · Maps delegated prompts to sandbox workers. · `src/sandbox_fanout/fanout.py`
- [Sandbox lifecycle](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/lifecycle.py): ACA provisioning · Creates from snapshots and always cleans up. · `src/sandbox_fanout/lifecycle.py`
- [Azure backend](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/backend.py): ACA CLI adapter · Uses configured Azure subscription, resource group, and sandbox group. · `src/sandbox_fanout/backend.py`
- [Worker SSE consumer](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/sse.py): event stream · `src/sandbox_fanout/sse.py`
- [Result models](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_fanout/models.py): safe payload schema · Constrains renderer-safe visualization payloads. · `src/sandbox_fanout/models.py`

### Worker execution plane

Ephemeral isolated sandbox agents.

- [Worker snapshot build](https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/build_snapshot.sh): build script · `scripts/build_snapshot.sh`
- [Worker API](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_agent/server.py): FastAPI worker · Single-use prompt endpoint and SSE event producer. · `src/sandbox_agent/server.py`
- [Worker agent](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/sandbox_agent/agent.py): OpenAI tool agent · Uses model tooling plus local Python and shell tools. · `src/sandbox_agent/agent.py`

### Live app panel

- [Served panel](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/dist/panel.html): bundled HTML · Committed panel served by the host. · `src/mcp_server/ui/dist/panel.html`
- [Panel application](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/src/main.ts): TypeScript UI · Renders a live pane per worker. · `src/mcp_server/ui/src/main.ts`
- [Visualization renderer](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/mcp_server/ui/src/visualization.ts): ECharts validator · Validates and renders visualization events. · `src/mcp_server/ui/src/visualization.ts`

### Optional Dataverse delegation

Host-owned delegated authorization.

- [Delegated token provider](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/dataverse_auth/provider.py): MSAL/PKCE auth · Keeps encrypted authorization state on the host. · `src/dataverse_auth/provider.py`
- [Dataverse auth config](https://github.com/adner/azurecontainerappssandboxes/blob/main/src/dataverse_auth/config.py): auth configuration · `src/dataverse_auth/config.py`
- [Dataverse auth bootstrap](https://github.com/adner/azurecontainerappssandboxes/blob/main/scripts/auth_dataverse.py): operator script · `scripts/auth_dataverse.py`

### M365 integration

- [Copilot agent package](https://github.com/adner/azurecontainerappssandboxes/blob/main/FanoutAgent/appPackage/manifest.json): M365 declarative agent · Calls the host MCP service. · `FanoutAgent/appPackage/manifest.json`
- [MCP tool declaration](https://github.com/adner/azurecontainerappssandboxes/blob/main/FanoutAgent/appPackage/mcp-tools.json): Copilot tool config · `FanoutAgent/appPackage/mcp-tools.json`
- [Deployment security posture](https://github.com/adner/azurecontainerappssandboxes/blob/main/cloud_auth_spec.md): security documentation · No production-grade inbound authentication; public exposure is unsafe. · `cloud_auth_spec.md`

## Connections

- MCP server launcher → MCP application: starts
- MCP application → Run registry: creates and awaits runs
- MCP application → Fanout orchestration: delegates prompts
- Fan-out demo → Fanout orchestration: invokes directly
- Fanout orchestration → Sandbox lifecycle: provisions and cleans up
- Sandbox lifecycle → Azure backend: uses ACA CLI
- Worker snapshot build → Worker API: builds snapshot containing
- Sandbox lifecycle → Worker API: creates isolated workers
- Worker API → Worker agent: executes assigned prompt
- Worker API → Worker SSE consumer: streams SSE events
- Worker SSE consumer → Run registry: persists and relays
- Fanout orchestration → Result models: aggregates typed results
- Result models → Run registry: stores completion
- MCP application → Run HTTP routes: serves
- Run HTTP routes → Run registry: reads run streams
- Served panel → Panel application: bundles
- Panel application → Run HTTP routes: consumes /runs streams
- Panel application → Visualization renderer: renders safe payloads
- Dataverse auth bootstrap → Delegated token provider: establishes operator auth
- Delegated token provider → Dataverse auth config: uses
- MCP application → Delegated token provider: issues batch delegated tokens
- Delegated token provider → Worker agent: passes short-lived token
- Copilot agent package → MCP tool declaration: includes tool contract
- MCP tool declaration → MCP application: calls host MCP
- Deployment security posture → MCP application: documents inbound-auth risk
- Deployment security posture → Worker API: documents exposed-port risk
