Skip to content

Deploy tools 5/6: serve them on the remote server - #258

Closed
raymondk wants to merge 1 commit into
deploy-core-4-tools-localfrom
deploy-core-5-remote
Closed

raymondk wants to merge 1 commit into
deploy-core-4-tools-localfrom
deploy-core-5-remote

Conversation

@raymondk

Copy link
Copy Markdown
Contributor

Fifth of six. Stacked on the tools PR.

What this PR adds

The deploy tools on the imcp2 server. An embedder turns them on with McpServer::with_deploy; the bundled binary always serves them.

  • Upload route. PUT {mcp_path}/deploy/upload on a ticket from icp_deploy_upload. The route is outside the bearer gate, since the ticket is its authentication. The body is extracted as it arrives: 413 when the archive or its extracted size passes its limit, 422 when it is not a bundle or carries a sync plugin the server does not run. The deploy tool here refuses a path.
  • Progress streaming. A deploy call is answered as an event stream so each phase reaches the client before the result. Every other request keeps the plain JSON answer. A client that disconnects stops its deploy.
  • One deploy state per process (Deploys). Slots, the upload store and the stopping signal are shared by every endpoint. It locks its directory, so a second server over the same state directory is refused instead of clearing the first one's uploads.
  • Shutdown. Deploys::stop() before the drain, Deploys::stopped().await after it, then shutdown(). The second call matters because a deploy whose client has gone holds no connection for a drain to wait on. spawn_session_reaper and shutdown cover the sweeper and the stop for an embedder that does nothing more.
  • Configuration is DeployConfig. The library reads no environment variables; the bundled binary maps MCP_DEPLOY_* onto it and refuses to start on contradictory values.

Behaviour change for the bundled binary

It now always serves the deploy tools. There is no cargo feature and no runtime switch. Anyone running the imcp2 binary gets the upload endpoint and deploy tools, behind Internet Identity sign-in and the limits above.

What the deploy tools hold in memory

On a RAM-backed state directory, the upload store's budget is the bound: by default one 128 MiB bundle per concurrent deploy plus one, so 384 MiB with the default of two deploys. A running deploy also reads one module at a time onto the heap.

Open question carried over from #243

A reviewer asked whether the upload works through the IC gateways (thread). I have not tested it there. Things to check on the gateway side: any request-body size limit or timeout in front of the MCP routes, and that a streamed PUT body is passed through unbuffered.

For the gateway's integration

  • Deploys::new returns a Result; it fails when another server owns the state directory or an allowed-plugin entry is not a SHA-256.
  • The three shutdown calls above.

Testing

The whole loop through the router (ticket, upload, refusals, deploy as an event stream), shared state across two endpoints, the directory lock, the lifecycle hooks, the bounded shutdown wait, and configuration parsing. Not tested: a real HTTP client's view of an upload stopped mid-body, and a restart during a real deploy.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV

@raymondk
raymondk requested a review from a team as a code owner November 19, 2026 02:52
Copilot AI max review requested due to automatic review settings November 19, 2026 02:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical upload-lifecycle and quota-accounting flaws can delete directories still used by workers and allow concurrent uploads or retained records to bypass resource limits.

6 open issues
What changed in this PR

Integrates the existing core deploy tools into the hosted MCP server. Embedders opt in with McpServer::with_deploy; the bundled binary always enables them.

Changes:

  • Adds ticket-authenticated uploads and streamed deploy progress, with cancellation when clients disconnect.
  • Shares deploy limits, storage, directory locking, cleanup, and shutdown state across endpoints.
  • Adds binary configuration, integration tests, and deployment documentation.
