task-graph-mcp 0.2.1

MCP server for agent task workflows with phases, prompts, gates, and multi-agent coordination
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# Task Graph MCP - Design Document


> **Version:** 1.0  
> **Last Updated:** 2026-01-25  
> **Status:** Living Document

This document describes the architecture, design decisions, and implicit assumptions of the Task Graph MCP server.

---

## Table of Contents


- [Architecture Overview]#architecture-overview
- [Component Architecture]#component-architecture
- [Task State Machine]#task-state-machine
- [Dependency System]#dependency-system
- [Data Flow]#data-flow
- [File Coordination Model]#file-coordination-model
- [Design Decisions]#design-decisions
- [Assumptions & Constraints]#assumptions--constraints
- [Path Conventions]#path-conventions

---

## Architecture Overview


Task Graph MCP is a multi-agent coordination server that enables AI agents to work together on complex tasks without conflicts. It provides:

- **Task DAG** - Hierarchical task management with typed dependencies
- **Atomic Claiming** - Prevents race conditions when agents claim work
- **File Coordination** - Advisory locks for file-level coordination
- **Cost Tracking** - Token and USD accounting per task

```plantuml
@startuml architecture-overview
!theme plain
skinparam backgroundColor #FEFEFE

title Task Graph MCP - High-Level Architecture

cloud "AI Agents" as agents {
    actor "Agent A\n(Claude)" as a1
    actor "Agent B\n(GPT-4)" as a2
    actor "Agent C\n(Worker)" as a3
}

package "MCP Layer" as mcp {
    component "task-graph\nMCP Server" as tg1
    component "task-graph\nMCP Server" as tg2
    component "task-graph\nMCP Server" as tg3
}

database "SQLite + WAL\n.task-graph/tasks.db" as db

a1 -down-> tg1 : stdio
a2 -down-> tg2 : stdio
a3 -down-> tg3 : stdio

tg1 -down-> db
tg2 -down-> db
tg3 -down-> db

note right of db
  WAL mode enables
  concurrent access
  across processes
end note

@enduml
```

---

## Component Architecture


The server is organized into distinct layers:

```plantuml
@startuml component-architecture
!theme plain
skinparam backgroundColor #FEFEFE

title Task Graph MCP - Component Architecture

package "Entry Point" {
    [main.rs] as main
    [TaskGraphServer] as server
}

package "Tool Layer" {
    [ToolHandler] as tools
    
    package "Tool Modules" {
        [agents.rs] as t_agents
        [tasks.rs] as t_tasks
        [deps.rs] as t_deps
        [claiming.rs] as t_claiming
        [files.rs] as t_files
        [attachments.rs] as t_attachments
        [tracking.rs] as t_tracking
        [search.rs] as t_search
        [query.rs] as t_query
        [schema.rs] as t_schema
        [skills.rs] as t_skills
    }
}

package "Resource Layer" {
    [ResourceHandler] as resources
    
    package "Resource Modules" {
        [tasks.rs] as r_tasks
        [agents.rs] as r_agents
        [files.rs] as r_files
        [stats.rs] as r_stats
        [skills.rs] as r_skills
    }
}

package "Database Layer" {
    [Database] as db
    
    package "DB Modules" {
        [tasks.rs] as d_tasks
        [agents.rs] as d_agents
        [deps.rs] as d_deps
        [locks.rs] as d_locks
        [attachments.rs] as d_attachments
        [state_transitions.rs] as d_state
        [search.rs] as d_search
        [stats.rs] as d_stats
        [schema.rs] as d_schema
    }
}

package "Support" {
    [config.rs] as config
    [types.rs] as types
    [error.rs] as error
    [format.rs] as format
}

main --> server
server --> tools
server --> resources
tools --> db
resources --> db
tools ..> config
tools ..> types
tools ..> error
tools ..> format
db ..> types

@enduml
```

### Layer Responsibilities


| Layer | Responsibility |
|-------|---------------|
| **Entry Point** | CLI parsing, server initialization, MCP protocol handling |
| **Tool Layer** | MCP tool definitions, parameter validation, business logic |
| **Resource Layer** | MCP resource definitions, read-only data access |
| **Database Layer** | SQLite operations, transactions, migrations |
| **Support** | Configuration, types, error handling, output formatting |

---

## Task State Machine


Tasks follow a configurable state machine. The default configuration:

