Skip to content

imcp2-deploy 6/6: serve the deploy tools on the hosted server, with the bundle uploaded on a ticket - #243

Closed
raymondk wants to merge 2 commits into
deploy-3-mcp-toolsfrom
deploy-5-hosted
Closed

raymondk wants to merge 2 commits into
deploy-3-mcp-toolsfrom
deploy-5-hosted

Conversation

@raymondk

@raymondk raymondk commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The hosted composition: the deploy tools on the MCP endpoint of the imcp2 binary, behind a deploy cargo feature that the native build turns on. Everything in crates/imcp2-deploy is already in place below this PR; this one is the root crate.

  • src/deploy.rs, under #[cfg(feature = "deploy")], with McpServer::with_deploy(DeployConfig). The binary reads MCP_DEPLOY_MAX_CONCURRENT (default 2) and MCP_DEPLOY_MAX_UPLOAD_BYTES (default 64 MiB).

How the bundle arrives

  1. icp_deploy_upload mints a ticket inside the authenticated call and returns the upload URL, the ticket and the curl line to run.
  2. The agent runs it. PUT {mcp_path}/deploy/upload sits outside the bearer gate (the ticket is its authentication), streams the body to disk under the cap, refuses a declared length over it unread, and answers 201 with the upload id.
  3. icp_deploy_bundle takes bundle_upload; the archive is removed with the deploy. This server lists no bundle_path and refuses one.

The ticket's properties are #247.

How the deploy call is answered

The endpoint answers a POST with plain JSON, the one message the handler returns; in rmcp's JSON mode a notification emitted before the result would be returned as the result. So the dispatch reads the JSON-RPC method and tool name off the buffered body and hands a tools/call of icp_deploy_bundle to a second StreamableHttpService configured for SSE responses, still stateless. Everything else keeps the JSON service, so existing clients see no change.

A client that goes away

The SSE response body is wrapped (cancel_on_drop) so that its drop, which is what a disconnect does on the server, cancels a token placed in the request's extensions as ClientGone. The deploy stops at its next step and keeps its directory as the record.

Where things live

Uploads and working directories are under {state_dir}/deploys, resolved to absolute paths when the server starts.

What changed in this PR at the re-cut

Review so far

aterga's comments on this PR about the instructions, the skill and the slot count are answered in #232, where that text now lives; the ones about the ticket are carried to #247. Still open here:

  • How a malicious client exhausting every deploy slot is mitigated. Today: one deploy per session, a cap across sessions, refusal past it. Nothing more.
  • Whether the upload route works through the IC gateways (blind-oracle).

Caveats

  • Publishing. imcp2-deploy is not published, so the published imcp2 cannot carry the deploy feature until it is.
  • Plugin memory. The wasmtime store has no memory limiter here; that is a change to make in icp-cli. Until then a crafted bundle's plugin can grow to the process's MemoryMax.
  • Caddy's read_body 30s applies to the upload PUT; the native README says what that means for the cap.
  • No deploy metrics yet.
  • AuthStore::test_bearer is the same cfg(test) helper Cap the MCP request body behind the bearer gate #241 adds, with an identical hunk.

Changes

  • src/deploy.rs (221 lines of code, 241 of tests), src/lib.rs, src/main.rs, src/auth.rs.
  • Root Cargo.toml: the deploy feature and its dependencies.
  • .github/workflows/ci.yml: a test run with the feature. deploy/native/build.sh builds with it.
  • README.md, deploy/native/README.md.

Testing

  • cargo build --locked --workspace --all-targets
  • cargo test --locked --workspace --all-targets
  • cargo test --locked -p imcp2 --features deploy --all-targets
  • cargo clippy --locked --workspace --all-targets --features deploy (clean)
  • cargo fmt --all -- --check

src/deploy.rs tests the whole loop over the composed router with a minted test token: tools/list stays JSON and lists the deploy tools with bundle_upload alone; icp_deploy_upload returns a ticket; the PUT is refused without a ticket (401), on a wrong one (403), over the cap (413), accepted once (201) and spent; a bundle_path is refused without spending the upload; the deploy call is answered as text/event-stream carrying the result; an unauthenticated deploy call still gets the 401. Plus cancel_on_drop firing on body drop, and the dispatch predicate.

