Report progress from a long-running capability
Use this pattern when a capability can run longer than a caller’s patience or its client timeout. Many MCP clients abandon a silent tool call after about a minute, while an image or report generator may legitimately need several.
This recipe follows the repository’s image-engine example. Each of its
capabilities waits on a single provider request with a 150- to 180-second
timeout, so it reports liveness while that request is pending.
Report from the capability
Section titled “Report from the capability”-
Wrap the slow request
Create
src/capabilities/provider-progress-contract.ts:import type { ExecutionContext } from "@invokta/core";export const providerProgressIntervalMs = 15_000;export async function reportWhileProviderWorks<Value>(context: ExecutionContext,message: string,work: () => Promise<Value>,): Promise<Value> {let elapsedSeconds = 0;context.reportProgress({ progress: elapsedSeconds, message });const heartbeat = setInterval(() => {elapsedSeconds += providerProgressIntervalMs / 1_000;context.reportProgress({ progress: elapsedSeconds, message });}, providerProgressIntervalMs);try {return await work();} finally {clearInterval(heartbeat);}}The provider exposes no completion percentage, so
progresscounts elapsed seconds and omitstotal. When the work has countable steps, report the step number with atotalinstead. -
Use it inside
runasync run({ input, context }) {const images = await reportWhileProviderWorks(context,`Generating ${String(input.count)} campaign images.`,() => generator.generateSeries(input, { signal: context.signal }),);return { images };}The capability does not know which channel invoked it. Without a listening caller,
reportProgressis a no-op.
Observe it from each channel
Section titled “Observe it from each channel”Direct callers pass a sink in the invocation options:
await engine.invoke( "image.generate-campaign-series", { prompt: "A coherent launch series.", count: 3 }, { principal, onProgress: ({ progress, message }) => { console.error(`${progress}s ${message ?? ""}`); }, },);An MCP client sends a progress token with tools/call; the official SDK client
does that when the call supplies onprogress. Ask it to reset its own timeout
on progress so a long call keeps waiting:
await client.callTool( { name: "image_generate-campaign-series", arguments }, undefined, { onprogress: (progress) => console.error(progress), resetTimeoutOnProgress: true, maxTotalTimeout: 200_000, },);Over MCP HTTP, that one request is answered with a text/event-stream
response that carries its notifications and then its result. A CLI composed
with format: "human" writes one progress <progress>[/<total>][: <message>]
line per report to standard error; the default JSON format ignores progress.
The plain HTTP adapter @invokta/http answers with one JSON body and does not
deliver progress.
Run it
Section titled “Run it”From the repository root, build the example and start its stdio adapter from an MCP host whose client sends a progress token, such as the official SDK client call above:
yarn buildexport OPENAI_API_KEY='sk-...'export ARK_API_KEY='...'export GEMINI_API_KEY='...'node examples/image-engine/dist/mcp-stdio.jsThe first notification arrives as soon as the provider request starts, and one more follows every 15 seconds until the image is returned.
Verify it
Section titled “Verify it”The example tests the heartbeat with fake timers, its end after the provider request settles, and delivery through the built stdio and HTTP adapters with local provider stubs:
yarn workspace @invokta/example-image testRead the @invokta/core progress reference for
validation and lifetime rules, the
MCP progress reference for tokens,
rate limiting, and the HTTP response stream, and the complete
image-engine
example for the capabilities and their tests.