task-graph-mcp 0.5.0

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
//! Integration tests for the feedback tools (give_feedback and list_feedback).
//!
//! These tools write/read a human-readable markdown file in a directory. Tests use
//! a temporary directory so they are fully isolated and leave no artefacts.

use serde_json::json;
use task_graph_mcp::config::FeedbackConfig;
use task_graph_mcp::tools::feedback;
use tempfile::TempDir;

/// Create a fresh temporary directory for each test.
fn setup_dir() -> TempDir {
    TempDir::new().expect("Failed to create temp directory")
}

// ---------------------------------------------------------------------------
// give_feedback – happy path
// ---------------------------------------------------------------------------

mod give_feedback_tests {
    use super::*;

    #[test]
    fn happy_path_creates_file_and_returns_recorded() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "The search tool is great!"
            }),
        )
        .expect("give_feedback should succeed");

        // Response shape
        assert_eq!(result["status"], "recorded");
        let file_path = result["file"]
            .as_str()
            .expect("file field should be a string");
        assert!(file_path.contains("feedback.md"));

        // File should exist on disk
        let content = std::fs::read_to_string(file_path).expect("feedback file should exist");
        assert!(content.contains("# Agent Feedback"), "should have header");
        assert!(
            content.contains("The search tool is great!"),
            "should contain the message"
        );
        // Defaults
        assert!(
            content.contains("general"),
            "default category should be general"
        );
        assert!(
            content.contains("neutral"),
            "default sentiment should be neutral"
        );
    }

    #[test]
    fn with_all_optional_fields() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "Workflow needs improvement",
                "category": "workflow",
                "sentiment": "negative",
                "agent_id": "agent-42",
                "tool_name": "update",
                "task_id": "task-99"
            }),
        )
        .expect("give_feedback should succeed");

        assert_eq!(result["status"], "recorded");

        let content =
            std::fs::read_to_string(result["file"].as_str().unwrap()).expect("read feedback file");

        assert!(content.contains("workflow"));
        assert!(content.contains("negative"));
        assert!(content.contains("**Agent:** agent-42"));
        assert!(content.contains("**Tool:** update"));
        assert!(content.contains("**Task:** task-99"));
        assert!(content.contains("Workflow needs improvement"));
    }

    #[test]
    fn with_explicit_category_and_sentiment() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "Config is easy to understand",
                "category": "config",
                "sentiment": "positive"
            }),
        )
        .expect("give_feedback should succeed");

        let content =
            std::fs::read_to_string(result["file"].as_str().unwrap()).expect("read feedback file");

        assert!(content.contains("config"));
        assert!(content.contains("positive"));
    }

    #[test]
    fn suggestion_sentiment_accepted() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "It would be nice to have auto-complete",
                "sentiment": "suggestion"
            }),
        )
        .expect("give_feedback should succeed");

        let content =
            std::fs::read_to_string(result["file"].as_str().unwrap()).expect("read feedback file");

        assert!(content.contains("suggestion"));
    }

    // -----------------------------------------------------------------------
    // Validation errors
    // -----------------------------------------------------------------------

    #[test]
    fn missing_message_returns_error() {
        let dir = setup_dir();

        let result =
            feedback::give_feedback(dir.path(), &FeedbackConfig::default(), None, json!({}));

        assert!(result.is_err(), "missing message should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("message"),
            "error should mention 'message': {}",
            err_msg
        );
    }

    #[test]
    fn empty_message_returns_error() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": ""
            }),
        );

        assert!(result.is_err(), "empty message should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.to_lowercase().contains("empty"),
            "error should mention empty: {}",
            err_msg
        );
    }

    #[test]
    fn whitespace_only_message_returns_error() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "   \t\n  "
            }),
        );

        assert!(result.is_err(), "whitespace-only message should fail");
    }

    #[test]
    fn invalid_category_returns_error() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "some feedback",
                "category": "nonexistent"
            }),
        );

        assert!(result.is_err(), "invalid category should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("nonexistent"),
            "error should mention the bad category: {}",
            err_msg
        );
    }

    #[test]
    fn invalid_sentiment_returns_error() {
        let dir = setup_dir();

        let result = feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "some feedback",
                "sentiment": "angry"
            }),
        );

        assert!(result.is_err(), "invalid sentiment should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("angry"),
            "error should mention the bad sentiment: {}",
            err_msg
        );
    }

    #[test]
    fn all_valid_categories_accepted() {
        let dir = setup_dir();
        let categories = ["tool", "workflow", "config", "ux", "general"];

        for cat in &categories {
            let result = feedback::give_feedback(
                dir.path(),
                &FeedbackConfig::default(),
                None,
                json!({
                    "message": format!("testing {}", cat),
                    "category": cat
                }),
            );
            assert!(
                result.is_ok(),
                "category '{}' should be accepted but got error: {:?}",
                cat,
                result.err()
            );
        }
    }

    #[test]
    fn all_valid_sentiments_accepted() {
        let dir = setup_dir();
        let sentiments = ["positive", "negative", "neutral", "suggestion"];

        for s in &sentiments {
            let result = feedback::give_feedback(
                dir.path(),
                &FeedbackConfig::default(),
                None,
                json!({
                    "message": format!("testing {}", s),
                    "sentiment": s
                }),
            );
            assert!(
                result.is_ok(),
                "sentiment '{}' should be accepted but got error: {:?}",
                s,
                result.err()
            );
        }
    }
}

