bacnet-mcp 0.15.0

A Model Context Protocol server for BACnet sites, over the bacnet-ops engine, read-only unless a profile offers guarded writes
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
424
//! Every tool's structured content, success and error alike, against the
//! output schema the tool declares.
//!
//! The check covers the JSON Schema keywords the output schemas use:
//! `type` (one or a list), `properties`, `required`, `items`, `enum`,
//! `anyOf` and `additionalProperties` (`false`, or a schema every other
//! key's value must fit).
//! A test asserts the schemas use no other keyword that constrains a
//! value, so a new one can't pass unchecked.

use serde_json::{Value, json};

use super::Lab;
use crate::Policy;

/// The keywords this check understands; descriptions are notes.
const KNOWN: [&str; 8] = [
    "type",
    "properties",
    "required",
    "items",
    "enum",
    "anyOf",
    "additionalProperties",
    "description",
];

/// Why `value` doesn't fit `schema`, at `path`.
fn violations(schema: &Value, value: &Value, path: &str, out: &mut Vec<String>) {
    let Some(schema) = schema.as_object() else {
        return;
    };
    for key in schema.keys() {
        assert!(
            KNOWN.contains(&key.as_str()),
            "{path}: the check doesn't know `{key}`"
        );
    }
    if let Some(types) = schema.get("type") {
        let types: Vec<&str> = match types {
            Value::String(one) => vec![one.as_str()],
            Value::Array(many) => many.iter().filter_map(Value::as_str).collect(),
            _ => Vec::new(),
        };
        let fits = types.iter().any(|t| match *t {
            "object" => value.is_object(),
            "array" => value.is_array(),
            "string" => value.is_string(),
            "integer" => value.is_i64() || value.is_u64(),
            "number" => value.is_number(),
            "boolean" => value.is_boolean(),
            "null" => value.is_null(),
            _ => false,
        });
        if !fits {
            out.push(format!("{path}: {value} is not {types:?}"));
            return;
        }
    }
    if let Some(Value::Array(choices)) = schema.get("anyOf") {
        let fits_one = choices.iter().any(|choice| {
            let mut inner = Vec::new();
            violations(choice, value, path, &mut inner);
            inner.is_empty()
        });
        if !fits_one {
            out.push(format!("{path}: {value} fits none of anyOf"));
            return;
        }
    }
    if schema.get("additionalProperties") == Some(&Value::Bool(false))
        && let (Some(object), properties) = (value.as_object(), schema.get("properties"))
    {
        for key in object.keys() {
            if properties.and_then(|p| p.get(key)).is_none() {
                out.push(format!("{path}: unexpected {key}"));
            }
        }
    }
    if let (Some(extra @ Value::Object(_)), Some(object)) =
        (schema.get("additionalProperties"), value.as_object())
    {
        let properties = schema.get("properties");
        for (key, field) in object {
            if properties.and_then(|p| p.get(key)).is_none() {
                violations(extra, field, &format!("{path}.{key}"), out);
            }
        }
    }
    if let Some(Value::Array(allowed)) = schema.get("enum")
        && !allowed.contains(value)
    {
        out.push(format!("{path}: {value} is not one of {allowed:?}"));
    }
    if let (Some(Value::Object(properties)), Some(object)) =
        (schema.get("properties"), value.as_object())
    {
        for (key, inner) in properties {
            if let Some(field) = object.get(key) {
                violations(inner, field, &format!("{path}.{key}"), out);
            }
        }
    }
    if let (Some(Value::Array(required)), Some(object)) =
        (schema.get("required"), value.as_object())
    {
        for key in required.iter().filter_map(Value::as_str) {
            if !object.contains_key(key) {
                out.push(format!("{path}: missing {key}"));
            }
        }
    }
    if let (Some(items), Some(array)) = (schema.get("items"), value.as_array()) {
        for (i, item) in array.iter().enumerate() {
            violations(items, item, &format!("{path}[{i}]"), out);
        }
    }
}

