chore: add mcp write and read commands

This commit is contained in:
Louis 2025-04-14 22:03:06 +07:00 committed by Faisal Amir
parent ef1a85b58c
commit 70be283d0e
4 changed files with 37 additions and 30 deletions

View File

@ -7,6 +7,9 @@ use tauri::{AppHandle, Manager, Runtime, State};
use super::{server, setup, state::AppState}; use super::{server, setup, state::AppState};
const CONFIGURATION_FILE_NAME: &str = "settings.json"; const CONFIGURATION_FILE_NAME: &str = "settings.json";
const DEFAULT_MCP_CONFIG: &str = r#"{
"mcpServers": {}
}"#;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AppConfiguration { pub struct AppConfiguration {
@ -352,3 +355,29 @@ pub async fn call_tool(
Err(format!("Tool {} not found", tool_name)) Err(format!("Tool {} not found", tool_name))
} }
#[tauri::command]
pub async fn get_mcp_configs(app: AppHandle) -> Result<String, String> {
let mut path = get_jan_data_folder_path(app);
path.push("mcp_config.json");
log::info!("read mcp configs, path: {:?}", path);
// Create default empty config if file doesn't exist
if !path.exists() {
log::info!("mcp_config.json not found, creating default empty config");
fs::write(&path, DEFAULT_MCP_CONFIG)
.map_err(|e| format!("Failed to create default MCP config: {}", e))?;
}
let contents = fs::read_to_string(path).map_err(|e| e.to_string())?;
return Ok(contents);
}
#[tauri::command]
pub async fn save_mcp_configs(app: AppHandle, configs: String) -> Result<(), String> {
let mut path = get_jan_data_folder_path(app);
path.push("mcp_config.json");
log::info!("save mcp configs, path: {:?}", path);
fs::write(path, configs).map_err(|e| e.to_string())
}

View File

@ -39,6 +39,8 @@ pub fn run() {
core::cmd::app_token, core::cmd::app_token,
core::cmd::start_server, core::cmd::start_server,
core::cmd::stop_server, core::cmd::stop_server,
core::cmd::save_mcp_configs,
core::cmd::get_mcp_configs,
// MCP commands // MCP commands
core::cmd::get_tools, core::cmd::get_tools,
core::cmd::call_tool, core::cmd::call_tool,

View File

@ -17,31 +17,9 @@ const MCP = () => {
const readConfigFile = useCallback(async () => { const readConfigFile = useCallback(async () => {
try { try {
const configPath = await joinPath([janDataFolderPath, 'mcp_config.json']) // Read the file
const content = await window.core?.api.getMcpConfigs()
// Check if the file exists setConfigContent(content)
const fileExists = await fs.existsSync(configPath)
if (fileExists) {
// Read the file
const content = await fs.readFileSync(configPath, 'utf-8')
setConfigContent(content)
} else {
// Create a default config if it doesn't exist
const defaultConfig = JSON.stringify(
{
servers: [],
settings: {
enabled: true,
},
},
null,
2
)
await fs.writeFileSync(configPath, defaultConfig)
setConfigContent(defaultConfig)
}
setError('') setError('')
} catch (err) { } catch (err) {
@ -70,11 +48,7 @@ const MCP = () => {
setIsSaving(false) setIsSaving(false)
return return
} }
await window.core?.api?.saveMcpConfigs({ configs: configContent })
const configPath = await joinPath([janDataFolderPath, 'mcp_config.json'])
// Write to the file
await fs.writeFileSync(configPath, configContent)
setSuccess('Config saved successfully') setSuccess('Config saved successfully')
setIsSaving(false) setIsSaving(false)

View File

@ -19,6 +19,8 @@ export const Routes = [
'getThreadAssistant', 'getThreadAssistant',
'createThreadAssistant', 'createThreadAssistant',
'modifyThreadAssistant', 'modifyThreadAssistant',
'saveMcpConfigs',
'getMcpConfigs',
].map((r) => ({ ].map((r) => ({
path: `app`, path: `app`,
route: r, route: r,