LINE1 10 20 30 40 50 60 70 80 90 100 1101use std::env;2use std::path::Path;34use rmcp::{5 handler::server::wrapper::Json, model::ErrorData, schemars::JsonSchema, tool, tool_router,6 transport::stdio, ServiceExt,7};8use serde::Serialize;910use crate::report::{render_markdown, write_report};11use crate::{audit_project, utc_date, AuditReport};1213struct MargaretMcp;1415#[tool_router(server_handler)]16impl MargaretMcp {17 #[tool(18 description = "Measure Rust source file sizes in bytes and physical lines, and write a Markdown report."19 )]20 fn audit_project(&self) -> Result<Json<McpAuditResult>, ErrorData> {21 let root = env::current_dir().map_err(internal_error)?;22 let report = audit_project(&root).map_err(internal_error)?;23 let date = utc_date().map_err(internal_error)?;24 let report_path = write_report(&root, &date, &report).map_err(internal_error)?;2526 Ok(Json(build_result(&root, &report, &report_path)))27 }28}2930#[derive(Debug, Serialize, JsonSchema)]31pub struct McpAuditResult {32 pub project_root: String,33 pub discovered_source_count: usize,34 pub files: Vec<McpFileMetric>,35 pub total_bytes: u64,36 pub total_physical_lines: usize,37 pub scan_warnings: Vec<McpWarning>,38 pub verification_complete: bool,39 pub report_path: String,40 pub report_markdown: String,41}4243#[derive(Debug, Serialize, JsonSchema)]44pub struct McpFileMetric {45 pub path: String,46 pub bytes: u64,47 pub physical_lines: usize,48}4950#[derive(Debug, Serialize, JsonSchema)]51pub struct McpWarning {52 pub path: String,53 pub message: String,54}5556fn build_result(root: &Path, report: &AuditReport, report_path: &Path) -> McpAuditResult {57 McpAuditResult {58 project_root: root.display().to_string(),59 discovered_source_count: report.discovered_source_count,60 files: report61 .files62 .iter()63 .map(|metric| McpFileMetric {64 path: metric.path.to_string_lossy().into_owned(),65 bytes: metric.bytes,66 physical_lines: metric.physical_lines,67 })68 .collect(),69 total_bytes: report.files.iter().map(|file| file.bytes).sum(),70 total_physical_lines: report.files.iter().map(|file| file.physical_lines).sum(),71 scan_warnings: report72 .warnings73 .iter()74 .map(|warning| McpWarning {75 path: warning.path.to_string_lossy().into_owned(),76 message: warning.message.clone(),77 })78 .collect(),79 verification_complete: report.warnings.is_empty(),80 report_path: report_path.to_string_lossy().into_owned(),81 report_markdown: render_markdown(report),82 }83}8485fn internal_error(error: impl std::fmt::Display) -> ErrorData {86 ErrorData::internal_error(error.to_string(), None)87}8889pub fn serve_stdio() -> Result<(), String> {90 let runtime = tokio::runtime::Builder::new_multi_thread()91 .enable_all()92 .build()93 .map_err(|error| format!("could not start MCP runtime: {error}"))?;9495 runtime.block_on(async {96 let service = MargaretMcp97 .serve(stdio())98 .await99 .map_err(|error| format!("could not start MCP stdio server: {error}"))?;100 service101 .waiting()102 .await103 .map(|_| ())104 .map_err(|error| format!("MCP stdio server failed: {error}"))105 })106}