// ---------------------------------------------------------------------------
// list_feedback
// ---------------------------------------------------------------------------

mod list_feedback_tests {
    use super::*;

    #[test]
    fn no_file_returns_empty_content() {
        let dir = setup_dir();

        let result = feedback::list_feedback(dir.path()).expect("list_feedback should succeed");

        assert_eq!(result["content"], "");
        assert_eq!(result["message"], "No feedback recorded yet.");
        let file_path = result["file"]
            .as_str()
            .expect("file field should be a string");
        assert!(file_path.contains("feedback.md"));
    }

    #[test]
    fn returns_content_after_giving_feedback() {
        let dir = setup_dir();

        // Give some feedback first
        feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "This is my feedback"
            }),
        )
        .expect("give_feedback should succeed");

        let result = feedback::list_feedback(dir.path()).expect("list_feedback should succeed");

        let content = result["content"]
            .as_str()
            .expect("content should be a string");
        assert!(!content.is_empty(), "content should not be empty");
        assert!(content.contains("# Agent Feedback"));
        assert!(content.contains("This is my feedback"));
        // Should not have the "no feedback" message
        assert!(result.get("message").is_none());
    }
}

// ---------------------------------------------------------------------------
// Multiple entries / append behaviour
// ---------------------------------------------------------------------------

mod append_tests {
    use super::*;

    #[test]
    fn multiple_entries_append_correctly() {
        let dir = setup_dir();

        // First entry
        feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "First feedback entry",
                "category": "tool",
                "sentiment": "positive"
            }),
        )
        .expect("first give_feedback should succeed");

        // Second entry
        feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "Second feedback entry",
                "category": "ux",
                "sentiment": "negative"
            }),
        )
        .expect("second give_feedback should succeed");

        // Third entry with optional metadata
        feedback::give_feedback(
            dir.path(),
            &FeedbackConfig::default(),
            None,
            json!({
                "message": "Third entry with metadata",
                "agent_id": "worker-1",
                "tool_name": "search"
            }),
        )
        .expect("third give_feedback should succeed");

        // Read the file and verify all entries
        let result = feedback::list_feedback(dir.path()).expect("list_feedback should succeed");
        let content = result["content"]
            .as_str()
            .expect("content should be a string");

        // Header should appear only once
        assert_eq!(
            content.matches("# Agent Feedback").count(),
            1,
            "header should appear exactly once"
        );

        // Each entry is delimited by "---"
        // The implementation writes "---\n" before each entry, so we expect 3 separators.
        assert_eq!(
            content.matches("---").count(),
            3,
            "should have three separator lines for three entries"
        );

        // All three messages should be present
        assert!(content.contains("First feedback entry"));
        assert!(content.contains("Second feedback entry"));
        assert!(content.contains("Third entry with metadata"));

        // Category and sentiment from different entries
        assert!(content.contains("tool"));
        assert!(content.contains("positive"));
        assert!(content.contains("ux"));
        assert!(content.contains("negative"));

        // Metadata from third entry
        assert!(content.contains("**Agent:** worker-1"));
        assert!(content.contains("**Tool:** search"));
    }

    #[test]
    fn header_only_written_once_across_calls() {
        let dir = setup_dir();

        for i in 0..5 {
            feedback::give_feedback(
                dir.path(),
                &FeedbackConfig::default(),
                None,
                json!({
                    "message": format!("entry {}", i)
                }),
            )
            .expect("give_feedback should succeed");
        }

        let result = feedback::list_feedback(dir.path()).expect("list_feedback should succeed");
        let content = result["content"].as_str().unwrap();

        assert_eq!(
            content.matches("# Agent Feedback").count(),
            1,
            "header should appear exactly once even after many writes"
        );

        // All five entries should be present
        for i in 0..5 {
            assert!(
                content.contains(&format!("entry {}", i)),
                "entry {} should be present",
                i
            );
        }
    }
}