The stack

Six stacked PRs that bring icp deploy to the MCP servers through the icp CLI's own project library. Re-cut on 2026-11-18 so that each file arrives once, in the form it has at the top; the tree at the top is unchanged from before the re-cut, rebased onto current main.

PR What
1 #230 The crate and the icp-project host for an untrusted bundle
2 #246 Signing: canister calls over ic-agent, an identity that refreshes
3 #231 The deploy pipeline, its events and its interruption
4 #247 The upload-ticket store
5 #232 The MCP tools, and the local server serving them
6 #243 The hosted server serving them

#242 (streamed phases, interruption) and #245 (one bundle source per server, crates.io) are closed: their changes are folded into the PRs above.

Checklist

  • I have read the Contributing guidelines.
  • Docs (README / comments) updated for any user-visible change.
  • No secrets, credentials, or internal-only information are included.

🤖 Generated with Claude Code

@raymondk
raymondk added this pull request to stack #233 November 12, 2026 21:05
Comment thread crates/imcp2-deploy/src/server.rs Resolved
Comment thread crates/imcp2-deploy/src/server.rs Resolved
Comment thread crates/imcp2-deploy/src/server.rs Resolved
Comment thread crates/imcp2-deploy/src/server.rs Resolved

@aterga aterga left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How could we mitigate a DoS attack from a malicious MCP client exhausting all the available deploy slots?

Comment thread crates/imcp2-deploy/src/uploads.rs Resolved
}

/// `dir`, created if missing, and on Unix readable by its owner alone.
fn make_private_dir(dir: &Path) -> std::io::Result<()> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@blind-oracle could you check if this works with the IC gateways, please?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe a dumb question, but what's the point of having a skill here?

Shouldn't the MCP tools be self-documenting?

Who would consume this file?

Comment thread crates/imcp2-deploy/src/server.rs Resolved
Comment thread crates/imcp2-deploy/src/server.rs Resolved

@aterga aterga left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note to self: Check if the curl approach is reasonable.

Comment thread crates/imcp2-deploy/src/server.rs Outdated
tools act as. It needs an \"Actions & questions\" session, {concurrency}, lasts as \
long as the deploy (send a progressToken to see each phase as it completes; \
cancelling the call interrupts the deploy at its next step), and returns the \
canister-id mapping to pass back next time. {bundle} The `skill://{DEPLOY_SKILL_NAME}` \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we abstract away and call this, e.g., "app development metadata"?

I'd rather the agent not look into the mapping too closely, but just dump it into a file and use it as an opaque blob. This would avoid hallucinations, and I don't think the agent actually needs to look into that metadata.

Comment thread crates/imcp2-deploy/src/server.rs Outdated
cancelling the call interrupts the deploy at its next step), and returns the \
canister-id mapping to pass back next time. {bundle} The `skill://{DEPLOY_SKILL_NAME}` \
resource walks through the loop: bundle, deploy, write the mapping back into the \
project; `icp_deploy_guide` returns the same text."

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please justify why we need to duplicate the deployment guide and consider avoiding the redundancy.

raymondk added a commit that referenced this pull request Nov 13, 2026
… or the slot count

From the review of #243.

The instructions no longer call the deploy tool an exception: where core's
closing paragraph says nothing here deploys (now the exported
DEPLOYS_ELSEWHERE), a server with the deploy tools puts the paragraph on how
deploying works. That paragraph is about a third of its length.

What an agent reads (the instructions, the description of
icp_cycles_balance, the skill) no longer names https://cli.id.ai or the icp
CLI's identity commands: the principal is the developer principal of the
user's identity, where cycles are sent and what is added as a controller.

How many deploys a server runs at once is no longer in the instructions. A
server that is full says to try again in a few minutes.

The canister ids are passed in and written back unchanged, as the app's
development metadata: the skill no longer shows the file's shape or asks for
a merge, since what the tool returns holds every id passed in.

The instructions point at icp_deploy_guide alone, not at it and the skill
resource.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@raymondk
raymondk marked this pull request as ready for review November 18, 2026 01:23
@raymondk
raymondk requested review from a team and a max review from Copilot November 18, 2026 01:23

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

