optimization-runner 0.6.0

Local runner (optimization-run) and MCP server (optimization-mcp) for versioned optimization jobs on one CPU budget
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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! `optimization-mcp`: a Model Context Protocol server over one node root.
//!
//! The server speaks MCP over stdio, one JSON-RPC message per line. It is
//! dual-era: requests that carry `io.modelcontextprotocol/protocolVersion`
//! in `_meta` are served statelessly under revision 2026-07-28, with the
//! Tasks extension (`io.modelcontextprotocol/tasks`); an `initialize`
//! request selects a legacy revision (2025-11-25 and earlier) for the
//! process. Every tool drives the `optimization-run` command line, the
//! runner's documented interface, so the runner's validation, idempotency,
//! admission and recovery rules apply unchanged. The server registers no
//! workers and initializes no node: that stays with the person who owns the
//! node.
//!
//! See the guide's section [Agents and MCP](https://github.com/dietmarwo/optimization-jobs/blob/main/docs/job-api.md#agents-and-mcp) for the tools
//! and their mapping onto the runner.
mod apps;
mod guide;
mod inputs;
mod resources;
mod tasks;
mod tools;
mod uploads;

use clap::{Parser, ValueEnum};
use serde_json::{Map, Value, json};
use std::{
    collections::HashMap,
    io::{self, BufRead, Read, Write},
    path::PathBuf,
    sync::{
        Arc, Condvar, Mutex,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
    thread,
    time::Duration,
};

/// Protocol revisions served statelessly, with per-request `_meta`.
const MODERN: [&str; 1] = ["2026-07-28"];
/// Protocol revisions served after an `initialize` handshake, newest first.
const LEGACY: [&str; 4] = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
const PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion";
const CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities";
const SERVER_INFO: &str = "io.modelcontextprotocol/serverInfo";
const SUBSCRIPTION: &str = "io.modelcontextprotocol/subscriptionId";
/// The Tasks extension (SEP-2663).
const TASKS: &str = "io.modelcontextprotocol/tasks";
/// MCP error codes of revision 2026-07-28.
const MISSING_CAPABILITY: i64 = -32021;
const UNSUPPORTED_VERSION: i64 = -32022;
const INVALID_PARAMS: i64 = -32602;
const METHOD_NOT_FOUND: i64 = -32601;
const INVALID_REQUEST: i64 = -32600;
const PARSE_ERROR: i64 = -32700;
const INTERNAL_ERROR: i64 = -32603;
/// The server is at one of its limits below; the request may be retried.
const SERVER_BUSY: i64 = -32000;
/// Largest message, in bytes: the input files of a job are files, not part
/// of it. A longer line is skipped unread and answered with an error.
const MAX_MESSAGE: usize = 64 * 1024 * 1024;
/// Requests answered at the same time (each runs on a thread of its own).
const MAX_REQUESTS: usize = 64;
/// Of these, requests that wait (`wait_job`, `subscriptions/listen`): the
/// rest stays free for status, cancellation and other short calls.
const MAX_WAITING: usize = 32;
/// Calls that run worker validation (`validate_job`, `submit_job`) at the
/// same time; further ones queue. Validation runs before CPU-slot admission.
const MAX_VALIDATIONS: usize = 4;
/// How long clients may cache `tools/list` and `server/discover`.
const CACHE_MS: u64 = 3_600_000;

const INSTRUCTIONS: &str = "Runs durable optimization jobs on this node with its registered native \
workers (fcmaes, LP-IP, SQP and application workers). If no solver or profile has been chosen \
yet, call get_guide first: it says which solver and profile fit the problem (its section \
argument returns one part). Then call describe_node, and describe_worker for a worker's profiles \
and options, the JSON Schemas of its payloads and runnable examples. Write an \
optimization.job/0.1 envelope, check it with validate_job and start it with submit_job. Input \
files that are not on this machine yet, such as a new LP model, go through put_input, which \
returns the file, sha256 and input_dir for the job. The request_key makes \
submission idempotent: the same key with the same content returns the existing job, other \
content under that key is rejected, so choose a new key for a new job. Jobs run detached from \
this server and survive its restart. Follow a job with wait_job or get_job and read get_result \
when it has ended. cancel_job stops it gracefully and keeps completed work; kill_job stops it \
at once. Every task result carries a claim (what the worker asserts) and evidence (how much of \
it was independently rechecked); there is no automatic winner across tasks or jobs.";

#[derive(Parser)]
#[command(
    name = "optimization-mcp",
    version,
    about = "MCP server (stdio) for the durable optimization jobs of one optimization-run node (Linux)"
)]
struct Args {
    /// Node root, as for optimization-run.
    #[arg(
        long,
        env = "OPTIMIZATION_RUN_ROOT",
        default_value = ".optimization-jobs"
    )]
    root: PathBuf,
    /// The optimization-run executable; by default the one next to this
    /// server.
    #[arg(long)]
    runner: Option<PathBuf>,
    /// How submit_job starts jobs: `process` runs `optimization-run run` in a
    /// session of its own, which outlives this server; `systemd` uses
    /// `optimization-run submit` (a systemd user manager is required).
    #[arg(long, value_enum, default_value_t = Launch::Process)]
    launch: Launch,
    /// Directory that relative input files of inline jobs are read from, by
    /// default the working directory.
    #[arg(long)]
    input_dir: Option<PathBuf>,
    /// Largest total size of the input store of put_input (NODE/mcp/inputs),
    /// in bytes.
    #[arg(long, default_value_t = inputs::DEFAULT_STORE_BYTES)]
    input_store_bytes: u64,
    /// Experimental Files transfer origin. Requires a separate endpoint.
    #[arg(long)]
    files_endpoint: Option<String>,
}

