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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! What every tool call shares: the engine, the policy, the cursors and
//! caches ([`Ctx`]), and one call's budget and cost ([`Call`]).

use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use bacnet_client::client::{DeviceEventKind, DiscoveredRouter};
use bacnet_client::discovery::DiscoveredDevice;
use bacnet_ops::engine::Engine;
use bacnet_ops::limits::{Account, TokenBucket};
use bacnet_ops::target::{self, Target};
use bacnet_ops::watch::Hub;
use bacnet_transport::port::TransportPort;
use bacnet_types::primitives::ObjectIdentifier;
use serde::Deserialize;
use serde_json::{Value, json};
use tokio::sync::broadcast::error::RecvError;
use tokio::sync::watch;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;

use crate::cursor::Cursors;
use crate::errors::ToolError;
use crate::listen::{Subs, TOOL_IDLE};
use crate::policy::Policy;
use crate::resources::Cache;

/// The server's state, shared by every call.
pub(crate) struct Ctx<T: TransportPort + 'static> {
    /// The engine; `None` in passive mode, where no client is built.
    pub(crate) engine: Option<Arc<Engine<T>>>,
    pub(crate) policy: Policy,
    /// The one principal of a stdio server: the process's OS user.
    pub(crate) principal: String,
    pub(crate) cursors: Cursors,
    /// The principal's token bucket.
    pub(crate) bucket: Arc<TokenBucket>,
    /// When the last broad Who-Is went.
    pub(crate) broad: Mutex<Option<Instant>>,
    /// Object lists read, by device, for paging without reading again.
    pub(crate) lists: Mutex<HashMap<u32, CachedList>>,
    /// One lock a device, held while its list is read, so two calls on one
    /// device take turns and the second goes on from where the first
    /// stopped.
    pub(crate) list_locks: Mutex<HashMap<u32, Arc<tokio::sync::Mutex<()>>>>,
    /// Calls and requests per device, logged as a summary at shutdown.
    pub(crate) reads: Mutex<BTreeMap<u32, (u64, u64)>>,
    /// Object names and descriptions read by `bacnet_find_object`, by
    /// device, so its later pages don't read them again.
    pub(crate) names: Mutex<HashMap<u32, CachedNames>>,
    /// What Who-Is-Router-To-Network queries found, and when the last one
    /// went; at most one goes a minute.
    pub(crate) routers: Mutex<Routers>,
    /// When the last BBMD survey of `bacnet_network_map` began; at most
    /// one goes a minute.
    pub(crate) bbmd_asked: Mutex<Option<Instant>>,
    /// The watch hub; `None` in passive mode.
    pub(crate) hub: Option<Hub<T>>,
    /// What the principal watches, and the subscriptions waiting for their
    /// streams.
    pub(crate) subs: Arc<Subs>,
    /// Cancelled when the server shuts down gracefully: listen streams
    /// complete.
    pub(crate) closing: CancellationToken,
    /// Listen streams running.
    pub(crate) listening: watch::Sender<usize>,
    /// The discovery table's devices and their networks, kept for the
    /// subscription acknowledgment, which the SDK asks for synchronously.
    pub(crate) known: Mutex<HashMap<u32, u16>>,
    /// Device summaries served to `bacnet://device/{instance}`.
    pub(crate) described: Cache,
    /// Alarm pages served to the alarm resources.
    pub(crate) alarms: Cache,
    /// The write session, the plan grant's budget and the confirmation
    /// sealer (#2068).
    pub(crate) writes: crate::writes::Writes,
    /// Writes and reverts handed to tasks of their own, which a shutdown
    /// waits for.
    pub(crate) handoffs: tokio_util::task::TaskTracker,
}

/// Counts a listen stream while it lives.
pub(crate) struct Listening<'a>(&'a watch::Sender<usize>);

impl Drop for Listening<'_> {
    fn drop(&mut self) {
        self.0.send_modify(|n| *n = n.saturating_sub(1));
    }
}

/// The routers known from Who-Is-Router-To-Network queries.
#[derive(Debug, Clone, Default)]
pub(crate) struct Routers {
    /// When the last query went: a claim taken before it is sent, which
    /// holds off the next for a minute whether or not it finished.
    pub(crate) asked: Option<Instant>,
    /// When the last query that finished went, and the routers it heard
    /// (at most [`ROUTER_CAP`]).
    pub(crate) heard: Option<(Instant, Vec<DiscoveredRouter>)>,
}