Unresolved critical issues allow cross-session upload access, unbounded upload storage growth, and deletion of pre-existing state during tests.

9 open issues
What changed in this PR

Adds optional hosted deployment support to imcp2, reusing the local server’s deploy tools with ticket-based bundle uploads and streamed progress responses.

Changes:

  • Extracts shared deploy-handler composition for local and hosted servers.
  • Adds upload tickets, deploy concurrency controls, SSE responses, and disconnect cancellation.
  • Enables hosted deploys in native builds and adds tests, CI coverage, and documentation.
File Description
src/​main.rs Reads deploy configuration and enables it on hosted instances.
src/​lib.rs Adds optional deploy composition and upload-route integration.
src/​deploy.rs Implements uploads, JSON/SSE dispatch, and disconnect detection.
src/​auth.rs Adds a test-only bearer-token helper.
README.md Documents hosted deployment workflows and configuration.
deploy/​native/​README.md Documents deployment storage, limits, and upload timeouts.
deploy/​native/​build.sh Enables the deploy feature in native release builds.
crates/​imcp2-local/​src/​server.rs Reuses the shared deploy handler while retaining local login tools.
crates/​imcp2-local/​README.md Updates composition and deployment logging documentation.
crates/​imcp2-deploy/​static/​skills/​deploy-with-mcp.md Explains uploads, concurrency, and hosted recovery.
crates/​imcp2-deploy/​src/​uploads.rs Implements ticket and upload lifecycles with per-file limits.
crates/​imcp2-deploy/​src/​server.rs Adds shared tool, resource, and instruction composition.
crates/​imcp2-deploy/​src/​lib.rs Adds upload inputs, ticket issuance, concurrency accounting, and cancellation links.
crates/​imcp2-deploy/​src/​deploy.rs Handles upload-source validation and adds deployment tracing.
crates/​imcp2-deploy/​Cargo.toml Adds upload and logging-test dependencies.
Cargo.toml Declares the hosted deploy feature and dependencies.
Cargo.lock Updates dependency records.
.github/​workflows/​ci.yml Adds tests with the hosted deploy feature enabled.

Review effort: Max

Comment thread crates/imcp2-deploy/src/lib.rs Resolved
Comment thread crates/imcp2-deploy/src/lib.rs Resolved
Comment on lines +209 to +212
state.uploads.insert(
upload_id.clone(),
Upload { session: session.clone(), path, expires: Instant::now() + UPLOAD_TTL },
);
.and_then(|()| dir.canonicalize())
.or_else(|_| std::path::absolute(&dir))
.unwrap_or(dir);
Self { inner: Arc::new(Inner { dir, max_bytes, state: Mutex::default() }) }
make_private_dir(&self.inner.dir).map_err(|e| ReceiveError::Io(e.to_string()))?;
let upload_id = random_token();
let path = self.inner.dir.join(format!("{upload_id}.tar.gz"));
let received = write_up_to(&path, &mut body, max).await;
Comment thread deploy/native/build.sh
COPY src ./src
COPY crates ./crates
RUN cargo build --release
RUN cargo build --release --features deploy
Comment thread src/deploy.rs
Comment on lines +100 to +101
let deploy = IcDeployTools::new(self.identities.clone(), auth::bearer_session_resolver())
.concurrent(config.concurrent)
Comment thread src/deploy.rs
Comment on lines +143 to +146
let deploy_call = parts.method == Method::POST && is_deploy_call(&bytes);
let mut request = Request::from_parts(parts, Body::from(bytes));
if !deploy_call {
return mcp.json.handle(request).await.into_response();
Comment thread src/deploy.rs Resolved
@raymondk raymondk changed the title Serve the deploy tools on the hosted server, with the bundle uploaded on a ticket imcp2-deploy 6/6: serve the deploy tools on the hosted server, with the bundle uploaded on a ticket Nov 18, 2026

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.

Comment thread deploy/native/build.sh
COPY src ./src
COPY crates ./crates
RUN cargo build --release
RUN cargo build --release --features deploy

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #253, stacked on this: a hosted server now runs only the sync plugins it knows, by the SHA-256 of the module, checked on the upload and again at deploy preflight before anything is created. A bundle carrying any other plugin is refused. The runtime still has no memory limiter (that belongs upstream in icp-sync-plugin), but an unknown plugin is no longer run at all. Leaving this open until #253 merges.

Comment thread src/deploy.rs
#[tokio::test(flavor = "multi_thread")]
async fn the_hosted_endpoint_serves_the_deploy_loop() {
let (_dir, server) = server();
let bearer = format!("Bearer {}", server.store.test_bearer().await);
Comment thread src/main.rs
cimd_enabled: cimd_on,
});
#[cfg(feature = "deploy")]
let beta = beta.with_deploy(deploy.clone());
Copilot AI max review requested due to automatic review settings November 18, 2026 03:17
… on a ticket