File Description
src/​main.rs Configures shared deploy state and coordinates shutdown.
src/​lib.rs Adds optional deploy integration and lifecycle hooks.
src/​deploy.rs Implements upload routing, progress streaming, shared state, and tests.
src/​auth.rs Adds a test-only bearer-token helper.
README.md Documents hosted deployment behavior and configuration.
deploy/​native/​README.md Documents operational limits, upload timeouts, and recovery records.
deploy/​native/​imcp2.service Updates shutdown comments; service settings are unchanged.
Cargo.toml Adds streaming support and deploy-test dependencies.
Cargo.lock Updates the root package’s dependency entries.

Review effort: Max

Comment thread src/deploy.rs
///
/// Also an error: an entry of [`DeployConfig::allowed_plugins`] that is
/// not a sha256. A list read wrong is not run with.
pub fn new(config: &DeployConfig, state_dir: &Path) -> std::io::Result<Self> {
Comment thread src/deploy.rs
/// One sweep; see [`Self::spawn_sweeper`].
fn sweep(&self) {
self.uploads.sweep();
let removed = imcp2_core::deploy::remove_old_deploy_records(&self.dir, DEPLOY_RECORD_TTL);
Comment thread src/deploy.rs
json: service(transport.clone().with_json_response(true)),
sse: service(transport.with_json_response(false)),
uploads,
_owned: deploys._owned.clone(),
Comment thread src/deploy.rs
Comment on lines +239 to +240
let dir = state_dir.join("deploys");
let owned = own_directory(&dir)?;
Comment thread src/deploy.rs
Comment on lines +501 to +504
let reader = tokio_util::io::StreamReader::new(
request.into_body().into_data_stream().map_err(std::io::Error::other),
);
let received = mcp.uploads.receive(&ticket, declared, reader).await;
Comment thread src/main.rs
Comment on lines +150 to +153
anyhow::ensure!(
config.max_upload_bytes <= config.max_extracted_bytes,
"MCP_DEPLOY_MAX_UPLOAD_BYTES ({}) must be at most MCP_DEPLOY_MAX_EXTRACTED_BYTES ({}): \
an archive is never larger than what it extracts to",
…loaded on a ticket

The deploy tools on the `imcp2` server, for an embedder that turns them on
(`McpServer::with_deploy`); the bundled binary always does.

- **How the bundle arrives.** `icp_deploy_upload` mints a ticket inside an
  authenticated call, and the archive is `PUT` to `{mcp_path}/deploy/upload`
  on it. The route is outside the bearer gate — the ticket is its
  authentication — and the body is extracted as it arrives: 413 at the
  byte that takes the archive, or what it extracts to, past its limit, and
  422 for what is not a bundle or carries a sync plugin the server does
  not run. The deploy tool here refuses a path: the server opens no file a
  caller names.
- **A deploy call is answered as an event stream**, so each phase reaches
  the client before the result; every other request keeps the plain JSON
  answer. A client that goes away stops its deploy.
- **One deploy state for the process** (`Deploys`): the slots, the upload
  store and the word that the server is stopping are shared by every
  endpoint. It locks its directory, so a second server over the same
  state directory is refused rather than clearing the first one's
  uploads; clears what a killed process left; and sweeps what has lapsed.
- **Stopping.** `Deploys::stop` before the drain tells running deploys to
  end and refuses new ones, and `Deploys::stopped` waits, bounded, for
  them to have ended: a deploy waiting on a canister call whose client
  has gone holds no connection for a drain to wait on. The server's own
  `spawn_session_reaper` and `shutdown` cover the sweeper and the stop for
  an embedder that does nothing more.
- **Configuration** is `DeployConfig`: deploys at once, the archive and
  extracted limits, the budget for every bundle held, and the sync plugins
  run besides the ones the server knows. The library reads no environment
  variable; the bundled binary maps `MCP_DEPLOY_*` onto it and refuses to
  start on values that contradict one another.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
@raymondk
raymondk force-pushed the deploy-core-5-remote branch from 193629e to cd5bd52 Compare November 19, 2026 16:21
@aterga aterga closed this Nov 19, 2026
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.

3 participants