/// The most routers kept, the size of a router table.
pub(crate) const ROUTER_CAP: usize = 256;

/// One device's object names and descriptions as read.
#[derive(Debug, Clone)]
pub(crate) struct CachedNames {
    /// When the first was read; the whole entry goes after [`LIST_TTL`].
    pub(crate) at: Instant,
    pub(crate) objects: HashMap<ObjectIdentifier, Names>,
}

/// An object's name and description, kept already cleaned and capped
/// ([`crate::untrusted::clean`]): the raw device text is never stored.
#[derive(Debug, Clone, Default)]
pub(crate) struct Names {
    pub(crate) name: Option<Cleaned>,
    /// `None` until read; then the description, if the device gave one.
    pub(crate) description: Option<Option<Cleaned>>,
}

/// Device text after [`crate::untrusted::clean`], kept long enough to
/// match on, and whether that cap cut it.
#[derive(Debug, Clone)]
pub(crate) struct Cleaned {
    pub(crate) text: String,
    pub(crate) truncated: bool,
}

/// A device's Object_List as read so far.
#[derive(Debug, Clone)]
pub(crate) struct CachedList {
    /// When it was read whole, or, for one read element by element, when
    /// that read began (not when it last went on).
    pub(crate) at: Instant,
    /// The entries read, in device order.
    pub(crate) objects: Arc<Vec<ObjectIdentifier>>,
    /// Read element by element: the element count, and the next element to
    /// read (past `total` once complete).
    pub(crate) indexed: Option<(u32, u32)>,
    /// Elements the device answered with an error, skipped.
    pub(crate) gaps: Vec<u32>,
}

impl CachedList {
    /// Whether every entry has been read.
    pub(crate) fn complete(&self) -> bool {
        self.indexed.is_none_or(|(total, next)| next > total)
    }

    /// Whether it may still be used: a complete list for [`LIST_TTL`]
    /// after it was read, a partial one for [`PARTIAL_TTL`] after its read
    /// began.
    pub(crate) fn fresh(&self) -> bool {
        let ttl = if self.complete() {
            LIST_TTL
        } else {
            PARTIAL_TTL
        };
        self.at.elapsed() < ttl
    }
}

/// How long an object list read for one page serves the next ones.
pub(crate) const LIST_TTL: Duration = Duration::from_secs(300);

/// How long an element-by-element read may go on across calls before it
/// starts again.
pub(crate) const PARTIAL_TTL: Duration = Duration::from_secs(600);

/// The most devices whose lists are kept; past it the oldest goes.
pub(crate) const LIST_CAP: usize = 64;

/// One tool call's budget and tally.
pub(crate) struct Call {
    pub(crate) started: Instant,
    /// Past this, a call that pages returns what it has.
    pub(crate) deadline: Instant,
    pub(crate) account: Arc<Account>,
    timeouts: AtomicU32,
    /// The device the call reached, for the read log.
    device: Mutex<Option<u32>>,
}

impl Call {
    pub(crate) fn new(budget: Duration, account: Arc<Account>) -> Self {
        let started = Instant::now();
        Self {
            started,
            deadline: started + budget,
            account,
            timeouts: AtomicU32::new(0),
            device: Mutex::new(None),
        }
    }

    /// True once the call's budget is spent.
    pub(crate) fn out_of_time(&self) -> bool {
        Instant::now() >= self.deadline
    }

