car-connectors 0.25.0

Remote MCP connectors for the Common Agent Runtime — connect to remote MCP servers over HTTP, register their tools, and route calls through CAR's governance layer (validator, policy, eventlog).
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Connector lifecycle manager.
//!
//! Owns the set of configured remote MCP connectors, drives their
//! connect/discover/enable lifecycle, and persists configuration to
//! `~/.car/connectors.json`. It plugs discovered sessions into a shared
//! [`McpToolExecutor`] (so the engine's existing routing + fallback
//! handle dispatch) and hands enabled [`ToolEntry`]s back to the caller
//! (the daemon) to register into per-session runtimes.
//!
//! ## Enablement gating
//!
//! A freshly discovered tool is **not** routable and **not** registered
//! until the user enables it via [`enable_tools`](ConnectorManager::enable_tools).
//! This is structural, not a permission flag: a disabled tool has no
//! route in the executor and no entry in any runtime registry, so it is
//! invisible to the model and impossible to dispatch.

use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use car_engine::{McpSession, McpToolExecutor, McpToolInfo, ToolEntry};
use serde::Serialize;
use tokio::sync::{Mutex, RwLock};

use crate::error::ConnectorError;
use crate::manifest::{self, ConnectorConfig, ConnectorsFile, OAuthConfig, StdioConfig};
use crate::oauth::{self, StoredTokens};
use crate::schema;
use crate::secrets;
use crate::transport::McpHttpSession;

/// Refresh an access token this many seconds before its stated expiry.
const TOKEN_REFRESH_SKEW_SECS: u64 = 60;

/// In-flight OAuth authorization, keyed by `state` between
/// `authenticate` and `complete_authentication`. Holds the material the
/// token exchange needs so secrets never round-trip through the client.
struct PendingAuth {
    name: String,
    url: String,
    redirect_uri: String,
    verifier: String,
    client_id: String,
    client_secret: Option<String>,
    token_endpoint: String,
    resource: Option<String>,
    scopes: Vec<String>,
}

/// In-memory state for one connector.
struct ConnectorState {
    config: ConnectorConfig,
    /// Tools discovered on the last successful `tools/list`.
    discovered: Vec<McpToolInfo>,
    connected: bool,
    last_error: Option<String>,
}

/// Manages remote MCP connectors over their full lifecycle.
pub struct ConnectorManager {
    executor: Arc<McpToolExecutor>,
    path: PathBuf,
    inner: RwLock<BTreeMap<String, ConnectorState>>,
    /// Shared HTTP client for OAuth discovery / registration / token
    /// calls (the per-connector MCP transport owns its own client).
    http: reqwest::Client,
    /// In-flight OAuth flows keyed by `state`.
    pending_auth: RwLock<HashMap<String, PendingAuth>>,
}

/// Connection-level status for `connectors.list`.
#[derive(Debug, Clone, Serialize)]
pub struct ConnectorStatus {
    pub slug: String,
    pub name: String,
    pub url: String,
    pub connected: bool,
    pub tool_count: usize,
    pub enabled_count: usize,
    pub last_error: Option<String>,
}

/// One tool's view for `connectors.tools`.
#[derive(Debug, Clone, Serialize)]
pub struct ToolView {
    /// Bare server-side tool name.
    pub name: String,
    /// Canonical, model-visible name (`mcp_{slug}_{name}`).
    pub canonical: String,
    pub description: String,
    pub enabled: bool,
}

impl ConnectorManager {
    /// Build a manager backed by the default `~/.car/connectors.json`.
    pub fn new(executor: Arc<McpToolExecutor>) -> Result<Self, ConnectorError> {
        Ok(Self::with_path(executor, manifest::connectors_path()?))
    }

    /// Build a manager with an explicit manifest path (tests).
    pub fn with_path(executor: Arc<McpToolExecutor>, path: PathBuf) -> Self {
        Self {
            executor,
            path,
            inner: RwLock::new(BTreeMap::new()),
            http: reqwest::Client::new(),
            pending_auth: RwLock::new(HashMap::new()),
        }
    }

