Skip to content

Commit c4ca574

Browse files
committed
feat: implement configuration file existence check in Recently component
- Added a new function to check if the mcplinker configuration file exists before adding server items. - Enhanced error handling and user feedback with toast notifications during the server addition process. - Updated the loading logic for saved server configurations to include the new check.
1 parent 48e1569 commit c4ca574

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src-tauri/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl ClientConfig {
7474
PathBuf::from("")
7575
}
7676
};
77-
77+
7878
Self {
7979
name: name.to_string(),
8080
path,

src-tauri/src/cmd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::client::ClientConfig;
22
use crate::json_manager::JsonManager;
33
use serde_json::{json, Value};
4+
use std::path::PathBuf;
45

56
#[tauri::command]
67
pub async fn read_json_file(client_name: String, path: Option<String>) -> Result<Value, String> {
@@ -91,3 +92,10 @@ pub async fn update_mcp_server(
9192

9293
JsonManager::update_mcp_server(file_path, &client_name, &server_name, server_config)
9394
}
95+
96+
#[tauri::command]
97+
pub fn check_mcplinker_config_exists() -> bool {
98+
let home_dir = dirs::home_dir().unwrap_or_default();
99+
let config_path: PathBuf = home_dir.join(".config/mcplinker/mcp.json");
100+
config_path.exists()
101+
}

src-tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn run() {
8282
cmd::add_mcp_server,
8383
cmd::remove_mcp_server,
8484
cmd::update_mcp_server,
85+
cmd::check_mcplinker_config_exists,
8586
installer::check_command_exists,
8687
installer::install_command,
8788
get_path_env,

src/pages/recently.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,42 @@ export default function Recently() {
1515

1616
// Load saved server configurations
1717
useEffect(() => {
18+
async function checkFileExists(parsed: any) {
19+
try {
20+
const exists = await invoke<boolean>("check_mcplinker_config_exists");
21+
if (!exists) {
22+
toast.info("start add");
23+
parsed.forEach(async (s: ServerListItem) => {
24+
const saveServerItem = {
25+
clientName: "mcplinker",
26+
path: "",
27+
serverName: s.name,
28+
serverConfig: s.config,
29+
};
30+
toast.info(JSON.stringify(saveServerItem));
31+
try {
32+
await invoke("add_mcp_server", saveServerItem);
33+
} catch (e) {
34+
// already exists
35+
}
36+
});
37+
}
38+
} catch (e) {
39+
console.log("Failed to check config file");
40+
}
41+
}
1842
const saved = localStorage.getItem("myservers");
1943
if (saved) {
20-
console.log(saved);
44+
try {
45+
const parsed = JSON.parse(saved) as {
46+
name: string;
47+
config: ServerConfig;
48+
}[];
49+
checkFileExists(parsed);
50+
setServerList(parsed);
51+
} catch (error) {
52+
console.error("Failed to load myservers:", error);
53+
}
2154
}
2255
invoke<ConfigType>("read_json_file", {
2356
clientName: "mcplinker",

0 commit comments

Comments
 (0)