use metis_core::{
application::Application,
dal::Database,
};
use rust_mcp_sdk::{
macros::{mcp_tool, JsonSchema},
schema::{schema_utils::CallToolError, CallToolResult, TextContent},
};
use serde::{Deserialize, Serialize};
use std::path::Path;
use tokio::fs;
#[mcp_tool(
name = "update_document_content",
description = "Update content of a specific H2 section in a document",
idempotent_hint = false,
destructive_hint = false,
open_world_hint = false,
read_only_hint = false
)]
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct UpdateDocumentContentTool {
pub project_path: String,
pub document_path: String,
pub section_heading: String,
pub new_content: String,
}
impl UpdateDocumentContentTool {
pub async fn call_tool(&self) -> std::result::Result<CallToolResult, CallToolError> {
let metis_dir = Path::new(&self.project_path);
if !metis_dir.exists() || !metis_dir.is_dir() {
return Err(CallToolError::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!(
"Metis workspace not found at {}. Run initialize_project first.",
metis_dir.display()
),
)));
}
let full_document_path = metis_dir.join(&self.document_path);
if !full_document_path.exists() {
return Err(CallToolError::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Document not found at {}", full_document_path.display()),
)));
}
let content = fs::read_to_string(&full_document_path)
.await
.map_err(|e| CallToolError::new(e))?;
let updated_content = self.update_section_content(&content)?;
fs::write(&full_document_path, updated_content)
.await
.map_err(|e| CallToolError::new(e))?;
self.sync_workspace(metis_dir).await?;
let response = serde_json::json!({
"success": true,
"document_path": self.document_path,
"section_heading": self.section_heading,
"updated": true,
"message": format!("Successfully updated section '{}' in document", self.section_heading),
"auto_synced": true
});
Ok(CallToolResult::text_content(vec![TextContent::from(
serde_json::to_string_pretty(&response).map_err(CallToolError::new)?,
)]))
}
fn update_section_content(&self, content: &str) -> Result<String, CallToolError> {
let lines: Vec<&str> = content.lines().collect();
let mut result_lines = Vec::new();
let mut in_target_section = false;
let mut found_section = false;
let target_heading = format!("## {}", self.section_heading);
for line in lines {
if line.trim() == target_heading.trim() {
found_section = true;
in_target_section = true;
result_lines.push(line);
result_lines.push("");
result_lines.push(&self.new_content);
result_lines.push("");
continue;
}
if in_target_section {
if line.trim().starts_with("## ") {
in_target_section = false;
result_lines.push(line);
} else {
continue;
}
} else {
result_lines.push(line);
}
}
if !found_section {
return Err(CallToolError::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!(
"Section heading '{}' not found in document",
self.section_heading
),
)));
}
Ok(result_lines.join("\n"))
}
async fn sync_workspace(&self, metis_dir: &Path) -> Result<(), CallToolError> {
let db_path = metis_dir.join("metis.db");
let database = Database::new(db_path.to_str().unwrap()).map_err(|e| {
CallToolError::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to open database for sync: {}", e),
))
})?;
let app = Application::new(database);
app.sync_directory(metis_dir)
.await
.map_err(|e| CallToolError::new(e))?;
Ok(())
}
}