-
Notifications
You must be signed in to change notification settings - Fork 0
Async Jobs
Long-running MATLAB code is automatically handled through the async job system.
- You call
execute_codewith your MATLAB code - The server starts executing synchronously in a background task
- If execution exceeds
sync_timeout(default 30 seconds), the job is promoted to async - You get back a
job_idimmediately - Poll
get_job_statusfor progress updates - Call
get_job_resultwhen the job completes
PENDING → RUNNING → COMPLETED
→ FAILED
→ CANCELLED
- PENDING: Job created, waiting for engine acquisition and execution start
- RUNNING: Engine acquired, code execution in progress
- COMPLETED: Code execution finished successfully with result
- FAILED: Code execution raised an error (MATLAB or system error)
-
CANCELLED: Job was explicitly cancelled via
cancel_job
When your code completes within sync_timeout (default 30 seconds):
- Code executes in a background task with stdout/stderr capture
- Server waits for completion
- Result returned immediately with
status: "completed" - No polling required
Response example:
{
"status": "completed",
"job_id": "j-abc123",
"text": "0.5023"
}When code exceeds sync_timeout:
- Server stops waiting and returns immediately with
status: "pending" - Code continues executing in the background
- You must poll
get_job_statusto monitor progress - Call
get_job_resultwhen ready to retrieve the result
Initial response:
{
"status": "pending",
"job_id": "j-abc123"
}After promotion, the job continues running in the background while you poll for status.
Use the mcp_progress() helper function in your MATLAB code to report progress back to the agent. This is especially useful for long-running jobs:
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
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"}
-
mcp_progress.mwrites a JSON file to__mcp_temp_dir__/<job_id>.progress -
get_job_statusreads this file and includes progress in the response - The file is cleaned up when the job reaches a terminal state (completed, failed, or cancelled)
Use get_job_status to check on a job's progress:
{
"status": "running",
"progress": 45,
"message": "Processing batch 3 of 7"
}Or when complete:
{
"status": "completed",
"progress": 100
}Call get_job_result to retrieve the full result of a completed job:
{
"status": "completed",
"text": "0.5023",
"variables": {
"mean_result": 0.5023
}
}For failed jobs:
{
"status": "failed",
"error": {
"type": "MATLABExecutionException",
"message": "Undefined function or variable 'x'.",
"matlab_id": "MATLAB:UndefinedFunction",
"stack_trace": "Error in line 5: ..."
}
}Call cancel_job to stop a pending or running job:
- If the job is PENDING: transitions immediately to CANCELLED
- If the job is RUNNING: attempts to interrupt the background execution and transitions to CANCELLED
- If the job is already COMPLETED, FAILED, or CANCELLED: no-op
Cancellation is best-effort; the underlying engine may not interrupt immediately.
Job metadata is retained in the tracker for a configurable duration after reaching a terminal state (COMPLETED, FAILED, or CANCELLED). This allows clients time to poll for results.
- Jobs enter the retention period when they reach a terminal state
- The
pruneoperation (run periodically by the server) removes jobs older thanjob_retention_seconds - Default retention: 24 hours (86400 seconds)
- After a job is pruned, it can no longer be queried
- Set appropriate retention: Balance between client query windows and memory usage
- Poll promptly: Retrieve results before the retention period expires
-
Monitor active jobs: Use
list_jobsto track pending/running jobs
| Tool | Description |
|---|---|
execute_code |
Execute MATLAB code (sync or async promotion) |
get_job_status |
Current status + progress percentage and message |
get_job_result |
Full result of a completed job (or error details if failed) |
cancel_job |
Cancel a pending or running job |
list_jobs |
List all jobs in the session (or globally) |
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 terminal state- Short code (< 30s): Results return inline, no job ID needed
-
Medium code (30s - minutes): Auto-promoted, poll with
get_job_status -
Long code (hours): Add
mcp_progress()calls so the agent can report status -
Cancel: Call
cancel_jobif you need to stop a running job -
Increase timeout: Set
sync_timeout: 60if most of your code takes 30-60s - Monitor engine load: Check active jobs before submitting additional long-running tasks