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
//! `bacnet_discover`: Who-Is, then the discovery table, paged.

use std::sync::PoisonError;
use std::time::Duration;

use bacnet_client::discovery::DiscoveredDevice;
use bacnet_ops::engine::{Wanted, WhoIsScope};
use bacnet_ops::label::kebab;
use bacnet_services::who_is::DeviceInstanceRange;
use bacnet_transport::port::TransportPort;
use bacnet_types::enums::PropertyIdentifier;
use bacnet_types::primitives::ObjectIdentifier;
use serde::Deserialize;
use serde_json::{Map, Value, json};
use tokio::time::Instant;

use super::{Spec, next, schema, start};
use crate::context::{Call, Ctx};
use crate::errors::ToolError;
use crate::untrusted::envelope;
use crate::value::row_text;

pub(super) const NAME: &str = "bacnet_discover";

pub(super) const SPEC: Spec = Spec {
    name: NAME,
    title: "Discover BACnet devices",
    description: "Send a Who-Is (unless send is false) and list the devices that have \
                  answered: instance, address, network, vendor id, APDU size and \
                  segmentation. scope local (the default) asks this network, network asks one \
                  remote network, directed asks one address, global asks every network. A \
                  global Who-Is, or one without a range or wider than 1000 instances, needs \
                  broad discovery in the server's policy, at most one a minute: give a range \
                  (1000-1999) otherwise. A directed Who-Is counts too, unless its address is \
                  a device already discovered (an unknown address may be a subnet broadcast). \
                  When the policy names the devices it may reach, the range must lie inside \
                  them. names: true also reads each listed device's \
                  name. send: false only lists what is known; pages index a live table. \
                  Read-only.",
    input,
    output,
    idempotent: true,
    open_world: true,
    passive: true,
};

/// How long one broad Who-Is holds off the next.
const BROAD_INTERVAL: Duration = Duration::from_secs(60);

/// The widest range of instances a Who-Is may ask without broad
/// discovery, when the policy doesn't name the devices.
pub(crate) const BROAD_WIDTH: u32 = 1000;

fn input() -> Value {
    schema::input(
        json!({
            "send": {
                "type": "boolean",
                "default": true,
                "description": "Send a Who-Is first; false lists the devices already known",
            },
            "scope": { "enum": ["local", "network", "directed", "global"], "default": "local" },
            "network": {
                "type": "integer",
                "minimum": 1,
                "maximum": 65534,
                "description": "The remote network, for scope network",
            },
            "address": {
                "type": "string",
                "description": "The station address, for scope directed (192.168.1.20, mstp:5, ...)",
            },
            "range": {
                "type": "string",
                "pattern": "^[0-9]+(-[0-9]+)?$",
                "description": "Device instances to ask, low-high or one instance",
            },
            "wait_s": { "type": "integer", "minimum": 1, "maximum": 30, "default": 3 },
            "names": {
                "type": "boolean",
                "default": false,
                "description": "Also read each listed device's Object_Name",
            },
            "cursor": schema::cursor(),
        }),
        &[],
    )
}