/// Check `body` against `tool`'s output schema, as the server lists it.
pub(super) async fn check_output(agent: &mut super::Agent, tool: &str, body: &Value) {
    let tools = agent.request("tools/list", json!({})).await;
    let schema = tools["result"]["tools"]
        .as_array()
        .unwrap()
        .iter()
        .find(|t| t["name"] == json!(tool))
        .map(|t| t["outputSchema"].clone())
        .unwrap();
    let mut out = Vec::new();
    violations(&schema, body, "$", &mut out);
    assert!(out.is_empty(), "{tool}: {out:?}");
}

/// The validator itself catches a wrong type, a missing field and a value
/// outside an enum.
#[test]
fn the_check_finds_violations() {
    let schema = json!({
        "type": "object",
        "properties": { "n": { "type": "integer" }, "e": { "enum": ["a"] } },
        "required": ["n"],
    });
    let mut out = Vec::new();
    violations(&schema, &json!({ "n": "1", "e": "b" }), "$", &mut out);
    violations(&schema, &json!({}), "$", &mut out);
    assert_eq!(out.len(), 3, "{out:?}");
    let either = json!({ "anyOf": [
        { "type": "integer" },
        { "type": "object", "properties": { "a": {} }, "additionalProperties": false },
    ] });
    let mut out = Vec::new();
    violations(&either, &json!(3), "$", &mut out);
    violations(&either, &json!({ "a": 1 }), "$", &mut out);
    assert!(out.is_empty(), "{out:?}");
    violations(&either, &json!({ "b": 1 }), "$", &mut out);
    violations(&either, &json!("x"), "$", &mut out);
    assert_eq!(out.len(), 2, "{out:?}");
    // `additionalProperties` as a schema: every other key's value fits it.
    let map = json!({ "type": "object", "additionalProperties": { "type": "integer" } });
    let mut out = Vec::new();
    violations(&map, &json!({ "x": 1 }), "$", &mut out);
    assert!(out.is_empty(), "{out:?}");
    violations(&map, &json!({ "x": "s" }), "$", &mut out);
    assert_eq!(out.len(), 1, "{out:?}");
}

