-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclient.rs
More file actions
355 lines (311 loc) · 10.3 KB
/
Copy pathclient.rs
File metadata and controls
355 lines (311 loc) · 10.3 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
//! Client management commands
//!
//! IPC commands for managing AI clients (Cursor, VS Code, etc.).
use mcpmux_core::{Client, ConnectionMode};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tauri::State;
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::commands::gateway::GatewayAppState;
use crate::state::AppState;
/// Response for client listing
#[derive(Debug, Serialize)]
pub struct ClientResponse {
pub id: String,
pub name: String,
pub client_type: String,
pub connection_mode: String,
pub locked_space_id: Option<String>,
pub grants: HashMap<String, Vec<String>>,
pub last_seen: Option<String>,
}
impl From<Client> for ClientResponse {
fn from(c: Client) -> Self {
let (mode, locked_id) = match &c.connection_mode {
ConnectionMode::Locked { space_id } => {
("locked".to_string(), Some(space_id.to_string()))
}
ConnectionMode::FollowActive => ("follow_active".to_string(), None),
ConnectionMode::AskOnChange { .. } => ("ask_on_change".to_string(), None),
};
let grants: HashMap<String, Vec<String>> = c
.grants
.iter()
.map(|(k, v)| (k.to_string(), v.iter().map(|u| u.to_string()).collect()))
.collect();
Self {
id: c.id.to_string(),
name: c.name,
client_type: c.client_type,
connection_mode: mode,
locked_space_id: locked_id,
grants,
last_seen: c.last_seen.map(|dt| dt.to_rfc3339()),
}
}
}
/// Input for creating a client
#[derive(Debug, Deserialize)]
pub struct CreateClientInput {
pub name: String,
pub client_type: String,
pub connection_mode: String,
pub locked_space_id: Option<String>,
}
/// Input for updating client grants
#[derive(Debug, Deserialize)]
pub struct UpdateGrantsInput {
pub space_id: String,
pub feature_set_ids: Vec<String>,
}
/// List all clients.
#[tauri::command]
pub async fn list_clients(state: State<'_, AppState>) -> Result<Vec<ClientResponse>, String> {
let clients = state
.client_repository
.list()
.await
.map_err(|e| e.to_string())?;
Ok(clients.into_iter().map(Into::into).collect())
}
/// Get a client by ID.
#[tauri::command]
pub async fn get_client(
id: String,
state: State<'_, AppState>,
) -> Result<Option<ClientResponse>, String> {
let uuid = Uuid::parse_str(&id).map_err(|e| e.to_string())?;
let client = state
.client_repository
.get(&uuid)
.await
.map_err(|e| e.to_string())?;
Ok(client.map(Into::into))
}
/// Create a new client.
#[tauri::command]
pub async fn create_client(
input: CreateClientInput,
state: State<'_, AppState>,
) -> Result<ClientResponse, String> {
let connection_mode = match input.connection_mode.as_str() {
"locked" => {
let space_id = input
.locked_space_id
.ok_or("locked_space_id required for locked mode")?;
let uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?;
ConnectionMode::Locked { space_id: uuid }
}
"ask_on_change" => ConnectionMode::AskOnChange { triggers: vec![] },
_ => ConnectionMode::FollowActive,
};
let mut client = Client::new(&input.name, &input.client_type);
client.connection_mode = connection_mode;
state
.client_repository
.create(&client)
.await
.map_err(|e| e.to_string())?;
Ok(client.into())
}
/// Delete a client.
#[tauri::command]
pub async fn delete_client(id: String, state: State<'_, AppState>) -> Result<(), String> {
let uuid = Uuid::parse_str(&id).map_err(|e| e.to_string())?;
state
.client_repository
.delete(&uuid)
.await
.map_err(|e| e.to_string())
}
/// Update client grants for a specific space (using client_grants table).
#[tauri::command]
pub async fn update_client_grants(
client_id: String,
input: UpdateGrantsInput,
state: State<'_, AppState>,
) -> Result<ClientResponse, String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
// Verify client exists
let client = state
.client_repository
.get(&client_uuid)
.await
.map_err(|e| e.to_string())?
.ok_or("Client not found")?;
// Update grants using the client_grants table
state
.client_repository
.set_grants_for_space(&client_uuid, &input.space_id, &input.feature_set_ids)
.await
.map_err(|e| e.to_string())?;
Ok(client.into())
}
/// Get effective grants for a specific client and space.
/// This includes explicit grants PLUS the default feature set (merged as a set).
#[tauri::command]
pub async fn get_client_grants(
client_id: String,
space_id: String,
state: State<'_, AppState>,
) -> Result<Vec<String>, String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
// Get effective grants (explicit + default, deduplicated)
state
.client_service
.get_effective_grants(&client_uuid, &space_id)
.await
.map_err(|e| e.to_string())
}
/// Get all grants for a client across all spaces.
#[tauri::command]
pub async fn get_all_client_grants(
client_id: String,
state: State<'_, AppState>,
) -> Result<HashMap<String, Vec<String>>, String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
state
.client_repository
.get_all_grants(&client_uuid)
.await
.map_err(|e| e.to_string())
}
/// Grant a specific feature set to a client.
///
/// Emits MCP list_changed notifications to connected clients.
#[tauri::command]
pub async fn grant_feature_set_to_client(
client_id: String,
space_id: String,
feature_set_id: String,
state: State<'_, AppState>,
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<(), String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
let space_uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?;
// Grant the feature set
state
.client_repository
.grant_feature_set(&client_uuid, &space_id, &feature_set_id)
.await
.map_err(|e| e.to_string())?;
// Emit notifications if gateway is running
let gw_state = gateway_state.read().await;
if let Some(ref emitter) = gw_state.event_emitter {
emitter.emit_all_changed_for_space(space_uuid);
}
Ok(())
}
/// Revoke a specific feature set from a client.
///
/// Emits MCP list_changed notifications to connected clients.
#[tauri::command]
pub async fn revoke_feature_set_from_client(
client_id: String,
space_id: String,
feature_set_id: String,
state: State<'_, AppState>,
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<(), String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
let space_uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?;
// Revoke the feature set
state
.client_repository
.revoke_feature_set(&client_uuid, &space_id, &feature_set_id)
.await
.map_err(|e| e.to_string())?;
// Emit notifications if gateway is running
let gw_state = gateway_state.read().await;
if let Some(ref emitter) = gw_state.event_emitter {
emitter.emit_all_changed_for_space(space_uuid);
}
Ok(())
}
/// Update client connection mode.
///
/// Emits MCP list_changed notifications when the client's effective space changes.
#[tauri::command]
pub async fn update_client_mode(
client_id: String,
mode: String,
locked_space_id: Option<String>,
state: State<'_, AppState>,
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<ClientResponse, String> {
let client_uuid = Uuid::parse_str(&client_id).map_err(|e| e.to_string())?;
let mut client = state
.client_repository
.get(&client_uuid)
.await
.map_err(|e| e.to_string())?
.ok_or("Client not found")?;
client.connection_mode = match mode.as_str() {
"locked" => {
let space_id = locked_space_id.ok_or("locked_space_id required for locked mode")?;
let uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?;
ConnectionMode::Locked { space_id: uuid }
}
"ask_on_change" => ConnectionMode::AskOnChange { triggers: vec![] },
_ => ConnectionMode::FollowActive,
};
client.updated_at = chrono::Utc::now();
state
.client_repository
.update(&client)
.await
.map_err(|e| e.to_string())?;
// Emit notifications for the space this client is now using
let gw_state = gateway_state.read().await;
if let Some(emitter) = &gw_state.event_emitter {
match &client.connection_mode {
ConnectionMode::Locked { space_id } => {
emitter.emit_all_changed_for_space(*space_id);
}
_ => {
// For follow_active or ask_on_change, notifications will be sent
// when the client reconnects and resolves its space
}
}
}
Ok(client.into())
}
/// Create preset clients (Cursor, VS Code, Claude Desktop).
#[tauri::command]
pub async fn init_preset_clients(state: State<'_, AppState>) -> Result<(), String> {
let existing = state
.client_repository
.list()
.await
.map_err(|e| e.to_string())?;
// Create Cursor if not exists
if !existing.iter().any(|c| c.client_type == "cursor") {
let cursor = Client::cursor();
state
.client_repository
.create(&cursor)
.await
.map_err(|e| e.to_string())?;
}
// Create VS Code if not exists
if !existing.iter().any(|c| c.client_type == "vscode") {
let vscode = Client::vscode();
state
.client_repository
.create(&vscode)
.await
.map_err(|e| e.to_string())?;
}
// Create Claude Desktop if not exists
if !existing.iter().any(|c| c.client_type == "claude") {
let claude = Client::claude_desktop();
state
.client_repository
.create(&claude)
.await
.map_err(|e| e.to_string())?;
}
Ok(())
}