localharness 0.42.0

Agents that own themselves: one Rust crate that's both an agent SDK (streaming, tools, hooks, policies, triggers, MCP) and a wallet-owning, self-sovereign agent that runs in the browser.
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
//! JSON-RPC 2.0 + MCP wire types.
//!
//! Just enough to do `initialize`, `tools/list`, and `tools/call`.
//! Subscriptions, sampling, prompts, resources — all deferred.

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Pin the protocol revision we negotiate with the server.
pub const MCP_PROTOCOL_VERSION: &str = "2025-03-26";

// =============================================================================
// JSON-RPC envelope
// =============================================================================

#[derive(Debug, Clone, Serialize)]
pub struct Request<'a> {
    pub jsonrpc: &'static str,
    pub id: u64,
    pub method: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl<'a> Request<'a> {
    pub fn new(id: u64, method: &'a str, params: Option<Value>) -> Self {
        Self {
            jsonrpc: "2.0",
            id,
            method,
            params,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Notification<'a> {
    pub jsonrpc: &'static str,
    pub method: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl<'a> Notification<'a> {
    pub fn new(method: &'a str, params: Option<Value>) -> Self {
        Self {
            jsonrpc: "2.0",
            method,
            params,
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Response {
    #[allow(dead_code)]
    pub jsonrpc: String,
    pub id: Option<u64>,
    #[serde(default)]
    pub result: Option<Value>,
    #[serde(default)]
    pub error: Option<RpcError>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct RpcError {
    pub code: i32,
    pub message: String,
    #[allow(dead_code)]
    #[serde(default)]
    pub data: Option<Value>,
}

// =============================================================================
// MCP message bodies
// =============================================================================

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeParams<'a> {
    pub protocol_version: &'a str,
    pub capabilities: Value,
    pub client_info: ClientInfo<'a>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ClientInfo<'a> {
    pub name: &'a str,
    pub version: &'a str,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
    /// Kept for diagnostics; we don't currently enforce protocol-version mismatches.
    #[allow(dead_code)]
    #[serde(default)]
    pub protocol_version: Option<String>,
    /// Kept for diagnostics; we don't gate features on declared server capabilities yet.
    #[allow(dead_code)]
    #[serde(default)]
    pub capabilities: Value,
    #[serde(default)]
    pub server_info: Option<ServerInfo>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ServerInfo {
    pub name: String,
    #[allow(dead_code)]
    #[serde(default)]
    pub version: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolsListResult {
    pub tools: Vec<McpToolDecl>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpToolDecl {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    /// JSON schema describing the tool's args. May be absent for
    /// no-arg tools.
    #[serde(default)]
    pub input_schema: Option<Value>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ToolCallParams<'a> {
    pub name: &'a str,
    pub arguments: Value,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolCallResult {
    #[serde(default)]
    pub content: Vec<ContentBlock>,
    #[serde(default)]
    pub is_error: Option<bool>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ContentBlock {
    Text {
        text: String,
    },
    Image {
        data: String,
        // MCP sends camelCase `mimeType`; `rename_all = "lowercase"` above
        // only renames the variant tags (the `type` value), NOT struct
        // fields — so this needs its own rename or every image-bearing
        // tool result fails to decode and the whole call errors out.
        #[serde(rename = "mimeType")]
        mime_type: String,
    },
    Resource {
        resource: Value,
    },
    #[serde(other)]
    Unknown,
}

impl ToolCallResult {
    /// Best-effort flattening of the response into a JSON value
    /// suitable to feed back to Gemini as a function_response.
    pub fn flatten(self) -> Value {
        let mut text = String::new();
        let mut images: Vec<Value> = Vec::new();
        for block in self.content {
            match block {
                ContentBlock::Text { text: t } => {
                    if !text.is_empty() {
                        text.push('\n');
                    }
                    text.push_str(&t);
                }
                ContentBlock::Image { data, mime_type } => {
                    images.push(serde_json::json!({
                        "mime_type": mime_type,
                        "data_base64": data,
                    }));
                }
                ContentBlock::Resource { resource } => {
                    if !text.is_empty() {
                        text.push('\n');
                    }
                    text.push_str(&resource.to_string());
                }
                ContentBlock::Unknown => {}
            }
        }
        let mut out = serde_json::Map::new();
        if !text.is_empty() {
            out.insert("text".into(), Value::String(text));
        }
        if !images.is_empty() {
            out.insert("images".into(), Value::Array(images));
        }
        if let Some(true) = self.is_error {
            out.insert("is_error".into(), Value::Bool(true));
        }
        Value::Object(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // ---- Response envelope decoding (untrusted child stdout) ----

    #[test]
    fn decodes_result_response() {
        let resp: Response =
            serde_json::from_str(r#"{"jsonrpc":"2.0","id":7,"result":{"ok":true}}"#).unwrap();
        assert_eq!(resp.id, Some(7));
        assert_eq!(resp.result, Some(json!({"ok": true})));
        assert!(resp.error.is_none());
    }

    #[test]
    fn decodes_error_response() {
        let resp: Response = serde_json::from_str(
            r#"{"jsonrpc":"2.0","id":3,"error":{"code":-32601,"message":"Method not found"}}"#,
        )
        .unwrap();
        assert_eq!(resp.id, Some(3));
        assert!(resp.result.is_none());
        let err = resp.error.unwrap();
        assert_eq!(err.code, -32601);
        assert_eq!(err.message, "Method not found");
    }

    #[test]
    fn error_response_carries_optional_data() {
        let resp: Response = serde_json::from_str(
            r#"{"jsonrpc":"2.0","id":1,"error":{"code":1,"message":"x","data":{"detail":42}}}"#,
        )
        .unwrap();
        assert_eq!(resp.error.unwrap().data, Some(json!({"detail": 42})));
    }

    #[test]
    fn missing_data_field_in_error_is_none() {
        let resp: Response = serde_json::from_str(
            r#"{"jsonrpc":"2.0","id":1,"error":{"code":1,"message":"x"}}"#,
        )
        .unwrap();
        assert!(resp.error.unwrap().data.is_none());
    }

    #[test]
    fn notification_decodes_with_no_id() {
        // A server-initiated notification has a `method`, no `id`. It must
        // decode (unknown fields ignored) and surface id=None so the
        // dispatcher drops it rather than mistaking it for a response.
        let resp: Response = serde_json::from_str(
            r#"{"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info"}}"#,
        )
        .unwrap();
        assert_eq!(resp.id, None);
        assert!(resp.result.is_none());
        assert!(resp.error.is_none());
    }

    #[test]
    fn response_with_extra_unknown_fields_decodes() {
        // Servers may add fields we don't model; must not break decoding.
        let resp: Response = serde_json::from_str(
            r#"{"jsonrpc":"2.0","id":9,"result":{},"meta":{"trace":"abc"},"_extra":1}"#,
        )
        .unwrap();
        assert_eq!(resp.id, Some(9));
    }

    #[test]
    fn response_missing_jsonrpc_field_fails() {
        // `jsonrpc` is required; a line without it is not a valid Response
        // and is treated as undecodable noise by the dispatcher.
        let r: std::result::Result<Response, _> =
            serde_json::from_str(r#"{"id":1,"result":{}}"#);
        assert!(r.is_err());
    }

    #[test]
    fn non_json_line_fails_to_decode() {
        // A server logging plain text to stdout must not parse as a Response.
        let r: std::result::Result<Response, _> =
            serde_json::from_str("INFO: server started, listening on stdio");
        assert!(r.is_err());
    }

    // ---- initialize / tools/list decoding ----

    #[test]
    fn initialize_result_decodes_camelcase() {
        let r: InitializeResult = serde_json::from_str(
            r#"{"protocolVersion":"2025-03-26","capabilities":{"tools":{}},"serverInfo":{"name":"demo","version":"1.0"}}"#,
        )
        .unwrap();
        assert_eq!(r.protocol_version.as_deref(), Some("2025-03-26"));
        assert_eq!(r.server_info.unwrap().name, "demo");
    }

    #[test]
    fn initialize_result_tolerates_missing_server_info() {
        let r: InitializeResult =
            serde_json::from_str(r#"{"protocolVersion":"2025-03-26","capabilities":{}}"#)
                .unwrap();
        assert!(r.server_info.is_none());
    }

    #[test]
    fn tools_list_decodes_with_and_without_optional_fields() {
        let r: ToolsListResult = serde_json::from_str(
            r#"{"tools":[
                {"name":"a","description":"does a","inputSchema":{"type":"object"}},
                {"name":"b"}
            ]}"#,
        )
        .unwrap();
        assert_eq!(r.tools.len(), 2);
        assert_eq!(r.tools[0].name, "a");
        assert_eq!(r.tools[0].description.as_deref(), Some("does a"));
        assert!(r.tools[0].input_schema.is_some());
        // Tool "b" has no description / schema — both optional.
        assert_eq!(r.tools[1].name, "b");
        assert!(r.tools[1].description.is_none());
        assert!(r.tools[1].input_schema.is_none());
    }

    #[test]
    fn tools_list_empty_is_ok() {
        let r: ToolsListResult = serde_json::from_str(r#"{"tools":[]}"#).unwrap();
        assert!(r.tools.is_empty());
    }

    // ---- tools/call result + flatten ----

    #[test]
    fn flatten_joins_multiple_text_blocks() {
        let r: ToolCallResult = serde_json::from_str(
            r#"{"content":[{"type":"text","text":"line1"},{"type":"text","text":"line2"}]}"#,
        )
        .unwrap();
        let v = r.flatten();
        assert_eq!(v["text"], json!("line1\nline2"));
        assert!(v.get("is_error").is_none());
    }

    #[test]
    fn flatten_marks_error_results() {
        let r: ToolCallResult = serde_json::from_str(
            r#"{"content":[{"type":"text","text":"boom"}],"isError":true}"#,
        )
        .unwrap();
        let v = r.flatten();
        assert_eq!(v["text"], json!("boom"));
        assert_eq!(v["is_error"], json!(true));
    }

    #[test]
    fn flatten_empty_content_is_empty_object() {
        let r: ToolCallResult = serde_json::from_str(r#"{"content":[]}"#).unwrap();
        assert_eq!(r.flatten(), json!({}));
    }

    #[test]
    fn tool_call_result_tolerates_missing_content() {
        // `content` is #[serde(default)] — a result object with no content
        // field must still decode to an empty vec, not error.
        let r: ToolCallResult = serde_json::from_str(r#"{}"#).unwrap();
        assert!(r.content.is_empty());
        assert_eq!(r.flatten(), json!({}));
    }

    #[test]
    fn flatten_unknown_content_block_is_skipped_not_fatal() {
        // A future/unsupported block type must not abort decoding of the
        // whole result (would drop a real tool response on the floor).
        let r: ToolCallResult = serde_json::from_str(
            r#"{"content":[{"type":"audio","data":"...","mimeType":"audio/wav"},{"type":"text","text":"hi"}]}"#,
        )
        .unwrap();
        let v = r.flatten();
        assert_eq!(v["text"], json!("hi"));
    }

    #[test]
    fn flatten_resource_block_serialized_into_text() {
        let r: ToolCallResult = serde_json::from_str(
            r#"{"content":[{"type":"resource","resource":{"uri":"file:///x","text":"body"}}]}"#,
        )
        .unwrap();
        let v = r.flatten();
        assert!(v["text"].as_str().unwrap().contains("file:///x"));
    }

    #[test]
    fn flatten_image_block_uses_camelcase_mime_type() {
        // The MCP wire format sends `mimeType` (camelCase). The image block
        // must decode and surface the mime type. Regression guard for a
        // field-rename bug that silently dropped the whole tool response.
        let r: ToolCallResult = serde_json::from_str(
            r#"{"content":[{"type":"image","data":"BASE64","mimeType":"image/png"}]}"#,
        )
        .unwrap();
        let v = r.flatten();
        let imgs = v["images"].as_array().expect("images array present");
        assert_eq!(imgs.len(), 1);
        assert_eq!(imgs[0]["mime_type"], json!("image/png"));
        assert_eq!(imgs[0]["data_base64"], json!("BASE64"));
    }
}