mod access;
pub use access::AccessControl;
use std::path::Path;
use uuid::Uuid;
use rmcp::model::{CallToolResult, ContentBlock, ProtocolVersion};
use tokio::io::AsyncWriteExt;
pub const SPEC_VERSION: &str = "2026-07-28";
pub static SUPPORTED_PROTOCOL_VERSIONS: &[ProtocolVersion] = &[ProtocolVersion::V_2026_07_28];
pub fn reject_unsupported_initialize()
-> std::future::Ready<Result<rmcp::model::InitializeResult, rmcp::ErrorData>> {
std::future::ready(Err(rmcp::ErrorData::method_not_found::<
rmcp::model::InitializeResultMethod,
>()))
}
pub fn configure_minimal_process_environment(command: &mut tokio::process::Command) {
command.env_clear();
for name in [
"PATH",
"HOME",
"USERPROFILE",
"SYSTEMROOT",
"PATHEXT",
"TEMP",
"TMP",
"TMPDIR",
"LANG",
"LC_ALL",
"LC_CTYPE",
] {
if let Some(value) = std::env::var_os(name) {
command.env(name, value);
}
}
}
pub fn tool_error(message: impl Into<String>) -> CallToolResult {
CallToolResult::error(vec![ContentBlock::text(message)])
}
pub fn text_result(text: impl Into<String>) -> CallToolResult {
CallToolResult::success(vec![ContentBlock::text(text)])
}
pub const MAX_TOOL_RESULT_BYTES: usize = 64 * 1024;
pub fn truncate_text(text: &str, max_bytes: usize, notice: &str) -> String {
if text.len() <= max_bytes {
return text.to_string();
}
if max_bytes == 0 {
return String::new();
}
let notice = truncate_utf8_prefix(notice, max_bytes);
if notice.len() == max_bytes {
return notice.to_string();
}
let content_budget = max_bytes - notice.len();
let content = truncate_utf8_prefix(text, content_budget);
let mut truncated = String::with_capacity(content.len() + notice.len());
truncated.push_str(content);
truncated.push_str(notice);
truncated
}
fn truncate_utf8_prefix(value: &str, max_bytes: usize) -> &str {
let mut end = max_bytes.min(value.len());
while end > 0 && !value.is_char_boundary(end) {
end -= 1;
}
&value[..end]
}
pub async fn atomic_write(path: &Path, content: &[u8]) -> std::io::Result<()> {
let parent = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let file_name = path.file_name().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"atomic write target must have a file name",
)
})?;
let file_name = file_name.to_string_lossy();
let existing_permissions = capture_existing_permissions(path).await;
let (temp, mut file) = loop {
let temp = parent.join(format!(".{file_name}.{}.tmp", Uuid::now_v7().simple()));
let open = tokio::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&temp)
.await;
match open {
Ok(file) => break (temp, file),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error),
}
};
let result = async {
file.write_all(content).await?;
if let Some(permissions) = existing_permissions {
file.set_permissions(permissions).await?;
}
file.sync_all().await?;
drop(file);
tokio::fs::rename(&temp, path).await?;
sync_parent_directory(parent).await
}
.await;
if result.is_err() {
let _ = tokio::fs::remove_file(&temp).await;
}
result
}
#[cfg(unix)]
async fn sync_parent_directory(parent: &Path) -> std::io::Result<()> {
tokio::fs::File::open(parent).await?.sync_all().await
}
#[cfg(not(unix))]
async fn sync_parent_directory(_parent: &Path) -> std::io::Result<()> {
Ok(())
}
#[cfg(unix)]
async fn capture_existing_permissions(path: &Path) -> Option<std::fs::Permissions> {
let metadata = tokio::fs::symlink_metadata(path).await.ok()?;
metadata.is_file().then(|| metadata.permissions())
}
#[cfg(not(unix))]
async fn capture_existing_permissions(_path: &Path) -> Option<std::fs::Permissions> {
None
}
#[cfg(all(test, unix))]
mod tests;
#[cfg(test)]
mod truncate_tests;