-
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
- 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
When you submit code:
- A job is created in PENDING state
- An engine is acquired from the pool
- Job context is injected into the MATLAB workspace (including
__mcp_job_id__and__mcp_temp_dir__) - Code executes in the background with
background=True - The server waits up to
sync_timeoutseconds for completion
Result: If the future completes within the timeout, the result is returned inline with status: "completed" (no polling needed).
When execution exceeds sync_timeout:
- The job transitions to background execution
- The server returns immediately with
status: "pending"and thejob_id - A background task continues monitoring the job
- You must poll
get_job_statusto track progress and completion
Result: Asynchronous background task handles job lifecycle; client polls for updates.
Use get_job_status to check progress:
→ get_job_status(job_id)
← {
status: "running",
progress: 25,
message: "Processed 250000/1000000 items",
elapsed_seconds: 12.5
}
Repeat polling until status is one of: "completed", "failed", or "cancelled".
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
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()writes 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 completes
Use get_job_result to retrieve the full result of a completed job:
→ get_job_result(job_id)
← {
status: "completed",
text: "0.5023",
variables: {...}
}
For failed jobs:
← {
status: "failed",
error: {
type: "MATLAB:UndefinedFunction",
message: "Undefined function or method 'process_item'",
matlab_id: "MATLAB:UndefinedFunction",
stack_trace: "Error in code (line 4)..."
}
}
Note: Results are retained in memory for job_retention_seconds (default 24 hours) after job completion.
Use cancel_job to stop a pending or running job:
→ cancel_job(job_id)
← {status: "cancelled", job_id: "j-..."}
Once cancelled, the job transitions to CANCELLED state and the background task is cleaned up. Results cannot be retrieved from a cancelled job.
Job metadata is retained according to job_retention_seconds (default 86400 = 24 hours):
- Active jobs (PENDING, RUNNING): Always retained while active
- Terminal jobs (COMPLETED, FAILED, CANCELLED): Retained for 24 hours after completion
-
Automatic pruning: Expired jobs are removed by periodic cleanup; call
list_jobsto see currently tracked jobs
To manually check which jobs exist:
→ list_jobs(session_id)
← [job_1, job_2, ...]
| Tool | Description |
|---|---|
execute_code |
Submit MATLAB code for sync/async execution |
get_job_status |
Current status, progress percentage, and message |
get_job_result |
Full result of a completed job |
cancel_job |
Cancel a pending or running job |
list_jobs |
List all tracked jobs in a session |
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- 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 -
Elapsed time:
get_job_statusincludeselapsed_secondsto monitor long-running jobs