jamjet-api 0.3.1

JamJet REST API server — control plane for workflow management
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
//! MCP bridge — exposes core JamJet operations as MCP tools at `/mcp`.
//!
//! This lets MCP clients (Claude Code, Cursor, etc.) interact with the
//! JamJet runtime directly: run workflows, inspect executions, manage agents.

use crate::state::AppState;
use axum::Router;
use chrono::Utc;
use jamjet_agents::{AgentFilter, AgentStatus};
use jamjet_core::workflow::{ExecutionId, WorkflowExecution, WorkflowStatus};
use jamjet_mcp::server::McpServer;
use jamjet_mcp::types::{McpContent, McpTool};
use jamjet_state::{Event, EventKind, TenantId, WorkItem};
use serde_json::{json, Value};
use std::sync::Arc;
use uuid::Uuid;

/// Build the MCP bridge router with all JamJet runtime tools.
///
/// The returned router is meant to be merged into the main API router.
/// No auth layer — same security model as `jamjet dev` (local-only).
pub fn build_mcp_bridge(state: AppState) -> Router {
    let st = Arc::new(state);

    let server = McpServer::new("jamjet-runtime", env!("CARGO_PKG_VERSION"), 0);

    // ── jamjet_run_workflow ──────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_run_workflow".into(),
            description: Some("Start a workflow execution".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "workflow_id": { "type": "string", "description": "ID of the workflow to run" },
                    "input": { "type": "object", "description": "Input data for the workflow" },
                    "workflow_version": { "type": "string", "description": "Workflow version (default: 1.0.0)" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                },
                "required": ["workflow_id", "input"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let workflow_id = args.get("workflow_id").and_then(|v| v.as_str()).unwrap_or("").to_string();
                let input = args.get("input").cloned().unwrap_or(json!({}));
                let version = args.get("workflow_version").and_then(|v| v.as_str()).unwrap_or("1.0.0").to_string();
                let tenant_id = TenantId::from(
                    args.get("tenant_id").and_then(|v| v.as_str()).unwrap_or("default"),
                );

                let backend = s.backend_for(&tenant_id);

                let def = backend
                    .get_workflow(&workflow_id, &version)
                    .await
                    .map_err(|e| format!("failed to get workflow: {e}"))?
                    .ok_or_else(|| format!("workflow {} v{} not found", workflow_id, version))?;

                let start_node = def.ir.get("start_node")
                    .and_then(|v| v.as_str())
                    .unwrap_or("start")
                    .to_string();

                let now = Utc::now();
                let execution = WorkflowExecution {
                    execution_id: ExecutionId::new(),
                    workflow_id: workflow_id.clone(),
                    workflow_version: version.clone(),
                    status: WorkflowStatus::Running,
                    initial_input: input.clone(),
                    current_state: input.clone(),
                    started_at: now,
                    updated_at: now,
                    completed_at: None,
                    session_type: None,
                };
                let eid = execution.execution_id.clone();
                backend.create_execution(execution).await.map_err(|e| format!("{e}"))?;

                let event = Event::new(eid.clone(), 1, EventKind::WorkflowStarted {
                    workflow_id: workflow_id.clone(),
                    workflow_version: version.clone(),
                    initial_input: input.clone(),
                });
                backend.append_event(event).await.map_err(|e| format!("{e}"))?;

                let queue_type = "general".to_string();
                let sched_event = Event::new(eid.clone(), 2, EventKind::NodeScheduled {
                    node_id: start_node.clone(),
                    queue_type: queue_type.clone(),
                });
                backend.append_event(sched_event).await.map_err(|e| format!("{e}"))?;

                let work_item = WorkItem {
                    id: Uuid::new_v4(),
                    execution_id: eid.clone(),
                    node_id: start_node,
                    queue_type,
                    payload: json!({"workflow_id": workflow_id, "workflow_version": version}),
                    attempt: 0,
                    max_attempts: 3,
                    created_at: now,
                    lease_expires_at: None,
                    worker_id: None,
                    tenant_id: tenant_id.0.clone(),
                };
                backend.enqueue_work_item(work_item).await.map_err(|e| format!("{e}"))?;

                Ok(vec![McpContent::Text {
                    text: json!({"execution_id": eid.to_string()}).to_string(),
                }])
            }
        },
    );

    // ── jamjet_get_execution ─────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_get_execution".into(),
            description: Some("Get details of a workflow execution".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "execution_id": { "type": "string", "description": "Execution ID (exec_<uuid> or bare UUID)" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                },
                "required": ["execution_id"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let id_str = args.get("execution_id").and_then(|v| v.as_str()).unwrap_or("");
                let tenant_id = TenantId::from(
                    args.get("tenant_id").and_then(|v| v.as_str()).unwrap_or("default"),
                );
                let eid = parse_execution_id(id_str)?;
                let backend = s.backend_for(&tenant_id);
                let exec = backend.get_execution(&eid).await.map_err(|e| format!("{e}"))?
                    .ok_or_else(|| format!("execution {id_str} not found"))?;
                let text = serde_json::to_string(&exec).map_err(|e| format!("{e}"))?;
                Ok(vec![McpContent::Text { text }])
            }
        },
    );

    // ── jamjet_list_executions ───────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_list_executions".into(),
            description: Some("List workflow executions with optional filters".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "status": { "type": "string", "description": "Filter by status: running, paused, completed, failed" },
                    "limit": { "type": "integer", "description": "Max results (default 50)" },
                    "offset": { "type": "integer", "description": "Offset for pagination" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                }
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let tenant_id = TenantId::from(
                    args.get("tenant_id").and_then(|v| v.as_str()).unwrap_or("default"),
                );
                let status = args.get("status").and_then(|v| v.as_str()).and_then(parse_status);
                let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(50) as u32;
                let offset = args.get("offset").and_then(|v| v.as_u64()).unwrap_or(0) as u32;

                let backend = s.backend_for(&tenant_id);
                let execs = backend.list_executions(status, limit, offset).await.map_err(|e| format!("{e}"))?;
                let text = serde_json::to_string(&json!({"executions": execs})).map_err(|e| format!("{e}"))?;
                Ok(vec![McpContent::Text { text }])
            }
        },
    );

    // ── jamjet_cancel_execution ──────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_cancel_execution".into(),
            description: Some("Cancel a running workflow execution".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "execution_id": { "type": "string", "description": "Execution ID" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                },
                "required": ["execution_id"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let id_str = args
                    .get("execution_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let tenant_id = TenantId::from(
                    args.get("tenant_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("default"),
                );
                let eid = parse_execution_id(id_str)?;
                let backend = s.backend_for(&tenant_id);

                let exec = backend
                    .get_execution(&eid)
                    .await
                    .map_err(|e| format!("{e}"))?
                    .ok_or_else(|| format!("execution {id_str} not found"))?;

                if exec.status.is_terminal() {
                    return Err(format!(
                        "execution {id_str} is already terminal: {:?}",
                        exec.status
                    ));
                }

                let seq = backend
                    .latest_sequence(&eid)
                    .await
                    .map_err(|e| format!("{e}"))?
                    + 1;
                let event = Event::new(
                    eid.clone(),
                    seq,
                    EventKind::WorkflowCancelled {
                        reason: Some("cancelled via MCP".into()),
                    },
                );
                backend
                    .append_event(event)
                    .await
                    .map_err(|e| format!("{e}"))?;
                backend
                    .update_execution_status(&eid, WorkflowStatus::Cancelled)
                    .await
                    .map_err(|e| format!("{e}"))?;

                Ok(vec![McpContent::Text {
                    text: json!({"execution_id": id_str, "status": "cancelled"}).to_string(),
                }])
            }
        },
    );

    // ── jamjet_get_events ────────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_get_events".into(),
            description: Some("Get the event log for an execution".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "execution_id": { "type": "string", "description": "Execution ID" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                },
                "required": ["execution_id"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let id_str = args
                    .get("execution_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let tenant_id = TenantId::from(
                    args.get("tenant_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("default"),
                );
                let eid = parse_execution_id(id_str)?;
                let backend = s.backend_for(&tenant_id);
                let events = backend.get_events(&eid).await.map_err(|e| format!("{e}"))?;
                let text = serde_json::to_string(&json!({"events": events}))
                    .map_err(|e| format!("{e}"))?;
                Ok(vec![McpContent::Text { text }])
            }
        },
    );

    // ── jamjet_approve ───────────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_approve".into(),
            description: Some("Approve or reject a paused execution".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "execution_id": { "type": "string", "description": "Execution ID" },
                    "decision": { "type": "string", "description": "approved or rejected" },
                    "node_id": { "type": "string", "description": "Node that requested approval" },
                    "comment": { "type": "string", "description": "Optional comment" },
                    "tenant_id": { "type": "string", "description": "Tenant ID (default: default)" }
                },
                "required": ["execution_id", "decision"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let id_str = args
                    .get("execution_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let decision_str = args.get("decision").and_then(|v| v.as_str()).unwrap_or("");
                let node_id = args
                    .get("node_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let comment = args
                    .get("comment")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                let tenant_id = TenantId::from(
                    args.get("tenant_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("default"),
                );

                let decision = match decision_str {
                    "approved" => jamjet_state::event::ApprovalDecision::Approved,
                    "rejected" => jamjet_state::event::ApprovalDecision::Rejected,
                    other => return Err(format!("unknown decision: {other}")),
                };

                let eid = parse_execution_id(id_str)?;
                let backend = s.backend_for(&tenant_id);

                let seq = backend
                    .latest_sequence(&eid)
                    .await
                    .map_err(|e| format!("{e}"))?
                    + 1;
                let event = Event::new(
                    eid.clone(),
                    seq,
                    EventKind::ApprovalReceived {
                        node_id,
                        user_id: "mcp-client".into(),
                        decision,
                        comment,
                        state_patch: None,
                    },
                );
                backend
                    .append_event(event)
                    .await
                    .map_err(|e| format!("{e}"))?;

                if let Ok(Some(exec)) = backend.get_execution(&eid).await {
                    if exec.status == WorkflowStatus::Paused {
                        backend
                            .update_execution_status(&eid, WorkflowStatus::Running)
                            .await
                            .map_err(|e| format!("{e}"))?;
                    }
                }

                Ok(vec![McpContent::Text {
                    text: json!({"execution_id": id_str, "accepted": true}).to_string(),
                }])
            }
        },
    );

    // ── jamjet_list_agents ───────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_list_agents".into(),
            description: Some("List registered agents with optional filters".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "status": { "type": "string", "description": "Filter by status: registered, active, paused, deactivated" },
                    "skill": { "type": "string", "description": "Filter by skill" },
                    "protocol": { "type": "string", "description": "Filter by protocol" }
                }
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let status = args.get("status").and_then(|v| v.as_str()).and_then(|s| match s {
                    "registered" => Some(AgentStatus::Registered),
                    "active" => Some(AgentStatus::Active),
                    "paused" => Some(AgentStatus::Paused),
                    "deactivated" => Some(AgentStatus::Deactivated),
                    _ => None,
                });
                let filter = AgentFilter {
                    status,
                    skill: args.get("skill").and_then(|v| v.as_str()).map(String::from),
                    protocol: args.get("protocol").and_then(|v| v.as_str()).map(String::from),
                };
                let agents = s.agents.find(filter).await.map_err(|e| e.to_string())?;
                let text = serde_json::to_string(&json!({"agents": agents})).map_err(|e| format!("{e}"))?;
                Ok(vec![McpContent::Text { text }])
            }
        },
    );

    // ── jamjet_discover_agent ────────────────────────────────────────────
    let s = st.clone();
    let server = server.register_tool(
        McpTool {
            name: "jamjet_discover_agent".into(),
            description: Some("Discover and register a remote agent by URL".into()),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "url": { "type": "string", "description": "URL of the remote agent to discover" }
                },
                "required": ["url"]
            }),
        },
        move |args: Value| {
            let s = s.clone();
            async move {
                let url = args.get("url").and_then(|v| v.as_str()).unwrap_or("");
                let agent = s.agents.discover_remote(url).await.map_err(|e| e.to_string())?;
                let text = serde_json::to_string(&agent).map_err(|e| format!("{e}"))?;
                Ok(vec![McpContent::Text { text }])
            }
        },
    );

    server.into_router()
}

// ── Helpers ──────────────────────────────────────────────────────────────────

fn parse_execution_id(s: &str) -> Result<ExecutionId, String> {
    let hex = s.strip_prefix("exec_").unwrap_or(s);
    let uuid = Uuid::parse_str(hex).map_err(|_| format!("invalid execution id: {s}"))?;
    Ok(ExecutionId(uuid))
}

fn parse_status(s: &str) -> Option<WorkflowStatus> {
    match s {
        "running" => Some(WorkflowStatus::Running),
        "paused" => Some(WorkflowStatus::Paused),
        "completed" => Some(WorkflowStatus::Completed),
        "failed" => Some(WorkflowStatus::Failed),
        _ => None,
    }
}