diff --git a/.gitignore b/.gitignore index e5b43f0..6541125 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ temp.json *.txt summary.md __pycache__/ +server/mcp_store.redb +server/tantivy_index/ diff --git a/server/Cargo.toml b/server/Cargo.toml index 433589f..4862951 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,7 +14,7 @@ clap = { version = "4.6.6", features = ["derive"] } dirs = "7.0.0" futures-util = "0.3.34" redb = "4.3.0" -reqwest = { version = "0.13", default-features = false, features = ["stream", "json"] } +reqwest = { version = "0.13", default-features = false, features = ["stream", "json", "rustls"] } schemars = "1.2.2" serde = { version = "1.0.229", features = ["derive"] } serde_json = "1.0.151" diff --git a/server/src/handlers/ast.rs b/server/src/handlers/ast.rs index c959b73..53968d9 100644 --- a/server/src/handlers/ast.rs +++ b/server/src/handlers/ast.rs @@ -440,7 +440,7 @@ fn scan_workspace_for_symbol( if is_match { results.push(serde_json::json!({ - "file_path": path.to_string_lossy(), + "file_path": crate::handlers::utils::sanitize_path(&path.to_string_lossy()), "line": line_num + 1, "content": line.trim(), })); diff --git a/server/src/handlers/git.rs b/server/src/handlers/git.rs index 1411356..e30910b 100644 --- a/server/src/handlers/git.rs +++ b/server/src/handlers/git.rs @@ -66,7 +66,8 @@ impl McpTool for GetActiveWorktreeContextHandler { for delta in diff.deltas() { if let Some(path) = delta.new_file().path() { - files.push(path.to_string_lossy().into_owned()); + let path_str = path.to_string_lossy().into_owned(); + files.push(crate::handlers::utils::sanitize_path(&path_str)); } } } @@ -121,7 +122,7 @@ impl McpTool for QueryGitDiffsHandler { || change.git_commit.as_ref().is_some_and(|c| c.contains(&q)) { matches.push(json!({ - "file_path": change.file_path, + "file_path": crate::handlers::utils::sanitize_path(&change.file_path), "description": change.description, "commit": change.git_commit, "branch": change.git_branch, diff --git a/server/src/handlers/http.rs b/server/src/handlers/http.rs new file mode 100644 index 0000000..0b854f5 --- /dev/null +++ b/server/src/handlers/http.rs @@ -0,0 +1,138 @@ +use crate::router::McpTool; +use crate::state::MemoryState; +use crate::tools::FetchUrlTool; +use async_trait::async_trait; +use reqwest::Client; +use serde_json::Value; +use std::sync::Arc; + +pub struct FetchUrlHandler; + +#[async_trait] +impl McpTool for FetchUrlHandler { + fn name(&self) -> &'static str { + "fetch_url" + } + + fn schema(&self) -> Value { + crate::mcp::tool_def::( + "fetch_url", + "Fetch content from a URL via an HTTP GET request natively (supports proxy config via environment variables: HTTP_PROXY, HTTPS_PROXY, NO_PROXY)", + ) + } + + async fn execute(&self, args: Value, _state: Arc) -> crate::error::Result { + let req: FetchUrlTool = serde_json::from_value(args).map_err(|e| { + crate::error::AppError::BadRequest(format!( + "Schema validation failed. Your JSON arguments do not match the expected tool schema: {}", + e + )) + })?; + + let mut client_builder = Client::builder(); + + // Add optional user agent + if let Some(ua) = req.user_agent { + client_builder = client_builder.user_agent(ua); + } else { + client_builder = client_builder.user_agent("mcp-memory-server/1.0"); + } + + // Reqwest automatically uses HTTP_PROXY, HTTPS_PROXY, NO_PROXY + // environment variables by default, so we don't need to manually + // extract and apply them, the builder handles it. + + let client = client_builder.build().map_err(|e| { + crate::error::AppError::Internal(format!("Failed to build HTTP client: {}", e)) + })?; + + let res = client.get(&req.url).send().await.map_err(|e| { + crate::error::AppError::Internal(format!("HTTP request to {} failed: {}", req.url, e)) + })?; + + let status = res.status(); + let content = res.text().await.map_err(|e| { + crate::error::AppError::Internal(format!("Failed to read response body: {}", e)) + })?; + + if !status.is_success() { + return Ok(format!( + "HTTP Error: {} {}\n\nResponse Body:\n{}", + status.as_u16(), + status.canonical_reason().unwrap_or("Unknown"), + content + )); + } + + Ok(content) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::{routing::get, Router}; + use tokio::net::TcpListener; + + async fn spawn_test_server() -> String { + let app = Router::new() + .route("/test", get(|| async { "Hello, World!" })) + .route( + "/not_found", + get(|| async { + ( + axum::http::StatusCode::NOT_FOUND, + "Not Found Error", + ) + }), + ); + + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + + tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }); + + format!("http://127.0.0.1:{}", port) + } + + #[tokio::test] + async fn test_fetch_url_success() { + let base_url = spawn_test_server().await; + let handler = FetchUrlHandler; + let url = format!("{}/test", base_url); + + let res = handler + .execute( + serde_json::json!({ + "url": url + }), + Arc::new(MemoryState::new("")), + ) + .await + .unwrap(); + + assert_eq!(res, "Hello, World!"); + } + + #[tokio::test] + async fn test_fetch_url_not_found() { + let base_url = spawn_test_server().await; + let handler = FetchUrlHandler; + let url = format!("{}/not_found", base_url); + + let res = handler + .execute( + serde_json::json!({ + "url": url + }), + Arc::new(MemoryState::new("")), + ) + .await + .unwrap(); + + assert!(res.contains("HTTP Error: 404 Not Found")); + assert!(res.contains("Not Found Error")); + } +} diff --git a/server/src/handlers/meta.rs b/server/src/handlers/meta.rs index a2ec980..244a389 100644 --- a/server/src/handlers/meta.rs +++ b/server/src/handlers/meta.rs @@ -331,6 +331,10 @@ impl McpTool for QueryRecentChangesHandler { .skip(offset) .take(limit) .cloned() + .map(|mut c| { + c.file_path = crate::handlers::utils::sanitize_path(&c.file_path); + c + }) .collect(); Ok::(serde_yaml::to_string(&filtered)?) })?; diff --git a/server/src/handlers/mod.rs b/server/src/handlers/mod.rs index b8dac4a..d0f7d3e 100644 --- a/server/src/handlers/mod.rs +++ b/server/src/handlers/mod.rs @@ -1,4 +1,5 @@ pub mod ast; +pub mod http; pub mod env; pub mod git; pub mod graph; diff --git a/server/src/handlers/utils.rs b/server/src/handlers/utils.rs index 7198f3a..44c425f 100644 --- a/server/src/handlers/utils.rs +++ b/server/src/handlers/utils.rs @@ -15,7 +15,8 @@ pub fn now_secs() -> u64 { } pub fn validate_safe_path(path_str: &str) -> crate::error::Result { - let path = std::path::Path::new(path_str); + let sanitized_str = sanitize_path(path_str); + let path = std::path::Path::new(&sanitized_str); for component in path.components() { if component == std::path::Component::ParentDir { return Err(crate::error::AppError::BadRequest(format!( @@ -27,6 +28,35 @@ pub fn validate_safe_path(path_str: &str) -> crate::error::Result String { + if cfg!(unix) { + if raw_path.contains(":\\") { + if let Ok(output) = std::process::Command::new("wslpath").arg("-u").arg(raw_path).output() { + if output.status.success() { + return String::from_utf8_lossy(&output.stdout).trim().to_string(); + } + } + if raw_path.len() >= 3 && &raw_path[1..3] == ":\\" { + let drive_letter = &raw_path[0..1].to_lowercase(); + let remainder = raw_path[3..].replace("\\", "/"); + return format!("/mnt/{}/{}", drive_letter, remainder); + } + } + } else if cfg!(windows) { + if raw_path.starts_with("/mnt/") && raw_path.len() > 6 { + let drive_letter = &raw_path[5..6].to_uppercase(); + let remainder = raw_path[7..].replace("/", "\\"); + return format!("{}:\\{}", drive_letter, remainder); + } else if raw_path.starts_with("/") { + return format!("\\\\wsl$\\Ubuntu{}", raw_path.replace("/", "\\")); + } + } + raw_path.to_string() +} + #[cfg(test)] mod tests { use super::*; diff --git a/server/src/router.rs b/server/src/router.rs index 1e9f503..4a9d77c 100644 --- a/server/src/router.rs +++ b/server/src/router.rs @@ -536,6 +536,7 @@ impl MemoryHandler { register!(graph::SummarizeSubgraphHandler); register!(meta::AgentSignalsHandler); register!(meta::AutoSessionCheckpointHandler); + register!(http::FetchUrlHandler); Self { state, diff --git a/server/src/tools.rs b/server/src/tools.rs index 2888d06..507eaf8 100644 --- a/server/src/tools.rs +++ b/server/src/tools.rs @@ -1033,3 +1033,12 @@ pub struct ClipboardTool { /// Image path (for 'write'). pub image_path: Option, } + +/// Fetch content from a URL via an HTTP GET request natively (supports proxy config via environment variables: HTTP_PROXY, HTTPS_PROXY, NO_PROXY). +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct FetchUrlTool { + /// URL to fetch content from. + pub url: String, + /// Optional custom user-agent string. + pub user_agent: Option, +}