feat(gemini): implement full MCP management functionality
- Add gemini_mcp.rs module for Gemini MCP file I/O operations - Implement sync_enabled_to_gemini to export enabled MCPs to ~/.gemini/settings.json - Implement import_from_gemini to import MCPs from Gemini config - Add Gemini sync logic in services/mcp.rs (upsert_server, delete_server, set_enabled) - Register Tauri commands for Gemini MCP sync and import - Update frontend API calls and McpPanel to support Gemini Fixes the issue where adding MCP servers in Gemini tab would not sync to ~/.gemini/settings.json
This commit is contained in:
@@ -129,3 +129,17 @@ pub async fn import_mcp_from_claude(state: State<'_, AppState>) -> Result<usize,
|
||||
pub async fn import_mcp_from_codex(state: State<'_, AppState>) -> Result<usize, String> {
|
||||
McpService::import_from_codex(&state).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 手动同步:将启用的 MCP 投影到 ~/.gemini/settings.json
|
||||
#[tauri::command]
|
||||
pub async fn sync_enabled_mcp_to_gemini(state: State<'_, AppState>) -> Result<bool, String> {
|
||||
McpService::sync_enabled(&state, AppType::Gemini)
|
||||
.map(|_| true)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 从 ~/.gemini/settings.json 导入 MCP 定义到 config.json
|
||||
#[tauri::command]
|
||||
pub async fn import_mcp_from_gemini(state: State<'_, AppState>) -> Result<usize, String> {
|
||||
McpService::import_from_gemini(&state).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
213
src-tauri/src/gemini_mcp.rs
Normal file
213
src-tauri/src/gemini_mcp.rs
Normal file
@@ -0,0 +1,213 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Map, Value};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::config::atomic_write;
|
||||
use crate::error::AppError;
|
||||
use crate::gemini_config::get_gemini_settings_path;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpStatus {
|
||||
pub user_config_path: String,
|
||||
pub user_config_exists: bool,
|
||||
pub server_count: usize,
|
||||
}
|
||||
|
||||
/// 获取 Gemini MCP 配置文件路径(~/.gemini/settings.json)
|
||||
fn user_config_path() -> PathBuf {
|
||||
get_gemini_settings_path()
|
||||
}
|
||||
|
||||
fn read_json_value(path: &Path) -> Result<Value, AppError> {
|
||||
if !path.exists() {
|
||||
return Ok(serde_json::json!({}));
|
||||
}
|
||||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||||
let value: Value = serde_json::from_str(&content).map_err(|e| AppError::json(path, e))?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn write_json_value(path: &Path, value: &Value) -> Result<(), AppError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
let json =
|
||||
serde_json::to_string_pretty(value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
atomic_write(path, json.as_bytes())
|
||||
}
|
||||
|
||||
/// 获取 Gemini MCP 状态
|
||||
pub fn get_mcp_status() -> Result<McpStatus, AppError> {
|
||||
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 {
|
||||
(false, 0)
|
||||
};
|
||||
|
||||
Ok(McpStatus {
|
||||
user_config_path: path.to_string_lossy().to_string(),
|
||||
user_config_exists: exists,
|
||||
server_count: count,
|
||||
})
|
||||
}
|
||||
|
||||
/// 读取 Gemini MCP 配置文件的完整 JSON 文本
|
||||
pub fn read_mcp_json() -> Result<Option<String>, AppError> {
|
||||
let path = user_config_path();
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(Some(content))
|
||||
}
|
||||
|
||||
/// 在 Gemini settings.json 中新增或更新一个 MCP 服务器
|
||||
pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
// 基础字段校验(尽量宽松)
|
||||
if !spec.is_object() {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器定义必须为 JSON 对象".into(),
|
||||
));
|
||||
}
|
||||
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) {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 type 必须是 'stdio' 或 'http'(或省略表示 stdio)".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// stdio 类型必须有 command
|
||||
if is_stdio {
|
||||
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if cmd.is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// http 类型必须有 url
|
||||
if is_http {
|
||||
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if url.is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let path = user_config_path();
|
||||
let mut root = if path.exists() {
|
||||
read_json_value(&path)?
|
||||
} else {
|
||||
serde_json::json!({})
|
||||
};
|
||||
|
||||
// 确保 mcpServers 对象存在
|
||||
{
|
||||
let obj = root
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| AppError::Config("settings.json 根必须是对象".into()))?;
|
||||
if !obj.contains_key("mcpServers") {
|
||||
obj.insert("mcpServers".into(), serde_json::json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
let before = root.clone();
|
||||
if let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) {
|
||||
servers.insert(id.to_string(), spec);
|
||||
}
|
||||
|
||||
if before == root && path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
write_json_value(&path, &root)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 删除 Gemini settings.json 中的一个 MCP 服务器
|
||||
pub fn delete_mcp_server(id: &str) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
let path = user_config_path();
|
||||
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)
|
||||
}
|
||||
|
||||
/// 将给定的启用 MCP 服务器映射写入到 Gemini settings.json 的 mcpServers 字段
|
||||
/// 仅覆盖 mcpServers,其他字段保持不变
|
||||
pub fn set_mcp_servers_map(
|
||||
servers: &std::collections::HashMap<String, Value>,
|
||||
) -> Result<(), AppError> {
|
||||
let path = user_config_path();
|
||||
let mut root = if path.exists() {
|
||||
read_json_value(&path)?
|
||||
} else {
|
||||
serde_json::json!({})
|
||||
};
|
||||
|
||||
// 构建 mcpServers 对象:移除 UI 辅助字段(enabled/source),仅保留实际 MCP 规范
|
||||
let mut out: Map<String, Value> = Map::new();
|
||||
for (id, spec) in servers.iter() {
|
||||
let mut obj = if let Some(map) = spec.as_object() {
|
||||
map.clone()
|
||||
} else {
|
||||
return Err(AppError::McpValidation(format!(
|
||||
"MCP 服务器 '{id}' 不是对象"
|
||||
)));
|
||||
};
|
||||
|
||||
// 提取 server 字段(如果存在)
|
||||
if let Some(server_val) = obj.remove("server") {
|
||||
let server_obj = server_val.as_object().cloned().ok_or_else(|| {
|
||||
AppError::McpValidation(format!("MCP 服务器 '{id}' server 字段不是对象"))
|
||||
})?;
|
||||
obj = server_obj;
|
||||
}
|
||||
|
||||
// 移除 UI 辅助字段
|
||||
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));
|
||||
}
|
||||
|
||||
{
|
||||
let obj = root
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| AppError::Config("~/.gemini/settings.json 根必须是对象".into()))?;
|
||||
obj.insert("mcpServers".into(), Value::Object(out));
|
||||
}
|
||||
|
||||
write_json_value(&path, &root)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod codex_config;
|
||||
mod commands;
|
||||
mod config;
|
||||
mod error;
|
||||
mod gemini_mcp;
|
||||
mod gemini_config; // 新增
|
||||
mod init_status;
|
||||
mod mcp;
|
||||
@@ -23,7 +24,8 @@ pub use commands::*;
|
||||
pub use config::{get_claude_mcp_path, get_claude_settings_path, read_json_file};
|
||||
pub use error::AppError;
|
||||
pub use mcp::{
|
||||
import_from_claude, import_from_codex, sync_enabled_to_claude, sync_enabled_to_codex,
|
||||
import_from_claude, import_from_codex, import_from_gemini, sync_enabled_to_claude,
|
||||
sync_enabled_to_codex, sync_enabled_to_gemini,
|
||||
};
|
||||
pub use provider::{Provider, ProviderMeta};
|
||||
pub use services::{
|
||||
@@ -539,8 +541,10 @@ pub fn run() {
|
||||
commands::set_mcp_enabled,
|
||||
commands::sync_enabled_mcp_to_claude,
|
||||
commands::sync_enabled_mcp_to_codex,
|
||||
commands::sync_enabled_mcp_to_gemini,
|
||||
commands::import_mcp_from_claude,
|
||||
commands::import_mcp_from_codex,
|
||||
commands::import_mcp_from_gemini,
|
||||
// Prompt management
|
||||
commands::get_prompts,
|
||||
commands::upsert_prompt,
|
||||
|
||||
@@ -712,3 +712,82 @@ pub fn sync_enabled_to_codex(config: &MultiAppConfig) -> Result<(), AppError> {
|
||||
crate::config::write_text_file(&path, &new_text)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 将 config.json 中 enabled==true 的项投影写入 ~/.gemini/settings.json
|
||||
pub fn sync_enabled_to_gemini(config: &MultiAppConfig) -> Result<(), AppError> {
|
||||
let enabled = collect_enabled_servers(&config.mcp.gemini);
|
||||
crate::gemini_mcp::set_mcp_servers_map(&enabled)
|
||||
}
|
||||
|
||||
/// 从 ~/.gemini/settings.json 导入 mcpServers 到 config.json(设为 enabled=true)。
|
||||
/// 已存在的项仅强制 enabled=true,不覆盖其他字段。
|
||||
pub fn import_from_gemini(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
||||
let text_opt = crate::gemini_mcp::read_mcp_json()?;
|
||||
let Some(text) = text_opt else { return Ok(0) };
|
||||
let mut changed = normalize_servers_for(config, &AppType::Gemini);
|
||||
let v: Value = serde_json::from_str(&text)
|
||||
.map_err(|e| AppError::McpValidation(format!("解析 ~/.gemini/settings.json 失败: {e}")))?;
|
||||
let Some(map) = v.get("mcpServers").and_then(|x| x.as_object()) else {
|
||||
return Ok(changed);
|
||||
};
|
||||
|
||||
for (id, spec) in map.iter() {
|
||||
// 校验目标 spec
|
||||
validate_server_spec(spec)?;
|
||||
|
||||
let entry = config
|
||||
.mcp_for_mut(&AppType::Gemini)
|
||||
.servers
|
||||
.entry(id.clone());
|
||||
use std::collections::hash_map::Entry;
|
||||
match entry {
|
||||
Entry::Vacant(vac) => {
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
vac.insert(Value::Object(obj));
|
||||
changed += 1;
|
||||
}
|
||||
Entry::Occupied(mut occ) => {
|
||||
let value = occ.get_mut();
|
||||
let Some(existing) = value.as_object_mut() else {
|
||||
log::warn!("MCP 条目 '{id}' 不是 JSON 对象,覆盖为导入数据");
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
occ.insert(Value::Object(obj));
|
||||
changed += 1;
|
||||
continue;
|
||||
};
|
||||
|
||||
let mut modified = false;
|
||||
let prev_enabled = existing
|
||||
.get("enabled")
|
||||
.and_then(|b| b.as_bool())
|
||||
.unwrap_or(false);
|
||||
if !prev_enabled {
|
||||
existing.insert(String::from("enabled"), json!(true));
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("server").is_none() {
|
||||
log::warn!("MCP 条目 '{id}' 缺少 server 字段,覆盖为导入数据");
|
||||
existing.insert(String::from("server"), spec.clone());
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("id").is_none() {
|
||||
log::warn!("MCP 条目 '{id}' 缺少 id 字段,自动填充");
|
||||
existing.insert(String::from("id"), json!(id));
|
||||
modified = true;
|
||||
}
|
||||
if modified {
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
@@ -30,11 +30,12 @@ impl McpService {
|
||||
spec: Value,
|
||||
sync_other_side: bool,
|
||||
) -> Result<bool, AppError> {
|
||||
let (changed, snapshot, sync_claude, sync_codex): (
|
||||
let (changed, snapshot, sync_claude, sync_codex, sync_gemini): (
|
||||
bool,
|
||||
Option<MultiAppConfig>,
|
||||
bool,
|
||||
bool,
|
||||
bool,
|
||||
) = {
|
||||
let mut cfg = state.config.write()?;
|
||||
let changed = mcp::upsert_in_config_for(&mut cfg, &app, id, spec)?;
|
||||
@@ -51,6 +52,7 @@ impl McpService {
|
||||
|
||||
let mut sync_claude = matches!(app, AppType::Claude) && enabled;
|
||||
let mut sync_codex = matches!(app, AppType::Codex) && enabled;
|
||||
let mut sync_gemini = matches!(app, AppType::Gemini) && enabled;
|
||||
|
||||
// 修复:sync_other_side=true 时,先将 MCP 复制到另一侧,然后强制同步
|
||||
// 这才是"同步到另一侧"的正确语义:将 MCP 跨应用复制
|
||||
@@ -83,13 +85,13 @@ impl McpService {
|
||||
}
|
||||
}
|
||||
|
||||
let snapshot = if sync_claude || sync_codex {
|
||||
let snapshot = if sync_claude || sync_codex || sync_gemini {
|
||||
Some(cfg.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
(changed, snapshot, sync_claude, sync_codex)
|
||||
(changed, snapshot, sync_claude, sync_codex, sync_gemini)
|
||||
};
|
||||
|
||||
// 保持原有行为:始终尝试持久化,避免遗漏 normalize 带来的隐式变更
|
||||
@@ -102,6 +104,9 @@ impl McpService {
|
||||
if sync_codex {
|
||||
mcp::sync_enabled_to_codex(&snapshot)?;
|
||||
}
|
||||
if sync_gemini {
|
||||
mcp::sync_enabled_to_gemini(&snapshot)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(changed)
|
||||
@@ -121,7 +126,7 @@ impl McpService {
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
AppType::Gemini => {} // Gemini 暂不支持 MCP 同步
|
||||
AppType::Gemini => mcp::sync_enabled_to_gemini(&snapshot)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,7 +153,7 @@ impl McpService {
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
AppType::Gemini => {} // Gemini 暂不支持 MCP 同步
|
||||
AppType::Gemini => mcp::sync_enabled_to_gemini(&snapshot)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,7 +173,7 @@ impl McpService {
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
AppType::Gemini => {} // Gemini 暂不支持 MCP 同步
|
||||
AppType::Gemini => mcp::sync_enabled_to_gemini(&snapshot)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -194,4 +199,15 @@ impl McpService {
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// 从 Gemini 客户端配置导入 MCP 定义。
|
||||
pub fn import_from_gemini(state: &AppState) -> Result<usize, AppError> {
|
||||
let mut cfg = state.config.write()?;
|
||||
let changed = mcp::import_from_gemini(&mut cfg)?;
|
||||
drop(cfg);
|
||||
if changed > 0 {
|
||||
state.save()?;
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user