fn output() -> Value {
    schema::output(json!({
        "devices": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "instance": { "type": "integer" },
                    "address": { "type": "string" },
                    "router": { "type": ["string", "null"] },
                    "network": { "type": "integer" },
                    "vendor_id": { "type": "integer" },
                    "max_apdu": { "type": "integer" },
                    "segmentation": { "type": "string" },
                    "last_seen_s": { "type": "integer" },
                    "name": schema::envelope(),
                },
                "required": ["instance", "address", "network"],
            },
        },
        "total": { "type": "integer" },
        "sent": { "type": "boolean" },
        "scope": { "type": "string" },
    }))
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
enum Scope {
    #[default]
    Local,
    Network,
    Directed,
    Global,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Args {
    send: Option<bool>,
    #[serde(default)]
    scope: Scope,
    network: Option<u32>,
    address: Option<String>,
    range: Option<String>,
    wait_s: Option<u64>,
    #[serde(default)]
    names: bool,
    cursor: Option<String>,
}

/// A range argument: `low-high` or one instance.
pub(super) fn parse_range(text: &str) -> Result<(u32, u32), ToolError> {
    let bad = || ToolError::invalid("range", format!("'{text}' is not low-high or an instance"));
    let (low, high) = match text.split_once('-') {
        Some((low, high)) => (
            low.parse().map_err(|_| bad())?,
            high.parse().map_err(|_| bad())?,
        ),
        None => {
            let one = text.parse().map_err(|_| bad())?;
            (one, one)
        }
    };
    if low > high || high > ObjectIdentifier::MAX_INSTANCE {
        return Err(bad());
    }
    Ok((low, high))
}

pub(super) async fn run<T: TransportPort + 'static>(
    ctx: &Ctx<T>,
    call: &Call,
    args: Args,
    raw: &Map<String, Value>,
) -> Result<Value, ToolError> {
    let range = args.range.as_deref().map(parse_range).transpose()?;
    let network = args
        .network
        .map(|n| {
            u16::try_from(n)
                .ok()
                .filter(|n| (1..=65534).contains(n))
                .ok_or_else(|| ToolError::invalid("network", "a network is 1 to 65534"))
        })
        .transpose()?;
    let first = start(ctx, NAME, args.cursor.as_deref(), raw)? as usize;
    // A later page lists what the first one found; it sends nothing. A
    // passive server sends nothing either, so it only lists.
    let send = args.send.unwrap_or(ctx.engine.is_some()) && args.cursor.is_none();
    if send && ctx.engine.is_none() {
        return Err(ToolError::refused(
            "passive",
            "the server is passive: it sends no Who-Is and only lists devices already heard, \
             which, with no client, is none; call with send false",
        ));
    }
    let wait = Duration::from_secs(args.wait_s.unwrap_or(3).clamp(1, 30));
    let scope_text = match (args.scope, network, &args.address) {
        (Scope::Local, ..) => "local".to_string(),
        (Scope::Network, Some(network), _) => format!("network {network}"),
        (Scope::Directed, _, Some(address)) => format!("directed {address}"),
        (Scope::Global, ..) => "global".to_string(),
        (Scope::Network, None, _) => {
            return Err(ToolError::invalid(
                "network",
                "scope network needs a network",
            ));
        }
        (Scope::Directed, _, None) => {
            return Err(ToolError::invalid(
                "address",
                "scope directed needs an address",
            ));
        }
    };
    if send {
        who_is(ctx, &args, network, range).await?;
        tokio::time::sleep(wait).await;
    }
    let mut devices = match &ctx.engine {
        Some(engine) => engine.devices().await,
        // Passive: no client, so nothing is known.
        None => Vec::new(),
    };
    devices.retain(|found| {
        let instance = found.object_identifier.instance_number();
        let on = found.source_network.unwrap_or(0);
        let in_scope = match args.scope {
            Scope::Local => found.is_local(),
            Scope::Network => found.source_network == network,
            Scope::Directed | Scope::Global => true,
        };
        in_scope
            && range.is_none_or(|(low, high)| (low..=high).contains(&instance))
            && ctx.policy.allows_network(on)
            && ctx.policy.allows_device(instance)
    });
    devices.sort_by_key(|found| found.object_identifier.instance_number());
    let page = ctx.policy.caps.devices;
    let cap = ctx.policy.caps.text;
    let mut rows = Vec::new();
    let mut resume = None;
    for (at, found) in devices.iter().enumerate().skip(first) {
        if at >= first + page || (args.names && at > first && call.out_of_time()) {
            resume = Some(at);
            break;
        }
        let instance = found.object_identifier.instance_number();
        let mut row = device_row(ctx, found);
        if args.names
            && let Some(engine) = &ctx.engine
        {
            let object = found.object_identifier;
            let name = engine
                .read(
                    instance,
                    object,
                    Wanted::List(&[PropertyIdentifier::OBJECT_NAME]),
                )
                .await
                .ok()
                .and_then(|read| row_text(&read.rows, PropertyIdentifier::OBJECT_NAME));
            let source = format!("device {instance} device:{instance} object-name");
            row["name"] = name.map_or(Value::Null, |name| envelope(&name, source, cap));
        }
        rows.push(row);
    }
    let mut body = json!({
        "devices": rows,
        "total": devices.len(),
        "sent": send,
        "scope": scope_text,
        "truncated": resume.is_some(),
        "next_cursor": resume.map(|at| next(ctx, NAME, raw, at as u64)),
        "limits": ctx.limits(Some(page)),
    });
    if ctx.engine.is_none() {
        body["note"] = json!("passive: nothing is sent, and only devices already heard are listed");
    }
    Ok(body)
}

/// One device of the discovery table as a result row: identifiers and
/// what its I-Am said, no device text.
pub(crate) fn device_row<T: TransportPort + 'static>(
    ctx: &Ctx<T>,
    found: &DiscoveredDevice,
) -> Value {
    let routed = found.source_network.zip(found.source_address.as_deref());
    let (address, router) = match &ctx.engine {
        Some(engine) => engine.address_style().locate(&found.mac_address, routed),
        None => (String::new(), None),
    };
    json!({
        "instance": found.object_identifier.instance_number(),
        "address": address,
        "router": router,
        "network": found.source_network.unwrap_or(0),
        "vendor_id": found.vendor_id,
        "max_apdu": found.max_apdu_length,
        "segmentation": kebab(found.segmentation_supported),
        "last_seen_s": found.last_seen.elapsed().as_secs(),
    })
}

