2025-10-08 22:34:58 +08:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-10-09 21:08:42 +08:00
|
|
|
|
use serde_json::{Map, Value};
|
2025-10-08 22:34:58 +08:00
|
|
|
|
use std::env;
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
2025-10-27 09:02:48 +08:00
|
|
|
|
use crate::config::{atomic_write, get_claude_mcp_path, get_default_claude_mcp_path};
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
use crate::error::AppError;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
|
pub struct McpStatus {
|
2025-10-08 23:22:19 +08:00
|
|
|
|
pub user_config_path: String,
|
|
|
|
|
|
pub user_config_exists: bool,
|
2025-10-08 22:34:58 +08:00
|
|
|
|
pub server_count: usize,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 23:22:19 +08:00
|
|
|
|
fn user_config_path() -> PathBuf {
|
2025-10-27 09:02:48 +08:00
|
|
|
|
ensure_mcp_override_migrated();
|
|
|
|
|
|
get_claude_mcp_path()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn ensure_mcp_override_migrated() {
|
|
|
|
|
|
if crate::settings::get_claude_override_dir().is_none() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let new_path = get_claude_mcp_path();
|
|
|
|
|
|
if new_path.exists() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let legacy_path = get_default_claude_mcp_path();
|
|
|
|
|
|
if !legacy_path.exists() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(parent) = new_path.parent() {
|
|
|
|
|
|
if let Err(err) = fs::create_dir_all(parent) {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::warn!("创建 MCP 目录失败: {err}");
|
2025-10-27 09:02:48 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match fs::copy(&legacy_path, &new_path) {
|
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
|
log::info!(
|
|
|
|
|
|
"已根据覆盖目录复制 MCP 配置: {} -> {}",
|
|
|
|
|
|
legacy_path.display(),
|
|
|
|
|
|
new_path.display()
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
log::warn!(
|
|
|
|
|
|
"复制 MCP 配置失败: {} -> {}: {}",
|
|
|
|
|
|
legacy_path.display(),
|
|
|
|
|
|
new_path.display(),
|
|
|
|
|
|
err
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
fn read_json_value(path: &Path) -> Result<Value, AppError> {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if !path.exists() {
|
|
|
|
|
|
return Ok(serde_json::json!({}));
|
|
|
|
|
|
}
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
2025-10-28 11:58:57 +08:00
|
|
|
|
let value: Value = serde_json::from_str(&content).map_err(|e| AppError::json(path, e))?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
Ok(value)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
fn write_json_value(path: &Path, value: &Value) -> Result<(), AppError> {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if let Some(parent) = path.parent() {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let json =
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
serde_json::to_string_pretty(value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
atomic_write(path, json.as_bytes())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
pub fn get_mcp_status() -> Result<McpStatus, AppError> {
|
2025-10-08 23:22:19 +08:00
|
|
|
|
let path = user_config_path();
|
|
|
|
|
|
let (exists, count) = if path.exists() {
|
|
|
|
|
|
let v = read_json_value(&path)?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
let servers = v.get("mcpServers").and_then(|x| x.as_object());
|
|
|
|
|
|
(true, servers.map(|m| m.len()).unwrap_or(0))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
(false, 0)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Ok(McpStatus {
|
2025-10-08 23:22:19 +08:00
|
|
|
|
user_config_path: path.to_string_lossy().to_string(),
|
|
|
|
|
|
user_config_exists: exists,
|
2025-10-08 22:34:58 +08:00
|
|
|
|
server_count: count,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
pub fn read_mcp_json() -> Result<Option<String>, AppError> {
|
2025-10-08 23:22:19 +08:00
|
|
|
|
let path = user_config_path();
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if !path.exists() {
|
|
|
|
|
|
return Ok(None);
|
|
|
|
|
|
}
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
Ok(Some(content))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, AppError> {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if id.trim().is_empty() {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 基础字段校验(尽量宽松)
|
|
|
|
|
|
if !spec.is_object() {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器定义必须为 JSON 对象".into(),
|
|
|
|
|
|
));
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let t_opt = spec.get("type").and_then(|x| x.as_str());
|
|
|
|
|
|
let is_stdio = t_opt.map(|t| t == "stdio").unwrap_or(true); // 兼容缺省(按 stdio 处理)
|
|
|
|
|
|
let is_http = t_opt.map(|t| t == "http").unwrap_or(false);
|
|
|
|
|
|
if !(is_stdio || is_http) {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器 type 必须是 'stdio' 或 'http'(或省略表示 stdio)".into(),
|
|
|
|
|
|
));
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
2025-10-09 12:04:37 +08:00
|
|
|
|
|
|
|
|
|
|
// stdio 类型必须有 command
|
2025-10-09 22:02:56 +08:00
|
|
|
|
if is_stdio {
|
2025-10-09 12:04:37 +08:00
|
|
|
|
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
|
|
if cmd.is_empty() {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
|
|
|
|
|
));
|
2025-10-09 12:04:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// http 类型必须有 url
|
2025-10-09 22:02:56 +08:00
|
|
|
|
if is_http {
|
2025-10-09 12:04:37 +08:00
|
|
|
|
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
|
|
if url.is_empty() {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
|
|
|
|
|
));
|
2025-10-09 12:04:37 +08:00
|
|
|
|
}
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 23:22:19 +08:00
|
|
|
|
let path = user_config_path();
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let mut root = if path.exists() {
|
|
|
|
|
|
read_json_value(&path)?
|
|
|
|
|
|
} else {
|
|
|
|
|
|
serde_json::json!({})
|
|
|
|
|
|
};
|
2025-10-08 22:34:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 确保 mcpServers 对象存在
|
|
|
|
|
|
{
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let obj = root
|
|
|
|
|
|
.as_object_mut()
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
.ok_or_else(|| AppError::Config("mcp.json 根必须是对象".into()))?;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if !obj.contains_key("mcpServers") {
|
|
|
|
|
|
obj.insert("mcpServers".into(), serde_json::json!({}));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let before = root.clone();
|
2025-10-12 16:21:32 +08:00
|
|
|
|
if let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
servers.insert(id.to_string(), spec);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if before == root && path.exists() {
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
write_json_value(&path, &root)?;
|
|
|
|
|
|
Ok(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
pub fn delete_mcp_server(id: &str) -> Result<bool, AppError> {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if id.trim().is_empty() {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
2025-10-08 22:34:58 +08:00
|
|
|
|
}
|
2025-10-08 23:22:19 +08:00
|
|
|
|
let path = user_config_path();
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if !path.exists() {
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
let mut root = read_json_value(&path)?;
|
|
|
|
|
|
let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) else {
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
let existed = servers.remove(id).is_some();
|
|
|
|
|
|
if !existed {
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
write_json_value(&path, &root)?;
|
|
|
|
|
|
Ok(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
pub fn validate_command_in_path(cmd: &str) -> Result<bool, AppError> {
|
2025-10-08 22:34:58 +08:00
|
|
|
|
if cmd.trim().is_empty() {
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果包含路径分隔符,直接判断是否存在可执行文件
|
|
|
|
|
|
if cmd.contains('/') || cmd.contains('\\') {
|
|
|
|
|
|
return Ok(Path::new(cmd).exists());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let path_var = env::var_os("PATH").unwrap_or_default();
|
|
|
|
|
|
let paths = env::split_paths(&path_var);
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
|
let exts: Vec<String> = env::var("PATHEXT")
|
|
|
|
|
|
.unwrap_or(".COM;.EXE;.BAT;.CMD".into())
|
|
|
|
|
|
.split(';')
|
|
|
|
|
|
.map(|s| s.trim().to_uppercase())
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
for p in paths {
|
|
|
|
|
|
let candidate = p.join(cmd);
|
|
|
|
|
|
if candidate.is_file() {
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
|
{
|
|
|
|
|
|
for ext in &exts {
|
|
|
|
|
|
let cand = p.join(format!("{}{}", cmd, ext));
|
|
|
|
|
|
if cand.is_file() {
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(false)
|
|
|
|
|
|
}
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
2025-11-14 12:51:24 +08:00
|
|
|
|
/// 读取 ~/.claude.json 中的 mcpServers 映射
|
|
|
|
|
|
pub fn read_mcp_servers_map() -> Result<std::collections::HashMap<String, Value>, AppError> {
|
|
|
|
|
|
let path = user_config_path();
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
|
return Ok(std::collections::HashMap::new());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let root = read_json_value(&path)?;
|
|
|
|
|
|
let servers = root
|
|
|
|
|
|
.get("mcpServers")
|
|
|
|
|
|
.and_then(|v| v.as_object())
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
|
2025-11-14 12:51:24 +08:00
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
|
|
Ok(servers)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 21:08:42 +08:00
|
|
|
|
/// 将给定的启用 MCP 服务器映射写入到用户级 ~/.claude.json 的 mcpServers 字段
|
|
|
|
|
|
/// 仅覆盖 mcpServers,其他字段保持不变
|
2025-10-12 16:21:32 +08:00
|
|
|
|
pub fn set_mcp_servers_map(
|
|
|
|
|
|
servers: &std::collections::HashMap<String, Value>,
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
) -> Result<(), AppError> {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
let path = user_config_path();
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let mut root = if path.exists() {
|
|
|
|
|
|
read_json_value(&path)?
|
|
|
|
|
|
} else {
|
|
|
|
|
|
serde_json::json!({})
|
|
|
|
|
|
};
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建 mcpServers 对象:移除 UI 辅助字段(enabled/source),仅保留实际 MCP 规范
|
|
|
|
|
|
let mut out: Map<String, Value> = Map::new();
|
|
|
|
|
|
for (id, spec) in servers.iter() {
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let mut obj = if let Some(map) = spec.as_object() {
|
|
|
|
|
|
map.clone()
|
2025-10-09 21:08:42 +08:00
|
|
|
|
} else {
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
return Err(AppError::McpValidation(format!(
|
2025-11-12 10:47:34 +08:00
|
|
|
|
"MCP 服务器 '{id}' 不是对象"
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
)));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(server_val) = obj.remove("server") {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
let server_obj = server_val.as_object().cloned().ok_or_else(|| {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
AppError::McpValidation(format!("MCP 服务器 '{id}' server 字段不是对象"))
|
2025-10-28 11:58:57 +08:00
|
|
|
|
})?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
obj = server_obj;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-12 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
|
obj.remove("enabled");
|
|
|
|
|
|
obj.remove("source");
|
|
|
|
|
|
obj.remove("id");
|
|
|
|
|
|
obj.remove("name");
|
|
|
|
|
|
obj.remove("description");
|
|
|
|
|
|
obj.remove("tags");
|
|
|
|
|
|
obj.remove("homepage");
|
|
|
|
|
|
obj.remove("docs");
|
|
|
|
|
|
|
|
|
|
|
|
out.insert(id.clone(), Value::Object(obj));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
let obj = root
|
|
|
|
|
|
.as_object_mut()
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
.ok_or_else(|| AppError::Config("~/.claude.json 根必须是对象".into()))?;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
obj.insert("mcpServers".into(), Value::Object(out));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
write_json_value(&path, &root)?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|