Skip to content

Async Jobs

github-actions[bot] edited this page Mar 18, 2026 · 11 revisions

Async Jobs

Long-running MATLAB code is automatically handled through the async job system.

How It Works

  1. You call execute_code with your MATLAB code
  2. The server starts executing synchronously in a background task
  3. If execution exceeds sync_timeout (default 30 seconds), the job is promoted to async
  4. You get back a job_id immediately
  5. Poll get_job_status for progress updates
  6. Call get_job_result when the job completes

Job Lifecycle

PENDING → RUNNING → COMPLETED
                  → FAILED
                  → CANCELLED

State Transitions

  • PENDING: Job created but not yet started
  • RUNNING: Engine acquired and code is executing
  • COMPLETED: Execution finished successfully with a result
  • FAILED: Execution encountered an error
  • CANCELLED: Job was cancelled via cancel_job

Sync vs. Async Execution

Sync Execution (< sync_timeout)

When your code completes within sync_timeout (default 30 seconds):

{
  "status": "completed",
  "job_id": "j-...",
  "text": "output here",
  "error": null
}

Async Promotion (> sync_timeout)

When your code exceeds sync_timeout:

  1. The job is automatically promoted to background execution
  2. An engine is held until completion
  3. You receive a pending response immediately:
{
  "status": "pending",
  "job_id": "j-..."
}
  1. Poll get_job_status to monitor progress and retrieve the result

Progress Reporting

Use the mcp_progress() helper function in your MATLAB code to report progress back to the agent:

mcp_progress(__mcp_job_id__, percentage, message)
  • __mcp_job_id__ — automatically injected into the workspace by the server
  • percentage — number from 0 to 100
  • message — optional status message

Example

n = 1e6;
results = zeros(n, 1);
for i = 1:n
    results(i) = process_item(i);
    if mod(i, 1e5) == 0
        mcp_progress(__mcp_job_id__, i/n*100, ...
            sprintf('Processed %d/%d items', i, n));
    end
end
disp(mean(results));

The agent sees:

get_job_status → {status: "running", progress: 10, message: "Processed 100000/1000000 items"}
get_job_status → {status: "running", progress: 50, message: "Processed 500000/1000000 items"}
get_job_status → {status: "running", progress: 100, message: "Processed 1000000/1000000 items"}
get_job_result → {status: "completed", text: "0.5023"}

How Progress Works Internally

  1. mcp_progress.m writes a JSON file to __mcp_temp_dir__/<job_id>.progress
  2. get_job_status reads this file and includes progress in the response
  3. The file is cleaned up when the job completes or is pruned

Job Management Tools

Tool Description
get_job_status Current status, progress percentage, and elapsed time
get_job_result Full result of a completed job (or error details for failed jobs)
cancel_job Cancel a pending or running job
list_jobs List all jobs in the session

Job Context Injection

The server automatically injects job context into the MATLAB workspace:

  • __mcp_job_id__ — unique identifier for the current job
  • __mcp_temp_dir__ — temporary directory for progress files and outputs
  • __mcp_session_id__ — session identifier

These variables are available throughout your MATLAB code execution.

Error Handling

When a job fails, get_job_result returns detailed error information:

{
  "status": "failed",
  "job_id": "j-...",
  "error": {
    "type": "MATLABError",
    "message": "Undefined function or variable 'x'",
    "matlab_id": "MATLAB:undefinedVarOrFunction",
    "stack_trace": "Error in code (line 5)\n    result = x + 1;"
  }
}

Configuration

execution:
  sync_timeout: 30           # Seconds before async promotion
  max_execution_time: 86400  # Hard limit (24h)

sessions:
  job_retention_seconds: 86400  # Keep job metadata for 24h after completion

Retention and Cleanup

  • Completed, failed, and cancelled jobs are retained for job_retention_seconds (default 24 hours)
  • The server periodically prunes expired jobs to manage memory
  • Job metadata is removed, but results are available until pruning occurs
  • Active jobs (PENDING or RUNNING) are never pruned while executing

Elapsed Time Tracking

Jobs automatically track elapsed time from start to completion:

  • elapsed_seconds available in job metadata
  • Useful for monitoring long-running operations
  • Reported in job status responses

Tips

  • Short code (< 30s): Results return inline, no polling needed
  • Medium code (30s - minutes): Auto-promoted, poll with get_job_status for progress
  • Long code (hours): Add mcp_progress() calls so the agent can report status to users
  • Cancel long jobs: Call cancel_job if you need to stop a running job immediately
  • Increase timeout: Set sync_timeout: 60 if most of your code takes 30-60s to reduce async overhead
  • Check active jobs: Use list_jobs to see all pending/running jobs for a session before starting new work

Clone this wiki locally