promptforge-mcp-server 0.1.0

PromptForge MCP server: serves prompts as MCP tools for agentic harnesses
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
//! Running one catalog entry, and what happens when the run outlives its call.
//!
//! A call that reaches a prompt goes through four gates in order. A broken entry
//! fails before anything is reserved. Then admission: one of
//! `max_concurrent_runs` slots, waited for up to `admission_timeout` and
//! refused with a message the calling model can retry on. Then the run itself,
//! which happens on a task of its own rather than inline, and whose clock starts
//! once admission is through so the duration it reports is the run's and not the
//! queue's. Then the reply deadline, which decides whether this call carries the
//! result or a `run_id`.
//!
//! The run is a spawned task even when it finishes in milliseconds, because that
//! is what lets the deadline give up on it without cancelling it: past the
//! deadline the registry hands the join handle to a supervisor, which detaches
//! the run from this call while still owning its outcome. The work continues, the
//! record lands in the registry when it ends however it ends, and `check_run`
//! collects it.
//!
//! Progress belongs to the call that asked for it. The `progressToken` names a
//! stream that closes when this call is answered, so the pump is finished on
//! both paths - a bounded flush, then abandonment - and a run that outlived its
//! call keeps reporting into a queue with nothing on the other end, where every
//! later frame is counted as dropped. Nothing on the run's path blocks either
//! way, and the alternative, holding the stream open past its reply, is not
//! something the protocol offers.

use std::sync::Arc;
use std::time::Duration;

use promptforge_core::CancelHandle;
use promptforge_core::client::GatewayClient;
use promptforge_core::execute::{self, ResolutionContext, RunConfig};
use promptforge_core::parser::Prompt;
use promptforge_core::store::StoreRef;
use rmcp::model::{CallToolResult, ErrorData};
use tokio::sync::oneshot;
use tokio::time::Instant;

use crate::catalog::Entry;
use crate::config::Config;
use crate::progress::{McpObserver, ProgressPump};
use crate::registry::{DuplicateRun, RunRegistry, RunSlot, elapsed_ms};
use crate::result::{NO_TURNS, RunResult};

use super::{PreparedTools, Reporting, bind, run_result, text_error};

/// Everything one background run owns for as long as it lasts.
///
/// It is owned rather than borrowed because the run outlives the call: the
/// prompt is the snapshot the run started under, so a reload part way through
/// cannot change the definition underneath it.
struct Launch {
    /// The run's identifier, which is also how it is collected.
    run_id: String,
    /// The prompt to run, cloned from the catalog snapshot.
    prompt: Prompt,
    /// The prompt's name, as the result reports it.
    name: String,
    /// The run's whole input.
    args: String,
    /// The complete immutable live registry and prepared picker.
    tools: Arc<PreparedTools>,
    /// Where the run reports itself, and what counts its turns.
    observer: Arc<McpObserver>,
    /// The gateway the run's model calls go through.
    client: GatewayClient,
    /// When the run started, for `elapsed_ms`.
    started: Instant,
    /// The admission slot, returned when this value drops.
    slot: RunSlot,
    /// The run's cancellation handle, installed on the core run so an abandoned
    /// call stops it cooperatively.
    cancel: CancelHandle,
}

/// One run's observer, correlation id, and optional progress pump.
struct Observation {
    run_id: String,
    observer: Arc<McpObserver>,
    pump: Option<ProgressPump>,
}

/// Runs one entry and answers the call that asked for it.
///
/// # Errors
/// Returns `-32603` when the result cannot be serialized. Everything the
/// calling model can act on - a broken prompt, an unresolvable capability, a refused
/// admission, a run that failed - is an `Ok` result carrying `isError`.
pub(super) async fn run(
    config: &Config,
    registry: &Arc<RunRegistry>,
    tools: Arc<PreparedTools>,
    entry: &Entry,
    args: &str,
    reporting: Option<Reporting>,
) -> Result<CallToolResult, ErrorData> {
    let run_id = new_run_id();

    let Some(source) = entry.source() else {
        let problem = entry
            .problem()
            .unwrap_or("the prompt is unavailable")
            .to_owned();
        return run_result(&RunResult::failed(
            run_id,
            entry.name(),
            problem,
            NO_TURNS,
            0,
        ));
    };

    let (observer, pump) = match reporting {
        Some((peer, token)) => {
            let (observer, pump) = McpObserver::reporting(peer, token);
            (Arc::new(observer), Some(pump))
        }
        None => (Arc::new(McpObserver::silent()), None),
    };

    run_observed(
        config,
        registry,
        tools,
        entry,
        args,
        source,
        Observation {
            run_id,
            observer,
            pump,
        },
    )
    .await
}