    /// Load the manifest and connect every configured connector,
    /// re-registering routes for already-enabled tools. Returns the
    /// `ToolEntry`s for all enabled tools so the caller can seed
    /// existing runtimes. Connection failures are recorded per
    /// connector (surfaced via [`list`](Self::list)) and do not abort
    /// the others.
    pub async fn load_and_connect(&self) -> Result<Vec<ToolEntry>, ConnectorError> {
        let file = manifest::load_from(&self.path)?;
        let mut entries = Vec::new();
        for config in file.connectors {
            let slug = config.slug.clone();
            self.connect_one(config).await;
            entries.extend(self.route_and_collect_enabled(&slug).await);
        }

        // Team-shareable, secret-free defs from the nearest
        // `.car/connectors.toml` (project scope). Connect any not
        // already defined by the user manifest (by slug); these are not
        // persisted back to the user manifest until the user interacts
        // with them (e.g. enabling a tool).
        for config in manifest::load_team_connectors() {
            let slug = config.slug.clone();
            if self.inner.read().await.contains_key(&slug) {
                continue;
            }
            self.connect_one(config).await;
            entries.extend(self.route_and_collect_enabled(&slug).await);
        }
        Ok(entries)
    }

    /// Add a new connector: store secret headers in the keychain,
    /// persist the config, connect, and discover tools. No tools are
    /// enabled yet — call [`enable_tools`](Self::enable_tools) next.
    pub async fn add(
        &self,
        name: &str,
        url: &str,
        secret_headers: Vec<(String, String)>,
    ) -> Result<ConnectorStatus, ConnectorError> {
        let slug = self.unique_slug(&schema::slugify(name)).await;

        let mut header_names = Vec::new();
        for (h, v) in &secret_headers {
            secrets::put_header_secret(&slug, h, v)?;
            header_names.push(h.clone());
        }

        let config = ConnectorConfig {
            slug: slug.clone(),
            name: name.to_string(),
            url: url.to_string(),
            stdio: None,
            secret_headers: header_names,
            oauth: None,
            enabled_tools: Vec::new(),
        };

        self.connect_one(config).await;
        self.persist().await?;
        self.status(&slug)
            .await
            .ok_or_else(|| ConnectorError::NotFound(slug))
    }

    /// Add a local **stdio** connector: launch `command` (with `args` /
    /// `env`) as an MCP server over stdin/stdout, discover its tools.
    /// Reuses `car_engine::McpServer`, so the only difference from a
    /// remote connector is the transport.
    pub async fn add_stdio(
        &self,
        name: &str,
        command: &str,
        args: Vec<String>,
        env: std::collections::BTreeMap<String, String>,
    ) -> Result<ConnectorStatus, ConnectorError> {
        let slug = self.unique_slug(&schema::slugify(name)).await;
        let config = ConnectorConfig {
            slug: slug.clone(),
            name: name.to_string(),
            url: String::new(),
            stdio: Some(StdioConfig {
                command: command.to_string(),
                args,
                env,
            }),
            secret_headers: Vec::new(),
            oauth: None,
            enabled_tools: Vec::new(),
        };
        self.connect_one(config).await;
        self.persist().await?;
        self.status(&slug)
            .await
            .ok_or_else(|| ConnectorError::NotFound(slug))
    }

    /// Begin an OAuth 2.1 authorization for a remote MCP server. Runs
    /// discovery (Protected Resource + Authorization Server metadata)
    /// and Dynamic Client Registration, then builds a PKCE authorize
    /// URL. The pending flow is held under the returned `state`; the
    /// GUI drives the browser leg with `redirect_uri` (its own callback)
    /// and finishes via [`complete_authentication`](Self::complete_authentication).
    /// Returns `(authorize_url, state)`.
    pub async fn authenticate(
        &self,
        name: &str,
        url: &str,
        redirect_uri: &str,
    ) -> Result<(String, String), ConnectorError> {
        let (resource, asm) = oauth::discover(&self.http, url).await?;
        let reg = oauth::register_client(&self.http, &asm, redirect_uri, name).await?;

        let verifier = car_auth::pkce_verifier();
        let state = car_auth::new_state();
        let challenge = car_auth::pkce_challenge(&verifier);
        let authorize_url = oauth::authorization_url(
            &asm,
            &reg.client_id,
            redirect_uri,
            &state,
            &challenge,
            resource.as_deref(),
            &asm.scopes_supported,
        )?;

        let pending = PendingAuth {
            name: name.to_string(),
            url: url.to_string(),
            redirect_uri: redirect_uri.to_string(),
            verifier,
            client_id: reg.client_id,
            client_secret: reg.client_secret,
            token_endpoint: asm.token_endpoint,
            resource,
            scopes: asm.scopes_supported,
        };
        self.pending_auth
            .write()
            .await
            .insert(state.clone(), pending);
        Ok((authorize_url, state))
    }

