harn-cli 0.10.127

CLI for the Harn programming language — run, test, REPL, format, and lint
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
pub(crate) enum RunFileMcpServeMode {
    Stdio { watch: bool },
    Http(Box<RunFileMcpServeHttp>),
    App(Box<RunFileAppServe>),
}

pub(crate) struct RunFileMcpServeHttp {
    pub options: harn_serve::McpHttpServeOptions,
    pub auth_policy: harn_serve::AuthPolicy,
}

pub(crate) struct RunFileAppServe {
    pub bind: std::net::SocketAddr,
    pub resource: Option<String>,
    pub open: bool,
}

/// Executable registry loaded from the same script path used by MCP serving.
/// The connector guard must live as long as the VM because handlers may defer
/// connector initialization until their first dispatch.
pub(crate) struct LoadedToolRegistry {
    pub(crate) vm: harn_vm::Vm,
    pub(crate) registry: harn_vm::VmValue,
    pub(crate) diagnostics: String,
    pub(crate) resources: Vec<harn_vm::McpResourceDef>,
    pub(crate) resource_templates: Vec<harn_vm::McpResourceTemplateDef>,
    pub(crate) prompts: Vec<harn_vm::McpPromptDef>,
    pub(crate) metadata: Option<harn_vm::McpServerMetadata>,
    _connector_clients: harn_vm::ActiveConnectorClientsGuard,
}

#[derive(Debug)]
pub(crate) struct ToolRegistryLoadError {
    pub(crate) message: String,
    pub(crate) exit_code: i32,
}

// Registry publication still enters through VM thread-local slots. Serialize
// script initialization until the VM can return a publication bundle directly;
// otherwise two in-process adapters scheduled on one runtime thread could
// clear or consume each other's registry between await points.
static TOOL_REGISTRY_LOAD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Compile and execute a script until it publishes its tool registry.
/// Presentation adapters call this once and then consume the same VM-owned
/// handlers; no adapter is allowed to rebuild or proxy the dispatch table.
pub(crate) async fn load_file_tool_registry(
    path: &str,
) -> Result<LoadedToolRegistry, ToolRegistryLoadError> {
    tokio::task::LocalSet::new()
        .run_until(load_file_tool_registry_local(path))
        .await
}

