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
//! A scripted agent: a client of the daemon that does exactly what it is
//! told, with no model behind it.
//!
//! It connects the way a real client's proxy does, over the same MCP
//! transport, saying the name of the client it stands in for, so the daemon
//! attributes its writes and the Brain shows them as it would show the real
//! thing. It hashes a fact's sources and checks facts in answers against a
//! project directory, as the proxy does, so freshness works too.
//!
//! `memfork demo` drives two of these. Nothing here spends anything or
//! touches a real store: the caller says which daemon and which directory.
use std::path::{Path, PathBuf};
use serde_json::{json, Map, Value as Json};
use crate::persist::Endpoint;
use crate::proxy::{Hello, Upstream};
/// One scripted agent, connected.
#[derive(Debug)]
pub struct FakeAgent {
/// The name a person knows the client by.
pub display: String,
/// The project it works in.
pub namespace: String,
root: PathBuf,
upstream: Upstream,
hasher: crate::facts::Hasher,
}
/// The MCP client name for a display name, from the registry: what that
/// client sends in `initialize`. A name the registry does not know is used
/// as it is.
pub fn mcp_name_for(display: &str) -> String {
crate::clients::all()
.into_iter()
.find(|c| c.display.eq_ignore_ascii_case(display))
.and_then(|c| {
c.mcp_names
.first()
.map(|n| n.trim_end_matches('*').to_owned())
})
.unwrap_or_else(|| display.to_owned())
}
/// Two clients to stand in for, from the registry: the first two whose
/// MCP name is confirmed. Data, not a choice made in code.
pub fn default_pair() -> (String, String) {
let mut names = crate::clients::all()
.into_iter()
.filter(|c| c.unverified.is_none() && !c.mcp_names.is_empty())
.map(|c| c.display);
let a = names.next().unwrap_or_else(|| "Agent A".to_owned());
let b = names.next().unwrap_or_else(|| "Agent B".to_owned());
(a, b)
}
impl FakeAgent {
/// Connect to the daemon an endpoint file describes, as `display`, in
/// `namespace`, with `root` as the project directory.
pub async fn connect(
endpoint: &Endpoint,
display: &str,
namespace: &str,
root: &Path,
) -> Result<Self, String> {
let hello = Hello {
namespace: namespace.to_owned(),
client: Some(mcp_name_for(display)),
session: Some(format!(
"fake-{}-{}",
mcp_name_for(display),
std::process::id()
)),
};
let upstream = Upstream::connect(endpoint, &hello)
.await
.map_err(|e| e.to_string())?;
Ok(FakeAgent {
display: display.to_owned(),
namespace: namespace.to_owned(),
root: root.to_path_buf(),
upstream,
hasher: crate::facts::Hasher::default(),
})
}
/// Call one tool. A `memfork_put` with `sources` has them hashed here,
/// where the files are; facts in any answer are checked against the
/// files and the verdicts reported, as a proxy would.
pub async fn call(&self, tool: &str, args: Json) -> Result<Json, String> {
let mut arguments: Map<String, Json> = match args {
Json::Object(map) => map,
_ => Map::new(),
};
if tool == "memfork_put" {
let sources: Option<Vec<String>> = arguments
.get("sources")
.and_then(|s| serde_json::from_value(s.clone()).ok());
if let Some(Ok(sources)) = sources.map(|s| crate::facts::normalise(&s)) {
let hashes = self.hasher.record(&self.root, &sources);
arguments.insert("source_hashes".to_owned(), json!(hashes));
}
}
let params =
rmcp::model::CallToolRequestParams::new(tool.to_owned()).with_arguments(arguments);
let result = self
.upstream
.call_tool(¶ms)
.await
.map_err(|e| e.to_string())?;
if result.get("isError") == Some(&json!(true)) {
return Err(format!(
"`{tool}` failed: {}",
result["content"][0]["text"]
.as_str()
.unwrap_or("no reason given")
));
}
let mut content = result
.get("structuredContent")
.cloned()
.unwrap_or(Json::Null);
let checked = crate::facts::check(&mut content, &self.root, &self.hasher);
if !checked.is_empty() {
self.upstream
.report(&json!({
"client": mcp_name_for(&self.display),
"namespace": self.namespace,
"facts": checked.facts.iter().map(|(k, s)| json!([k, s])).collect::<Vec<_>>(),
}))
.await;
}
Ok(content)
}
/// End the session, so the daemon stops listing this client.
pub async fn disconnect(self) {
self.upstream.close().await;
}
}