```plantuml
@startuml task-state-machine
!theme plain
skinparam backgroundColor #FEFEFE

title Task State Machine (Default Configuration)

state "pending" as pending : Initial state
state "assigned" as assigned : Push coordination
state "working" as working : **Timed state**\nAccumulates time_actual_ms
state "completed" as completed : Terminal (success)
state "failed" as failed : Terminal (retriable)
state "cancelled" as cancelled : Terminal (abandoned)

[*] --> pending : create()

pending --> assigned : update(assignee=X)
pending --> working : claim() / update(status)
pending --> cancelled : update(status)

assigned --> working : claim() by assignee
assigned --> pending : release
assigned --> cancelled : update(status)

working --> completed : update(status)
working --> failed : update(status)
working --> pending : release / disconnect

failed --> pending : retry

completed --> [*]
cancelled --> [*]

note right of working
  Only timed states contribute
  to time_actual_ms tracking.
  
  Worker heartbeat refreshed
  via thinking() calls.
end note

note left of assigned
  Push coordination:
  Coordinator assigns task
  to specific worker.
end note

@enduml
```

### State Properties


| State | Timed | Terminal | Blocking | Description |
|-------|-------|----------|----------|-------------|
| `pending` | No | No | Yes | Initial state, waiting for claim |
| `assigned` | No | No | Yes | Assigned to specific worker (push model) |
| `working` | **Yes** | No | Yes | Active work, time tracked |
| `completed` | No | Yes | No | Successfully finished |
| `failed` | No | Yes | No | Failed, can retry |
| `cancelled` | No | Yes | No | Abandoned, cannot retry |

### Auto-Advance


When enabled, tasks automatically transition from `pending` to a target state (e.g., `ready`) when their blocking dependencies are satisfied.

---

## Dependency System


Dependencies form a DAG (Directed Acyclic Graph) with typed edges:

```plantuml
@startuml dependency-types
!theme plain
skinparam backgroundColor #FEFEFE

title Dependency Types

package "Blocking Dependencies" {
    rectangle "Task A" as a1
    rectangle "Task B" as b1
    a1 -right-> b1 : **blocks**\n(start)
    note bottom of b1
      B cannot be claimed
      until A completes
    end note
}

package "Sequential Dependencies" {
    rectangle "Step 1" as s1
    rectangle "Step 2" as s2
    rectangle "Step 3" as s3
    s1 -right-> s2 : **follows**\n(start)
    s2 -right-> s3 : **follows**\n(start)
    note bottom of s3
      Auto-created by
      sibling_type='follows'
    end note
}

package "Hierarchical Dependencies" {
    rectangle "Parent" as p1
    rectangle "Child 1" as c1
    rectangle "Child 2" as c2
    p1 -down-> c1 : **contains**\n(completion)
    p1 -down-> c2 : **contains**\n(completion)
    note right of p1
      Parent cannot complete
      until all children complete
    end note
}

package "Informational Links" {
    rectangle "Original" as o1
    rectangle "Duplicate" as d1
    rectangle "Related" as r1
    o1 ..> d1 : **duplicate**
    o1 ..> r1 : **see-also**
    note bottom of d1
      No blocking effect,
      just metadata
    end note
}

@enduml
```

### Dependency Properties


| Type | Display | Blocks | Use Case |
|------|---------|--------|----------|
| `blocks` | Horizontal | Start | Explicit prerequisite |
| `follows` | Horizontal | Start | Sequential execution |
| `contains` | Vertical | Completion | Parent-child hierarchy |
| `duplicate` | Horizontal | None | Mark as duplicate |
| `see-also` | Horizontal | None | Related reference |
| `relates-to` | Horizontal | None | General relationship |

### Cycle Detection


The system prevents cycles in blocking dependencies (`blocks`, `follows`, `contains`). Non-blocking links (`duplicate`, `see-also`, `relates-to`) are not checked for cycles.

---

## Data Flow