/// Local-task variant used by a live stdio server when a watched source tree
/// changes. Keeping compilation and execution in this one loader preserves the
/// exact same validation and connector setup as initial startup and CLI calls.
pub(crate) async fn load_file_tool_registry_local(
    path: &str,
) -> Result<LoadedToolRegistry, ToolRegistryLoadError> {
    use std::path::Path;

    use crate::skill_loader::{
        emit_loader_warnings, install_skills_global, load_skills, SkillLoaderInputs,
    };

    use super::{compile_or_load_chunk_for_run, entry_source_dir, LoadedChunk};

    let _load_guard = TOOL_REGISTRY_LOAD.lock().await;

    let mut diagnostics = String::new();
    let LoadedChunk {
        source,
        chunk,
        link_table,
    } = compile_or_load_chunk_for_run(path, &mut diagnostics).map_err(|_| {
        ToolRegistryLoadError {
            message: diagnostics.clone(),
            exit_code: 1,
        }
    })?;

    let mut vm = harn_vm::Vm::new();
    vm.set_graph_link_table(link_table);
    harn_vm::register_vm_stdlib(&mut vm);
    crate::install_default_hostlib(&mut vm);
    let source_parent = Path::new(path).parent().unwrap_or(Path::new("."));
    let project_root = harn_vm::stdlib::process::find_project_root(source_parent);
    let store_base = project_root.as_deref().unwrap_or(source_parent);
    harn_vm::register_store_builtins(&mut vm, store_base);
    harn_vm::register_metadata_builtins(&mut vm, store_base);
    let pipeline_name = Path::new(path)
        .file_stem()
        .and_then(|name| name.to_str())
        .unwrap_or("default");
    harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
    vm.set_source_info(path, &source);
    if let Some(root) = project_root.as_ref() {
        vm.set_project_root(root);
    }
    vm.set_source_dir(&entry_source_dir(path));

    let skills = load_skills(&SkillLoaderInputs {
        cli_dirs: Vec::new(),
        source_path: Some(path.into()),
    });
    emit_loader_warnings(&skills.loader_warnings);
    install_skills_global(&mut vm, &skills);

    let connector_clients = super::manifest_runtime::install_manifest_runtime(
        Path::new(path),
        &mut vm,
        crate::package::ManifestHandlerInitialization::OnDispatch,
        true,
    )
    .await
    .map_err(|error| ToolRegistryLoadError {
        message: format!("failed to install {}: {error}", error.label()),
        exit_code: 1,
    })?;

    vm.set_source_dir(&entry_source_dir(path));
    // A missing publication must never read as success because another
    // in-process adapter left a registry in thread-local state.
    let _stale_registry = harn_vm::take_mcp_serve_registry();
    let _stale_resources = harn_vm::take_mcp_serve_resources();
    let _stale_resource_templates = harn_vm::take_mcp_serve_resource_templates();
    let _stale_prompts = harn_vm::take_mcp_serve_prompts();
    let _stale_metadata = harn_vm::take_mcp_serve_metadata();
    vm.execute(&chunk)
        .await
        .map_err(|error| ToolRegistryLoadError {
            message: format!(
                "{diagnostics}{}{}",
                vm.output(),
                vm.format_runtime_error(&error)
            ),
            exit_code: error.process_exit_code().unwrap_or(1),
        })?;
    if !vm.output().is_empty() {
        diagnostics.push_str(vm.output());
    }
    let registry = harn_vm::take_mcp_serve_registry().ok_or_else(|| ToolRegistryLoadError {
        message: format!(
            "{diagnostics}pipeline did not publish a tool registry\n\
                         hint: call harness.tools.mcp_tools(tools) from main"
        ),
        exit_code: 1,
    })?;
    harn_vm::tool_registry::tool_registry_catalog(&registry).map_err(|error| {
        ToolRegistryLoadError {
            message: format!("invalid tool registry: {error}"),
            exit_code: 1,
        }
    })?;
    Ok(LoadedToolRegistry {
        vm,
        registry,
        diagnostics,
        resources: harn_vm::take_mcp_serve_resources(),
        resource_templates: harn_vm::take_mcp_serve_resource_templates(),
        prompts: harn_vm::take_mcp_serve_prompts(),
        metadata: harn_vm::take_mcp_serve_metadata(),
        _connector_clients: connector_clients,
    })
}

pub(super) async fn run_server(
    mut server: harn_vm::McpServer,
    mut vm: harn_vm::Vm,
    mode: RunFileMcpServeMode,
) {
    let result = match mode {
        RunFileMcpServeMode::Stdio { watch: false } => {
            server.run(&mut vm).await.map_err(|error| error.to_string())
        }
        RunFileMcpServeMode::Stdio { watch: true } => {
            unreachable!("watch mode is owned by run_file_mcp_serve")
        }
        RunFileMcpServeMode::Http(http) => {
            let RunFileMcpServeHttp {
                options,
                auth_policy,
            } = *http;
            crate::commands::serve::run_script_mcp_http_server(server, vm, options, auth_policy)
                .await
        }
        RunFileMcpServeMode::App(app) => {
            let RunFileAppServe {
                bind,
                resource,
                open,
            } = *app;
            crate::commands::app::run_script_app_server(server, vm, bind, resource, open).await
        }
    };
    if let Err(error) = result {
        eprintln!("error: MCP server error: {error}");
        std::process::exit(1);
    }
}