/// Calls that succeed and calls that fail, for every tool. A watched
/// point's values come in the same shapes as a read's.
#[tokio::test(start_paused = true)]
async fn results_fit_their_output_schemas() {
    let mut lab = Lab::start_site(Policy::default(), std::time::Duration::ZERO).await;
    lab.add_logs().await;
    lab.agent.initialize().await;
    let tools = lab.agent.request("tools/list", json!({})).await;
    let schemas: Vec<(String, Value)> = tools["result"]["tools"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| {
            (
                t["name"].as_str().unwrap().to_string(),
                t["outputSchema"].clone(),
            )
        })
        .collect();
    let calls = [
        ("bacnet_discover", json!({ "range": "1201", "names": true })),
        ("bacnet_discover", json!({ "send": false })),
        ("bacnet_discover", json!({ "range": "1-4194303" })),
        (
            "bacnet_discover",
            json!({ "scope": "network", "network": 0 }),
        ),
        (
            "bacnet_read",
            json!({ "device": "1201", "reads": [{ "object": "ai:3" }], "preset": "point" }),
        ),
        (
            "bacnet_read",
            json!({ "device": "1201", "reads": [{ "object": "ai:1", "properties": ["all"] }] }),
        ),
        (
            "bacnet_read",
            json!({ "device": "4242", "reads": [{ "object": "ai:1" }] }),
        ),
        (
            "bacnet_read",
            json!({ "device": "2001:300", "reads": [{ "object": "ai:1" }] }),
        ),
        (
            "bacnet_read",
            json!({ "device": "1201", "reads": [{ "object": "nope:1" }] }),
        ),
        (
            "bacnet_list_objects",
            json!({ "device": "1201", "names": true }),
        ),
        ("bacnet_list_objects", json!({ "device": "4242" })),
        (
            "bacnet_list_objects",
            json!({ "device": "1201", "cursor": "00.00" }),
        ),
        (
            "bacnet_describe_device",
            json!({ "device": "1201", "count_objects": true }),
        ),
        ("bacnet_describe_device", json!({ "device": "2001:300" })),
        (
            "bacnet_find_object",
            json!({ "name": "zone", "description": "", "range": "1201" }),
        ),
        (
            "bacnet_find_object",
            json!({ "name": "zone", "range": "1201" }),
        ),
        (
            "bacnet_find_object",
            json!({ "types": ["ai"], "range": "1201-1202" }),
        ),
        ("bacnet_find_object", json!({ "range": "1201" })),
        ("bacnet_network_map", json!({})),
        ("bacnet_network_map", json!({ "send": true })),
        // The lab's link is no B/IP: refused (tests/bbmd.rs checks a survey).
        ("bacnet_network_map", json!({ "bbmd": true })),
        (
            "bacnet_read_trend",
            json!({ "device": 1201, "object": "trend-log:1", "max_records": 60 }),
        ),
        (
            "bacnet_read_trend",
            json!({ "device": 1201, "object": "trend-log:2", "summary": true }),
        ),
        // State columns labelled with enveloped state texts (#2122).
        (
            "bacnet_read_trend",
            json!({ "device": 1201, "object": "trend-log-multiple:1", "max_records": 8 }),
        ),
        (
            "bacnet_read_trend",
            json!({ "device": 1201, "object": "ai:1" }),
        ),
        (
            "bacnet_read_trend",
            json!({ "device": 1201, "object": "trend-log:9" }),
        ),
        // Event and audit records, device text enveloped (#2103).
        (
            "bacnet_read_log_records",
            json!({ "device": 1201, "object": "event-log:1", "max_records": 12 }),
        ),
        (
            "bacnet_read_log_records",
            json!({ "device": 1201, "object": "audit-log:1", "max_records": 12 }),
        ),
        (
            "bacnet_read_log_records",
            json!({ "device": 1201, "object": "audit-log:1", "summary": true }),
        ),
        (
            "bacnet_read_log_records",
            json!({ "device": 1201, "object": "trend-log:1" }),
        ),
        (
            "bacnet_alarms",
            json!({ "device": 1201, "summaries": true }),
        ),
        ("bacnet_alarms", json!({ "device": 1201, "max": 1 })),
        ("bacnet_alarms", json!({ "device": 4242 })),
        (
            "bacnet_decode",
            json!({ "hex": super::site::ack_with_text("Fan\u{202E}") }),
        ),
        ("bacnet_decode", json!({ "hex": "10 08", "link": "apdu" })),
        ("bacnet_decode", json!({ "hex": "zz" })),
        (
            "bacnet_watch",
            json!({ "points": [{ "device": 1201, "object": "ai:1" },
                               { "device": 1201, "object": "ai:3", "property": "description" },
                               { "device": 4242, "object": "ai:1" }] }),
        ),
        (
            "bacnet_watch",
            json!({ "points": [{ "device": 1201, "object": "nope:1" }] }),
        ),
    ];
    // Each tool's (successes, errors) checked.
    let mut checked: std::collections::BTreeMap<&str, (u32, u32)> = Default::default();
    for (tool, args) in calls {
        let result = lab.agent.call(tool, args.clone()).await;
        let tally = checked.entry(tool).or_default();
        if result["isError"] == json!(true) {
            tally.1 += 1;
        } else {
            tally.0 += 1;
        }
        let schema = &schemas.iter().find(|(name, _)| name == tool).unwrap().1;
        let mut out = Vec::new();
        violations(schema, &result["structuredContent"], "$", &mut out);
        assert!(out.is_empty(), "{tool} {args}: {out:?}");
    }
    assert_eq!(checked.len(), 11, "every tool: {checked:?}");
    for (tool, (successes, errors)) in &checked {
        assert!(
            *successes > 0 && *errors > 0,
            "{tool}: {successes} successes, {errors} errors"
        );
    }
    lab.stop().await;
}