```plantuml
@startuml data-flow
!theme plain
skinparam backgroundColor #FEFEFE

title Data Flow - Task Lifecycle

participant "Agent" as agent
participant "MCP Server" as server
participant "ToolHandler" as tools
participant "Database" as db

== Connection ==
agent -> server : connect(worker_id, tags)
server -> tools : call_tool("connect", args)
tools -> db : register_worker()
db --> tools : worker record
tools --> server : {worker_id, paths}
server --> agent : result

== Task Discovery ==
agent -> server : list_tasks(ready=true)
server -> tools : call_tool("list_tasks", args)
tools -> db : query_tasks(filters)
db --> tools : task list
tools --> server : formatted tasks
server --> agent : claimable tasks

== Claiming ==
agent -> server : claim(worker_id, task)
server -> tools : call_tool("claim", args)
tools -> db : BEGIN TRANSACTION
tools -> db : check_dependencies()
tools -> db : check_tag_requirements()
tools -> db : update_task(status=working)
tools -> db : record_state_transition()
tools -> db : COMMIT
db --> tools : claimed task
tools --> server : {success, task}
server --> agent : confirmation

== Working ==
agent -> server : thinking(worker_id, thought)
server -> tools : call_tool("thinking", args)
tools -> db : update_heartbeat()
tools -> db : update_current_thought()
db --> tools : ok
tools --> server : {success}
server --> agent : ack

== Completion ==
agent -> server : update(status=completed, attachments)
server -> tools : call_tool("update", args)
tools -> db : BEGIN TRANSACTION
tools -> db : update_task(status, attachments)
tools -> db : record_state_transition()
tools -> db : calculate_time_actual_ms()
tools -> db : find_unblocked_tasks()
tools -> db : COMMIT
db --> tools : {task, unblocked}
tools --> server : {task, unblocked}
server --> agent : result with unblocked list

@enduml
```

---

## File Coordination Model


Advisory file locks enable agents to coordinate without conflicts:

```plantuml
@startuml file-coordination
!theme plain
skinparam backgroundColor #FEFEFE

title File Coordination Flow

participant "Agent A" as a
participant "Agent B" as b
participant "Server" as s
database "claim_sequence" as cs

== Agent A marks file ==
a -> s : mark_file("src/main.rs", "refactoring auth")
s -> cs : INSERT claimed event
s --> a : ok

== Agent B checks before editing ==
b -> s : mark_file("src/main.rs", "fixing bug")
s -> cs : check existing marks
s --> b : WARNING: Agent A has mark\n"refactoring auth"

note over b
  Agent B can now decide:
  - Wait for A to finish
  - Work around A's changes
  - Choose different file
end note

== Agent B polls for updates ==
b -> s : mark_updates()
s -> cs : SELECT since last_sequence
s --> b : no changes yet

== Agent A releases ==
a -> s : unmark_file("src/main.rs", "ready for review")
s -> cs : INSERT released event
s --> a : ok

== Agent B sees release ==
b -> s : mark_updates()
s -> cs : SELECT since last_sequence
s --> b : [{file: "src/main.rs", event: "released",\n  reason: "ready for review"}]

b -> s : mark_file("src/main.rs", "fixing bug")
s --> b : ok (no conflict)

@enduml
```

### Key Points


- **Advisory, not mandatory** - Marks signal intent, don't prevent access
- **Reason visibility** - Agents see *why* a file is marked
- **Polling-based** - `mark_updates()` returns changes since last call
- **Task association** - Marks can be tied to tasks for auto-cleanup

---

## Design Decisions


### Why SQLite?


| Alternative | Rejected Because |
|-------------|------------------|
| PostgreSQL | Requires server setup, network overhead |
| Redis | No persistence guarantees, complex setup |
| File-based | No concurrent access, no transactions |
| In-memory | Lost on restart |

**SQLite with WAL mode** provides:
- Zero configuration
- ACID transactions
- Concurrent readers
- Process-safe writes
- Single file deployment

### Why Configurable States?


Different workflows need different state machines:
- Simple: `pending``working``completed`
- With review: Add `review` state before `completed`
- With ready queue: Add `ready` state after `pending`

### Why Typed Dependencies?


Generic "depends on" is insufficient:
- Need to distinguish blocking vs informational
- Need to support both sequence and hierarchy
- Need to allow custom workflow edges

---

## Assumptions & Constraints


### Runtime Assumptions


| Assumption | Impact | Mitigation |
|------------|--------|------------|
| **Single machine** | WAL mode assumes local filesystem | Document limitation |
| **SQLite available** | No abstraction for other DBs | Could add trait layer |
| **Filesystem access** | Media dir, log dir, config | Check permissions |
| **UTF-8 everywhere** | JSON content, file paths | Document requirement |

### Data Assumptions


| Assumption | Impact | Mitigation |
|------------|--------|------------|
| **Unix timestamps** | All times in epoch seconds | Consistent across platforms |
| **Task IDs unique** | UUID7 or user-provided | Validation on create |
| **Worker IDs unique** | User-provided or petname | Force flag for recovery |
| **JSON in TEXT** | Tags stored as JSON strings | Parse on read |

### Concurrency Assumptions


| Assumption | Impact | Mitigation |
|------------|--------|------------|
| **WAL mode enabled** | Concurrent reads, serial writes | Auto-enabled on open |
| **No distributed locking** | Single SQLite file | Document limitation |
| **Eventual consistency** | Polling-based coordination | Document latency |
| **Heartbeat timeout** | Default 5 min stale detection | Configurable |

