background_tasks
Background task management for MADA orchestration.
This module contains the async task bookkeeping used by interfaces and server-side MCP background tools.
BackgroundTaskManager
Manage interface background queries and MCP server-side background tools.
Attributes:
| Name | Type | Description |
|---|---|---|
session_manager |
Chat session manager used to read and persist chat history. |
|
mcp_tools_by_server |
Mutable mapping of connected MCP tools by server name. The orchestrator owns the mapping and updates it as servers connect. |
|
collect_message_response |
Callable that runs a user message and returns the full assistant response. |
Source code in src/mada/core/background_tasks.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
__init__(session_manager, mcp_tools_by_server, collect_message_response)
Initialize the background task manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_manager
|
ChatSessionManager
|
Chat session manager used to read and persist chat history. |
required |
mcp_tools_by_server
|
Dict[str, Any]
|
Mutable mapping of connected MCP tools by server name. |
required |
collect_message_response
|
CollectMessageResponse
|
Callable that runs a user message and returns the full assistant response. |
required |
Source code in src/mada/core/background_tasks.py
cleanup()
async
Cancel active background tasks and reset task state.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in src/mada/core/background_tasks.py
count_pending_tasks()
async
Return the current number of in-flight background tasks.
Returns:
| Type | Description |
|---|---|
int
|
Number of tasks currently tracked as pending. |
Source code in src/mada/core/background_tasks.py
get_task_snapshot()
async
Return a copy of interface and MCP background task state.
Each key is a task ID, and each value contains status metadata suitable for CLI or Gradio task-status displays.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, str]]
|
Deep copy of task state keyed by task ID. |
Source code in src/mada/core/background_tasks.py
run_query(user_input, blocking=True)
async
Run a user query inline or as an interface background task.
In blocking mode, the full response is collected and returned. In non-blocking mode, the query is detached only after the agent starts a tool call; simple text-only responses are still returned inline. Additional non-blocking queries use isolated agent sessions while a background agent task is already running, which avoids unsafe append-only merges of provider session state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_input
|
str
|
User query text to process. |
required |
blocking
|
bool
|
If True, wait for the full response inline. If False, return a task acknowledgement after the first tool call starts. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The full assistant response, or a background-task start |
str
|
acknowledgement. |
Raises:
| Type | Description |
|---|---|
Exception
|
Propagates failures from the response collection callable before the query is detached. |
Source code in src/mada/core/background_tasks.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
start_background_tool_poll_from_reply_if_needed(reply_text)
Start polling when an assistant reply contains a running MCP task descriptor.
MCP tools can return a JSON object containing task_id, status, and
tool_name. When the status is running, this method registers a poller
task that waits for the server-side task result and persists the final
assistant message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reply_text
|
str
|
Assistant reply text that may contain a background task descriptor as JSON. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called without a running event loop while a poller needs to be created. |
Source code in src/mada/core/background_tasks.py
user_message_already_started_background_task(message)
Return whether chat history already contains a background-task ack.
This prevents isolated follow-up processing from writing a duplicate user turn when an interface has already persisted the background-task start message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
User message text to look for in chat history. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the message is followed by a background-task acknowledgement, |
bool
|
otherwise False. History loading errors are suppressed and treated |
bool
|
as no match. |