/// How `submit_job` starts jobs (`--launch`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Launch {
    /// `optimization-run run` in a session of its own, which outlives the
    /// server.
    Process,
    /// `optimization-run submit`, which needs a systemd user manager.
    Systemd,
}

impl Launch {
    /// The option value: `process` or `systemd`.
    pub fn name(self) -> &'static str {
        match self {
            Launch::Process => "process",
            Launch::Systemd => "systemd",
        }
    }
}

// Written out rather than derived: a derived ValueEnum turns the variants'
// doc comments into option help, which would change `--help`.
impl ValueEnum for Launch {
    fn value_variants<'a>() -> &'a [Self] {
        &[Launch::Process, Launch::Systemd]
    }

    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
        Some(clap::builder::PossibleValue::new(self.name()))
    }
}

/// The MCP server over one node root.
pub struct Server {
    /// The node root.
    pub root: PathBuf,
    /// The `optimization-run` executable that every tool calls.
    pub runner: PathBuf,
    /// How jobs are started.
    pub launch: Launch,
    /// The directory that relative input files of inline jobs are read from.
    pub input_dir: PathBuf,
    /// Limit of the input store's total size.
    pub input_store_bytes: u64,
    uploads: Option<uploads::Uploads>,
    out: Mutex<io::Stdout>,
    inflight: Mutex<HashMap<String, Arc<AtomicBool>>>,
    initialized: AtomicBool,
    /// JSON-RPC batches, part of protocol 2025-03-26 only.
    batches: AtomicBool,
    /// Set at the end of input; waiting requests stop, jobs keep running.
    pub shutdown: AtomicBool,
    /// Requests counted against [`MAX_WAITING`].
    waiting: AtomicUsize,
    /// Running validations and the signal that one ended.
    validations: (Mutex<usize>, Condvar),
}

/// One of the `MAX_VALIDATIONS` validation places, given back on drop.
pub struct Permit<'a>(&'a Server);

impl Drop for Permit<'_> {
    fn drop(&mut self) {
        let (count, ended) = &self.0.validations;
        *count.lock().unwrap_or_else(|e| e.into_inner()) -= 1;
        ended.notify_one();
    }
}

/// One request: its era and whether it may be cancelled or report progress.
pub struct Ctx {
    /// The request is served statelessly under revision 2026-07-28.
    pub modern: bool,
    /// The client declared the Tasks extension on this request.
    pub tasks: bool,
    /// Set when the client cancels the request.
    pub cancel: Arc<AtomicBool>,
    /// The request's progress token, if it asked for progress.
    pub progress: Option<Value>,
    /// The JSON-RPC ID of the request.
    pub id: Value,
}