    /// Count a request that timed out.
    pub(crate) fn timed_out(&self) {
        self.timeouts.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn reached(&self, device: u32) {
        *self.device.lock().unwrap_or_else(PoisonError::into_inner) = Some(device);
    }

    pub(crate) fn device(&self) -> Option<u32> {
        *self.device.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// The `cost` block every result carries.
    pub(crate) fn cost(&self) -> Value {
        let ms = |d: Duration| u64::try_from(d.as_millis()).unwrap_or(u64::MAX);
        json!({
            "requests": self.account.requests(),
            "elapsed_ms": ms(self.started.elapsed()),
            "throttled_ms": ms(self.account.throttled()),
            "timeouts": self.timeouts.load(Ordering::Relaxed),
        })
    }
}

/// A device argument: an instance, `dnet:instance` or `dnet:0xMAC`, as a
/// string, or an instance as a number.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub(crate) enum DeviceArg {
    Number(u32),
    Text(String),
}

impl DeviceArg {
    pub(crate) fn text(&self) -> String {
        match self {
            Self::Number(n) => n.to_string(),
            Self::Text(text) => text.clone(),
        }
    }
}

/// A device the policy lets the call reach, from the discovery table.
pub(crate) struct Device {
    pub(crate) instance: u32,
    /// Its network, 0 for the local one.
    pub(crate) network: u16,
    pub(crate) entry: DiscoveredDevice,
}

const DISCOVER_HINT: &str = "call bacnet_discover first (with a range or network that includes it)";

impl<T: TransportPort + 'static> Ctx<T> {
    /// The engine, or a refusal in passive mode.
    pub(crate) fn engine(&self) -> Result<&Engine<T>, ToolError> {
        self.engine.as_deref().ok_or_else(|| {
            ToolError::refused(
                "passive",
                "the server is passive: it builds no client and sends nothing",
            )
        })
    }

    /// The device `arg` names, if the discovery table holds it and the
    /// policy allows it. Targets are numeric: a device is never found by
    /// name, and an address is no device.
    pub(crate) async fn device(&self, call: &Call, arg: &DeviceArg) -> Result<Device, ToolError> {
        let engine = self.engine()?;
        let text = arg.text();
        let unknown = || ToolError::UnknownDevice {
            device: text.clone(),
            hint: DISCOVER_HINT.into(),
        };
        let parsed = target::parse_target(&text).map_err(|e| ToolError::invalid("device", e))?;
        let entry = match parsed {
            Target::Instance(instance) => engine.device(instance).await.ok_or_else(unknown)?,
            Target::Routed { dnet, instance } => engine
                .device(instance)
                .await
                .filter(|found| found.source_network == Some(dnet))
                .ok_or_else(unknown)?,
            Target::RoutedMac { dnet, mac } => engine
                .devices()
                .await
                .into_iter()
                .find(|found| {
                    found.source_network == Some(dnet)
                        && found.source_address.as_deref() == Some(&mac[..])
                })
                .ok_or_else(unknown)?,
            Target::Mac(_) | Target::Bip6(_) => {
                return Err(ToolError::invalid(
                    "device",
                    "a device instance, dnet:instance or dnet:0xMAC, not an address",
                ));
            }
        };
        let device = Device {
            instance: entry.object_identifier.instance_number(),
            network: entry.source_network.unwrap_or(0),
            entry,
        };
        self.allow(device.instance, device.network)?;
        self.remember(&device.entry);
        call.reached(device.instance);
        Ok(device)
    }

    /// Note a device of the discovery table for the subscription check.
    pub(crate) fn remember(&self, found: &DiscoveredDevice) {
        let instance = found.object_identifier.instance_number();
        let network = found.source_network.unwrap_or(0);
        self.known
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .insert(instance, network);
    }

    /// Whether `instance` is a known device the policy lets the server
    /// reach, without waiting for the discovery table.
    pub(crate) fn reach_known(&self, instance: u32) -> bool {
        self.reach_check(instance).is_ok()
    }

    /// [`reach_known`](Self::reach_known), saying why not: the device
    /// isn't in the discovery table, or the policy leaves it out.
    pub(crate) fn reach_check(&self, instance: u32) -> Result<(), ToolError> {
        let network = self
            .known
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get(&instance)
            .copied();
        let network = network.ok_or_else(|| ToolError::UnknownDevice {
            device: instance.to_string(),
            hint: DISCOVER_HINT.into(),
        })?;
        self.allow(instance, network)
    }

    /// The known devices in reach, in order.
    pub(crate) fn known_in_reach(&self) -> Vec<u32> {
        let known = self.known.lock().unwrap_or_else(PoisonError::into_inner);
        let mut devices: Vec<u32> = known
            .iter()
            .filter(|(instance, network)| self.allow(**instance, **network).is_ok())
            .map(|(instance, _)| *instance)
            .collect();
        devices.sort_unstable();
        devices
    }