/// Runs one validated source snapshot under an already-created observation
/// context. Keeping this boundary explicit lets the runner regression retain
/// the observer while exercising the same parse-to-run path as production.
async fn run_observed(
    config: &Config,
    registry: &Arc<RunRegistry>,
    tools: Arc<PreparedTools>,
    entry: &Entry,
    args: &str,
    source: &str,
    observation: Observation,
) -> Result<CallToolResult, ErrorData> {
    let Observation {
        run_id,
        observer,
        pump,
    } = observation;
    let prompt = match Prompt::parse(source, &run_id, observer.as_ref()) {
        Ok(prompt) => prompt,
        Err(error) => {
            return preparation_failed(run_id, entry, error.to_string(), observer, pump).await;
        }
    };

    let Some(slot) = registry.admit().await else {
        drop(observer);
        finish_pump(pump).await;
        return Ok(text_error(refused(registry.admission_timeout())));
    };

    // The clock starts after admission, so a run's reported duration includes
    // live H1 resolution and execution but not the admission wait.
    let started = Instant::now();

    let client = match bind::gateway_client(&config.gateway) {
        Ok(client) => client,
        Err(error) => {
            drop(slot);
            return preparation_failed(run_id, entry, error.to_string(), observer, pump).await;
        }
    };

    let cancel = CancelHandle::new();
    let (result_tx, result_rx) = oneshot::channel();
    let launch = Launch {
        run_id: run_id.clone(),
        prompt,
        name: entry.name().to_owned(),
        args: args.to_owned(),
        tools,
        observer,
        client,
        started,
        slot,
        cancel: cancel.clone(),
    };

    // The supervisor owns the run from the instant it is registered, so the
    // slot and observer inside `launch` are released whether the run is
    // admitted or refused as a duplicate id.
    if let Err(DuplicateRun) = registry.launch(
        run_id.clone(),
        entry.name().to_owned(),
        cancel.clone(),
        result_tx,
        move || tokio::spawn(execute_run(launch)),
    ) {
        finish_pump(pump).await;
        return run_result(&RunResult::failed(
            run_id,
            entry.name(),
            "the run id was already in use".to_owned(),
            NO_TURNS,
            0,
        ));
    }

    let result = registry.settle(&run_id, entry.name(), result_rx).await;
    if let Some(pump) = pump {
        pump.finish().await;
    }
    run_result(&result)
}

/// Drives the production runner lifecycle while retaining its observer for a
/// correlation regression.
#[cfg(test)]
pub(super) async fn run_recorded(
    config: &Config,
    registry: &Arc<RunRegistry>,
    tools: Arc<PreparedTools>,
    entry: &Entry,
    args: &str,
    observer: Arc<McpObserver>,
) -> Result<CallToolResult, ErrorData> {
    let run_id = new_run_id();
    let source = entry
        .source()
        .expect("the recorded runner fixture must be a healthy entry");
    run_observed(
        config,
        registry,
        tools,
        entry,
        args,
        source,
        Observation {
            run_id,
            observer,
            pump: None,
        },
    )
    .await
}

