indra 0.8.1

Run, watch and orchestrate AI coding agents (Claude Code, Codex, Gemini, Ollama) across machines: a web console, workflows with conditions and loops, boards, an MCP server and graph memory, in one binary.
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! `indra mcp`: an MCP server (stdio, JSON-RPC 2.0) that lets an AI agent
//! design workflows: read the format and what this machine has, look at the
//! workflows there are, validate a draft, and save it to `indra.toml` or a
//! repository's `.indra/workflows/`.
//!
//! Add it to Claude Code with `claude mcp add indra -- indra mcp`; the
//! console's **Create with Claude** starts Claude Code with it. Nothing here
//! runs a workflow or a command: it only reads and writes workflow
//! definitions, each checked exactly as `indra serve` would load it.

use crate::deck::config::{self, ConfigFile, LoadOptions, RepoFile, Settings, WorkflowCfg};
use serde_json::{json, Value};
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

/// The format, for the agent that writes a workflow.
pub const GUIDE: &str = r#"# indra workflows

A workflow is a graph of steps. Each step runs an AI agent (a prompt), a
skill, a shell command, or another workflow, in a target directory on a node.
`needs` orders steps; steps with nothing between them run in parallel.

```toml
[[workflow]]
name = "ci-fix"                       # letters, digits, - and _
description = "Test, and let an agent fix failures"

[[workflow.input]]                    # asked for when it runs
name = "branch"                       # used as {input.branch} in prompts
label = "Branch"
required = true
# kind = "text" | "choice" | "bool" | "target" | "profile"
# choice: options = [{ value = "a", label = "A" }]; bool: when_true = "--deep"
# text: pattern = '[a-z0-9/_-]+' (a regular expression for the whole value)

[[workflow.task]]
id = "test"                           # unique in the workflow
run = "cargo test 2>&1"               # a SHELL STEP: bash (sh), PowerShell on Windows
allow_failure = true                  # its failure does not fail the workflow

[[workflow.task]]
id = "fix"
needs = ["test"]
when = "test.failed"                  # a CONDITION; false: the step is skipped
profile = "worker"                    # how the agent runs (see Profiles below)
prompt = """Tests failed on {input.branch}:
{steps.test.output}
Fix the code so they pass."""         # an AGENT STEP

[[workflow.task]]
id = "retest"
needs = ["fix"]
run = "cargo test"
until = "self.exit_code == 0"         # a LOOP: run again until it holds
max_runs = 3                          # (default 3, at most 20)

[[workflow.task]]
id = "per-service"
foreach = ["api", "web", "worker"]    # a LOOP: one copy per item, in parallel
run = "make -C services/$INDRA_ITEM lint"   # or "{input.services}" (comma or newline separated)

[[workflow.task]]
id = "notify-failure"
needs = ["retest", "per-service"]
on = "failure"                        # runs only if something it needs failed
run = "echo \"failed: $INDRA_STATUS_RETEST\""

[[workflow.task]]
id = "nothing-to-do"
needs = ["test"]
when = "test.succeeded"
stop = "success"                      # a STOP STEP: ends the workflow here, as success
prompt = "Tests pass; nothing to fix."  # (or "failure"); steps not started are skipped

[[workflow.task]]
id = "report"
needs = ["retest", "per-service"]
on = "always"                         # runs once they finish, however they went
skill = "summarize"                   # a SKILL STEP: Claude Code runs /summarize <prompt>
prompt = "{target}"
```

