refactor(mcp): switch to user-level config ~/.claude.json and remove project-level logic
- Read/write ~/.claude.json (preserve unknown fields) for mcpServers - Remove settings.local.json and mcp.json handling - Drop enableAllProjectMcpServers command and UI toggle - Update types, Tauri APIs, and MCP panel to reflect new status fields - Keep atomic write and command validation behaviors
This commit is contained in:
@@ -4,29 +4,21 @@ use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::config::{atomic_write, get_claude_config_dir};
|
||||
use crate::config::atomic_write;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpStatus {
|
||||
pub settings_local_path: String,
|
||||
pub settings_local_exists: bool,
|
||||
pub enable_all_project_mcp_servers: bool,
|
||||
pub mcp_json_path: String,
|
||||
pub mcp_json_exists: bool,
|
||||
pub user_config_path: String,
|
||||
pub user_config_exists: bool,
|
||||
pub server_count: usize,
|
||||
}
|
||||
|
||||
fn claude_dir() -> PathBuf {
|
||||
get_claude_config_dir()
|
||||
}
|
||||
|
||||
fn settings_local_path() -> PathBuf {
|
||||
claude_dir().join("settings.local.json")
|
||||
}
|
||||
|
||||
fn mcp_json_path() -> PathBuf {
|
||||
claude_dir().join("mcp.json")
|
||||
fn user_config_path() -> PathBuf {
|
||||
// 用户级 MCP 配置文件:~/.claude.json
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".claude.json")
|
||||
}
|
||||
|
||||
fn read_json_value(path: &Path) -> Result<Value, String> {
|
||||
@@ -50,20 +42,9 @@ fn write_json_value(path: &Path, value: &Value) -> Result<(), String> {
|
||||
}
|
||||
|
||||
pub fn get_mcp_status() -> Result<McpStatus, String> {
|
||||
let settings_local = settings_local_path();
|
||||
let mcp_path = mcp_json_path();
|
||||
|
||||
let mut enable = false;
|
||||
if settings_local.exists() {
|
||||
let v = read_json_value(&settings_local)?;
|
||||
enable = v
|
||||
.get("enableAllProjectMcpServers")
|
||||
.and_then(|x| x.as_bool())
|
||||
.unwrap_or(false);
|
||||
}
|
||||
|
||||
let (exists, count) = if mcp_path.exists() {
|
||||
let v = read_json_value(&mcp_path)?;
|
||||
let path = user_config_path();
|
||||
let (exists, count) = if path.exists() {
|
||||
let v = read_json_value(&path)?;
|
||||
let servers = v.get("mcpServers").and_then(|x| x.as_object());
|
||||
(true, servers.map(|m| m.len()).unwrap_or(0))
|
||||
} else {
|
||||
@@ -71,17 +52,14 @@ pub fn get_mcp_status() -> Result<McpStatus, String> {
|
||||
};
|
||||
|
||||
Ok(McpStatus {
|
||||
settings_local_path: settings_local.to_string_lossy().to_string(),
|
||||
settings_local_exists: settings_local.exists(),
|
||||
enable_all_project_mcp_servers: enable,
|
||||
mcp_json_path: mcp_path.to_string_lossy().to_string(),
|
||||
mcp_json_exists: exists,
|
||||
user_config_path: path.to_string_lossy().to_string(),
|
||||
user_config_exists: exists,
|
||||
server_count: count,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_mcp_json() -> Result<Option<String>, String> {
|
||||
let path = mcp_json_path();
|
||||
let path = user_config_path();
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -90,28 +68,6 @@ pub fn read_mcp_json() -> Result<Option<String>, String> {
|
||||
Ok(Some(content))
|
||||
}
|
||||
|
||||
pub fn set_enable_all_projects(enable: bool) -> Result<bool, String> {
|
||||
let path = settings_local_path();
|
||||
let mut v = if path.exists() { read_json_value(&path)? } else { serde_json::json!({}) };
|
||||
|
||||
let current = v
|
||||
.get("enableAllProjectMcpServers")
|
||||
.and_then(|x| x.as_bool())
|
||||
.unwrap_or(false);
|
||||
if current == enable && path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
if let Some(obj) = v.as_object_mut() {
|
||||
obj.insert(
|
||||
"enableAllProjectMcpServers".to_string(),
|
||||
Value::Bool(enable),
|
||||
);
|
||||
}
|
||||
write_json_value(&path, &v)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, String> {
|
||||
if id.trim().is_empty() {
|
||||
return Err("MCP 服务器 ID 不能为空".into());
|
||||
@@ -132,7 +88,7 @@ pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, String> {
|
||||
return Err("MCP 服务器缺少 command".into());
|
||||
}
|
||||
|
||||
let path = mcp_json_path();
|
||||
let path = user_config_path();
|
||||
let mut root = if path.exists() { read_json_value(&path)? } else { serde_json::json!({}) };
|
||||
|
||||
// 确保 mcpServers 对象存在
|
||||
@@ -163,7 +119,7 @@ pub fn delete_mcp_server(id: &str) -> Result<bool, String> {
|
||||
if id.trim().is_empty() {
|
||||
return Err("MCP 服务器 ID 不能为空".into());
|
||||
}
|
||||
let path = mcp_json_path();
|
||||
let path = user_config_path();
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -215,4 +171,3 @@ pub fn validate_command_in_path(cmd: &str) -> Result<bool, String> {
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
||||
@@ -676,11 +676,6 @@ pub async fn read_claude_mcp_config() -> Result<Option<String>, String> {
|
||||
claude_mcp::read_mcp_json()
|
||||
}
|
||||
|
||||
/// 设置 enableAllProjectMcpServers 开关
|
||||
#[tauri::command]
|
||||
pub async fn set_claude_mcp_enable_all_projects(enable: bool) -> Result<bool, String> {
|
||||
claude_mcp::set_enable_all_projects(enable)
|
||||
}
|
||||
|
||||
/// 新增或更新一个 MCP 服务器条目
|
||||
#[tauri::command]
|
||||
|
||||
@@ -425,7 +425,6 @@ pub fn run() {
|
||||
// Claude MCP management
|
||||
commands::get_claude_mcp_status,
|
||||
commands::read_claude_mcp_config,
|
||||
commands::set_claude_mcp_enable_all_projects,
|
||||
commands::upsert_claude_mcp_server,
|
||||
commands::delete_claude_mcp_server,
|
||||
commands::validate_mcp_command,
|
||||
|
||||
@@ -77,17 +77,7 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
|
||||
reload();
|
||||
}, []);
|
||||
|
||||
const handleToggleEnable = async (enable: boolean) => {
|
||||
try {
|
||||
const changed = await window.api.setClaudeMcpEnableAllProjects(enable);
|
||||
if (changed) {
|
||||
await reload();
|
||||
onNotify?.(t("mcp.notice.restartClaude"), "success", 2000);
|
||||
}
|
||||
} catch (e: any) {
|
||||
onNotify?.(e?.message || t("mcp.error.toggleFailed"), "error", 5000);
|
||||
}
|
||||
};
|
||||
// 用户级 MCP:不需要项目级启用开关
|
||||
|
||||
const resetForm = () => {
|
||||
setEditingId(null);
|
||||
@@ -200,25 +190,14 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
|
||||
<div className="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
{/* Left: status & list */}
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<div className="mb-4">
|
||||
<div className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{t("mcp.enableProject")}
|
||||
{t("mcp.userLevelPath")}
|
||||
</div>
|
||||
<div className="text-xs text-gray-400 dark:text-gray-500">
|
||||
{status?.settingsLocalPath}
|
||||
<div className="text-xs text-gray-400 dark:text-gray-500 break-all">
|
||||
{status?.userConfigPath}
|
||||
</div>
|
||||
</div>
|
||||
<label className="inline-flex items-center cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="sr-only peer"
|
||||
checked={!!status?.enableAllProjectMcpServers}
|
||||
onChange={(e) => handleToggleEnable(e.target.checked)}
|
||||
/>
|
||||
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-emerald-500 relative" />
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 mb-3">
|
||||
<button
|
||||
@@ -233,12 +212,6 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
|
||||
>
|
||||
<Wrench size={16} /> {t("mcp.template.fetch")}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => window.api.openConfigFolder("claude")}
|
||||
className="inline-flex items-center gap-2 px-3 py-2 text-sm font-medium rounded-md bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-200 dark:hover:bg-gray-700"
|
||||
>
|
||||
{t("mcp.openFolder")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="border border-gray-200 dark:border-gray-800 rounded-lg overflow-hidden">
|
||||
@@ -246,11 +219,7 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
|
||||
<span>
|
||||
{t("mcp.serverList")} ({status?.serverCount || 0})
|
||||
</span>
|
||||
{status?.mcpJsonExists ? (
|
||||
<span className="text-gray-400">{status?.mcpJsonPath}</span>
|
||||
) : (
|
||||
<span className="text-gray-400">mcp.json</span>
|
||||
)}
|
||||
<span className="text-gray-400">{status?.userConfigPath}</span>
|
||||
</div>
|
||||
<div className="max-h-64 overflow-auto divide-y divide-gray-200 dark:divide-gray-800">
|
||||
{loading && (
|
||||
@@ -403,4 +372,3 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
|
||||
};
|
||||
|
||||
export default McpPanel;
|
||||
|
||||
|
||||
@@ -251,8 +251,7 @@
|
||||
},
|
||||
"mcp": {
|
||||
"title": "MCP Management",
|
||||
"enableProject": "Enable project-level MCP servers",
|
||||
"openFolder": "Open ~/.claude",
|
||||
"userLevelPath": "User-level MCP path",
|
||||
"serverList": "Servers",
|
||||
"loading": "Loading...",
|
||||
"empty": "No MCP servers",
|
||||
@@ -281,7 +280,6 @@
|
||||
"templateAdded": "Template added"
|
||||
},
|
||||
"error": {
|
||||
"toggleFailed": "Failed to update toggle",
|
||||
"idRequired": "Please enter identifier",
|
||||
"commandRequired": "Please enter command",
|
||||
"saveFailed": "Save failed",
|
||||
|
||||
@@ -251,8 +251,7 @@
|
||||
},
|
||||
"mcp": {
|
||||
"title": "MCP 管理",
|
||||
"enableProject": "启用项目级 MCP 服务器",
|
||||
"openFolder": "打开 ~/.claude",
|
||||
"userLevelPath": "用户级 MCP 配置路径",
|
||||
"serverList": "服务器列表",
|
||||
"loading": "加载中...",
|
||||
"empty": "暂无 MCP 服务器",
|
||||
@@ -281,7 +280,6 @@
|
||||
"templateAdded": "已添加模板"
|
||||
},
|
||||
"error": {
|
||||
"toggleFailed": "更新开关失败",
|
||||
"idRequired": "请填写标识",
|
||||
"commandRequired": "请填写命令",
|
||||
"saveFailed": "保存失败",
|
||||
|
||||
@@ -279,7 +279,7 @@ export const tauriAPI = {
|
||||
}
|
||||
},
|
||||
|
||||
// Claude MCP:获取状态(settings.local.json + mcp.json)
|
||||
// Claude MCP:获取状态(用户级 ~/.claude.json)
|
||||
getClaudeMcpStatus: async (): Promise<McpStatus> => {
|
||||
try {
|
||||
return await invoke<McpStatus>("get_claude_mcp_status");
|
||||
@@ -289,7 +289,7 @@ export const tauriAPI = {
|
||||
}
|
||||
},
|
||||
|
||||
// Claude MCP:读取 mcp.json 文本
|
||||
// Claude MCP:读取 ~/.claude.json 文本
|
||||
readClaudeMcpConfig: async (): Promise<string | null> => {
|
||||
try {
|
||||
return await invoke<string | null>("read_claude_mcp_config");
|
||||
@@ -299,16 +299,6 @@ export const tauriAPI = {
|
||||
}
|
||||
},
|
||||
|
||||
// Claude MCP:设置项目级启用开关
|
||||
setClaudeMcpEnableAllProjects: async (enable: boolean): Promise<boolean> => {
|
||||
try {
|
||||
return await invoke<boolean>("set_claude_mcp_enable_all_projects", { enable });
|
||||
} catch (error) {
|
||||
console.error("写入 settings.local.json 失败:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Claude MCP:新增/更新服务器定义
|
||||
upsertClaudeMcpServer: async (
|
||||
id: string,
|
||||
|
||||
@@ -65,10 +65,7 @@ export interface McpServer {
|
||||
|
||||
// MCP 配置状态
|
||||
export interface McpStatus {
|
||||
settingsLocalPath: string;
|
||||
settingsLocalExists: boolean;
|
||||
enableAllProjectMcpServers: boolean;
|
||||
mcpJsonPath: string;
|
||||
mcpJsonExists: boolean;
|
||||
userConfigPath: string;
|
||||
userConfigExists: boolean;
|
||||
serverCount: number;
|
||||
}
|
||||
|
||||
1
src/vite-env.d.ts
vendored
1
src/vite-env.d.ts
vendored
@@ -64,7 +64,6 @@ declare global {
|
||||
// Claude MCP
|
||||
getClaudeMcpStatus: () => Promise<McpStatus>;
|
||||
readClaudeMcpConfig: () => Promise<string | null>;
|
||||
setClaudeMcpEnableAllProjects: (enable: boolean) => Promise<boolean>;
|
||||
upsertClaudeMcpServer: (
|
||||
id: string,
|
||||
spec: Record<string, any>,
|
||||
|
||||
Reference in New Issue
Block a user