read.rs

 1use std::path::PathBuf;
 2
 3use async_lsp::LanguageServer;
 4use async_lsp::lsp_types::{
 5    DocumentDiagnosticParams, DocumentDiagnosticReportResult, PartialResultParams,
 6    TextDocumentIdentifier, Url, WorkDoneProgressParams,
 7};
 8use rmcp::ErrorData as MCPError;
 9use rmcp::{handler::server::wrapper::Parameters, model::CallToolResult, schemars, serde_json};
10
11use crate::mcp::*;
12
13#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
14pub struct ReadToolArgs {
15    pub path: PathBuf,
16    pub range_start: usize,
17    pub range_end: usize,
18}
19
20#[derive(Debug, serde::Serialize)]
21pub struct ReadToolOutput {
22    pub content: Option<String>,
23    pub diagnostics: DocumentDiagnosticReportResult,
24}
25
26pub async fn call(
27    server: &MCPServer,
28    Parameters(args): Parameters<ReadToolArgs>,
29) -> Result<CallToolResult, MCPError> {
30    let file_path = server.project_root.join(&args.path);
31    let file_path = tokio::fs::canonicalize(file_path).await.unwrap();
32    let content = tokio::fs::read_to_string(&file_path)
33        .await
34        .map_err(|e| MCPError::invalid_request(format!("Failed to read file: {e}"), None))?;
35
36    let mut lsp_server = server.lsp_server.clone();
37
38    let diagnostic_report = lsp_server
39        .document_diagnostic(DocumentDiagnosticParams {
40            text_document: TextDocumentIdentifier::new(Url::from_file_path(file_path).unwrap()),
41            identifier: None,
42            previous_result_id: None,
43            work_done_progress_params: WorkDoneProgressParams {
44                work_done_token: None,
45            },
46            partial_result_params: PartialResultParams {
47                partial_result_token: None,
48            },
49        })
50        .await
51        .unwrap();
52
53    Ok(CallToolResult {
54        content: vec![],
55        structured_content: Some(serde_json::json!(ReadToolOutput {
56            content: Some(content),
57            diagnostics: diagnostic_report
58        })),
59        is_error: None,
60        meta: None,
61    })
62}