/// The answer to one request.
pub enum Reply {
    /// A result.
    Ok(Value),
    /// A JSON-RPC error: its code, message and optional data.
    Err(i64, String, Option<Value>),
    /// No response: the request was cancelled, or it ended a subscription
    /// the client cancelled.
    Silent,
}

impl Ctx {
    /// Whether the request was cancelled or the server is shutting down.
    pub fn stopped(&self, server: &Server) -> bool {
        self.cancel.load(Ordering::Relaxed) || server.shutdown.load(Ordering::Relaxed)
    }
}

fn supported() -> Vec<&'static str> {
    MODERN.iter().chain(LEGACY.iter()).copied().collect()
}

fn extension<'a>(params: &'a Value, id: &str) -> &'a Value {
    &params["_meta"][CLIENT_CAPABILITIES]["extensions"][id]
}

fn server_info() -> Value {
    json!({"name":"optimization-mcp","title":"Optimization jobs","version":env!("CARGO_PKG_VERSION")})
}

fn missing_tasks_capability() -> Reply {
    Reply::Err(
        MISSING_CAPABILITY,
        "Missing required client capability".into(),
        Some(json!({"requiredCapabilities":{"extensions":{TASKS:{}}}})),
    )
}

/// The protocol error of a task whose job cannot be read or cancelled. A job
/// removed from the node makes its task unknown.
fn job_error(job_id: &str, action: &str, e: &tools::ToolError) -> Reply {
    if e.code == "unknown_job" {
        return Reply::Err(
            INVALID_PARAMS,
            format!("Failed to retrieve task: its job {job_id} no longer exists on this node"),
            None,
        );
    }
    Reply::Err(
        INTERNAL_ERROR,
        format!("cannot {action} job {job_id}: {}: {}", e.code, e.message),
        None,
    )
}

