-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.rs
More file actions
401 lines (371 loc) · 12.9 KB
/
Copy pathstate.rs
File metadata and controls
401 lines (371 loc) · 12.9 KB
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
//! Persistent state store.
//!
//! [`StateStore`] owns everything mcp2cli persists between runs:
//!
//! - **Discovery inventory** — the last-seen list of tools, resources,
//! resource templates, and prompts for each named config. Powers
//! the dynamic CLI without re-querying the server on every
//! invocation; invalidated by
//! `notifications/tools/list_changed` / `.../resources/list_changed`
//! / `.../prompts/list_changed`.
//! - **Negotiated capability snapshot** — what the server advertised
//! during the last `initialize`. Used by `mcp2cli doctor` and by
//! dispatch logic that needs to skip unsupported operations early.
//! - **Auth session records** — long-lived credentials (e.g. OAuth
//! authorization-code flows) tied to a config. Bearer tokens
//! themselves live in [`crate::runtime::TokenStore`]; this store
//! only tracks session metadata (state, last refresh, scopes).
//! - **Job records** — for `--background` invocations,
//! [`JobRecord`]s persist job id, server-side task id, status,
//! start/update timestamps, and final result so that
//! `jobs list/show/wait/cancel/watch` work across invocations.
//! Records are retained for the lifetime of the state file; there
//! is no automatic TTL eviction today.
//!
//! Storage is simple JSON files under the runtime data dir
//! ([`crate::config::RuntimeLayout`]). Writes are serialised through
//! an in-process `Mutex` so concurrent commands within a single
//! process don't race; cross-process writes rely on file locking
//! implicit in atomic rename.
use std::{collections::BTreeMap, path::PathBuf};
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::{fs, sync::Mutex};
use uuid::Uuid;
use crate::mcp::model::DiscoveryCategory;
use crate::mcp::protocol::{McpClientSession, PeerInfo, ServerCapabilities};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AuthSessionState {
Authenticated,
LoggedOut,
}
impl AuthSessionState {
pub fn as_str(&self) -> &'static str {
match self {
Self::Authenticated => "authenticated",
Self::LoggedOut => "logged_out",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AuthSessionRecord {
pub config_name: String,
pub app_id: String,
pub state: AuthSessionState,
pub account: Option<String>,
pub server: Option<String>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum JobStatus {
Queued,
Running,
Completed,
Canceled,
Failed,
}
impl JobStatus {
pub fn as_str(&self) -> &'static str {
match self {
Self::Queued => "queued",
Self::Running => "running",
Self::Completed => "completed",
Self::Canceled => "canceled",
Self::Failed => "failed",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobRecord {
pub job_id: String,
pub config_name: String,
pub app_id: String,
pub command: String,
pub status: JobStatus,
pub detail: Option<String>,
pub remote_task_id: Option<String>,
pub result: Option<Value>,
pub failure_reason: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NegotiatedCapabilityView {
pub config_name: String,
pub app_id: String,
pub protocol_version: String,
pub session_id: Option<String>,
pub server_info: Option<PeerInfo>,
pub server_capabilities: ServerCapabilities,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DiscoveryInventoryView {
pub config_name: String,
pub app_id: String,
pub tools: Option<Vec<Value>>,
pub resources: Option<Vec<Value>>,
pub resource_templates: Option<Vec<Value>>,
pub prompts: Option<Vec<Value>>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct PersistedState {
#[serde(default)]
auth_sessions: BTreeMap<String, AuthSessionRecord>,
#[serde(default)]
negotiated_capabilities: BTreeMap<String, NegotiatedCapabilityView>,
#[serde(default)]
discovery_inventory: BTreeMap<String, DiscoveryInventoryView>,
#[serde(default)]
jobs: Vec<JobRecord>,
}
pub struct StateStore {
path: PathBuf,
state: Mutex<PersistedState>,
}
impl StateStore {
pub async fn load(path: PathBuf) -> Result<Self> {
let state = if path.exists() {
let bytes = fs::read(&path)
.await
.with_context(|| format!("failed to read state file: {}", path.display()))?;
serde_json::from_slice(&bytes)
.with_context(|| format!("failed to parse state file: {}", path.display()))?
} else {
PersistedState::default()
};
Ok(Self {
path,
state: Mutex::new(state),
})
}
pub async fn auth_session(&self, config_name: &str) -> Option<AuthSessionRecord> {
self.state
.lock()
.await
.auth_sessions
.get(config_name)
.cloned()
}
pub async fn upsert_auth_session(&self, record: AuthSessionRecord) -> Result<()> {
let bytes = {
let mut state = self.state.lock().await;
state
.auth_sessions
.insert(record.config_name.clone(), record);
serde_json::to_vec_pretty(&*state).context("failed to serialize auth state")?
};
self.persist_bytes(bytes).await
}
pub async fn negotiated_capability_view(
&self,
config_name: &str,
) -> Option<NegotiatedCapabilityView> {
self.state
.lock()
.await
.negotiated_capabilities
.get(config_name)
.cloned()
}
pub async fn upsert_negotiated_capability_view(
&self,
config_name: &str,
app_id: &str,
session: &McpClientSession,
) -> Result<()> {
let Some(server_capabilities) = session.server_capabilities.clone() else {
return Ok(());
};
let record = NegotiatedCapabilityView {
config_name: config_name.to_owned(),
app_id: app_id.to_owned(),
protocol_version: session.protocol_version.clone(),
session_id: session.session_id.clone(),
server_info: session.server_info.clone(),
server_capabilities,
updated_at: Utc::now(),
};
let bytes = {
let mut state = self.state.lock().await;
state
.negotiated_capabilities
.insert(config_name.to_owned(), record);
serde_json::to_vec_pretty(&*state)
.context("failed to serialize negotiated capability state")?
};
self.persist_bytes(bytes).await
}
pub async fn discovery_inventory_view(
&self,
config_name: &str,
) -> Option<DiscoveryInventoryView> {
self.state
.lock()
.await
.discovery_inventory
.get(config_name)
.cloned()
}
pub async fn upsert_discovery_inventory(
&self,
config_name: &str,
app_id: &str,
category: DiscoveryCategory,
items: Vec<Value>,
) -> Result<()> {
let bytes = {
let mut state = self.state.lock().await;
let entry = state
.discovery_inventory
.entry(config_name.to_owned())
.or_insert_with(|| DiscoveryInventoryView {
config_name: config_name.to_owned(),
app_id: app_id.to_owned(),
tools: None,
resources: None,
resource_templates: None,
prompts: None,
updated_at: Utc::now(),
});
entry.app_id = app_id.to_owned();
entry.updated_at = Utc::now();
match category {
DiscoveryCategory::Capabilities => entry.tools = Some(items),
DiscoveryCategory::Resources => {
// Separate concrete resources from resource templates
let mut concrete = Vec::new();
let mut templates = Vec::new();
for item in items {
if item.get("kind").and_then(Value::as_str) == Some("resource_template")
|| item.get("uriTemplate").is_some()
{
templates.push(item);
} else {
concrete.push(item);
}
}
entry.resources = Some(concrete);
if !templates.is_empty() {
entry.resource_templates = Some(templates);
}
}
DiscoveryCategory::Prompts => entry.prompts = Some(items),
}
serde_json::to_vec_pretty(&*state)
.context("failed to serialize discovery inventory state")?
};
self.persist_bytes(bytes).await
}
pub async fn create_job(
&self,
config_name: &str,
app_id: &str,
command: &str,
detail: Option<String>,
remote_task_id: Option<String>,
) -> Result<JobRecord> {
let job = JobRecord {
job_id: Uuid::new_v4().to_string(),
config_name: config_name.to_owned(),
app_id: app_id.to_owned(),
command: command.to_owned(),
status: JobStatus::Queued,
detail,
remote_task_id,
result: None,
failure_reason: None,
created_at: Utc::now(),
updated_at: Utc::now(),
};
let bytes = {
let mut state = self.state.lock().await;
state.jobs.push(job.clone());
serde_json::to_vec_pretty(&*state).context("failed to serialize job state")?
};
self.persist_bytes(bytes).await?;
Ok(job)
}
pub async fn jobs_for_config(&self, config_name: &str) -> Vec<JobRecord> {
let mut jobs = self
.state
.lock()
.await
.jobs
.iter()
.filter(|job| job.config_name == config_name)
.cloned()
.collect::<Vec<_>>();
jobs.sort_by(|left, right| right.created_at.cmp(&left.created_at));
jobs
}
pub async fn job_for_config(&self, config_name: &str, job_id: &str) -> Option<JobRecord> {
self.state
.lock()
.await
.jobs
.iter()
.find(|job| job.config_name == config_name && job.job_id == job_id)
.cloned()
}
pub async fn latest_job_for_config(
&self,
config_name: &str,
command: Option<&str>,
) -> Option<JobRecord> {
self.jobs_for_config(config_name)
.await
.into_iter()
.find(|job| command.map(|value| job.command == value).unwrap_or(true))
}
pub async fn update_job_status(
&self,
config_name: &str,
job_id: &str,
status: JobStatus,
detail: Option<String>,
result: Option<Value>,
failure_reason: Option<String>,
) -> Result<JobRecord> {
let updated = {
let mut state = self.state.lock().await;
let job = state
.jobs
.iter_mut()
.find(|job| job.config_name == config_name && job.job_id == job_id)
.with_context(|| {
format!("job '{}' not found for config '{}'", job_id, config_name)
})?;
job.status = status;
if detail.is_some() {
job.detail = detail;
}
if result.is_some() {
job.result = result;
}
job.failure_reason = failure_reason;
job.updated_at = Utc::now();
let snapshot = job.clone();
let bytes =
serde_json::to_vec_pretty(&*state).context("failed to serialize job state")?;
(snapshot, bytes)
};
self.persist_bytes(updated.1).await?;
Ok(updated.0)
}
async fn persist_bytes(&self, bytes: Vec<u8>) -> Result<()> {
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent).await.with_context(|| {
format!("failed to create state directory: {}", parent.display())
})?;
}
fs::write(&self.path, bytes)
.await
.with_context(|| format!("failed to write state file: {}", self.path.display()))
}
}