refactor: apply 5-pass audit optimizations across mcp-memory codebase

This commit is contained in:
Riz Ashraf committed 2026-10-06 06:05:38 +01:00
1 parent 924b6d09fa
commit 5bd8b1587a
43 files changed
+1866 -1658

No files matched your search

+3
View File
@@ -5,3 +5,6 @@ edition = "2024"
[dependencies]
tokio = { version = "1.53.1", features = ["io-util", "macros"] }
[dev-dependencies]
tokio = { version = "1.53.1", features = ["rt", "macros"] }
+10 -4
View File
@@ -15,8 +15,14 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
let trimmed_start = line.trim_start();
if trimmed_start.starts_with('{') {
let trimmed = line.trim();
return Some(trimmed.to_string());
let mut val = std::mem::take(&mut line);
let trimmed_len = val.trim_end().len();
val.truncate(trimmed_len);
let start = val.len() - val.trim_start().len();
if start > 0 {
val.drain(..start);
}
return Some(val);
}
let line = line.trim_end();
@@ -24,7 +30,7 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
break;
}
if line.len() >= 15 && line[..15].eq_ignore_ascii_case("content-length:") {
if line.as_bytes().len() >= 15 && line.as_bytes()[..15].eq_ignore_ascii_case(b"content-length:") {
length = line[15..].trim().parse().unwrap_or(0);
}
}
@@ -35,7 +41,7 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
return None;
}
let mut buffer = Vec::with_capacity(length);
let mut buffer = Vec::with_capacity(length.min(64 * 1024));
if stdin.take(length as u64).read_to_end(&mut buffer).await.is_err() || buffer.len() != length {
return None;
}