Update src/mcp.rs
Browse files- src/mcp.rs +10 -13
src/mcp.rs
CHANGED
|
@@ -4,7 +4,6 @@ use anyhow::Result;
|
|
| 4 |
use axum::{
|
| 5 |
extract::State,
|
| 6 |
response::sse::{Event, KeepAlive, Sse},
|
| 7 |
-
response::IntoResponse,
|
| 8 |
routing::{get, post},
|
| 9 |
Json, Router,
|
| 10 |
};
|
|
@@ -17,7 +16,8 @@ use std::path::Path;
|
|
| 17 |
use std::sync::Arc;
|
| 18 |
use std::time::Duration;
|
| 19 |
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
| 20 |
-
use
|
|
|
|
| 21 |
use walkdir::WalkDir;
|
| 22 |
|
| 23 |
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
@@ -198,7 +198,6 @@ impl McpServer {
|
|
| 198 |
return Err(anyhow::anyhow!("Directory does not exist: {:?}", dir_path));
|
| 199 |
}
|
| 200 |
|
| 201 |
-
// 1. Collect all valid candidate files
|
| 202 |
let paths: Vec<_> = WalkDir::new(&dir_path)
|
| 203 |
.into_iter()
|
| 204 |
.filter_map(|e| e.ok())
|
|
@@ -208,7 +207,6 @@ impl McpServer {
|
|
| 208 |
|
| 209 |
let total_found = paths.len();
|
| 210 |
|
| 211 |
-
// 2. Parallel ingestion with Rayon
|
| 212 |
let parsed_docs: Vec<ParsedDocument> = paths
|
| 213 |
.par_iter()
|
| 214 |
.filter_map(|path| match DocumentParser::parse_file(path) {
|
|
@@ -221,8 +219,6 @@ impl McpServer {
|
|
| 221 |
.collect();
|
| 222 |
|
| 223 |
let parsed_count = parsed_docs.len();
|
| 224 |
-
|
| 225 |
-
// 3. Batch commit to Tantivy engine
|
| 226 |
let indexed_count = self.engine.add_documents(collection, parsed_docs).await?;
|
| 227 |
|
| 228 |
Ok(json!({
|
|
@@ -292,7 +288,6 @@ impl McpServer {
|
|
| 292 |
}
|
| 293 |
}
|
| 294 |
|
| 295 |
-
/// Runs the server loop over standard I/O (STDIO) transport
|
| 296 |
pub async fn run_stdio(self: Arc<Self>) -> Result<()> {
|
| 297 |
let stdin = tokio::io::stdin();
|
| 298 |
let mut reader = BufReader::new(stdin).lines();
|
|
@@ -323,7 +318,6 @@ impl McpServer {
|
|
| 323 |
Ok(())
|
| 324 |
}
|
| 325 |
|
| 326 |
-
/// Runs the server over Axum SSE/HTTP transport
|
| 327 |
pub async fn run_http(self: Arc<Self>, host: &str, port: u16) -> Result<()> {
|
| 328 |
let app = Router::new()
|
| 329 |
.route("/rpc", post(handle_http_rpc))
|
|
@@ -351,11 +345,14 @@ async fn handle_http_rpc(
|
|
| 351 |
async fn handle_sse(
|
| 352 |
State(_server): State<Arc<McpServer>>,
|
| 353 |
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
|
| 354 |
-
let
|
| 355 |
-
Ok(Event::default().event("endpoint").data("/rpc"))
|
| 356 |
-
])
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
Sse::new(stream).keep_alive(KeepAlive::default())
|
| 361 |
}
|
|
|
|
| 4 |
use axum::{
|
| 5 |
extract::State,
|
| 6 |
response::sse::{Event, KeepAlive, Sse},
|
|
|
|
| 7 |
routing::{get, post},
|
| 8 |
Json, Router,
|
| 9 |
};
|
|
|
|
| 16 |
use std::sync::Arc;
|
| 17 |
use std::time::Duration;
|
| 18 |
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
| 19 |
+
use tokio_stream::StreamExt;
|
| 20 |
+
use tracing::info;
|
| 21 |
use walkdir::WalkDir;
|
| 22 |
|
| 23 |
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
| 198 |
return Err(anyhow::anyhow!("Directory does not exist: {:?}", dir_path));
|
| 199 |
}
|
| 200 |
|
|
|
|
| 201 |
let paths: Vec<_> = WalkDir::new(&dir_path)
|
| 202 |
.into_iter()
|
| 203 |
.filter_map(|e| e.ok())
|
|
|
|
| 207 |
|
| 208 |
let total_found = paths.len();
|
| 209 |
|
|
|
|
| 210 |
let parsed_docs: Vec<ParsedDocument> = paths
|
| 211 |
.par_iter()
|
| 212 |
.filter_map(|path| match DocumentParser::parse_file(path) {
|
|
|
|
| 219 |
.collect();
|
| 220 |
|
| 221 |
let parsed_count = parsed_docs.len();
|
|
|
|
|
|
|
| 222 |
let indexed_count = self.engine.add_documents(collection, parsed_docs).await?;
|
| 223 |
|
| 224 |
Ok(json!({
|
|
|
|
| 288 |
}
|
| 289 |
}
|
| 290 |
|
|
|
|
| 291 |
pub async fn run_stdio(self: Arc<Self>) -> Result<()> {
|
| 292 |
let stdin = tokio::io::stdin();
|
| 293 |
let mut reader = BufReader::new(stdin).lines();
|
|
|
|
| 318 |
Ok(())
|
| 319 |
}
|
| 320 |
|
|
|
|
| 321 |
pub async fn run_http(self: Arc<Self>, host: &str, port: u16) -> Result<()> {
|
| 322 |
let app = Router::new()
|
| 323 |
.route("/rpc", post(handle_http_rpc))
|
|
|
|
| 345 |
async fn handle_sse(
|
| 346 |
State(_server): State<Arc<McpServer>>,
|
| 347 |
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
|
| 348 |
+
let initial = tokio_stream::iter(vec![
|
| 349 |
+
Ok(Event::default().event("endpoint").data("/rpc")),
|
| 350 |
+
]);
|
| 351 |
+
|
| 352 |
+
let interval = tokio_stream::wrappers::IntervalStream::new(tokio::time::interval(Duration::from_secs(15)))
|
| 353 |
+
.map(|_| Ok(Event::default().comment("keep-alive")));
|
| 354 |
+
|
| 355 |
+
let stream = initial.chain(interval);
|
| 356 |
|
| 357 |
Sse::new(stream).keep_alive(KeepAlive::default())
|
| 358 |
}
|