logana 0.5.1

Turn any log source — files, compressed archives, Docker, or OTel streams — into structured data. Filter by pattern, field, or date range; annotate lines; bookmark findings; and export to Markdown, Jira, or AI assistants via the built-in MCP server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::net::SocketAddr;
use std::sync::Arc;

use axum::Router;
use rmcp::{
    ErrorData, RoleServer, ServerHandler,
    handler::server::{router::tool::ToolRouter, wrapper::Parameters},
    model::*,
    schemars, serde,
    service::RequestContext,
    tool, tool_handler, tool_router,
    transport::streamable_http_server::{
        StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
    },
};
use tokio::sync::{RwLock, mpsc};
use tokio_util::sync::CancellationToken;

use crate::db::LogManager;
use crate::ingestion::FileReader;
use crate::types::Comment;

#[derive(Default)]
pub struct McpSnapshot {
    pub marked_lines: Vec<(usize, String)>,
    pub annotations: Vec<Comment>,
}

pub enum McpCommand {
    ToggleMark(usize),
    AddAnnotation {
        text: String,
        line_indices: Vec<usize>,
    },
    RemoveAnnotation(usize),
}

pub struct McpServerHandle {
    pub cancel: CancellationToken,
    pub port: u16,
}

pub fn build_marked_lines(reader: &FileReader, log_manager: &LogManager) -> Vec<(usize, String)> {
    log_manager
        .get_marked_indices()
        .into_iter()
        .filter(|&i| i < reader.line_count())
        .map(|i| {
            let text = String::from_utf8_lossy(reader.get_line(i)).into_owned();
            (i + 1, text)
        })
        .collect()
}

pub fn build_annotations(log_manager: &LogManager) -> Vec<Comment> {
    log_manager.get_comments().to_vec()
}

pub fn format_marks_resource(snapshot: &McpSnapshot) -> String {
    snapshot
        .marked_lines
        .iter()
        .map(|(n, t)| format!("{n}: {t}"))
        .collect::<Vec<_>>()
        .join("\n")
}

