Handle tasks/get — derive status from the callback manager.
(task_id: str)
| 59 | |
| 60 | |
| 61 | def get_task(task_id: str) -> GetTaskResult: |
| 62 | """Handle tasks/get — derive status from the callback manager.""" |
| 63 | tool_name, job_id, cache_key, created_at = parse_task_id(task_id) |
| 64 | |
| 65 | manager = _get_callback_manager(tool_name) |
| 66 | if manager is None: |
| 67 | return GetTaskResult( |
| 68 | taskId=task_id, |
| 69 | status="failed", |
| 70 | statusMessage="No background callback manager configured.", |
| 71 | createdAt=created_at, |
| 72 | lastUpdatedAt=datetime.now(timezone.utc), |
| 73 | ttl=None, |
| 74 | ) |
| 75 | |
| 76 | running = manager.job_running(job_id) |
| 77 | progress = manager.get_progress(cache_key) |
| 78 | |
| 79 | # Check result_ready before job_running: the process may store its result |
| 80 | # while still technically alive (teardown race), so result_ready wins. |
| 81 | status: Literal["working", "completed", "failed"] |
| 82 | if manager.result_ready(cache_key): |
| 83 | status = "completed" |
| 84 | elif running: |
| 85 | status = "working" |
| 86 | else: |
| 87 | status = "failed" |
| 88 | |
| 89 | adapter = get_app().mcp_callback_map.find_by_tool_name(tool_name) |
| 90 | interval = None |
| 91 | if adapter is not None: |
| 92 | # pylint: disable-next=protected-access |
| 93 | interval = adapter._cb_info.get("background", {}).get("interval", 1000) |
| 94 | |
| 95 | return GetTaskResult( |
| 96 | taskId=task_id, |
| 97 | status=status, |
| 98 | statusMessage=str(progress) if progress else None, |
| 99 | createdAt=created_at, |
| 100 | lastUpdatedAt=datetime.now(timezone.utc), |
| 101 | ttl=manager.expire * 1000 if manager.expire else None, |
| 102 | pollInterval=interval, |
| 103 | ) |
| 104 | |
| 105 | |
| 106 | def get_task_result(task_id: str) -> Any: |
no test coverage detected
searching dependent graphs…