Behind the `deploy` cargo feature, which the native build turns on, the
hosted `imcp2` mounts `WithDeploy` over the core surface. The agent cannot
hand this server a file, so the bundle arrives by upload: `icp_deploy_upload`
mints a ticket inside an authenticated call, and `PUT {mcp_path}/deploy/upload`
takes the archive on that ticket, outside the bearer gate and under the
upload cap.

The deploy call alone is answered as an event stream, so its phases reach
the client while it runs; a response body dropped by the client cancels the
deploy (`ClientGone`). Deploys run one per session and up to a configured
number across sessions. Uploads and working directories live under the
state directory, resolved to absolute paths when the server starts.

CI tests the feature in a run of its own.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

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.

Comment thread Cargo.toml
# feature. imcp2-deploy is not published, so the published `imcp2` cannot
# carry the feature until it is; a release build with the feature is a
# deployment's choice.
imcp2-deploy = { path = "crates/imcp2-deploy", optional = true }
Comment thread src/deploy.rs
transport: StreamableHttpServerConfig,
) -> (Router, Router) {
let deploys_dir = self.state_dir().join("deploys");
let uploads = Uploads::new(deploys_dir.join("uploads"), config.max_upload_bytes);
Comment thread src/deploy.rs
let gone = CancellationToken::new();
request.extensions_mut().insert(ClientGone(gone.clone()));
let response = mcp.sse.handle(request).await;
cancel_on_drop(response.into_response(), gone)
Comment thread src/deploy.rs
let reader = tokio_util::io::StreamReader::new(
request.into_body().into_data_stream().map_err(std::io::Error::other),
);
match mcp.uploads.receive(&ticket, declared, reader).await {
Comment thread deploy/native/build.sh
COPY src ./src
COPY crates ./crates
RUN cargo build --release
RUN cargo build --release --features deploy
Comment thread src/deploy.rs
return mcp.json.handle(request).await.into_response();
}
let gone = CancellationToken::new();
request.extensions_mut().insert(ClientGone(gone.clone()));
Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
Copilot AI max review requested due to automatic review settings November 18, 2026 03:31

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.

Comment thread src/deploy.rs
let gone = CancellationToken::new();
request.extensions_mut().insert(ClientGone(gone.clone()));
let response = mcp.sse.handle(request).await;
cancel_on_drop(response.into_response(), gone)
Comment thread src/deploy.rs
let reader = tokio_util::io::StreamReader::new(
request.into_body().into_data_stream().map_err(std::io::Error::other),
);
match mcp.uploads.receive(&ticket, declared, reader).await {
…e failure's status, and a test's cleanup

- An empty IMCP2_STATE_DIR reads as unset again, instead of panicking at
  startup where the path is resolved.
- MCP_DEPLOY_MAX_CONCURRENT=0 refuses to start, rather than being served
  as a cap of one.
- An upload the server could not store answers 500, not 400: ReceiveError
  tells a body that failed to arrive from a file that could not be written.
- The relative-deploy-directory test works in a directory of its own and
  no longer removes a `state` directory it did not make.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
Copilot AI max review requested due to automatic review settings November 18, 2026 17:44

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.

@raymondk

Copy link
Copy Markdown
Contributor Author

Superseded by the stack #254 → #259, which carries the same code in six PRs from main, with the deploy tools living in imcp2-core instead of a separate crate. The remote server is in #258. The four open review questions here are carried over, quoted and linked, in #257 and #258.

@raymondk raymondk 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