list_jobs
Retrieve and monitor all background jobs, including job ID, status, command, and start time, sorted by newest first. Facilitates efficient process tracking and management.
Instructions
List all background jobs with their status.
Returns a list of all background jobs, including their job ID, status, command, and start time. Jobs are sorted by start time (newest first).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/mcp_background_job/server.py:34-48 (handler)MCP tool handler function for 'list_jobs'. It retrieves the job manager and calls list_jobs on it, returning ListOutput. Registered via @mcp.tool() decorator.@mcp.tool() async def list_jobs() -> ListOutput: """List all background jobs with their status. Returns a list of all background jobs, including their job ID, status, command, and start time. Jobs are sorted by start time (newest first). """ try: job_manager = get_job_manager() jobs = await job_manager.list_jobs() return ListOutput(jobs=jobs) except Exception as e: logger.error(f"Error listing jobs: {e}") raise ToolError(f"Failed to list jobs: {str(e)}")
- Pydantic output schema for the list_jobs tool, containing a list of JobSummary objects.class ListOutput(BaseModel): """Output from list tool.""" jobs: List[JobSummary] = Field(..., description="List of all background jobs")
- Core implementation in JobManager that lists all jobs by updating statuses and creating JobSummary objects, sorted by start time newest first.async def list_jobs(self) -> List[JobSummary]: """List all jobs. Returns: List of JobSummary objects for all jobs """ # Update all job statuses for job_id in list(self._jobs.keys()): try: await self._update_job_status(job_id) except Exception as e: logger.warning(f"Failed to update status for job {job_id}: {e}") # Create summaries summaries = [] for job in self._jobs.values(): summaries.append( JobSummary( job_id=job.job_id, status=job.status, command=job.command, started=job.started, ) ) # Sort by start time (newest first) summaries.sort(key=lambda x: x.started, reverse=True) return summaries
- Pydantic model used within ListOutput for individual job summaries in list_jobs output.class JobSummary(BaseModel): """Minimal job information for listing operations.""" job_id: str = Field(..., description="UUID v4 job identifier") status: JobStatus = Field(..., description="Current job status") command: str = Field(..., description="Shell command being executed") started: datetime = Field(..., description="UTC timestamp when job started")