| 1 | use std::path::Path; |
| 2 | |
| 3 | use toml_edit::{DocumentMut, InlineTable, Item, Table, value}; |
| 4 | |
| 5 | use super::entry::{SERVER_NAME, ServerEntry}; |
| 6 | use super::error::InstallError; |
| 7 | use super::file; |
| 8 | |
| 9 | const SERVERS_KEY: &str = "mcp_servers"; |
| 10 | const TOOL_TIMEOUT_SECONDS: i64 = 300; |
| 11 | |
| 12 | fn load(path: &Path) -> Result<Option<DocumentMut>, InstallError> { |
| 13 | match file::read_if_exists(path)? { |
| 14 | Some(text) => text |
| 15 | .parse::<DocumentMut>() |
| 16 | .map(Some) |
| 17 | .map_err(|error| InstallError::malformed(path, error)), |
| 18 | None => Ok(None), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn entry_of(document: &DocumentMut) -> Option<ServerEntry> { |
| 23 | let server = document.get(SERVERS_KEY)?.get(SERVER_NAME)?; |
| 24 | Some(ServerEntry { |
| 25 | url: server.get("url")?.as_str()?.to_string(), |
| 26 | authorization: server |
| 27 | .get("http_headers")? |
| 28 | .get("Authorization")? |
| 29 | .as_str()? |
| 30 | .to_string(), |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | pub(super) fn read_entry(path: &Path) -> Result<Option<ServerEntry>, InstallError> { |
| 35 | Ok(load(path)?.and_then(|document| entry_of(&document))) |
| 36 | } |
| 37 | |
| 38 | pub(super) fn upsert(path: &Path, entry: &ServerEntry) -> Result<(), InstallError> { |
| 39 | let mut document = load(path)?.unwrap_or_default(); |
| 40 | if entry_of(&document).as_ref() != Some(entry) { |
| 41 | let server = server_table(&mut document, path)?; |
| 42 | server["url"] = value(entry.url.as_str()); |
| 43 | let mut headers = InlineTable::new(); |
| 44 | headers.insert("Authorization", entry.authorization.as_str().into()); |
| 45 | server["http_headers"] = value(headers); |
| 46 | } |
| 47 | let server = server_table(&mut document, path)?; |
| 48 | if !server.contains_key("tool_timeout_sec") { |
| 49 | server["tool_timeout_sec"] = value(TOOL_TIMEOUT_SECONDS); |
| 50 | } |
| 51 | file::write_atomic(path, &document.to_string()) |
| 52 | } |
| 53 | |
| 54 | fn server_table<'document>( |
| 55 | document: &'document mut DocumentMut, |
| 56 | path: &Path, |
| 57 | ) -> Result<&'document mut Table, InstallError> { |
| 58 | let servers = document |
| 59 | .entry(SERVERS_KEY) |
| 60 | .or_insert_with(|| { |
| 61 | let mut table = Table::new(); |
| 62 | table.set_implicit(true); |
| 63 | Item::Table(table) |
| 64 | }) |
| 65 | .as_table_mut() |
| 66 | .ok_or_else(|| InstallError::malformed(path, format!("`{SERVERS_KEY}` is not a table")))?; |
| 67 | servers |
| 68 | .entry(SERVER_NAME) |
| 69 | .or_insert_with(|| Item::Table(Table::new())) |
| 70 | .as_table_mut() |
| 71 | .ok_or_else(|| InstallError::malformed(path, format!("`{SERVER_NAME}` is not a table"))) |
| 72 | } |
| 73 | |
| 74 | pub(super) fn remove(path: &Path) -> Result<(), InstallError> { |
| 75 | let Some(mut document) = load(path)? else { |
| 76 | return Ok(()); |
| 77 | }; |
| 78 | let removed = document |
| 79 | .get_mut(SERVERS_KEY) |
| 80 | .and_then(Item::as_table_like_mut) |
| 81 | .and_then(|servers| servers.remove(SERVER_NAME)) |
| 82 | .is_some(); |
| 83 | if removed { |
| 84 | file::write_atomic(path, &document.to_string())?; |
| 85 | } |
| 86 | Ok(()) |
| 87 | } |