impl Server {
    /// Waits for a validation place; a cancelled request stops waiting.
    pub fn validation_permit(&self, ctx: &Ctx) -> Result<Permit<'_>, tools::Fail> {
        let (count, ended) = &self.validations;
        let mut running = count.lock().unwrap_or_else(|e| e.into_inner());
        while *running >= MAX_VALIDATIONS {
            if ctx.stopped(self) {
                return Err(tools::Fail::Cancelled);
            }
            running = ended
                .wait_timeout(running, Duration::from_millis(100))
                .unwrap_or_else(|e| e.into_inner())
                .0;
        }
        *running += 1;
        Ok(Permit(self))
    }

    /// Writes one message as one line; a closed stdout ends the server.
    pub fn send(&self, message: &Value) {
        let mut out = self.out.lock().unwrap_or_else(|e| e.into_inner());
        let ok = serde_json::to_writer(&mut *out, message).is_ok()
            && out.write_all(b"\n").is_ok()
            && out.flush().is_ok();
        if !ok {
            std::process::exit(0);
        }
    }

    /// Sends the notification `method` with `params`.
    pub fn notify(&self, method: &str, params: Value) {
        self.send(&json!({"jsonrpc":"2.0","method":method,"params":params}));
    }

    /// Reports `progress` of `total` for a request that asked for progress.
    pub fn progress(&self, ctx: &Ctx, progress: u64, total: Option<u64>, message: &str) {
        if let Some(token) = &ctx.progress {
            let mut params = json!({"progressToken":token,"progress":progress,"message":message});
            if let Some(total) = total {
                params["total"] = json!(total);
            }
            self.notify("notifications/progress", params);
        }
    }

    /// The response to a request, if it gets one.
    fn finish(&self, id: &Value, cancel: &AtomicBool, reply: Reply, modern: bool) -> Option<Value> {
        self.inflight
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(&id.to_string());
        // A cancelled request gets no response at all.
        if cancel.load(Ordering::Relaxed) {
            return None;
        }
        Some(match reply {
            Reply::Silent => return None,
            Reply::Ok(mut result) => {
                if modern {
                    let object = result.as_object_mut().expect("results are objects");
                    object
                        .entry("resultType")
                        .or_insert_with(|| json!("complete"));
                    let meta = object.entry("_meta").or_insert_with(|| json!({}));
                    meta[SERVER_INFO] = server_info();
                }
                json!({"jsonrpc":"2.0","id":id,"result":result})
            }
            Reply::Err(code, message, data) => {
                let mut error = json!({"code":code,"message":message});
                if let Some(data) = data {
                    error["data"] = data;
                }
                json!({"jsonrpc":"2.0","id":id,"error":error})
            }
        })
    }

    /// Decides how a request is served: `(modern, tasks)`, or the error that
    /// rejects it.
    fn era(&self, method: &str, params: &Value) -> Result<(bool, bool), Reply> {
        let meta = &params["_meta"];
        if !meta[PROTOCOL_VERSION].is_null() {
            if !meta[PROTOCOL_VERSION]
                .as_str()
                .is_some_and(|v| MODERN.contains(&v))
            {
                return Err(Reply::Err(
                    UNSUPPORTED_VERSION,
                    format!(
                        "Unsupported protocol version: {} is served per request; {} only after initialize, without _meta",
                        MODERN.join(", "),
                        LEGACY.join(", ")
                    ),
                    Some(json!({"supported":supported(),"requested":meta[PROTOCOL_VERSION]})),
                ));
            }
            if !meta[CLIENT_CAPABILITIES].is_object() {
                return Err(Reply::Err(
                    INVALID_PARAMS,
                    format!("_meta lacks {CLIENT_CAPABILITIES}"),
                    None,
                ));
            }
            let tasks = meta[CLIENT_CAPABILITIES]["extensions"][TASKS].is_object();
            return Ok((true, tasks));
        }
        if method == "initialize" || method == "ping" || self.initialized.load(Ordering::Relaxed) {
            return Ok((false, false));
        }
        Err(Reply::Err(
            INVALID_PARAMS,
            format!(
                "request lacks _meta {PROTOCOL_VERSION}: this server serves {} per request, and {} after initialize",
                MODERN.join(", "),
                LEGACY.join(", ")
            ),
            None,
        ))
    }

    /// Serves one request and returns its response, if any. A panic is a
    /// bug, but it still answers the request.
    fn request(
        self: &Arc<Self>,
        id: Value,
        method: String,
        params: Value,
        cancel: Arc<AtomicBool>,
    ) -> Option<Value> {
        let (reply, modern) = match self.era(&method, &params) {
            Err(reply) => (reply, false),
            Ok((modern, tasks)) => {
                let ctx = Ctx {
                    modern,
                    tasks,
                    progress: params["_meta"].get("progressToken").cloned(),
                    cancel: cancel.clone(),
                    id: id.clone(),
                };
                let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    self.dispatch(&method, &params, &ctx)
                }))
                .unwrap_or_else(|_| {
                    Reply::Err(
                        INTERNAL_ERROR,
                        "internal error of optimization-mcp; its stderr has the details".into(),
                        None,
                    )
                });
                (reply, modern)
            }
        };
        self.finish(&id, &cancel, reply, modern)
    }

    fn dispatch(&self, method: &str, params: &Value, ctx: &Ctx) -> Reply {
        match (ctx.modern, method) {
            (true, "server/discover") => {
                let mut extensions = json!({TASKS: {}, resources::SKILLS: {}});
                if self.uploads.is_some() {
                    extensions[uploads::EXTENSION] = json!({"upload":{}});
                }
                Reply::Ok(json!({
                    "supportedVersions": supported(),
                    "capabilities": {"tools": {}, "resources": {}, "extensions": extensions},
                    "instructions": resources::instructions(),
                    "ttlMs": CACHE_MS,
                    "cacheScope": "public",
                }))
            }
            (false, "initialize") => {
                let version = params["protocolVersion"]
                    .as_str()
                    .filter(|v| LEGACY.contains(v))
                    .unwrap_or(LEGACY[0]);
                self.initialized.store(true, Ordering::Relaxed);
                self.batches
                    .store(version == "2025-03-26", Ordering::Relaxed);
                Reply::Ok(json!({
                    "protocolVersion": version,
                    "capabilities": {"tools": {"listChanged": false}},
                    "serverInfo": server_info(),
                    "instructions": INSTRUCTIONS,
                }))
            }
            (false, "ping") => Reply::Ok(json!({})),
            (_, "tools/list") => {
                let mut list = tools::definitions(ctx.modern);
                list.push(apps::definition(false));
                let general = list.len();
                let app = ctx.modern && extension(params, apps::EXTENSION).is_object();
                if app {
                    list.push(apps::definition(true));
                }
                let files_enabled = ctx.modern && self.uploads.is_some();
                if files_enabled && extension(params, uploads::EXTENSION).is_object() {
                    list.push(uploads::definition());
                }
                let extended = list.len() > general;
                let mut result = json!({"tools": list});
                if ctx.modern {
                    // The general listing is the same for every client. A
                    // listing extended for a declared extension must not be
                    // reused for a request that declares another set.
                    let (ttl, scope) = if extended {
                        (0, "private")
                    } else {
                        (CACHE_MS, "public")
                    };
                    result["ttlMs"] = json!(ttl);
                    result["cacheScope"] = json!(scope);
                }
                Reply::Ok(result)
            }
            (_, "tools/call") => self.call_tool(params, ctx),
            (true, "tasks/get" | "tasks/update" | "tasks/cancel") => {
                if !ctx.tasks {
                    return missing_tasks_capability();
                }
                self.task_method(method, params, ctx)
            }
            (true, "subscriptions/listen") => self.listen(params, ctx),
            (
                true,
                "skills/list"
                | "skills/get"
                | "resources/list"
                | "resources/read"
                | "resources/templates/list",
            ) => resources::dispatch(method, params),
            _ => Reply::Err(
                METHOD_NOT_FOUND,
                format!("Method not found: {method}"),
                None,
            ),
        }
    }

    fn call_tool(&self, params: &Value, ctx: &Ctx) -> Reply {
        let Some(name) = params["name"].as_str() else {
            return Reply::Err(INVALID_PARAMS, "tools/call needs a tool name".into(), None);
        };
        let arguments = match &params["arguments"] {
            Value::Null => json!({}),
            Value::Object(_) => params["arguments"].clone(),
            _ => {
                return Reply::Err(
                    INVALID_PARAMS,
                    "tool arguments must be an object".into(),
                    None,
                );
            }
        };
        if ctx.modern
            && name == uploads::TOOL
            && let Some(uploads) = &self.uploads
        {
            let supported = extension(params, uploads::EXTENSION)["upload"].is_object();
            return uploads::call(
                self,
                uploads,
                arguments,
                params.get("requestState"),
                supported,
            );
        }
        if ctx.modern && params.get("requestState").is_some() {
            return Reply::Err(
                INVALID_PARAMS,
                "unexpected requestState for this tool".into(),
                None,
            );
        }
        let paging =
            name == apps::PAGE && ctx.modern && extension(params, apps::EXTENSION).is_object();
        if name == apps::VIEW || paging {
            return match apps::view(self, &arguments, ctx, paging) {
                Ok(view) => Reply::Ok(view),
                Err(tools::Fail::Tool(error)) => Reply::Ok(tools::tool_result(error.value(), true)),
                Err(tools::Fail::Cancelled) => Reply::Silent,
            };
        }
        if !tools::exists(name) {
            return Reply::Err(INVALID_PARAMS, format!("Unknown tool: {name}"), None);
        }
        match tools::call(self, name, &arguments, ctx) {
            tools::Outcome::Done(value) => Reply::Ok(tools::tool_result(value, false)),
            tools::Outcome::Failed(error) => Reply::Ok(tools::tool_result(error.value(), true)),
            tools::Outcome::Task(task) => Reply::Ok(task),
            tools::Outcome::Cancelled => Reply::Silent,
        }
    }

    fn task_method(&self, method: &str, params: &Value, ctx: &Ctx) -> Reply {
        let Some(task_id) = params["taskId"].as_str() else {
            return Reply::Err(INVALID_PARAMS, "taskId must be a string".into(), None);
        };
        let job_id = match tasks::job_of(&self.root, task_id) {
            Ok(Some(job)) => job,
            Ok(None) => {
                return Reply::Err(
                    INVALID_PARAMS,
                    "Failed to retrieve task: Task not found".into(),
                    None,
                );
            }
            Err(e) => return Reply::Err(INTERNAL_ERROR, format!("task store: {e}"), None),
        };
        match method {
            "tasks/get" => match tasks::detailed(self, task_id, &job_id, ctx) {
                Ok(task) => Reply::Ok(task),
                Err(tools::Fail::Cancelled) => Reply::Silent,
                Err(tools::Fail::Tool(e)) => job_error(&job_id, "read", &e),
            },
            // No task of this server asks for input; responses are ignored.
            "tasks/update" => Reply::Ok(json!({})),
            _ => match tools::cancel(self, &job_id, false) {
                Ok(_) => Reply::Ok(json!({})),
                // A job that already ended has nothing to cancel; the
                // acknowledgement promises no transition.
                Err(tools::Fail::Tool(e)) if e.code == "already_terminal" => Reply::Ok(json!({})),
                Err(tools::Fail::Cancelled) => Reply::Silent,
                Err(tools::Fail::Tool(e)) => job_error(&job_id, "cancel", &e),
            },
        }
    }

    /// `subscriptions/listen`: task status notifications for `taskIds`. The
    /// server sends no other notification type; the stream stays open until
    /// the client cancels it.
    fn listen(&self, params: &Value, ctx: &Ctx) -> Reply {
        let Some(filter) = params["notifications"].as_object() else {
            return Reply::Err(
                INVALID_PARAMS,
                "subscriptions/listen needs a notifications object".into(),
                None,
            );
        };
        let wanted = filter.get("taskIds");
        if wanted.is_some() && !ctx.tasks {
            return missing_tasks_capability();
        }
        let mut tracked: Vec<(String, String)> = vec![];
        if let Some(ids) = wanted {
            let Some(ids) = ids.as_array() else {
                return Reply::Err(INVALID_PARAMS, "taskIds must be an array".into(), None);
            };
            for id in ids.iter().filter_map(Value::as_str) {
                if let Ok(Some(job)) = tasks::job_of(&self.root, id)
                    && !tracked.iter().any(|(t, _)| t == id)
                {
                    tracked.push((id.to_owned(), job));
                }
            }
        }
        let mut agreed = Map::new();
        if wanted.is_some() {
            agreed.insert(
                "taskIds".into(),
                json!(tracked.iter().map(|(t, _)| t).collect::<Vec<_>>()),
            );
        }
        self.notify(
            "notifications/subscriptions/acknowledged",
            json!({"_meta":{SUBSCRIPTION:ctx.id},"notifications":agreed}),
        );
        let mut last: HashMap<String, String> = HashMap::new();
        while !ctx.stopped(self) {
            tracked.retain(|(task_id, job_id)| {
                let Ok(mut task) = tasks::detailed(self, task_id, job_id, ctx) else {
                    return true;
                };
                let status = task["status"].as_str().unwrap_or_default().to_owned();
                if last.get(task_id) != Some(&status) {
                    if let Some(object) = task.as_object_mut() {
                        object.remove("resultType");
                        object.insert("_meta".into(), json!({SUBSCRIPTION:ctx.id}));
                    }
                    self.notify("notifications/tasks", task);
                    last.insert(task_id.clone(), status.clone());
                }
                !tasks::terminal_status(&status)
            });
            tools::pause(self, ctx, tasks::POLL);
        }
        Reply::Silent
    }

    fn notification(&self, method: &str, params: &Value) {
        if method == "notifications/cancelled" {
            let id = params["requestId"].to_string();
            if let Some(flag) = self
                .inflight
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .get(&id)
            {
                flag.store(true, Ordering::Relaxed);
            }
        }
        // notifications/initialized and anything else need no action.
    }

    fn error(id: Value, code: i64, message: &str) -> Value {
        json!({"jsonrpc":"2.0","id":id,"error":{"code":code,"message":message}})
    }

    fn busy(id: Value, message: String) -> Value {
        json!({"jsonrpc":"2.0","id":id,"error":{"code":SERVER_BUSY,"message":message,
            "data":{"retryable":true}}})
    }

    /// Starts one message of the input: a request runs on a thread of its
    /// own and yields its response through the handle; a notification or a
    /// client's response yields nothing.
    fn accept(self: &Arc<Self>, message: Value) -> Option<thread::JoinHandle<Option<Value>>> {
        let method = message["method"].as_str().map(str::to_owned);
        let params = message.get("params").cloned().unwrap_or(json!({}));
        let immediate = |reply: Value| Some(thread::spawn(move || Some(reply)));
        match (method, message.get("id")) {
            // Responses from the client: this server sends no requests.
            (None, _) if message.is_object() && message.get("id").is_some() => None,
            (Some(method), None) if message.is_object() => {
                self.notification(&method, &params);
                None
            }
            (Some(method), Some(id)) if id.is_string() || id.is_number() => {
                let cancel = Arc::new(AtomicBool::new(false));
                let waits = method == "subscriptions/listen"
                    || (method == "tools/call" && params["name"] == "wait_job");
                {
                    let mut inflight = self.inflight.lock().unwrap_or_else(|e| e.into_inner());
                    if inflight.contains_key(&id.to_string()) {
                        // Answering under that ID would give it two responses.
                        eprintln!("optimization-mcp: request id {id} is already in use");
                        return immediate(Self::error(
                            Value::Null,
                            INVALID_REQUEST,
                            &format!("request id {id} is already in use"),
                        ));
                    }
                    if inflight.len() >= MAX_REQUESTS {
                        return immediate(Self::busy(
                            id.clone(),
                            format!(
                                "server busy: {MAX_REQUESTS} requests in progress; retry later"
                            ),
                        ));
                    }
                    if waits && self.waiting.fetch_add(1, Ordering::Relaxed) >= MAX_WAITING {
                        self.waiting.fetch_sub(1, Ordering::Relaxed);
                        return immediate(Self::busy(
                            id.clone(),
                            format!(
                                "server busy: {MAX_WAITING} waiting requests (wait_job, subscriptions/listen) in progress; retry later"
                            ),
                        ));
                    }
                    inflight.insert(id.to_string(), cancel.clone());
                }
                let server = self.clone();
                let id = id.clone();
                Some(thread::spawn(move || {
                    let response = server.request(id, method, params, cancel);
                    if waits {
                        server.waiting.fetch_sub(1, Ordering::Relaxed);
                    }
                    response
                }))
            }
            _ => immediate(Self::error(Value::Null, INVALID_REQUEST, "Invalid request")),
        }
    }

    fn serve(self: Arc<Self>) {
        let mut input = io::stdin().lock();
        let mut line = Vec::new();
        loop {
            match read_message(&mut input, &mut line) {
                Ok(Frame::End) | Err(_) => break,
                Ok(Frame::TooLong) => {
                    self.send(&Self::error(
                        Value::Null,
                        INVALID_REQUEST,
                        &format!("message longer than {MAX_MESSAGE} bytes; skipped"),
                    ));
                    continue;
                }
                Ok(Frame::Message) => {}
            }
            if line.iter().all(u8::is_ascii_whitespace) {
                continue;
            }
            let message: Value = match serde_json::from_slice(&line) {
                Ok(message) => message,
                Err(e) => {
                    self.send(&Self::error(
                        Value::Null,
                        PARSE_ERROR,
                        &format!("Parse error: {e}"),
                    ));
                    continue;
                }
            };
            match message {
                // A JSON-RPC batch (protocol 2025-03-26): one array of the
                // responses, sent when all of them are ready, so a stream
                // that never ends cannot be part of one.
                Value::Array(messages)
                    if !messages.is_empty() && self.batches.load(Ordering::Relaxed) =>
                {
                    let handles: Vec<_> = messages
                        .into_iter()
                        .filter_map(|m| {
                            if m["method"] == "subscriptions/listen" {
                                let id = m.get("id").cloned().unwrap_or(Value::Null);
                                let reply = Self::error(
                                    id,
                                    INVALID_REQUEST,
                                    "subscriptions/listen cannot be part of a batch",
                                );
                                return Some(thread::spawn(move || Some(reply)));
                            }
                            self.accept(m)
                        })
                        .collect();
                    let server = self.clone();
                    thread::spawn(move || {
                        let responses: Vec<Value> = handles
                            .into_iter()
                            .filter_map(|h| h.join().ok().flatten())
                            .collect();
                        if !responses.is_empty() {
                            server.send(&Value::Array(responses));
                        }
                    });
                }
                Value::Array(messages) => self.send(&Self::error(
                    Value::Null,
                    INVALID_REQUEST,
                    if messages.is_empty() {
                        "empty batch"
                    } else {
                        "JSON-RPC batches are part of protocol 2025-03-26 only; initialize with it to send them"
                    },
                )),
                message => {
                    if let Some(handle) = self.accept(message) {
                        let server = self.clone();
                        thread::spawn(move || {
                            if let Ok(Some(response)) = handle.join() {
                                server.send(&response);
                            }
                        });
                    }
                }
            }
        }
        // End of input is the stdio shutdown signal. Jobs keep running.
        self.shutdown.store(true, Ordering::Relaxed);
    }
}

