scone-cli 0.2.1

Scone: a local-first temporal memory engine — CLI, MCP server, and HTTP API
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
//! MCP server: persistent memory for any MCP agent (spec §8).
//!
//! Space-scoped and input-bounded from the first commit — the predecessor
//! shipped unscoped document access and unbounded inputs, hardened only
//! fourteen months later (memory/lessons.md L-10, bugs.md P-1/P-4).

use std::sync::Mutex;

use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{CallToolResult, ContentBlock, ErrorData};
use rmcp::{ServerHandler, tool, tool_handler, tool_router};
use scone_core::{Engine, IngestInput, IngestOutcome, RecallOpts, auth};

const MAX_CONTENT: usize = 100_000;
const MAX_QUERY: usize = 1_000;
const MAX_ENTITY: usize = 200;
const MAX_REASON: usize = 500;
const MAX_LIMIT: usize = 50;

pub struct SconeMcp {
    engine: Mutex<Engine>,
    default_space: String,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct StoreParams {
    /// The content to remember (1..=100000 chars)
    pub content: String,
    /// Space to store into; defaults to the server's space
    pub space: Option<String>,
    /// Tags for focused retrieval later (each 1..=64 chars, max 10)
    pub tags: Option<Vec<String>>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct RecallParams {
    /// Natural-language query (1..=1000 chars)
    pub query: String,
    pub space: Option<String>,
    /// Max items (1..=50)
    pub limit: Option<usize>,
    /// Prepend the space's profile (identity facts + recent activity).
    /// Defaults to true.
    pub include_profile: Option<bool>,
    /// Focus recall to episodes carrying ALL of these tags.
    pub tags: Option<Vec<String>>,
    /// Evaluate fact validity at this ISO-8601 instant (time travel)
    pub as_of: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct FactsAboutParams {
    /// Entity to look up (person, project, tool …)
    pub entity: String,
    pub space: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct ForgetParams {
    /// Fact id to close (from memory_recall / memory_facts_about output)
    pub fact_id: i64,
    /// Why this fact should be forgotten (recorded, never deleted)
    pub reason: String,
    pub space: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct PendingParams {
    /// Max episodes to return (1..=20)
    pub limit: Option<usize>,
    pub space: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct SubmittedFact {
    pub subject: String,
    pub predicate: String,
    pub object: String,
    /// 0..=1; defaults to 0.8
    pub confidence: Option<f32>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct StoreFactsParams {
    /// Episode id from memory_pending
    pub episode_id: i64,
    /// Extracted facts (max 50)
    pub facts: Vec<SubmittedFact>,
    pub space: Option<String>,
}

fn tool_error(msg: impl Into<String>) -> CallToolResult {
    CallToolResult::error(vec![ContentBlock::text(msg.into())])
}

fn ok_text(msg: impl Into<String>) -> CallToolResult {
    CallToolResult::success(vec![ContentBlock::text(msg.into())])
}

impl SconeMcp {
    pub fn new(engine: Engine, default_space: &str) -> Self {
        Self {
            engine: Mutex::new(engine),
            default_space: default_space.to_owned(),
        }
    }

    /// Run one closure against the engine in a named space.
    fn with_space<T>(
        &self,
        space_override: &Option<String>,
        f: impl FnOnce(&mut Engine, &auth::ScopedSpace) -> scone_core::Result<T>,
    ) -> Result<T, String> {
        let name = space_override
            .clone()
            .unwrap_or_else(|| self.default_space.clone());
        let mut engine = self
            .engine
            .lock()
            .map_err(|_| "engine lock poisoned".to_owned())?;
        let space = auth::resolve(&mut engine, &name, true).map_err(|e| e.to_string())?;
        f(&mut engine, &space).map_err(|e| e.to_string())
    }
}

#[tool_router]
impl SconeMcp {
    /// Save content to persistent memory. Returns the episode id; duplicate
    /// content is recognized, not re-stored. When an LLM is configured,
    /// facts are distilled immediately.
    #[tool(name = "memory_store")]
    async fn memory_store(
        &self,
        Parameters(p): Parameters<StoreParams>,
    ) -> Result<CallToolResult, ErrorData> {
        if p.content.is_empty() || p.content.len() > MAX_CONTENT {
            return Ok(tool_error(format!(
                "content must be 1..={MAX_CONTENT} bytes, got {}",
                p.content.len()
            )));
        }
        let tags = p.tags.clone().unwrap_or_default();
        if tags.len() > 10 {
            return Ok(tool_error("at most 10 tags per store"));
        }
        let result = self.with_space(&p.space, |engine, space| {
            let outcome = engine.ingest(
                space,
                IngestInput::Note {
                    text: p.content.clone(),
                },
            )?;
            let episode_id = match &outcome {
                IngestOutcome::Ingested { episode_id, .. }
                | IngestOutcome::Deduplicated { episode_id } => *episode_id,
            };
            if !tags.is_empty() {
                let refs: Vec<&str> = tags.iter().map(String::as_str).collect();
                engine.tag_episode(space, episode_id, &refs)?;
            }
            let lane = if engine.has_llm() {
                let r = engine.distill(space, 10)?;
                format!(
                    "facts: +{} added, {} closed{}",
                    r.facts_added,
                    r.facts_closed,
                    if r.failed > 0 {
                        format!(", {} failed (recorded for retry)", r.failed)
                    } else {
                        String::new()
                    }
                )
            } else {
                "semantic lane paused (no LLM configured); episodic memory stored".to_owned()
            };
            Ok((outcome, lane))
        });
        Ok(match result {
            Ok((IngestOutcome::Ingested { episode_id, chunks }, lane)) => ok_text(format!(
                "stored episode {episode_id} ({chunks} chunks). {lane}"
            )),
            Ok((IngestOutcome::Deduplicated { episode_id }, _)) => ok_text(format!(
                "already stored as episode {episode_id} (deduplicated)"
            )),
            Err(e) => tool_error(e),
        })
    }

    /// Recall relevant memory: temporal facts first, then episodic chunks,
    /// each with provenance. `as_of` answers what was true at a past time.
    #[tool(name = "memory_recall")]
    async fn memory_recall(
        &self,
        Parameters(p): Parameters<RecallParams>,
    ) -> Result<CallToolResult, ErrorData> {
        if p.query.is_empty() || p.query.len() > MAX_QUERY {
            return Ok(tool_error(format!(
                "query must be 1..={MAX_QUERY} chars, got {}",
                p.query.len()
            )));
        }
        let limit = p.limit.unwrap_or(10).clamp(1, MAX_LIMIT);
        let include_profile = p.include_profile.unwrap_or(true);
        let result = self.with_space(&p.space, |engine, space| {
            let profile = if include_profile {
                Some(engine.profile(space, 5)?)
            } else {
                None
            };
            let pack = engine.recall(
                space,
                &p.query,
                &RecallOpts {
                    limit,
                    budget_bytes: None,
                    as_of: p.as_of.clone(),
                    expand_neighbors: true,
                    tags: p.tags.clone().unwrap_or_default(),
                },
            )?;
            Ok((profile, pack))
        });
        Ok(match result {
            Ok((profile, pack)) => {
                let mut out = String::new();
                if let Some(profile) = profile {
                    if !profile.static_facts.is_empty() {
                        out.push_str(
                            "## Profile
",
                        );
                        for f in &profile.static_facts {
                            out.push_str(&format!(
                                "- {} {} {} (conf {:.2})
",
                                f.subject, f.predicate, f.object, f.confidence
                            ));
                        }
                    }
                    if !profile.dynamic.is_empty() {
                        out.push_str(
                            "## Recent activity
",
                        );
                        for d in &profile.dynamic {
                            out.push_str(&format!(
                                "- {}
",
                                d.replace('\n', " ")
                            ));
                        }
                    }
                }
                for f in &pack.facts {
                    out.push_str(&format!(
                        "fact [{}] {} {} {} (conf {:.2}, {})\n",
                        f.fact_id, f.subject, f.predicate, f.object, f.confidence, f.status
                    ));
                }
                for item in &pack.items {
                    out.push_str(&format!(
                        "memory [{} | episode {}] {}\n",
                        item.day(),
                        item.episode_id,
                        item.text
                    ));
                }
                for d in &pack.degraded {
                    out.push_str(&format!("degraded: {d}\n"));
                }
                if out.is_empty() {
                    out.push_str("no matching memory");
                }
                ok_text(out)
            }
            Err(e) => tool_error(e),
        })
    }

    /// List what is currently known about one entity (active facts only).
    #[tool(name = "memory_facts_about")]
    async fn memory_facts_about(
        &self,
        Parameters(p): Parameters<FactsAboutParams>,
    ) -> Result<CallToolResult, ErrorData> {
        if p.entity.is_empty() || p.entity.len() > MAX_ENTITY {
            return Ok(tool_error(format!(
                "entity must be 1..={MAX_ENTITY} chars, got {}",
                p.entity.len()
            )));
        }
        let result = self.with_space(&p.space, |engine, space| {
            engine.facts_about(space, &p.entity)
        });
        Ok(match result {
            Ok(facts) if facts.is_empty() => ok_text(format!("no facts about {}", p.entity)),
            Ok(facts) => ok_text(
                facts
                    .iter()
                    .map(|f| {
                        format!(
                            "fact [{}] {} {} {} (conf {:.2}, since {})",
                            f.fact_id, f.subject, f.predicate, f.object, f.confidence, f.valid_from
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n"),
            ),
            Err(e) => tool_error(e),
        })
    }

    /// List episodes awaiting fact extraction. YOU are the extractor:
    /// read each episode, distill durable subject/predicate/object facts
    /// with your own reasoning, then submit them via memory_store_facts.
    #[tool(name = "memory_pending")]
    async fn memory_pending(
        &self,
        Parameters(p): Parameters<PendingParams>,
    ) -> Result<CallToolResult, ErrorData> {
        let limit = p.limit.unwrap_or(5).clamp(1, 20);
        let result = self.with_space(&p.space, |engine, space| {
            engine.pending_episodes(space, limit)
        });
        Ok(match result {
            Ok(rows) if rows.is_empty() => ok_text("nothing pending: memory is fully distilled"),
            Ok(rows) => {
                let mut out = String::from(
                    "Episodes awaiting fact extraction (submit via memory_store_facts):
",
                );
                for (id, content, created_at) in rows {
                    out.push_str(&format!(
                        "--- episode {id} ({created_at})
{content}
"
                    ));
                }
                ok_text(out)
            }
            Err(e) => tool_error(e),
        })
    }

    /// Submit facts you extracted from a pending episode. The engine
    /// applies contradiction closure and provenance; you only propose.
    #[tool(name = "memory_store_facts")]
    async fn memory_store_facts(
        &self,
        Parameters(p): Parameters<StoreFactsParams>,
    ) -> Result<CallToolResult, ErrorData> {
        if p.facts.len() > 50 {
            return Ok(tool_error("at most 50 facts per submission"));
        }
        let facts: Vec<scone_core::llm::ExtractedFact> = p
            .facts
            .iter()
            .map(|f| scone_core::llm::ExtractedFact {
                subject: f.subject.clone(),
                predicate: f.predicate.clone(),
                object: f.object.clone(),
                confidence: f.confidence.unwrap_or(0.8).clamp(0.0, 1.0),
            })
            .collect();
        let result = self.with_space(&p.space, |engine, space| {
            engine.complete_distillation(space, p.episode_id, &facts)
        });
        Ok(match result {
            Ok(report) => ok_text(format!(
                "episode {} distilled: {} fact{} added, {} closed, {} deduplicated",
                p.episode_id,
                report.added,
                if report.added == 1 { "" } else { "s" },
                report.closed,
                report.deduplicated,
            )),
            Err(e) => tool_error(e),
        })
    }

    /// Forget a fact: closes its validity interval with your reason.
    /// History is preserved; nothing is deleted.
    #[tool(name = "memory_forget")]
    async fn memory_forget(
        &self,
        Parameters(p): Parameters<ForgetParams>,
    ) -> Result<CallToolResult, ErrorData> {
        if p.reason.is_empty() || p.reason.len() > MAX_REASON {
            return Ok(tool_error(format!(
                "reason must be 1..={MAX_REASON} chars, got {}",
                p.reason.len()
            )));
        }
        let result = self.with_space(&p.space, |engine, space| {
            engine.facts_close(space, p.fact_id, &p.reason)
        });
        Ok(match result {
            Ok(()) => ok_text(format!("closed fact {}: {}", p.fact_id, p.reason)),
            Err(e) => tool_error(e),
        })
    }
}

#[tool_handler]
impl ServerHandler for SconeMcp {
    fn get_info(&self) -> rmcp::model::ServerInfo {
        let mut info = rmcp::model::ServerInfo::default();
        info.server_info.name = "scone".into();
        info.server_info.title = Some("Scone memory engine".into());
        info.server_info.version = env!("CARGO_PKG_VERSION").into();
        info.instructions = Some(
            "Persistent memory for this agent. Call memory_recall at task start; \
             memory_store for durable observations; memory_facts_about before acting \
             on an entity; memory_forget when the user retracts something. \
             Periodically (session start or idle), call memory_pending and distill \
             the returned episodes into subject/predicate/object facts with your own \
             reasoning, submitting via memory_store_facts; you are the extraction \
             model and no API key is needed."
                .into(),
        );
        info
    }
}