    /// Complete an OAuth flow: exchange `code` for tokens, persist them
    /// (+ any client secret) to the keychain, save the connector config,
    /// connect, and discover tools. `state` must match a prior
    /// [`authenticate`](Self::authenticate).
    pub async fn complete_authentication(
        &self,
        state: &str,
        code: &str,
    ) -> Result<ConnectorStatus, ConnectorError> {
        let pending = self
            .pending_auth
            .write()
            .await
            .remove(state)
            .ok_or_else(|| ConnectorError::Protocol("unknown or expired auth state".into()))?;

        let token = oauth::exchange_code(
            &self.http,
            &pending.token_endpoint,
            &pending.client_id,
            pending.client_secret.as_deref(),
            &pending.redirect_uri,
            code,
            &pending.verifier,
            pending.resource.as_deref(),
        )
        .await?;

        let slug = self.unique_slug(&schema::slugify(&pending.name)).await;
        let stored = StoredTokens::from_response(token, now_unix(), None);
        secrets::put_tokens(&slug, &stored)?;
        let has_client_secret = pending.client_secret.is_some();
        if let Some(secret) = &pending.client_secret {
            secrets::put_client_secret(&slug, secret)?;
        }

        let config = ConnectorConfig {
            slug: slug.clone(),
            name: pending.name,
            url: pending.url,
            stdio: None,
            secret_headers: Vec::new(),
            oauth: Some(OAuthConfig {
                token_endpoint: pending.token_endpoint,
                client_id: pending.client_id,
                has_client_secret,
                resource: pending.resource,
                scopes: pending.scopes,
            }),
            enabled_tools: Vec::new(),
        };

        self.connect_one(config).await;
        self.persist().await?;
        self.status(&slug)
            .await
            .ok_or_else(|| ConnectorError::NotFound(slug))
    }

    /// Disconnect a connector, drop its routes, delete its keychain
    /// secrets, and remove it from the manifest. Returns the canonical
    /// names of its enabled tools so the caller can unregister them from
    /// runtimes.
    pub async fn remove(&self, slug: &str) -> Result<Vec<String>, ConnectorError> {
        let state = {
            let mut inner = self.inner.write().await;
            inner.remove(slug)
        };
        let state = state.ok_or_else(|| ConnectorError::NotFound(slug.to_string()))?;
        self.executor.remove_server(slug).await;
        for h in &state.config.secret_headers {
            let _ = secrets::delete_header_secret(slug, h);
        }
        if state.config.oauth.is_some() {
            let _ = secrets::delete_tokens(slug);
            let _ = secrets::delete_client_secret(slug);
        }
        let canonicals: Vec<String> = state
            .config
            .enabled_tools
            .iter()
            .map(|t| schema::canonical_tool_name(slug, t))
            .collect();
        self.persist().await?;
        Ok(canonicals)
    }

    /// List all configured connectors with connection status.
    pub async fn list(&self) -> Vec<ConnectorStatus> {
        let inner = self.inner.read().await;
        inner.values().map(state_status).collect()
    }

    /// List the tools discovered for one connector, flagged by whether
    /// each is currently enabled.
    pub async fn tools(&self, slug: &str) -> Result<Vec<ToolView>, ConnectorError> {
        let inner = self.inner.read().await;
        let state = inner
            .get(slug)
            .ok_or_else(|| ConnectorError::NotFound(slug.to_string()))?;
        Ok(state
            .discovered
            .iter()
            .map(|t| ToolView {
                name: t.name.clone(),
                canonical: schema::canonical_tool_name(slug, &t.name),
                description: t.description.clone().unwrap_or_default(),
                enabled: state.config.enabled_tools.contains(&t.name),
            })
            .collect())
    }

    /// Enable a set of (bare, server-side) tool names for a connector:
    /// add their executor routes, persist, and return the `ToolEntry`s
    /// for the caller to register into runtimes. Unknown tool names are
    /// ignored. Returns only entries for tools not already enabled.
    pub async fn enable_tools(
        &self,
        slug: &str,
        tools: &[String],
    ) -> Result<Vec<ToolEntry>, ConnectorError> {
        {
            let mut inner = self.inner.write().await;
            let state = inner
                .get_mut(slug)
                .ok_or_else(|| ConnectorError::NotFound(slug.to_string()))?;
            for t in tools {
                if !state.config.enabled_tools.contains(t) {
                    state.config.enabled_tools.push(t.clone());
                }
            }
        }
        self.persist().await?;
        Ok(self.route_and_collect_enabled(slug).await)
    }