/// An Event_Parameters value decoded field by field (#2090) fits the read
/// tool's schema, its element kind stays plain, and the device's alarm
/// strings among its fields come back enveloped.
#[tokio::test(start_paused = true)]
async fn event_parameters_fields_fit_the_schema_and_envelope_device_text() {
    use bacnet_objects::event_enrollment::EventEnrollmentObject;
    use bacnet_types::constructed::{
        BACnetDeviceObjectPropertyReference, BACnetEventParameter, BACnetPropertyStates,
    };
    use bacnet_types::enums::{EventType, ObjectType, PropertyIdentifier};
    use bacnet_types::primitives::ObjectIdentifier;

    let mut lab = Lab::start(Policy::default()).await;
    let mut enrollment =
        EventEnrollmentObject::new(1, "EE-1", EventType::CHANGE_OF_CHARACTERSTRING).unwrap();
    enrollment.set_event_parameters(BACnetEventParameter::ChangeOfCharacterstring {
        time_delay: 5,
        list_of_alarm_values: vec!["FIRE".into(), super::hostile()],
    });
    let mut states = EventEnrollmentObject::new(2, "EE-2", EventType::CHANGE_OF_STATE).unwrap();
    states.set_event_parameters(BACnetEventParameter::ChangeOfState {
        time_delay: 0,
        list_of_values: vec![BACnetPropertyStates::State(3)],
    });
    let mut feedback = EventEnrollmentObject::new(3, "EE-3", EventType::COMMAND_FAILURE).unwrap();
    feedback.set_event_parameters(BACnetEventParameter::CommandFailure {
        time_delay: 1,
        feedback_property_reference: BACnetDeviceObjectPropertyReference::new_local(
            ObjectIdentifier::new(ObjectType::BINARY_INPUT, 4).unwrap(),
            PropertyIdentifier::PRESENT_VALUE.to_raw(),
        ),
    });
    {
        let mut db = lab.server.database().write().await;
        for object in [enrollment, states, feedback] {
            db.add(Box::new(object)).unwrap();
        }
    }
    lab.agent.initialize().await;
    let tools = lab.agent.request("tools/list", json!({})).await;
    let schema = tools["result"]["tools"]
        .as_array()
        .unwrap()
        .iter()
        .find(|t| t["name"] == "bacnet_read")
        .unwrap()["outputSchema"]
        .clone();
    let read = lab
        .agent
        .call(
            "bacnet_read",
            json!({ "device": "1201",
                    "reads": [{ "object": "event-enrollment:1",
                                "properties": ["event-parameters"] },
                              { "object": "event-enrollment:2",
                                "properties": ["event-parameters"] },
                              { "object": "event-enrollment:3",
                                "properties": ["event-parameters"] }] }),
        )
        .await;
    let mut out = Vec::new();
    violations(&schema, &read["structuredContent"], "$", &mut out);
    assert!(out.is_empty(), "{out:?}");

    let typed = &read["structuredContent"]["results"][0]["value"]["typed"];
    assert_eq!(typed["element"], json!("event-parameter"));
    assert!(typed["text"]["untrusted"].is_string(), "{typed}");
    let fields = &typed["fields"];
    assert_eq!(
        fields["algorithm"],
        json!({"value": 17, "name": "change-of-characterstring"})
    );
    assert_eq!(fields["time_delay"], json!(5));
    let values = fields["list_of_alarm_values"].as_array().unwrap();
    assert_eq!(values[0]["untrusted"], json!("FIRE"));
    let hostile = values[1]["untrusted"].as_str().unwrap();
    assert!(hostile.contains("hxxps://evil.example/x"), "{hostile}");
    assert!(!hostile.contains('\u{202E}'), "{hostile}");

    // A nested element is a value too: a property state's text (a kind
    // that may hold a string) is enveloped, a reference's left plain.
    let results = &read["structuredContent"]["results"];
    let state = &results[1]["value"]["typed"]["fields"]["list_of_values"][0];
    assert_eq!(state["element"], json!("property-state"));
    assert_eq!(state["text"]["untrusted"], json!("high-limit"), "{state}");
    let reference = &results[2]["value"]["typed"]["fields"]["feedback_property_reference"];
    assert_eq!(
        *reference,
        json!({"element": "device-object-property-reference",
               "text": "binary-input 4 present-value"})
    );
    lab.stop().await;
}