// ---------------------------------------------------------------------------
// get_tools – tool definitions
// ---------------------------------------------------------------------------

mod tool_definition_tests {
    use super::*;

    #[test]
    fn get_tools_returns_two_tools() {
        let tools = feedback::get_tools();
        assert_eq!(tools.len(), 2, "should define exactly two feedback tools");

        let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
        assert!(
            names.contains(&"give_feedback"),
            "should contain give_feedback"
        );
        assert!(
            names.contains(&"list_feedback"),
            "should contain list_feedback"
        );
    }
}

// ---------------------------------------------------------------------------
// Feature gate – feedback tools gated by FeedbackConfig.enabled
// ---------------------------------------------------------------------------

mod feature_gate_tests {
    use std::path::PathBuf;
    use std::sync::Arc;
    use task_graph_mcp::config::workflows::WorkflowsConfig;
    use task_graph_mcp::config::{
        AppConfig, AttachmentsConfig, AutoAdvanceConfig, DependenciesConfig, FeedbackConfig,
        IdsConfig, PhasesConfig, Prompts, ServerPaths, StatesConfig, TagsConfig,
    };
    use task_graph_mcp::db::Database;
    use task_graph_mcp::format::OutputFormat;
    use task_graph_mcp::paths::PathMapper;
    use task_graph_mcp::tools::ToolHandler;

    /// Build a ToolHandler with a given FeedbackConfig.
    fn handler_with_feedback_config(fc: FeedbackConfig) -> ToolHandler {
        let db = Arc::new(Database::open_in_memory().expect("Failed to create in-memory database"));
        let server_paths = Arc::new(ServerPaths {
            db_path: PathBuf::from(":memory:"),
            media_dir: PathBuf::from("test-media"),
            log_dir: PathBuf::from("test-logs"),
            config_path: None,
        });
        let config = AppConfig::new(
            Arc::new(StatesConfig::default()),
            Arc::new(PhasesConfig::default()),
            Arc::new(DependenciesConfig::default()),
            Arc::new(AutoAdvanceConfig::default()),
            Arc::new(AttachmentsConfig::default()),
            Arc::new(TagsConfig::default()),
            Arc::new(IdsConfig::default()),
            Arc::new(WorkflowsConfig::default()),
            Arc::new(fc),
        );
        ToolHandler::new(
            db,
            PathBuf::from("test-media"),
            PathBuf::from("test-skills"),
            server_paths,
            Arc::new(Prompts::default()),
            config,
            OutputFormat::Json,
            50,
            Arc::new(PathMapper::default()),
        )
    }

    #[test]
    fn feedback_tools_excluded_when_disabled() {
        let handler = handler_with_feedback_config(FeedbackConfig {
            enabled: false,
            ..Default::default()
        });
        let tools = handler.get_tools();
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();

        assert!(
            !names.contains(&"give_feedback"),
            "give_feedback should NOT be listed when feedback is disabled"
        );
        assert!(
            !names.contains(&"list_feedback"),
            "list_feedback should NOT be listed when feedback is disabled"
        );
    }

    #[test]
    fn feedback_tools_included_when_enabled() {
        let handler = handler_with_feedback_config(FeedbackConfig {
            enabled: true,
            ..Default::default()
        });
        let tools = handler.get_tools();
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();

        assert!(
            names.contains(&"give_feedback"),
            "give_feedback should be listed when feedback is enabled"
        );
        assert!(
            names.contains(&"list_feedback"),
            "list_feedback should be listed when feedback is enabled"
        );
    }

    #[test]
    fn default_feedback_config_enables_tools() {
        let handler = handler_with_feedback_config(FeedbackConfig::default());
        let tools = handler.get_tools();
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();

        assert!(
            names.contains(&"give_feedback"),
            "give_feedback should be listed with default FeedbackConfig (enabled)"
        );
        assert!(
            names.contains(&"list_feedback"),
            "list_feedback should be listed with default FeedbackConfig (enabled)"
        );
    }
}