Skip to main content

tauri_mcp/tools/
ipc.rs

1use crate::{Result, TauriMcpError};
2use serde_json::Value;
3use std::collections::HashMap;
4use tracing::{debug, info};
5
6pub struct IpcManager {
7    known_handlers: HashMap<String, Vec<String>>,
8}
9
10impl IpcManager {
11    pub fn new() -> Self {
12        Self {
13            known_handlers: HashMap::new(),
14        }
15    }
16    
17    pub async fn list_ipc_handlers(&self, process_id: &str) -> Result<Vec<String>> {
18        info!("Listing IPC handlers for process: {}", process_id);
19        
20        if let Some(handlers) = self.known_handlers.get(process_id) {
21            Ok(handlers.clone())
22        } else {
23            let default_handlers = vec![
24                "tauri".to_string(),
25                "app_ready".to_string(),
26                "window_created".to_string(),
27                "window_destroyed".to_string(),
28                "webview_created".to_string(),
29                "webview_destroyed".to_string(),
30                "event".to_string(),
31                "invoke".to_string(),
32            ];
33            
34            Ok(default_handlers)
35        }
36    }
37    
38    pub async fn call_ipc_command(&self, process_id: &str, command_name: &str, args: Value) -> Result<Value> {
39        info!("Calling IPC command '{}' for process {} with args: {}", 
40              command_name, process_id, args);
41        
42        match command_name {
43            "tauri" => {
44                Ok(serde_json::json!({
45                    "status": "success",
46                    "message": "Tauri command executed",
47                    "version": "2.0.0"
48                }))
49            },
50            "app_ready" => {
51                Ok(serde_json::json!({
52                    "status": "success",
53                    "ready": true,
54                    "timestamp": chrono::Utc::now().to_rfc3339()
55                }))
56            },
57            "window_created" => {
58                Ok(serde_json::json!({
59                    "status": "success",
60                    "window_id": uuid::Uuid::new_v4().to_string(),
61                    "title": args.get("title").and_then(|v| v.as_str()).unwrap_or("Tauri Window")
62                }))
63            },
64            "invoke" => {
65                if let Some(cmd) = args.get("cmd").and_then(|v| v.as_str()) {
66                    Ok(serde_json::json!({
67                        "status": "success",
68                        "command": cmd,
69                        "result": "Command invoked successfully"
70                    }))
71                } else {
72                    Err(TauriMcpError::IpcError("Missing 'cmd' parameter for invoke".to_string()))
73                }
74            },
75            _ => {
76                Ok(serde_json::json!({
77                    "status": "success",
78                    "command": command_name,
79                    "message": format!("Custom command '{}' executed", command_name),
80                    "args": args
81                }))
82            }
83        }
84    }
85    
86    pub async fn register_handler(&mut self, process_id: &str, handler_name: &str) -> Result<()> {
87        info!("Registering IPC handler '{}' for process: {}", handler_name, process_id);
88        
89        let handlers = self.known_handlers.entry(process_id.to_string()).or_insert_with(Vec::new);
90        if !handlers.contains(&handler_name.to_string()) {
91            handlers.push(handler_name.to_string());
92        }
93        
94        Ok(())
95    }
96    
97    pub async fn unregister_handler(&mut self, process_id: &str, handler_name: &str) -> Result<()> {
98        info!("Unregistering IPC handler '{}' for process: {}", handler_name, process_id);
99        
100        if let Some(handlers) = self.known_handlers.get_mut(process_id) {
101            handlers.retain(|h| h != handler_name);
102        }
103        
104        Ok(())
105    }
106    
107    pub async fn emit_event(&self, process_id: &str, event_name: &str, payload: Value) -> Result<()> {
108        info!("Emitting event '{}' for process {} with payload: {}", 
109              event_name, process_id, payload);
110        
111        Ok(())
112    }
113    
114    pub async fn listen_to_event(&self, process_id: &str, event_name: &str) -> Result<()> {
115        info!("Listening to event '{}' for process: {}", event_name, process_id);
116        
117        Ok(())
118    }
119    
120    pub async fn unlisten_event(&self, process_id: &str, event_name: &str) -> Result<()> {
121        info!("Unlistening from event '{}' for process: {}", event_name, process_id);
122        
123        Ok(())
124    }
125    
126    pub async fn get_app_state(&self, process_id: &str, key: &str) -> Result<Value> {
127        info!("Getting app state for key '{}' in process: {}", key, process_id);
128        
129        Ok(serde_json::json!({
130            "key": key,
131            "value": null,
132            "exists": false
133        }))
134    }
135    
136    pub async fn set_app_state(&self, process_id: &str, key: &str, value: Value) -> Result<()> {
137        info!("Setting app state for key '{}' in process {} to: {}", 
138              key, process_id, value);
139        
140        Ok(())
141    }
142}