    /// Disable a set of (bare, server-side) tool names for a connector:
    /// drop their executor routes, persist, and return the canonical
    /// (model-visible) names that were actually enabled, so the caller
    /// can unregister them from runtimes. Tools that weren't enabled are
    /// ignored.
    pub async fn disable_tools(
        &self,
        slug: &str,
        tools: &[String],
    ) -> Result<Vec<String>, ConnectorError> {
        let canonicals = {
            let mut inner = self.inner.write().await;
            let state = inner
                .get_mut(slug)
                .ok_or_else(|| ConnectorError::NotFound(slug.to_string()))?;
            let disabled: Vec<String> = tools
                .iter()
                .filter(|t| state.config.enabled_tools.contains(t))
                .cloned()
                .collect();
            state.config.enabled_tools.retain(|t| !tools.contains(t));
            disabled
                .iter()
                .map(|t| schema::canonical_tool_name(slug, t))
                .collect::<Vec<_>>()
        };
        for canonical in &canonicals {
            self.executor.remove_route(canonical).await;
        }
        self.persist().await?;
        Ok(canonicals)
    }

    /// Re-run `tools/list` for a connector (e.g. after the server's
    /// tool set changed) and return the refreshed views.
    pub async fn refresh(&self, slug: &str) -> Result<Vec<ToolView>, ConnectorError> {
        let config = {
            let inner = self.inner.read().await;
            inner
                .get(slug)
                .ok_or_else(|| ConnectorError::NotFound(slug.to_string()))?
                .config
                .clone()
        };
        self.connect_one(config).await;
        // Re-route any still-enabled tools that survived the refresh.
        let _ = self.route_and_collect_enabled(slug).await;
        self.tools(slug).await
    }

    /// All enabled tools across every connector, as `ToolEntry`s — used
    /// to seed a newly created session's runtime registry.
    pub async fn enabled_tool_entries(&self) -> Vec<ToolEntry> {
        let inner = self.inner.read().await;
        let mut entries = Vec::new();
        for state in inner.values() {
            for tool in &state.discovered {
                if state.config.enabled_tools.contains(&tool.name) {
                    entries.push(schema::tool_entry(&state.config.slug, tool));
                }
            }
        }
        entries
    }

    // ---- internals ---------------------------------------------------

    /// Connect a connector and discover its tools, updating in-memory
    /// state. On any failure the connector is still recorded (so it
    /// shows up in `list` with `last_error`).
    async fn connect_one(&self, config: ConnectorConfig) {
        let slug = config.slug.clone();
        let (discovered, connected, last_error) = self.dial(&config).await;
        let mut inner = self.inner.write().await;
        inner.insert(
            slug,
            ConnectorState {
                config,
                discovered,
                connected,
                last_error,
            },
        );
    }

    /// Resolve secret headers, open the HTTP session, initialize, and
    /// list tools. Registers the live session with the executor on a
    /// successful handshake.
    async fn dial(&self, config: &ConnectorConfig) -> (Vec<McpToolInfo>, bool, Option<String>) {
        if let Some(stdio) = &config.stdio {
            return self.dial_stdio(config, stdio).await;
        }

        let headers = match self.resolve_auth_headers(config).await {
            Ok(h) => h,
            Err(e) => return (vec![], false, Some(e)),
        };

        let mut session = match McpHttpSession::new(&config.slug, &config.url, headers) {
            Ok(s) => s,
            Err(e) => return (vec![], false, Some(e.to_string())),
        };
        if let Err(e) = session.initialize().await {
            return (vec![], false, Some(format!("initialize: {e}")));
        }
        let tools = session.list_tools().await;
        let arc: Arc<Mutex<dyn McpSession>> = Arc::new(Mutex::new(session));
        self.executor.add_session(config.slug.clone(), arc).await;
        match tools {
            Ok(tools) => (tools, true, None),
            Err(e) => (vec![], true, Some(format!("tools/list: {e}"))),
        }
    }

    /// Resolve the auth headers to send on this connector's MCP
    /// requests. Static `secret_headers` are read from the keychain; an
    /// OAuth connector contributes `Authorization: Bearer <token>`,
    /// refreshing first if the access token is expired.
    async fn resolve_auth_headers(
        &self,
        config: &ConnectorConfig,
    ) -> Result<Vec<(String, String)>, String> {
        let mut headers = Vec::new();
        for h in &config.secret_headers {
            let v = secrets::get_header_secret(&config.slug, h)
                .map_err(|e| format!("missing secret '{h}': {e}"))?;
            headers.push((h.clone(), v));
        }
        if let Some(oauth_cfg) = &config.oauth {
            let token = self
                .valid_access_token(&config.slug, oauth_cfg)
                .await
                .map_err(|e| format!("oauth: {e}"))?;
            headers.push(("Authorization".to_string(), format!("Bearer {token}")));
        }
        Ok(headers)
    }

