feat: implement hybrid fetch_url tool and isolate crashes
This commit is contained in:
1 parent
4342615649
commit
3533c243d2
9 files changed
+448
-61
No files matched your search
+46
-46
@@ -2,7 +2,6 @@ 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;
|
||||
|
||||
@@ -17,7 +16,7 @@ impl McpTool for FetchUrlHandler {
|
||||
fn schema(&self) -> Value {
|
||||
crate::mcp::tool_def::<FetchUrlTool>(
|
||||
"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)",
|
||||
"Fetch content from a URL via an HTTP request natively (supports proxy config via environment variables: HTTP_PROXY, HTTPS_PROXY, NO_PROXY)",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -29,54 +28,31 @@ impl McpTool for FetchUrlHandler {
|
||||
))
|
||||
})?;
|
||||
|
||||
let mut client_builder = Client::builder();
|
||||
let method_str = req.method.unwrap_or_else(|| "GET".to_string());
|
||||
|
||||
let headers: Vec<String> = req.headers.unwrap_or_default().into_iter()
|
||||
.map(|(k, v)| format!("{}={}", k, v))
|
||||
.collect();
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
// Allow bypassing SSL validation for self-signed certificates
|
||||
if req.ignore_ssl_errors.unwrap_or(false) {
|
||||
client_builder = client_builder.danger_accept_invalid_certs(true);
|
||||
}
|
||||
|
||||
// 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))
|
||||
// Pass to our agentic-tools-rs library directly
|
||||
let res = agentic_tools_rs::commands::llmfetch::run(
|
||||
&req.url,
|
||||
&method_str,
|
||||
req.body.as_ref(),
|
||||
&headers,
|
||||
true, // Always condense for MCP
|
||||
).await.map_err(|e| {
|
||||
crate::error::AppError::Internal(format!("Agentic fetch failed: {}", 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)
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::{routing::get, Router};
|
||||
use axum::{routing::{get, post}, Router};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
async fn spawn_test_server() -> String {
|
||||
@@ -90,7 +66,8 @@ mod tests {
|
||||
"Not Found Error",
|
||||
)
|
||||
}),
|
||||
);
|
||||
)
|
||||
.route("/echo", post(|body: String| async move { format!("ECHO: {}", body) }));
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
@@ -113,12 +90,13 @@ mod tests {
|
||||
serde_json::json!({
|
||||
"url": url
|
||||
}),
|
||||
Arc::new(MemoryState::new("")),
|
||||
Arc::new(MemoryState::new_in_memory()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(res, "Hello, World!");
|
||||
assert!(res.contains("HTTP Status: 200 OK"));
|
||||
assert!(res.contains("Hello, World!"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -132,12 +110,34 @@ mod tests {
|
||||
serde_json::json!({
|
||||
"url": url
|
||||
}),
|
||||
Arc::new(MemoryState::new("")),
|
||||
Arc::new(MemoryState::new_in_memory()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(res.contains("HTTP Error: 404 Not Found"));
|
||||
assert!(res.contains("HTTP Status: 404 Not Found"));
|
||||
assert!(res.contains("Not Found Error"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_url_post() {
|
||||
let base_url = spawn_test_server().await;
|
||||
let handler = FetchUrlHandler;
|
||||
let url = format!("{}/echo", base_url);
|
||||
|
||||
let res = handler
|
||||
.execute(
|
||||
serde_json::json!({
|
||||
"url": url,
|
||||
"method": "POST",
|
||||
"body": "test_body_content"
|
||||
}),
|
||||
Arc::new(MemoryState::new_in_memory()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(res.contains("HTTP Status: 200 OK"));
|
||||
assert!(res.contains("ECHO: test_body_content"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user