/// Send the Who-Is the arguments ask for, if the policy allows it.
async fn who_is<T: TransportPort + 'static>(
    ctx: &Ctx<T>,
    args: &Args,
    network: Option<u16>,
    range: Option<(u32, u32)>,
) -> Result<(), ToolError> {
    let engine = ctx.engine()?;
    let scope = match (args.scope, network, &args.address) {
        (Scope::Local, ..) => {
            ctx.allow_network(0)?;
            WhoIsScope::Local
        }
        (Scope::Network, Some(network), _) => {
            ctx.allow_network(network)?;
            WhoIsScope::Network(network)
        }
        (Scope::Directed, _, Some(address)) => {
            ctx.allow_network(0)?;
            WhoIsScope::Directed(address)
        }
        (Scope::Global, ..) => {
            if ctx.policy.networks.is_some() {
                return Err(ToolError::refused(
                    "network-not-allowed",
                    "a global Who-Is reaches every network, and this server's [mcp] networks \
                     limit which it may reach",
                ));
            }
            WhoIsScope::Global
        }
        _ => unreachable!("checked by the caller"),
    };
    // A directed Who-Is's address, checked before anything is charged, and
    // whether the discovery table knows a device there.
    let known_station = match scope {
        WhoIsScope::Directed(address) => {
            let mac = engine.station_mac(address).await.map_err(|e| match e {
                bacnet_types::error::Error::Encoding(message) => {
                    ToolError::invalid("address", message)
                }
                other => ToolError::invalid("address", other.to_string()),
            })?;
            engine
                .devices()
                .await
                .iter()
                .any(|found| found.is_local() && found.mac_address.as_slice() == mac.as_slice())
        }
        _ => false,
    };
    if broad(ctx, args.scope, range, known_station)? {
        broad_allowed(ctx)?;
    }
    let range = range
        .map(|(low, high)| DeviceInstanceRange::new(low, high))
        .transpose()
        .map_err(|e| ToolError::invalid("range", e.to_string()))?;
    engine.who_is(scope, range).await.map_err(|e| match e {
        bacnet_types::error::Error::Encoding(message) => ToolError::invalid("address", message),
        other => ToolError::Failed(other.to_string()),
    })
}

/// Whether a Who-Is is broad, after refusing one that would make devices
/// outside the policy answer.
///
/// - When the policy names its devices, the range must lie inside one of
///   its ranges, whatever the scope.
/// - Broad is about traffic, so in either case a global Who-Is is broad,
///   and so is one without a range or wider than [`BROAD_WIDTH`]
///   instances. The one exception is a directed Who-Is to a station the
///   discovery table already knows as a device (`known_station`): only it
///   answers. An unknown address may be a subnet's broadcast address, which
///   every device on that subnet hears.
fn broad<T: TransportPort + 'static>(
    ctx: &Ctx<T>,
    scope: Scope,
    range: Option<(u32, u32)>,
    known_station: bool,
) -> Result<bool, ToolError> {
    if let Some(allowed) = &ctx.policy.devices {
        let inside =
            range.is_some_and(|(low, high)| allowed.iter().any(|&(l, h)| l <= low && high <= h));
        if !inside {
            return Err(ToolError::refused(
                "device-not-allowed",
                "this server's [mcp] devices limit which devices it may reach: give a range \
                 inside them",
            ));
        }
    }
    let wide = range.is_none_or(|(low, high)| high - low >= BROAD_WIDTH);
    let one_station = scope == Scope::Directed && known_station;
    Ok(scope == Scope::Global || (wide && !one_station))
}

/// A broad Who-Is: allowed by the policy, and not within a minute of the
/// last one.
fn broad_allowed<T: TransportPort + 'static>(ctx: &Ctx<T>) -> Result<(), ToolError> {
    if !ctx.policy.broad_discovery {
        return Err(ToolError::refused(
            "broad-discovery",
            "a global Who-Is, or one without a range or wider than 1000 instances, needs \
             broad_discovery = true in the profile's [mcp] table; give a narrower range, such as \
             1000-1999, instead",
        ));
    }
    let mut last = ctx.broad.lock().unwrap_or_else(PoisonError::into_inner);
    let now = Instant::now();
    if let Some(at) = *last
        && now.duration_since(at) < BROAD_INTERVAL
    {
        let wait = BROAD_INTERVAL - now.duration_since(at);
        return Err(ToolError::Refused {
            reason: "rate-limited",
            message: "one broad Who-Is a minute".into(),
            retry_after_s: Some(wait.as_secs().max(1)),
        });
    }
    *last = Some(now);
    Ok(())
}