    /// Return a non-expired access token for an OAuth connector,
    /// refreshing and re-persisting if needed.
    async fn valid_access_token(
        &self,
        slug: &str,
        cfg: &OAuthConfig,
    ) -> Result<String, ConnectorError> {
        let tokens = secrets::get_tokens(slug)?;
        if !tokens.is_expired(now_unix(), TOKEN_REFRESH_SKEW_SECS) {
            return Ok(tokens.access_token);
        }
        let Some(refresh_token) = tokens.refresh_token.clone() else {
            // Expired and nothing to refresh with — surface so the GUI
            // can prompt re-authentication.
            return Err(ConnectorError::Protocol(
                "access token expired and no refresh token; re-authenticate".into(),
            ));
        };
        let client_secret = if cfg.has_client_secret {
            secrets::get_client_secret(slug).ok()
        } else {
            None
        };
        let resp = oauth::refresh(
            &self.http,
            &cfg.token_endpoint,
            &cfg.client_id,
            client_secret.as_deref(),
            &refresh_token,
            cfg.resource.as_deref(),
        )
        .await?;
        let refreshed = StoredTokens::from_response(resp, now_unix(), Some(refresh_token));
        secrets::put_tokens(slug, &refreshed)?;
        Ok(refreshed.access_token)
    }

    /// Launch a local stdio MCP server and discover its tools, reusing
    /// `car_engine::McpServer` (which implements `McpSession`).
    async fn dial_stdio(
        &self,
        config: &ConnectorConfig,
        stdio: &StdioConfig,
    ) -> (Vec<McpToolInfo>, bool, Option<String>) {
        let server_cfg = car_engine::McpServerConfig {
            name: config.slug.clone(),
            command: stdio.command.clone(),
            args: stdio.args.clone(),
            env: stdio.env.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
            cwd: None,
        };
        let mut server = match car_engine::McpServer::start(server_cfg).await {
            Ok(s) => s,
            Err(e) => return (vec![], false, Some(e)),
        };
        let tools = server.list_tools().await;
        let session: Arc<Mutex<dyn McpSession>> = Arc::new(Mutex::new(server));
        self.executor.add_session(config.slug.clone(), session).await;
        match tools {
            Ok(tools) => (tools, true, None),
            Err(e) => (vec![], true, Some(format!("tools/list: {e}"))),
        }
    }

    /// Add executor routes for the connector's enabled+discovered tools
    /// and return their `ToolEntry`s.
    async fn route_and_collect_enabled(&self, slug: &str) -> Vec<ToolEntry> {
        let inner = self.inner.read().await;
        let Some(state) = inner.get(slug) else {
            return Vec::new();
        };
        let mut entries = Vec::new();
        for tool in &state.discovered {
            if state.config.enabled_tools.contains(&tool.name) {
                let canonical = schema::canonical_tool_name(slug, &tool.name);
                self.executor.set_route(canonical, slug.to_string()).await;
                entries.push(schema::tool_entry(slug, tool));
            }
        }
        entries
    }

    async fn status(&self, slug: &str) -> Option<ConnectorStatus> {
        let inner = self.inner.read().await;
        inner.get(slug).map(state_status)
    }

    /// Produce a slug not already in use, appending `-2`, `-3`, … on
    /// collision.
    async fn unique_slug(&self, base: &str) -> String {
        let inner = self.inner.read().await;
        if !inner.contains_key(base) {
            return base.to_string();
        }
        let mut n = 2;
        loop {
            let candidate = format!("{base}_{n}");
            if !inner.contains_key(&candidate) {
                return candidate;
            }
            n += 1;
        }
    }

    /// Write all current configs back to `connectors.json`.
    async fn persist(&self) -> Result<(), ConnectorError> {
        let inner = self.inner.read().await;
        let file = ConnectorsFile {
            connectors: inner.values().map(|s| s.config.clone()).collect(),
        };
        manifest::save_to(&self.path, &file)
    }
}

/// Current Unix time in seconds (0 if the clock is before the epoch,
/// which can't happen on a sane host).
fn now_unix() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn state_status(state: &ConnectorState) -> ConnectorStatus {
    ConnectorStatus {
        slug: state.config.slug.clone(),
        name: state.config.name.clone(),
        url: state.config.url.clone(),
        connected: state.connected,
        tool_count: state.discovered.len(),
        enabled_count: state.config.enabled_tools.len(),
        last_error: state.last_error.clone(),
    }
}