pub fn format_annotations_resource(snapshot: &McpSnapshot) -> String {
    snapshot
        .annotations
        .iter()
        .map(|a| {
            let lines = a
                .line_indices
                .iter()
                .map(|&i| i.to_string())
                .collect::<Vec<_>>()
                .join(",");
            format!("Lines: {lines}\n{}\n---", a.text)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct ToggleMarkParams {
    /// 1-based line number to toggle mark on
    line_index: usize,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct AddAnnotationParams {
    /// Annotation text
    text: String,
    /// 1-based line numbers this annotation applies to
    line_indices: Vec<usize>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct RemoveAnnotationParams {
    /// 0-based annotation index
    index: usize,
}

#[derive(Clone)]
struct LoganaServer {
    snapshot: Arc<RwLock<McpSnapshot>>,
    cmd_tx: mpsc::Sender<McpCommand>,
    tool_router: ToolRouter<Self>,
}

impl LoganaServer {
    fn new(snapshot: Arc<RwLock<McpSnapshot>>, cmd_tx: mpsc::Sender<McpCommand>) -> Self {
        Self {
            snapshot,
            cmd_tx,
            tool_router: Self::tool_router(),
        }
    }
}

#[tool_router]
impl LoganaServer {
    #[tool(description = "Toggle a mark on a log line (1-based line number)")]
    async fn toggle_mark(
        &self,
        Parameters(ToggleMarkParams { line_index }): Parameters<ToggleMarkParams>,
    ) -> String {
        let _ = self.cmd_tx.send(McpCommand::ToggleMark(line_index)).await;
        format!("Mark toggled on line {line_index}")
    }

    #[tool(description = "Add an annotation to log lines (1-based line numbers)")]
    async fn add_annotation(
        &self,
        Parameters(AddAnnotationParams { text, line_indices }): Parameters<AddAnnotationParams>,
    ) -> String {
        let _ = self
            .cmd_tx
            .send(McpCommand::AddAnnotation { text, line_indices })
            .await;
        "Annotation added".to_string()
    }

    #[tool(description = "Remove an annotation by its 0-based index")]
    async fn remove_annotation(
        &self,
        Parameters(RemoveAnnotationParams { index }): Parameters<RemoveAnnotationParams>,
    ) -> String {
        let _ = self.cmd_tx.send(McpCommand::RemoveAnnotation(index)).await;
        format!("Annotation {index} removed")
    }
}

#[tool_handler(router = self.tool_router)]
impl ServerHandler for LoganaServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(
            ServerCapabilities::builder()
                .enable_resources()
                .enable_tools()
                .build(),
        )
        .with_server_info(Implementation::new("logana", env!("CARGO_PKG_VERSION")))
        .with_instructions("Logana MCP server: read marks and annotations")
    }

    async fn list_resources(
        &self,
        _request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListResourcesResult, ErrorData> {
        Ok(ListResourcesResult::with_all_items(vec![
            Resource::new(RawResource::new("logana://marks", "Marked Lines"), None),
            Resource::new(
                RawResource::new("logana://annotations", "Annotations"),
                None,
            ),
        ]))
    }

    async fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ReadResourceResult, ErrorData> {
        let snapshot = self.snapshot.read().await;
        let text = match request.uri.as_str() {
            "logana://marks" => format_marks_resource(&snapshot),
            "logana://annotations" => format_annotations_resource(&snapshot),
            _ => {
                return Err(ErrorData::invalid_params(
                    format!("Unknown resource URI: {}", request.uri),
                    None,
                ));
            }
        };
        Ok(ReadResourceResult::new(vec![ResourceContents::text(
            text,
            request.uri,
        )]))
    }
}

pub async fn start_mcp_server(
    port: u16,
    snapshot: Arc<RwLock<McpSnapshot>>,
    cmd_tx: mpsc::Sender<McpCommand>,
) -> std::io::Result<McpServerHandle> {
    let addr: SocketAddr = ([0, 0, 0, 0], port).into();
    let listener = tokio::net::TcpListener::bind(addr).await?;

    let cancel = CancellationToken::new();
    let child_token = cancel.child_token();

    tokio::spawn(async move {
        let server = LoganaServer::new(snapshot, cmd_tx);
        // `StreamableHttpServerConfig` is `#[non_exhaustive]`, so struct-expression
        // initialisation is not allowed from outside the defining crate.
        #[allow(clippy::field_reassign_with_default)]
        let config = {
            let mut c = StreamableHttpServerConfig::default();
            c.cancellation_token = child_token.clone();
            c
        };

        let service: StreamableHttpService<LoganaServer, LocalSessionManager> =
            StreamableHttpService::new(
                move || Ok(server.clone()),
                Arc::new(LocalSessionManager::default()),
                config,
            );

        let router = Router::new().nest_service("/mcp", service);

        if let Err(e) = axum::serve(listener, router)
            .with_graceful_shutdown(async move { child_token.cancelled_owned().await })
            .await
        {
            eprintln!("MCP server error: {e}");
        }
    });

    Ok(McpServerHandle { cancel, port })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio::sync::{RwLock, mpsc};

    use crate::db::Database;
    use crate::db::LogManager;
    use crate::types::Comment;

    async fn make_reader_and_manager(lines: &[&str]) -> (FileReader, LogManager) {
        let bytes = lines.join("\n").into_bytes();
        let reader = FileReader::from_bytes(bytes);
        let db = Arc::new(Database::in_memory().await.unwrap());
        let lm = LogManager::new(db, None).await;
        (reader, lm)
    }

    fn make_server() -> (LoganaServer, mpsc::Receiver<McpCommand>) {
        let snapshot = Arc::new(RwLock::new(McpSnapshot::default()));
        let (tx, rx) = mpsc::channel(16);
        let server = LoganaServer::new(snapshot, tx);
        (server, rx)
    }

    // ── build_marked_lines ───────────────────────────────────────────

    #[tokio::test]
    async fn test_build_marked_lines_empty() {
        let (reader, lm) = make_reader_and_manager(&["a", "b", "c"]).await;
        let result = build_marked_lines(&reader, &lm);
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_build_marked_lines_with_marks() {
        let (reader, mut lm) = make_reader_and_manager(&["a", "b", "c"]).await;
        lm.toggle_mark(0);
        lm.toggle_mark(2);
        let result = build_marked_lines(&reader, &lm);
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], (1, "a".to_string()));
        assert_eq!(result[1], (3, "c".to_string()));
    }

    // ── build_annotations ────────────────────────────────────────────

    #[tokio::test]
    async fn test_build_annotations_empty() {
        let (_, lm) = make_reader_and_manager(&["a"]).await;
        let result = build_annotations(&lm);
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_build_annotations_with_comments() {
        let (_, mut lm) = make_reader_and_manager(&["a", "b"]).await;
        lm.add_comment("note".to_string(), vec![0, 1]);
        let result = build_annotations(&lm);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].text, "note");
        assert_eq!(result[0].line_indices, vec![0, 1]);
    }

    // ── format helpers ───────────────────────────────────────────────

    #[test]
    fn test_read_resource_marks_uri() {
        let snap = McpSnapshot {
            marked_lines: vec![(2, "marked".to_string())],
            annotations: vec![],
        };
        let text = format_marks_resource(&snap);
        assert!(text.contains("2: marked"));
    }

    #[test]
    fn test_read_resource_annotations_uri() {
        let snap = McpSnapshot {
            marked_lines: vec![],
            annotations: vec![Comment {
                text: "my note".to_string(),
                line_indices: vec![1, 2],
            }],
        };
        let text = format_annotations_resource(&snap);
        assert!(text.contains("Lines: 1,2"));
        assert!(text.contains("my note"));
    }

    #[test]
    fn test_list_tools_returns_three() {
        let snap = McpSnapshot::default();
        let snapshot = Arc::new(RwLock::new(snap));
        let (tx, _rx) = mpsc::channel(1);
        let server = LoganaServer::new(snapshot, tx);
        let tools = server.tool_router.list_all();
        assert_eq!(tools.len(), 3);
    }

    // ── tool command dispatch ────────────────────────────────────────

    #[tokio::test]
    async fn test_toggle_mark_sends_command() {
        let (server, mut rx) = make_server();
        server
            .toggle_mark(Parameters(ToggleMarkParams { line_index: 5 }))
            .await;
        match rx.try_recv().unwrap() {
            McpCommand::ToggleMark(idx) => assert_eq!(idx, 5),
            _ => panic!("expected ToggleMark"),
        }
    }

    #[tokio::test]
    async fn test_add_annotation_sends_command() {
        let (server, mut rx) = make_server();
        server
            .add_annotation(Parameters(AddAnnotationParams {
                text: "note".to_string(),
                line_indices: vec![1, 2],
            }))
            .await;
        match rx.try_recv().unwrap() {
            McpCommand::AddAnnotation { text, line_indices } => {
                assert_eq!(text, "note");
                assert_eq!(line_indices, vec![1, 2]);
            }
            _ => panic!("expected AddAnnotation"),
        }
    }

    #[tokio::test]
    async fn test_remove_annotation_sends_command() {
        let (server, mut rx) = make_server();
        server
            .remove_annotation(Parameters(RemoveAnnotationParams { index: 3 }))
            .await;
        match rx.try_recv().unwrap() {
            McpCommand::RemoveAnnotation(idx) => assert_eq!(idx, 3),
            _ => panic!("expected RemoveAnnotation"),
        }
    }

    // ── start_mcp_server ─────────────────────────────────────────────

    #[tokio::test]
    async fn test_start_mcp_server_binds_successfully() {
        let snapshot = Arc::new(RwLock::new(McpSnapshot::default()));
        let (tx, _rx) = mpsc::channel(1);
        let result = start_mcp_server(0, snapshot, tx).await;
        assert!(result.is_ok());
        let handle = result.unwrap();
        assert!(handle.port == 0);
        handle.cancel.cancel();
    }

    #[tokio::test]
    async fn test_start_mcp_server_fails_on_used_port() {
        let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();

        let snapshot = Arc::new(RwLock::new(McpSnapshot::default()));
        let (tx, _rx) = mpsc::channel(1);
        let result = start_mcp_server(port, snapshot, tx).await;
        assert!(result.is_err());
    }
}