mod guide;
mod inputs;
mod tasks;
mod tools;
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,
};
const MODERN: [&str; 1] = ["2026-07-28"];
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";
const TASKS: &str = "io.modelcontextprotocol/tasks";
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;
const SERVER_BUSY: i64 = -32000;
const MAX_MESSAGE: usize = 64 * 1024 * 1024;
const MAX_REQUESTS: usize = 64;
const MAX_WAITING: usize = 32;
const MAX_VALIDATIONS: usize = 4;
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 {
#[arg(
long,
env = "OPTIMIZATION_RUN_ROOT",
default_value = ".optimization-jobs"
)]
root: PathBuf,
#[arg(long)]
runner: Option<PathBuf>,
#[arg(long, value_enum, default_value_t = Launch::Process)]
launch: Launch,
#[arg(long)]
input_dir: Option<PathBuf>,
#[arg(long, default_value_t = inputs::DEFAULT_STORE_BYTES)]
input_store_bytes: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, ValueEnum)]
pub enum Launch {
Process,
Systemd,
}
impl Launch {
pub fn name(self) -> &'static str {
match self {
Launch::Process => "process",
Launch::Systemd => "systemd",
}
}
}
pub struct Server {
pub root: PathBuf,
pub runner: PathBuf,
pub launch: Launch,
pub input_dir: PathBuf,
pub input_store_bytes: u64,
out: Mutex<io::Stdout>,
inflight: Mutex<HashMap<String, Arc<AtomicBool>>>,
initialized: AtomicBool,
batches: AtomicBool,
pub shutdown: AtomicBool,
waiting: AtomicUsize,
validations: (Mutex<usize>, Condvar),
}
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();
}
}
pub struct Ctx {
pub modern: bool,
pub tasks: bool,
pub cancel: Arc<AtomicBool>,
pub progress: Option<Value>,
pub id: Value,
}
pub enum Reply {
Ok(Value),
Err(i64, String, Option<Value>),
Silent,
}
impl Ctx {
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 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:{}}}})),
)
}
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 {
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))
}
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);
}
}
pub fn notify(&self, method: &str, params: Value) {
self.send(&json!({"jsonrpc":"2.0","method":method,"params":params}));
}
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);
}
}
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());
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})
}
})
}
fn era(&self, method: &str, params: &Value) -> Result<(bool, bool), Reply> {
let meta = ¶ms["_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,
))
}
fn request(
self: &Arc<Self>,
id: Value,
method: String,
params: Value,
cancel: Arc<AtomicBool>,
) -> Option<Value> {
let (reply, modern) = match self.era(&method, ¶ms) {
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, ¶ms, &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") => Reply::Ok(json!({
"supportedVersions": supported(),
"capabilities": {"tools": {}, "extensions": {TASKS: {}}},
"instructions": 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 result = json!({"tools": tools::definitions()});
if ctx.modern {
result["ttlMs"] = json!(CACHE_MS);
result["cacheScope"] = json!("public");
}
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),
_ => 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 ¶ms["arguments"] {
Value::Null => json!({}),
Value::Object(_) => params["arguments"].clone(),
_ => {
return Reply::Err(
INVALID_PARAMS,
"tool arguments must be an object".into(),
None,
);
}
};
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),
},
"tasks/update" => Reply::Ok(json!({})),
_ => match tools::cancel(self, &job_id, false) {
Ok(_) => Reply::Ok(json!({})),
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),
},
}
}
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);
}
}
}
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}}})
}
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")) {
(None, _) if message.is_object() && message.get("id").is_some() => None,
(Some(method), None) if message.is_object() => {
self.notification(&method, ¶ms);
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()) {
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 {
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);
}
});
}
}
}
}
self.shutdown.store(true, Ordering::Relaxed);
}
}
enum Frame {
Message,
TooLong,
End,
}
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);
}
}
}
}
pub fn main() {
if !cfg!(target_os = "linux") {
eprintln!("optimization-mcp: Linux is required");
std::process::exit(2);
}
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);
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,
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();
}