/// Runs one prompt to a [`RunResult`].
///
/// This is the body of the spawned task, so nothing here borrows from the call
/// that started it. Its returned result is what the supervisor in
/// [`RunRegistry::launch`] records; the run installs its own
/// [`CancelHandle`](promptforge_core::CancelHandle) so an abandoned call stops
/// it cooperatively and it ends as the failure the core reports.
async fn execute_run(launch: Launch) -> RunResult {
    let Launch {
        run_id,
        prompt,
        name,
        args,
        tools,
        observer,
        client,
        started,
        slot,
        cancel,
    } = launch;

    let store = StoreRef::memory();
    let config = RunConfig::new(run_id.as_str())
        .observer(Arc::clone(&observer) as Arc<dyn promptforge_core::observe::Observer>)
        .client(client)
        .cancel(cancel);
    let outcome = execute::run(
        &prompt,
        &args,
        ResolutionContext::new(tools.picker(), tools.models()),
        tools.tools(),
        &store,
        config,
    )
    .await;

    let turns = observer.turns();
    // Timed here rather than after the flush, so the run's duration is the run's
    // own and not the client's reading speed.
    let elapsed = elapsed_ms(started);
    // Dropping the observer closes the queue, which is what lets the pump finish
    // rather than wait for a frame that will never come.
    drop(observer);

    let result = match outcome {
        Ok(value) => RunResult::completed(run_id.clone(), &name, value, turns, elapsed),
        Err(error) => RunResult::failed(run_id.clone(), &name, error.to_string(), turns, elapsed),
    };
    log_terminal_result(&result);
    // Explicit, because returning the slot is the point of having held it.
    drop(slot);
    result
}

/// Converts a parse or preparation failure into a caller-facing failed run.
async fn preparation_failed(
    run_id: String,
    entry: &Entry,
    message: String,
    observer: Arc<McpObserver>,
    pump: Option<ProgressPump>,
) -> Result<CallToolResult, ErrorData> {
    let turns = observer.turns();
    drop(observer);
    finish_pump(pump).await;
    run_result(&RunResult::failed(run_id, entry.name(), message, turns, 0))
}

/// Closes a run's optional progress path after its observer has been dropped.
async fn finish_pump(pump: Option<ProgressPump>) {
    if let Some(pump) = pump {
        pump.finish().await;
    }
}

/// Logs one payload-free terminal record after every field is final.
fn log_terminal_result(result: &RunResult) {
    tracing::info!(
        run_id = %result.run_id(),
        prompt = %result.prompt(),
        status = ?result.status(),
        turns = result.turns(),
        elapsed_ms = result.elapsed_ms(),
        "run reached its terminal state"
    );
}

/// What a call refused admission is told: the wait it spent, so the calling
/// model can decide to spend it again.
fn refused(waited: Duration) -> String {
    format!(
        "every run slot is busy and none came free within {}. Retry in a moment.",
        humantime::format_duration(waited)
    )
}

/// A fresh run identifier: 128 random bits in hex, which is unguessable enough
/// for a value that only has to be unique within one process's lifetime.
fn new_run_id() -> String {
    format!("{:016x}{:016x}", fastrand::u64(..), fastrand::u64(..))
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use tracing::Level;

    use super::{log_terminal_result, new_run_id, refused};
    use crate::levels::recording;
    use crate::result::RunResult;

    #[test]
    fn a_run_id_is_thirty_two_hex_digits() {
        let id = new_run_id();
        assert_eq!(id.len(), 32);
        assert!(id.chars().all(|c| c.is_ascii_hexdigit()));
        assert_ne!(id, new_run_id(), "two runs do not share an identifier");
    }

    #[test]
    fn a_refusal_names_the_wait_it_spent() {
        let message = refused(Duration::from_secs(30));
        assert!(message.contains("30s"), "{message}");
    }

    #[test]
    fn terminal_log_carries_the_completed_result_measurements() {
        let (levels, _recording) = recording();
        let result = RunResult::completed("r1".into(), "echo", "secret".into(), 3, 42);

        log_terminal_result(&result);

        assert_eq!(levels.operator_visible(), vec![Level::INFO]);
        for field in [
            "message=run reached its terminal state",
            "run_id=r1",
            "prompt=echo",
            "status=Completed",
            "turns=3",
            "elapsed_ms=42",
        ] {
            assert!(
                levels.said(Level::INFO, field),
                "terminal log omitted {field}"
            );
        }
        assert!(
            !levels.mentioned(Level::INFO, "secret"),
            "terminal logging must exclude the run payload"
        );
    }
}