enum Frame {
    Message,
    TooLong,
    End,
}

/// Reads one line of at most [`MAX_MESSAGE`] bytes into `line`. The rest of
/// a longer line is consumed without being kept.
fn read_message(input: &mut impl BufRead, line: &mut Vec<u8>) -> io::Result<Frame> {
    line.clear();
    if input
        .by_ref()
        .take(MAX_MESSAGE as u64 + 1)
        .read_until(b'\n', line)?
        == 0
    {
        return Ok(Frame::End);
    }
    if line.len() <= MAX_MESSAGE || line.ends_with(b"\n") {
        return Ok(Frame::Message);
    }
    line.clear();
    line.shrink_to(1 << 16);
    loop {
        let buffer = input.fill_buf()?;
        if buffer.is_empty() {
            return Ok(Frame::TooLong);
        }
        match buffer.iter().position(|&b| b == b'\n') {
            Some(end) => {
                input.consume(end + 1);
                return Ok(Frame::TooLong);
            }
            None => {
                let all = buffer.len();
                input.consume(all);
            }
        }
    }
}

/// The `optimization-mcp` binary: parses its options and serves MCP on
/// standard input and output until the input ends.
pub fn main() {
    if !cfg!(target_os = "linux") {
        eprintln!("optimization-mcp: Linux is required");
        std::process::exit(2);
    }
    // Every runner this server starts refuses command jobs, also when a
    // spec_path changes after the server read it.
    // SAFETY: no other thread exists yet.
    unsafe { std::env::set_var(crate::command::REFUSE, "1") };
    let args = Args::parse();
    let fail = |message: String| -> ! {
        eprintln!("optimization-mcp: {message}");
        std::process::exit(2)
    };
    let runner = args.runner.unwrap_or_else(|| {
        std::env::current_exe()
            .ok()
            .and_then(|exe| exe.parent().map(|dir| dir.join("optimization-run")))
            .unwrap_or_else(|| PathBuf::from("optimization-run"))
    });
    let runner = std::fs::canonicalize(&runner)
        .unwrap_or_else(|e| fail(format!("runner {}: {e}", runner.display())));
    if let Err(e) = std::fs::create_dir_all(&args.root) {
        fail(format!("node root {}: {e}", args.root.display()));
    }
    let root = std::fs::canonicalize(&args.root)
        .unwrap_or_else(|e| fail(format!("node root {}: {e}", args.root.display())));
    let input_dir = args
        .input_dir
        .map_or_else(std::env::current_dir, Ok)
        .and_then(std::fs::canonicalize)
        .unwrap_or_else(|e| fail(format!("input directory: {e}")));
    tools::clean_staging(&root);
    inputs::clean(&root);
    let uploads = args.files_endpoint.as_deref().map(|endpoint| {
        uploads::Uploads::new(&root, endpoint)
            .unwrap_or_else(|e| fail(format!("Files endpoint: {e}")))
    });
    eprintln!(
        "optimization-mcp {}: node {}, runner {}, launch {}",
        env!("CARGO_PKG_VERSION"),
        root.display(),
        runner.display(),
        args.launch.name()
    );
    let server = Arc::new(Server {
        root,
        runner,
        launch: args.launch,
        input_dir,
        input_store_bytes: args.input_store_bytes,
        uploads,
        out: Mutex::new(io::stdout()),
        inflight: Mutex::new(HashMap::new()),
        initialized: AtomicBool::new(false),
        batches: AtomicBool::new(false),
        shutdown: AtomicBool::new(false),
        waiting: AtomicUsize::new(0),
        validations: (Mutex::new(0), Condvar::new()),
    });
    server.serve();
}