/// Run a .harn file as an MCP server using the script-driven surface.
///
/// The pipeline must publish a registry with `mcp_tools(...)` (or the legacy
/// `mcp_serve(...)` alias). Resources, templates, prompts, and optional server
/// card metadata are collected from the same execution before transport starts.
pub(crate) async fn run_file_mcp_serve(
    path: &str,
    card_source: Option<&str>,
    mode: RunFileMcpServeMode,
) {
    use std::process;

    let watch = matches!(&mode, RunFileMcpServeMode::Stdio { watch: true });
    let loaded = match load_mcp_runtime(path, card_source, watch).await {
        Ok(runtime) => runtime,
        Err(error) => {
            eprintln!("error: {}", error.message);
            process::exit(error.exit_code);
        }
    };
    if !loaded.diagnostics.is_empty() {
        eprint!("{}", loaded.diagnostics);
    }
    eprintln!(
        "[harn] serve mcp: serving {} as '{}'{}",
        loaded.capability_summary,
        loaded.server_name,
        if watch { " with source reload" } else { "" },
    );

    let local = tokio::task::LocalSet::new();
    local
        .run_until(async move {
            if watch {
                run_reloadable_stdio(path, card_source, loaded).await;
            } else {
                let LoadedMcpRuntime {
                    server,
                    vm,
                    connector_clients,
                    ..
                } = loaded;
                run_server(server, vm, mode).await;
                drop(connector_clients);
            }
        })
        .await;
}

struct LoadedMcpRuntime {
    server: harn_vm::McpServer,
    vm: harn_vm::Vm,
    diagnostics: String,
    server_name: String,
    capability_summary: String,
    connector_clients: harn_vm::ActiveConnectorClientsGuard,
}

async fn load_mcp_runtime(
    path: &str,
    card_source: Option<&str>,
    list_changes: bool,
) -> Result<LoadedMcpRuntime, ToolRegistryLoadError> {
    let loaded = load_file_tool_registry(path).await?;
    project_mcp_runtime(path, card_source, list_changes, loaded)
}

async fn load_mcp_runtime_local(
    path: &str,
    card_source: Option<&str>,
) -> Result<LoadedMcpRuntime, ToolRegistryLoadError> {
    let loaded = load_file_tool_registry_local(path).await?;
    project_mcp_runtime(path, card_source, true, loaded)
}

fn project_mcp_runtime(
    path: &str,
    card_source: Option<&str>,
    list_changes: bool,
    loaded: LoadedToolRegistry,
) -> Result<LoadedMcpRuntime, ToolRegistryLoadError> {
    use std::path::Path;

    let LoadedToolRegistry {
        vm,
        registry,
        diagnostics,
        resources,
        resource_templates,
        prompts,
        mut metadata,
        _connector_clients: connector_clients,
    } = loaded;
    let tools =
        harn_vm::tool_registry_to_mcp_tools(&registry).map_err(|error| ToolRegistryLoadError {
            message: error.to_string(),
            exit_code: 1,
        })?;
    let catalog = harn_vm::tool_registry::tool_registry_catalog(&registry)
        .expect("registry was validated by the shared loader");
    if let Some(info) = catalog.info {
        let metadata = metadata.get_or_insert_default();
        if metadata.name.is_none() {
            metadata.name = Some(info.name);
        }
        if metadata.version.is_none() {
            metadata.version = info.version;
        }
        if metadata.instructions.is_none() {
            metadata.instructions = info.description;
        }
    }

    let mut server_name = Path::new(path)
        .file_stem()
        .and_then(|name| name.to_str())
        .unwrap_or("harn")
        .to_string();
    if let Some(name) = metadata
        .as_ref()
        .and_then(|metadata| metadata.name.as_ref())
    {
        server_name = name.clone();
    }

    let mut capabilities = Vec::new();
    if !tools.is_empty() {
        capabilities.push(format!(
            "{} tool{}",
            tools.len(),
            if tools.len() == 1 { "" } else { "s" }
        ));
    }
    let total_resources = resources.len() + resource_templates.len();
    if total_resources > 0 {
        capabilities.push(format!(
            "{total_resources} resource{}",
            if total_resources == 1 { "" } else { "s" }
        ));
    }
    if !prompts.is_empty() {
        capabilities.push(format!(
            "{} prompt{}",
            prompts.len(),
            if prompts.len() == 1 { "" } else { "s" }
        ));
    }

    let mut server = harn_vm::McpServer::new(
        server_name.clone(),
        tools,
        resources,
        resource_templates,
        prompts,
    )
    .with_list_changes(list_changes);
    if let Some(metadata) = metadata {
        server = server.with_metadata(metadata);
    }
    if let Some(source) = card_source {
        server = server.with_server_card(resolve_card_source(source).map_err(|error| {
            ToolRegistryLoadError {
                message: format!("--card: {error}"),
                exit_code: 1,
            }
        })?);
    }

    Ok(LoadedMcpRuntime {
        server,
        vm,
        diagnostics,
        server_name,
        capability_summary: capabilities.join(", "),
        connector_clients,
    })
}

