Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 72 additions & 3 deletions apps/tauri/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ redis = { version = "0.27", features = ["aio", "tokio-comp"] }
mongodb = "3.10.1"
russh = { version = "0.65.1", features = ["default"] }
rand = "0.10.4"
rmcp = { version = "3.10.0", features = ["server", "transport-async-rw"] }

[profile.release]
codegen-units = 1
Expand Down
22 changes: 22 additions & 0 deletions apps/tauri/src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,25 @@ pub fn ssh_tunnel_list(
) -> Result<Vec<crate::ssh_tunnel::SshTunnelInfo>, crate::ssh_tunnel::SshTunnelError> {
tunnels.list()
}

/// Start the in-process MCP loopback server and write the endpoint file (#1900).
#[tauri::command]
pub fn mcp_start(
mcp: State<'_, crate::mcp::McpController>,
) -> Result<crate::mcp::McpStatus, crate::mcp::McpError> {
mcp.start()
}

#[tauri::command]
pub fn mcp_stop(
mcp: State<'_, crate::mcp::McpController>,
) -> Result<crate::mcp::McpStatus, crate::mcp::McpError> {
mcp.stop()
}

#[tauri::command]
pub fn mcp_status(
mcp: State<'_, crate::mcp::McpController>,
) -> Result<crate::mcp::McpStatus, crate::mcp::McpError> {
mcp.status()
}
9 changes: 8 additions & 1 deletion apps/tauri/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commands;
mod connection;
mod mcp;
mod profiles;
mod secrets;
mod ssh_tunnel;
Expand All @@ -13,6 +14,7 @@ pub use connection::{
};
pub use profiles::{ConnectionProfile, ProfileStore, ProfileStoreError};
pub use secrets::{KeyringSecretStore, MemorySecretStore, SecretError, SecretStore, SharedSecretStore};
pub use mcp::{McpController, McpError, McpStatus};
pub use ssh_tunnel::{
SshTunnelError, SshTunnelInfo, SshTunnelOpenRequest, SshTunnelRegistry,
};
Expand All @@ -31,7 +33,8 @@ use commands::{
mongo_list_databases, mongo_ping, profiles_connect_mongo, profiles_connect_mysql,
profiles_connect_postgres, profiles_connect_redis, profiles_connect_sqlite, profiles_delete,
profiles_list, profiles_upsert, redis_get, redis_key_metas, redis_ping, redis_read_key,
redis_scan, redis_set, ssh_tunnel_close, ssh_tunnel_list, ssh_tunnel_open,
redis_scan, redis_set, mcp_start, mcp_status, mcp_stop, ssh_tunnel_close, ssh_tunnel_list,
ssh_tunnel_open,
};

#[tauri::command]
Expand Down Expand Up @@ -86,6 +89,7 @@ pub fn run() {
.manage(ConnectionRegistry::new())
.manage(KeyringSecretStore::shared())
.manage(SshTunnelRegistry::new())
.manage(McpController::new())
.setup(|app| {
use tauri::Manager;
let store = ProfileStore::open_default().map_err(|e| {
Expand Down Expand Up @@ -138,6 +142,9 @@ pub fn run() {
ssh_tunnel_open,
ssh_tunnel_close,
ssh_tunnel_list,
mcp_start,
mcp_stop,
mcp_status,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
152 changes: 152 additions & 0 deletions apps/tauri/src-tauri/src/mcp/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//! Endpoint file shared with `querya-mcp` (Flutter-compatible shape).

use serde::{Deserialize, Serialize};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// Overrides the endpoint file location (tests, several app instances).
pub const ENV_OVERRIDE: &str = "QUERYA_MCP_ENDPOINT";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct McpEndpoint {
pub port: u16,
pub token: String,
pub pid: u32,
pub version: String,
}

impl McpEndpoint {
/// `$XDG_RUNTIME_DIR/querya/mcp-endpoint.json` on Linux (falls back to
/// `~/.cache/querya`), `~/Library/Caches/Querya` on macOS,
/// `%LOCALAPPDATA%\Querya` on Windows.
pub fn default_path() -> PathBuf {
if let Ok(override_path) = std::env::var(ENV_OVERRIDE) {
let trimmed = override_path.trim();
if !trimmed.is_empty() {
return PathBuf::from(trimmed);
}
}
#[cfg(target_os = "windows")]
{
let base = std::env::var("LOCALAPPDATA")
.or_else(|_| {
std::env::var("USERPROFILE").map(|h| {
format!("{h}\\AppData\\Local")
})
})
.unwrap_or_else(|_| ".".into());
return PathBuf::from(base).join("Querya").join("mcp-endpoint.json");
}
#[cfg(target_os = "macos")]
{
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
return PathBuf::from(home)
.join("Library")
.join("Caches")
.join("Querya")
.join("mcp-endpoint.json");
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
{
if let Ok(runtime) = std::env::var("XDG_RUNTIME_DIR") {
if !runtime.trim().is_empty() {
return PathBuf::from(runtime)
.join("querya")
.join("mcp-endpoint.json");
}
}
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
PathBuf::from(home)
.join(".cache")
.join("querya")
.join("mcp-endpoint.json")
}
}

/// 32 random bytes as hex (Flutter `McpEndpoint.newToken` parity).
pub fn new_token() -> String {
use rand::Rng;
let mut bytes = [0u8; 32];
rand::rng().fill_bytes(&mut bytes);
bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// Constant-time comparison for the handshake token.
pub fn tokens_equal(a: &str, b: &str) -> bool {
let x = a.as_bytes();
let y = b.as_bytes();
let mut diff = x.len() ^ y.len();
let n = x.len().min(y.len());
for i in 0..n {
diff |= usize::from(x[i] ^ y[i]);
}
diff == 0
}

pub fn write(&self, path: &Path) -> io::Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(parent, fs::Permissions::from_mode(0o700));
}
}
let tmp = path.with_extension("json.tmp");
fs::write(&tmp, "")?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&tmp, fs::Permissions::from_mode(0o600));
}
fs::write(&tmp, serde_json::to_vec(self).expect("endpoint json"))?;
fs::rename(&tmp, path)?;
Ok(())
}

pub fn read(path: &Path) -> io::Result<Option<Self>> {
match fs::read_to_string(path) {
Ok(raw) => Ok(serde_json::from_str(&raw).ok()),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}

pub fn clear(path: &Path) -> io::Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tokens_equal_rejects_mismatch() {
assert!(McpEndpoint::tokens_equal("abc", "abc"));
assert!(!McpEndpoint::tokens_equal("abc", "abd"));
assert!(!McpEndpoint::tokens_equal("abc", "ab"));
}

#[test]
fn write_read_round_trip() {
let dir = std::env::temp_dir().join(format!("querya-mcp-ep-{}", std::process::id()));
let path = dir.join("mcp-endpoint.json");
let ep = McpEndpoint {
port: 12345,
token: "deadbeef".into(),
pid: 42,
version: "0.1.0".into(),
};
ep.write(&path).unwrap();
let back = McpEndpoint::read(&path).unwrap().unwrap();
assert_eq!(back, ep);
McpEndpoint::clear(&path).unwrap();
let _ = fs::remove_dir_all(&dir);
}
}
Loading
Loading