Step fields:
- id (required), needs = [ids], node = "local" (default) or a node name,
  target = directory on that node (in a repository file: default the
  repository itself, relative paths inside it; in indra.toml: default the
  harness's first target; on another node: required).
- One of: prompt (an agent), skill (+ prompt as its arguments, default
  {target}), run (a shell command), workflow = "<other workflow>" (runs it as
  one step), component = "<saved task>" (fields left empty come from it).
- profile, runner (default claude), model, provider ("anthropic" | "ollama"),
  retries (extra attempts after a failure).
- Control flow: when, on ("success" default | "failure" | "always"),
  allow_failure, foreach, until, max_runs, and stop = "success" | "failure"
  (a step that only ends the workflow when reached; prompt is its message).

Prompts may use {target}, {node}, {input.NAME}, {item} (in a foreach copy) and
{steps.ID.output}, {steps.ID.exit_code}, {steps.ID.status} (an earlier step's
result; ID must be in its needs, directly or through other steps).

Shell commands never get text substituted (so no value can change the
command): they read environment variables instead.
- $INDRA_INPUT_<NAME> (upper case), $INDRA_ITEM, $INDRA_TARGET, $INDRA_NODE,
  $INDRA_WORKFLOW, $INDRA_STEP
- For each finished step: $INDRA_OUTPUT_<ID>, $INDRA_EXIT_<ID>,
  $INDRA_STATUS_<ID> (ID upper case, - becomes _)
- A command runs in its target, succeeds with exit code 0, and its output is
  what later conditions see.

Conditions (when, until):
- <step>.succeeded | failed | skipped | status | output | exit_code,
  input.<name>, and in until: self.<the same fields> (this step's last run).
- ==, !=, contains, <, <=, >, >= (numbers), and, or, not, ( ).
- Literals: "text" or 'text', numbers, true, false.
- A referenced step must run before the step (be in its needs, directly or
  through others), and cannot be a foreach step (follow a foreach step with
  on = "failure" or "always" instead).

Semantics:
- A step with on = "success" runs when every step it needs succeeded (or
  failed with allow_failure); it is skipped when one failed or was skipped.
- The workflow fails if a step failed without allow_failure; skipped steps
  (a condition, a branch not taken) do not fail it.
- until is checked after a successful run; a failed run uses retries.
- Prefer shell steps for exact commands (build, test, lint, git) and agent
  steps for judgement (fixing, reviewing, writing). Use the built-in profiles
  (planner read-only, worker edits, builder cheap edits, reviewer) unless the
  person asked for others.
"#;

fn context(opts: &LoadOptions) -> String {
    let s = match config::load(opts.clone()) {
        Ok(s) => s,
        Err(e) => {
            return format!(
                "\n## This machine\n\nThe configuration does not load: {}\n",
                e
            )
        }
    };
    let mut out = String::from("\n## This machine\n\n");
    out.push_str(&format!(
        "Config file: {}\n",
        s.config_path
            .as_ref()
            .map(|p| p.display().to_string())
            .unwrap_or_else(
                || config::edit_path(opts, None).display().to_string() + " (created on first save)"
            )
    ));
    out.push_str("\nSave locations (save_workflow `location`):\n- config (this machine's indra.toml; only here)\n");
    for d in &s.repo_dirs {
        out.push_str(&format!(
            "- {} (a repository: commit the file to share it)\n",
            d.display()
        ));
    }
    out.push_str("\nProfiles:\n");
    for p in &s.profiles {
        out.push_str(&format!(
            "- {}: {}{}\n",
            p.name,
            p.description,
            p.model
                .as_deref()
                .map(|m| format!(" (model {})", m))
                .unwrap_or_default()
        ));
    }
    out.push_str("\nNodes: local");
    for n in &s.nodes {
        out.push_str(&format!(", {} (targets: {})", n.name, n.targets.join(", ")));
    }
    out.push_str("\n\nRunners: ");
    out.push_str(
        &s.runners
            .iter()
            .map(|r| {
                format!(
                    "{}{}",
                    r.name,
                    if r.program.is_some() {
                        ""
                    } else {
                        " (not installed)"
                    }
                )
            })
            .collect::<Vec<_>>()
            .join(", "),
    );
    for h in &s.harnesses {
        let skills: Vec<_> = crate::deck::discover::discover(h)
            .into_iter()
            .filter(|d| d.kind == crate::deck::discover::Kind::Skill)
            .collect();
        out.push_str(&format!(
            "\n\nHarness {} (targets: {}); skills:\n",
            h.name,
            h.targets
                .iter()
                .map(|t| t.display().to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ));
        if skills.is_empty() {
            out.push_str("- none\n");
        }
        for d in skills.iter().take(80) {
            out.push_str(&format!(
                "- {}: {}\n",
                d.name,
                d.description.chars().take(160).collect::<String>()
            ));
        }
    }
    out.push_str("\nSaved tasks (component = \"<name>\"):\n");
    if s.components.is_empty() {
        out.push_str("- none\n");
    }
    for c in &s.components {
        out.push_str(&format!(
            "- {}: {}\n",
            c.name,
            c.run
                .as_deref()
                .map(|r| format!("run {}", r))
                .or_else(|| c.skill.as_deref().map(|k| format!("skill {}", k)))
                .unwrap_or_else(|| c.prompt.chars().take(120).collect())
        ));
    }
    out.push_str(
        "\nWorkflows already defined (names must differ unless you mean to replace one):\n",
    );
    for w in &s.workflows {
        out.push_str(&format!(
            "- {}{}\n",
            w.name,
            if w.builtin { " (built in)" } else { "" }
        ));
    }
    out
}

fn tools() -> Value {
    json!([
        { "name": "workflow_guide",
          "description": "The indra workflow format (steps, shell commands, conditions, loops, inputs) and what this machine has: profiles, nodes, repositories to save in, skills, saved tasks, existing workflows. Call it first.",
          "inputSchema": { "type": "object", "properties": {} } },
        { "name": "list_workflows",
          "description": "The workflows defined here, with where each lives.",
          "inputSchema": { "type": "object", "properties": {} } },
        { "name": "get_workflow",
          "description": "One workflow as TOML, as written (to change it, edit and save it back).",
          "inputSchema": { "type": "object", "properties": { "name": { "type": "string" } }, "required": ["name"] } },
        { "name": "validate_workflow",
          "description": "Check workflow TOML ([[workflow]] tables, optional [[component]] tables) exactly as indra would load it, for a save location. Returns the errors to fix, or a summary of the steps.",
          "inputSchema": { "type": "object", "properties": {
              "toml": { "type": "string" },
              "location": { "type": "string", "description": "\"config\" or a repository directory from workflow_guide (default config)" } },
            "required": ["toml"] } },
        { "name": "save_workflow",
          "description": "Validate and save workflow TOML to this machine's indra.toml (location \"config\") or to a repository's .indra/workflows/<name>.toml (location = the repository directory). A workflow with the same name there is replaced.",
          "inputSchema": { "type": "object", "properties": {
              "toml": { "type": "string" },
              "location": { "type": "string" } },
            "required": ["toml", "location"] } }
    ])
}

/// The config file workflows are checked against and saved to.
fn config_file(opts: &LoadOptions) -> (Option<PathBuf>, PathBuf) {
    let loaded = match &opts.config {
        Some(p) => Some(p.clone()).filter(|p| p.is_file()),
        None => config::default_config_paths(&opts.cwd)
            .into_iter()
            .find(|p| p.is_file()),
    };
    let edit = config::edit_path(opts, loaded.as_deref());
    (loaded, edit)
}

enum Loc {
    Config,
    Repo(PathBuf),
}

fn location(opts: &LoadOptions, raw: Option<&str>) -> Result<Loc, String> {
    let raw = raw
        .map(str::trim)
        .filter(|x| !x.is_empty())
        .unwrap_or("config");
    if raw == "config" {
        return Ok(Loc::Config);
    }
    let s = config::load(opts.clone())?;
    let want = PathBuf::from(raw);
    let want = std::fs::canonicalize(&want).unwrap_or(want);
    s.repo_dirs
        .iter()
        .find(|d| **d == want)
        .map(|d| Loc::Repo(d.clone()))
        .ok_or_else(|| {
            format!(
                "location must be \"config\" or one of these repositories: {}",
                s.repo_dirs
                    .iter()
                    .map(|d| d.display().to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        })
}

/// Load the draft with the rest of the configuration, as indra would.
fn check(opts: &LoadOptions, text: &str, loc: &Loc) -> Result<(RepoFile, Settings), String> {
    let rf: RepoFile =
        toml::from_str(text).map_err(|e| format!("not valid workflow TOML: {}", e))?;
    if rf.workflow.is_empty() {
        return Err("there is no [[workflow]] table in it".into());
    }
    let (loaded, _) = config_file(opts);
    let mut file: ConfigFile = match &loaded {
        Some(p) => {
            let (t, _) = crate::deck::confedit::read(p)?;
            crate::deck::confedit::parse(&t).map_err(|e| format!("{}: {}", p.display(), e))?
        }
        None => ConfigFile::default(),
    };
    for w in &rf.workflow {
        let mut w = w.clone();
        // In a repository, a step without a target runs in it, and relative
        // targets are inside it.
        if let Loc::Repo(dir) = loc {
            for t in &mut w.tasks {
                if t.workflow.is_none()
                    && (t.node.is_empty() || t.node == crate::deck::nodes::LOCAL)
                    && !t.target.contains("{input.")
                {
                    t.target = if t.target.trim().is_empty() {
                        dir.display().to_string()
                    } else {
                        config::expand(dir, &t.target).display().to_string()
                    };
                }
            }
        }
        file.workflow.retain(|x| x.name != w.name);
        file.workflow.push(w);
    }
    for c in &rf.component {
        file.component.retain(|x| x.name != c.name);
        file.component.push(c.clone());
    }
    let s = config::build_settings(opts, loaded, &file)?;
    Ok((rf, s))
}

fn summary(w: &WorkflowCfg) -> String {
    let mut out = format!("workflow '{}': {} steps\n", w.name, w.tasks.len());
    for t in &w.tasks {
        let kind = if let Some(r) = &t.run {
            format!("run {}", r.lines().next().unwrap_or(""))
        } else {
            format!(
                "agent: {}",
                t.prompt
                    .lines()
                    .next()
                    .unwrap_or("")
                    .chars()
                    .take(80)
                    .collect::<String>()
            )
        };
        out.push_str(&format!(
            "- {} [{}]{}{}{}{}{}\n",
            t.id,
            kind,
            if t.needs.is_empty() {
                String::new()
            } else {
                format!(" after {}", t.needs.join(", "))
            },
            t.on.as_deref()
                .filter(|o| *o != "success")
                .map(|o| format!(" on {}", o))
                .unwrap_or_default(),
            t.when
                .as_deref()
                .map(|c| format!(" when {}", c))
                .unwrap_or_default(),
            t.foreach
                .as_ref()
                .map(|f| format!(" for each of {}", f.list().join(", ")))
                .unwrap_or_default(),
            t.until
                .as_deref()
                .map(|c| format!(" until {}", c))
                .unwrap_or_default(),
        ));
    }
    out
}

fn call(opts: &LoadOptions, name: &str, args: &Value) -> Result<String, String> {
    let s_arg = |k: &str| args.get(k).and_then(|v| v.as_str());
    match name {
        "workflow_guide" => Ok(format!("{}{}", GUIDE, context(opts))),
        "list_workflows" => {
            let s = config::load(opts.clone())?;
            let mut out = String::new();
            for w in &s.workflows {
                out.push_str(&format!(
                    "- {} ({} steps){}: {}\n",
                    w.name,
                    w.tasks.len(),
                    if w.builtin {
                        " built in".to_string()
                    } else {
                        w.source
                            .as_ref()
                            .map(|p| format!(" in {}", p.display()))
                            .unwrap_or_else(|| " in indra.toml".into())
                    },
                    w.description
                ));
            }
            for e in &s.workflow_errors {
                out.push_str(&format!("! not usable: {}\n", e));
            }
            Ok(if out.is_empty() {
                "No workflows yet.".into()
            } else {
                out
            })
        }
        "get_workflow" => {
            let want = s_arg("name").ok_or("name is required")?;
            let (loaded, _) = config_file(opts);
            let mut found: Option<RepoFile> = None;
            if let Some(p) = &loaded {
                let (t, _) = crate::deck::confedit::read(p)?;
                if let Ok(cf) = crate::deck::confedit::parse(&t) {
                    if let Some(w) = cf.workflow.iter().find(|w| w.name == want) {
                        found = Some(RepoFile {
                            workflow: vec![w.clone()],
                            component: vec![],
                        });
                    }
                }
            }
            if found.is_none() {
                let s = config::load(opts.clone())?;
                for d in &s.repo_dirs {
                    for f in config::repo_workflow_files(d) {
                        if let Ok(rf) = config::read_repo_file(&f) {
                            if let Some(w) = rf.workflow.iter().find(|w| w.name == want) {
                                found = Some(RepoFile {
                                    workflow: vec![w.clone()],
                                    component: rf.component.clone(),
                                });
                            }
                        }
                    }
                }
                if found.is_none() {
                    if let Some(w) = s.workflows.iter().find(|w| w.name == want && w.builtin) {
                        found = Some(RepoFile {
                            workflow: vec![w.clone()],
                            component: vec![],
                        });
                    }
                }
            }
            let rf = found.ok_or_else(|| format!("no workflow named '{}'", want))?;
            toml::to_string(&rf).map_err(|e| e.to_string())
        }
        "validate_workflow" => {
            let loc = location(opts, s_arg("location"))?;
            let (rf, s) = check(opts, s_arg("toml").ok_or("toml is required")?, &loc)?;
            let mut out = String::from("Valid.\n");
            for w in &rf.workflow {
                if let Some(x) = s.workflows.iter().find(|x| x.name == w.name) {
                    out.push_str(&summary(x));
                }
            }
            Ok(out)
        }
        "save_workflow" => {
            let loc = location(opts, s_arg("location"))?;
            let text = s_arg("toml").ok_or("toml is required")?;
            let (rf, _) = check(opts, text, &loc)?;
            match loc {
                Loc::Config => {
                    let (_, path) = config_file(opts);
                    let (current, _) = crate::deck::confedit::read(&path)?;
                    let mut cf = crate::deck::confedit::parse(&current)?;
                    for w in &rf.workflow {
                        cf.workflow.retain(|x| x.name != w.name);
                        cf.workflow.push(w.clone());
                    }
                    let mut next = crate::deck::confedit::set_section(
                        &current,
                        "workflow",
                        &serde_json::to_value(&cf.workflow).map_err(|e| e.to_string())?,
                    )?;
                    if !rf.component.is_empty() {
                        for c in &rf.component {
                            cf.component.retain(|x| x.name != c.name);
                            cf.component.push(c.clone());
                        }
                        next = crate::deck::confedit::set_section(
                            &next,
                            "component",
                            &serde_json::to_value(&cf.component).map_err(|e| e.to_string())?,
                        )?;
                    }
                    crate::deck::confedit::write_atomic(&path, &next)?;
                    Ok(format!(
                        "Saved {} to {}. Run it from the console's Workflows page or with `indra run {}`.",
                        rf.workflow.iter().map(|w| w.name.as_str()).collect::<Vec<_>>().join(", "),
                        path.display(),
                        rf.workflow[0].name
                    ))
                }
                Loc::Repo(dir) => {
                    let mut saved = Vec::new();
                    for (i, w) in rf.workflow.iter().enumerate() {
                        let f = config::repo_workflow_dir(&dir).join(format!("{}.toml", w.name));
                        let mut file = if f.exists() {
                            config::read_repo_file(&f)
                                .map_err(|e| format!("{}: {}", f.display(), e))?
                        } else {
                            RepoFile::default()
                        };
                        file.workflow.retain(|x| x.name != w.name);
                        file.workflow.push(w.clone());
                        // The saved tasks go with the first workflow's file.
                        if i == 0 {
                            for c in &rf.component {
                                file.component.retain(|x| x.name != c.name);
                                file.component.push(c.clone());
                            }
                        }
                        config::write_repo_file(&f, &file)?;
                        saved.push(f.display().to_string());
                    }
                    Ok(format!(
                        "Saved {}. Commit it to share it. Run it from the console's Workflows page or with `indra run {}`.",
                        saved.join(", "),
                        rf.workflow[0].name
                    ))
                }
            }
        }
        _ => Err(format!("no tool named '{}'", name)),
    }
}

/// Answer one JSON-RPC message; None for a notification.
pub fn handle(opts: &LoadOptions, msg: &Value) -> Option<Value> {
    let id = msg.get("id").cloned()?;
    let method = msg.get("method").and_then(|m| m.as_str()).unwrap_or("");
    let ok = |result: Value| json!({ "jsonrpc": "2.0", "id": id, "result": result });
    let err = |code: i64, message: &str| json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": message } });
    Some(match method {
        "initialize" => ok(json!({
            "protocolVersion": msg["params"]["protocolVersion"].as_str().unwrap_or("2025-06-18"),
            "capabilities": { "tools": {} },
            "serverInfo": { "name": "indra", "version": env!("CARGO_PKG_VERSION") },
            "instructions": "Design indra workflows: call workflow_guide first, write the TOML, validate_workflow until it is valid, then save_workflow.",
        })),
        "ping" => ok(json!({})),
        "tools/list" => ok(json!({ "tools": tools() })),
        "tools/call" => {
            let name = msg["params"]["name"].as_str().unwrap_or("");
            let args = msg["params"].get("arguments").cloned().unwrap_or(json!({}));
            let (text, is_error) = match call(opts, name, &args) {
                Ok(t) => (t, false),
                Err(e) => (e, true),
            };
            ok(json!({ "content": [{ "type": "text", "text": text }], "isError": is_error }))
        }
        _ => err(-32601, "method not found"),
    })
}

pub fn serve(opts: LoadOptions) -> Result<ExitCode, String> {
    let stdin = std::io::stdin();
    let mut out = std::io::stdout();
    for line in stdin.lock().lines() {
        let line = line.map_err(|e| e.to_string())?;
        if line.trim().is_empty() {
            continue;
        }
        let reply = match serde_json::from_str::<Value>(&line) {
            Ok(msg) => handle(&opts, &msg),
            Err(_) => Some(
                json!({ "jsonrpc": "2.0", "id": null, "error": { "code": -32700, "message": "parse error" } }),
            ),
        };
        if let Some(r) = reply {
            writeln!(out, "{}", r).map_err(|e| e.to_string())?;
            out.flush().map_err(|e| e.to_string())?;
        }
    }
    Ok(ExitCode::SUCCESS)
}

/// The MCP config file Claude Code is started with (`--mcp-config`): this
/// binary as the `indra` server, loading what the console loads.
pub fn client_config(dir: &Path, opts: &LoadOptions) -> Result<PathBuf, String> {
    let exe = std::env::current_exe().map_err(|e| e.to_string())?;
    let mut args = vec!["mcp".to_string()];
    let (loaded, edit) = config_file(opts);
    args.push("--config".into());
    args.push(loaded.unwrap_or(edit).display().to_string());
    for t in &opts.targets {
        args.push("--target".into());
        args.push(opts.cwd.join(t).display().to_string());
    }
    let v = json!({ "mcpServers": { "indra": { "type": "stdio", "command": exe.display().to_string(), "args": args } } });
    std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
    let f = dir.join(format!("indra-mcp-{}.json", std::process::id()));
    std::fs::write(&f, serde_json::to_string_pretty(&v).unwrap_or_default())
        .map_err(|e| e.to_string())?;
    Ok(f)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_mcp_server_designs_and_saves_workflows() {
        let dir = std::env::temp_dir().join(format!("indra-mcp-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("repo")).unwrap();
        let cfg = dir.join("indra.toml");
        std::fs::write(
            &cfg,
            "# mine\n[[runner]]\nname = \"say\"\ncommand = \"echo\"\nargs = [\"{prompt}\"]\n",
        )
        .unwrap();
        let opts = LoadOptions {
            config: Some(cfg.clone()),
            targets: vec![dir.join("repo")],
            bind: None,
            read_only: false,
            cwd: dir.clone(),
        };
        let rpc = |method: &str, params: Value| {
            handle(
                &opts,
                &json!({ "jsonrpc": "2.0", "id": 1, "method": method, "params": params }),
            )
            .unwrap()
        };
        let init = rpc("initialize", json!({ "protocolVersion": "2025-06-18" }));
        assert_eq!(init["result"]["serverInfo"]["name"], "indra");
        assert!(handle(
            &opts,
            &json!({ "jsonrpc": "2.0", "method": "notifications/initialized" })
        )
        .is_none());
        let names: Vec<String> = rpc("tools/list", json!({}))["result"]["tools"]
            .as_array()
            .unwrap()
            .iter()
            .map(|t| t["name"].as_str().unwrap().to_string())
            .collect();
        assert_eq!(
            names,
            [
                "workflow_guide",
                "list_workflows",
                "get_workflow",
                "validate_workflow",
                "save_workflow"
            ]
        );
        let tool = |name: &str, args: Value| {
            let r = rpc("tools/call", json!({ "name": name, "arguments": args }));
            (
                r["result"]["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .to_string(),
                r["result"]["isError"] == true,
            )
        };
        let (guide, e) = tool("workflow_guide", json!({}));
        assert!(
            !e && guide.contains("until") && guide.contains("planner") && guide.contains("repo"),
            "{}",
            guide
        );
        // A broken draft: the errors come back to fix.
        let bad = "[[workflow]]\nname = \"w\"\n[[workflow.task]]\nid = \"a\"\ntarget = \"repo\"\nrun = \"true\"\nwhen = \"b.failed\"\n";
        let (msg, e) = tool(
            "validate_workflow",
            json!({ "toml": bad, "location": "config" }),
        );
        assert!(e && msg.contains("'b' is not a step"), "{}", msg);
        // A good one, for the repository (no target needed there).
        let good = "[[workflow]]\nname = \"ci\"\n[[workflow.task]]\nid = \"test\"\nrun = \"make test\"\nallow_failure = true\n[[workflow.task]]\nid = \"fix\"\nneeds = [\"test\"]\nwhen = \"test.failed\"\nprofile = \"worker\"\nprompt = \"fix {steps.test.output}\"\n";
        let repo = std::fs::canonicalize(dir.join("repo"))
            .unwrap()
            .display()
            .to_string();
        let (msg, e) = tool(
            "validate_workflow",
            json!({ "toml": good, "location": repo }),
        );
        assert!(
            !e && msg.contains("fix [agent") && msg.contains("when test.failed"),
            "{}",
            msg
        );
        // In indra.toml, a step without a target runs in the first target.
        let (msg, e) = tool("validate_workflow", json!({ "toml": good }));
        assert!(!e, "{}", msg);
        // On another node, a target is required.
        let remote = "[[workflow]]\nname = \"r\"\n[[workflow.task]]\nid = \"a\"\nnode = \"nowhere\"\nrun = \"true\"\n";
        let (msg, e) = tool("validate_workflow", json!({ "toml": remote }));
        assert!(e, "{}", msg);
        let (msg, e) = tool("save_workflow", json!({ "toml": good, "location": repo }));
        assert!(!e, "{}", msg);
        assert!(dir.join("repo/.indra/workflows/ci.toml").is_file());
        let (list, _) = tool("list_workflows", json!({}));
        assert!(list.contains("ci (2 steps) in"), "{}", list);
        let (got, e) = tool("get_workflow", json!({ "name": "ci" }));
        assert!(!e && got.contains("when = \"test.failed\""), "{}", got);
        // Saved to the config file, keeping its comments.
        let local = "[[workflow]]\nname = \"here\"\n[[workflow.task]]\nid = \"a\"\ntarget = \"repo\"\nrun = \"echo hi\"\n";
        let (msg, e) = tool(
            "save_workflow",
            json!({ "toml": local, "location": "config" }),
        );
        assert!(!e, "{}", msg);
        let text = std::fs::read_to_string(&cfg).unwrap();
        assert!(
            text.contains("# mine") && text.contains("name = \"here\""),
            "{}",
            text
        );
        // Only known repositories.
        let (msg, e) = tool(
            "save_workflow",
            json!({ "toml": local, "location": "/etc" }),
        );
        assert!(e && msg.contains("location must be"), "{}", msg);
        let _ = std::fs::remove_dir_all(&dir);
    }
}