codex_cancel
Stop a running codex job by sending SIGTERM to its subprocess. Provide the job ID from codex_start to cancel the task.
Instructions
Cancel a running codex job by sending SIGTERM to the subprocess.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| job_id | Yes | The job_id returned by codex_start. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/codex_async_mcp/server.py:50-58 (handler)The MCP tool handler for 'codex_cancel'. Decorated with @mcp.tool(), it receives a job_id and delegates to cancel_job().
@mcp.tool() def codex_cancel(job_id: str) -> dict: """ Cancel a running codex job by sending SIGTERM to the subprocess. Args: job_id: The job_id returned by codex_start. """ return cancel_job(job_id) - src/codex_async_mcp/server.py:50-50 (registration)The @mcp.tool() decorator registers 'codex_cancel' as a tool with the FastMCP server.
@mcp.tool() - src/codex_async_mcp/server.py:51-51 (schema)Type signature: accepts a string job_id and returns a dict (the schema for inputs/outputs).
def codex_cancel(job_id: str) -> dict: - The actual cancel logic: looks up the job, terminates the subprocess (via Popen.terminate() or os.kill with SIGTERM), updates the meta status to 'cancelled', and returns the result.
def cancel_job(job_id: str) -> dict: meta = _read_meta(job_id) if meta is None: return {"error": f"Job '{job_id}' not found"} if meta["status"] != "running": return {"job_id": job_id, "status": meta["status"], "message": "Job is not running"} with _lock: proc = _active_procs.get(job_id) killed = False if proc is not None: proc.terminate() killed = True else: pid = meta.get("pid") if pid and _is_pid_alive(pid): os.kill(pid, 15) # SIGTERM killed = True with _lock: meta["status"] = "cancelled" meta["finished_at"] = datetime.now(timezone.utc).isoformat() _write_meta(job_id, meta) _active_procs.pop(job_id, None) return {"job_id": job_id, "status": "cancelled", "killed": killed} - Imports subprocess module used by cancel_job for process management.
import subprocess