async fn run_reloadable_stdio(path: &str, card_source: Option<&str>, loaded: LoadedMcpRuntime) {
    use notify::{EventKind, RecursiveMode, Watcher};
    use std::path::Path;
    use std::time::Duration;

    // The channel is a dirty bit, not an event log. A bounded slot prevents a
    // rapid editor-save burst from accumulating while a replacement compiles.
    let (source_tx, mut source_rx) = tokio::sync::mpsc::channel(1);
    let mut watcher =
        match notify::recommended_watcher(move |result: notify::Result<notify::Event>| match result
        {
            Ok(event)
                if !matches!(event.kind, EventKind::Access(_))
                    && event.paths.iter().any(|path| {
                        path.extension().and_then(|extension| extension.to_str()) == Some("harn")
                            || path.file_name().and_then(|name| name.to_str()) == Some("harn.toml")
                    }) =>
            {
                let _ = source_tx.try_send(());
            }
            Ok(_) => {}
            Err(error) => eprintln!("[harn] serve mcp: source watcher error: {error}"),
        }) {
            Ok(watcher) => watcher,
            Err(error) => {
                eprintln!("error: failed to create MCP source watcher: {error}");
                std::process::exit(1);
            }
        };
    let watch_root = Path::new(path).parent().unwrap_or(Path::new("."));
    if let Err(error) = watcher.watch(watch_root, RecursiveMode::Recursive) {
        eprintln!(
            "error: failed to watch MCP source root {}: {error}",
            watch_root.display()
        );
        std::process::exit(1);
    }

    let (reload_tx, reload_rx) = tokio::sync::mpsc::unbounded_channel();
    let reload_path = path.to_string();
    let reload_card = card_source.map(str::to_string);
    tokio::task::spawn_local(async move {
        while source_rx.recv().await.is_some() {
            // One editor save can produce create, rename, and modify events.
            // Wait for that burst, then load the final source state once.
            tokio::time::sleep(Duration::from_millis(75)).await;
            while source_rx.try_recv().is_ok() {}
            let replacement = load_mcp_runtime_local(&reload_path, reload_card.as_deref())
                .await
                .map(|runtime| {
                    if !runtime.diagnostics.is_empty() {
                        eprint!("{}", runtime.diagnostics);
                    }
                    eprintln!(
                        "[harn] serve mcp: reloaded {} as '{}'",
                        runtime.capability_summary, runtime.server_name
                    );
                    harn_vm::McpServerReload::new(
                        runtime.server,
                        runtime.vm,
                        runtime.connector_clients,
                    )
                })
                .map_err(|error| error.message);
            if reload_tx.send(replacement).is_err() {
                break;
            }
        }
    });

    let LoadedMcpRuntime {
        server,
        vm,
        connector_clients,
        ..
    } = loaded;
    let result = server
        .run_reloadable(vm, connector_clients, reload_rx)
        .await;
    drop(watcher);
    if let Err(error) = result {
        eprintln!("error: MCP server error: {error}");
        std::process::exit(1);
    }
}

/// Parse `--card` as inline JSON when it starts with an object or array;
/// otherwise load it from a filesystem path.
pub(crate) fn resolve_card_source(source: &str) -> Result<serde_json::Value, String> {
    let trimmed = source.trim_start();
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        return serde_json::from_str(source)
            .map_err(|error| format!("inline JSON parse error: {error}"));
    }
    harn_vm::load_server_card_from_path(std::path::Path::new(source))
        .map_err(|error| format!("{error}"))
}