    /// Keep [`known`](Self::known) in step with the discovery table until
    /// the server closes: a snapshot, then its events (a snapshot again
    /// after a burst the receiver missed). It also lets `bacnet_watch`
    /// points go once nobody has asked for them in [`TOOL_IDLE`].
    pub(crate) async fn follow_devices(&self) {
        let Some(engine) = &self.engine else {
            return;
        };
        let mut events = engine.device_events();
        let mut resync = true;
        let mut sweep = tokio::time::interval(Duration::from_secs(60));
        loop {
            if resync {
                let devices = engine.devices().await;
                let mut known = self.known.lock().unwrap_or_else(PoisonError::into_inner);
                known.clear();
                for found in &devices {
                    known.insert(
                        found.object_identifier.instance_number(),
                        found.source_network.unwrap_or(0),
                    );
                }
                resync = false;
            }
            let event = tokio::select! {
                () = self.closing.cancelled() => return,
                event = events.recv() => event,
                _ = sweep.tick() => {
                    self.subs.expire_tool(TOOL_IDLE);
                    // A stream acknowledged but never begun (its
                    // acknowledgment failed, or the client cancelled first)
                    // leaves a reservation no later one may come to clear.
                    self.subs.prune();
                    continue;
                }
            };
            match event {
                Ok(event) if event.kind == DeviceEventKind::Lost => {
                    let instance = event.device.object_identifier.instance_number();
                    self.known
                        .lock()
                        .unwrap_or_else(PoisonError::into_inner)
                        .remove(&instance);
                }
                Ok(event) => self.remember(&event.device),
                Err(RecvError::Lagged(_)) => resync = true,
                Err(RecvError::Closed) => return,
            }
        }
    }

    /// Count a listen stream until the guard drops, unless `max` run
    /// already.
    pub(crate) fn listening(&self, max: usize) -> Option<Listening<'_>> {
        let mut taken = false;
        self.listening.send_if_modified(|n| {
            taken = *n < max;
            if taken {
                *n += 1;
            }
            taken
        });
        taken.then(|| Listening(&self.listening))
    }

    /// Refuse a network the policy leaves out.
    pub(crate) fn allow_network(&self, network: u16) -> Result<(), ToolError> {
        if self.policy.allows_network(network) {
            return Ok(());
        }
        Err(ToolError::refused(
            "network-not-allowed",
            format!("network {network} is not in this server's [mcp] networks"),
        ))
    }

    /// Refuse a device or network the policy leaves out.
    pub(crate) fn allow(&self, instance: u32, network: u16) -> Result<(), ToolError> {
        self.allow_network(network)?;
        if !self.policy.allows_device(instance) {
            return Err(ToolError::refused(
                "device-not-allowed",
                format!("device {instance} is not in this server's [mcp] devices"),
            ));
        }
        Ok(())
    }

    /// Record a finished call in the read log, as a summary line.
    pub(crate) fn log_read(&self, tool: &str, call: &Call, outcome: &str) {
        let requests = call.account.requests();
        let device = call.device();
        tracing::info!(
            target: "bacnet_mcp::reads",
            tool,
            device,
            requests,
            outcome,
            "read call"
        );
        if let Some(device) = device {
            let mut reads = self.reads.lock().unwrap_or_else(PoisonError::into_inner);
            let entry = reads.entry(device).or_default();
            entry.0 += 1;
            entry.1 += u64::from(requests);
        }
    }

    /// Log the read counts per device, at shutdown.
    pub(crate) fn log_summary(&self) {
        let reads = self.reads.lock().unwrap_or_else(PoisonError::into_inner);
        for (device, (calls, requests)) in reads.iter() {
            tracing::info!(
                target: "bacnet_mcp::reads",
                principal = %self.principal,
                device,
                calls,
                requests,
                "read summary"
            );
        }
    }

    /// The `limits` block every result carries.
    pub(crate) fn limits(&self, page: Option<usize>) -> Value {
        let rates = &self.policy.rates;
        let mut limits = json!({
            "rate_per_s": rates.global,
            "principal_rate_per_s": rates.principal,
            "per_device_outstanding": 1,
        });
        if let Some(page) = page {
            limits["page"] = json!(page);
        }
        limits
    }
}