### Security Assumptions


| Assumption | Impact | Mitigation |
|------------|--------|------------|
| **Trusted agents** | No auth/authz | Deploy in trusted environment |
| **Local filesystem** | No network exposure | Stdio transport only |
| **Read-only queries** | SQL tool restricted | Statement validation |

---

## Path Conventions


All paths in the system are **relative to the project root** unless they begin with a recognized prefix.

### Recognized Prefixes


| Prefix | Meaning | Example |
|--------|---------|---------|
| `~` | User home directory | `~/.config/app` |
| `$HOME` | User home (env var) | `$HOME/.config/app` |
| `/` | Absolute path (Unix) | `/etc/config` |
| `C:\`, `D:\`, etc. | Absolute path (Windows) | `C:\Users\config` |

### Relative Path Examples


| Path | Resolves To |
|------|-------------|
| `src/main.rs` | `{project_root}/src/main.rs` |
| `.task-graph/tasks.db` | `{project_root}/.task-graph/tasks.db` |
| `docs/README.md` | `{project_root}/docs/README.md` |

### Configuration Paths


```yaml
server:
  db_path: .task-graph/tasks.db     # Relative to project root
  media_dir: .task-graph/media      # Relative to project root
  log_dir: .task-graph/logs         # Relative to project root
  skills_dir: .task-graph/skills    # Relative to project root
```

### File Lock Paths


File marks use paths relative to project root:
```
mark_file(file="src/auth/login.rs", ...)  # Relative
mark_file(file="~/global/config", ...)    # Absolute (home)
```

---

## Appendix: Entity Relationship Diagram


```plantuml
@startuml entity-relationship
!theme plain
skinparam backgroundColor #FEFEFE

title Database Entity Relationships

entity "workers" as workers {
    * id : TEXT <<PK>>
    --
    tags : TEXT (JSON)
    max_claims : INTEGER
    registered_at : INTEGER
    last_heartbeat : INTEGER
    last_claim_sequence : INTEGER
}

entity "tasks" as tasks {
    * id : TEXT <<PK>>
    --
    title : TEXT
    description : TEXT
    status : TEXT
    priority : TEXT
    worker_id : TEXT <<FK>>
    claimed_at : INTEGER
    needed_tags : TEXT (JSON)
    wanted_tags : TEXT (JSON)
    tags : TEXT (JSON)
    points : INTEGER
    time_estimate_ms : INTEGER
    time_actual_ms : INTEGER
    started_at : INTEGER
    completed_at : INTEGER
    current_thought : TEXT
    cost_usd : REAL
    metrics : TEXT (JSON)
    created_at : INTEGER
    updated_at : INTEGER
}

entity "dependencies" as deps {
    * from_task_id : TEXT <<PK,FK>>
    * to_task_id : TEXT <<PK,FK>>
    * dep_type : TEXT <<PK>>
    --
}

entity "attachments" as attachments {
    * task_id : TEXT <<PK,FK>>
    * order_index : INTEGER <<PK>>
    --
    name : TEXT
    mime_type : TEXT
    content : TEXT
    file_path : TEXT
    created_at : INTEGER
}

entity "file_locks" as locks {
    * file_path : TEXT <<PK>>
    --
    worker_id : TEXT <<FK>>
    task_id : TEXT <<FK>>
    reason : TEXT
    locked_at : INTEGER
}

entity "claim_sequence" as claims {
    * id : INTEGER <<PK>>
    --
    file_path : TEXT
    agent_id : TEXT
    event : TEXT
    reason : TEXT
    timestamp : INTEGER
    end_timestamp : INTEGER
    claim_id : INTEGER
}

entity "task_state_sequence" as states {
    * id : INTEGER <<PK>>
    --
    task_id : TEXT <<FK>>
    agent_id : TEXT
    event : TEXT
    reason : TEXT
    timestamp : INTEGER
    end_timestamp : INTEGER
}

workers ||--o{ tasks : "claims"
workers ||--o{ locks : "holds"
workers ||--o{ claims : "creates"
workers ||--o{ states : "triggers"

tasks ||--o{ attachments : "has"
tasks ||--o{ deps : "from"
tasks ||--o{ deps : "to"
tasks ||--o{ states : "tracks"
tasks ||--o{ locks : "associated"

@enduml
```

---

## Document History


| Version | Date | Changes |
|---------|------|---------|
| 1.0 | 2026-01-25 | Initial design document |

---

*This document is maintained alongside the codebase. Update it when making architectural changes.*