leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// MCP JSON-RPC Protocol Types
//
// This module defines the JSON-RPC 2.0 protocol types used by the MCP server.

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// JSON-RPC 2.0 specification version
pub const JSONRPC_VERSION: &str = "2.0";

/// JSON-RPC 2.0 Request
///
/// Per the JSON-RPC 2.0 spec, a request must have:
/// - jsonrpc: "2.0"
/// - method: A string containing the method name to invoke
/// - id: Optional request identifier (absent for notifications)
/// - params: Optional parameters (can be omitted if not needed)
#[derive(Debug, Clone, Deserialize)]
pub struct JsonRpcRequest {
    /// JSON-RPC version string, must be "2.0"
    pub jsonrpc: String,
    /// Unique identifier for the request (None for notifications)
    #[serde(default)]
    pub id: Option<Value>,
    /// Method name to be invoked
    pub method: String,
    /// Parameters for the method call
    #[serde(default)]
    pub params: Option<Value>,
}

impl JsonRpcRequest {
    /// Validate the request conforms to JSON-RPC 2.0
    pub fn validate(&self) -> Result<(), JsonRpcError> {
        if self.jsonrpc != JSONRPC_VERSION {
            return Err(JsonRpcError::invalid_request(format!(
                "Unsupported JSON-RPC version: {}",
                self.jsonrpc
            )));
        }
        Ok(())
    }

    /// Check if this is a notification (no id field)
    pub fn is_notification(&self) -> bool {
        self.id.is_none()
    }

    /// Extract tool call parameters from the request
    ///
    /// Expects params to contain: {name: string, arguments: object}
    pub fn extract_tool_call(&self) -> Result<ToolCallParams, JsonRpcError> {
        let params = self
            .params
            .as_ref()
            .ok_or_else(|| JsonRpcError::invalid_params("Missing params"))?;

        let name = params
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| JsonRpcError::invalid_params("Missing or invalid 'name' field"))?;

        let arguments = params
            .get("arguments")
            .cloned()
            .unwrap_or_else(|| serde_json::json!({}));

        Ok(ToolCallParams {
            name: name.to_string(),
            arguments,
        })
    }
}

/// JSON-RPC 2.0 Notification
///
/// A notification is a Request object without an "id" member.
/// The Server MUST NOT reply to a notification, including errors.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JsonRpcNotification {
    /// JSON-RPC version string, must be "2.0"
    pub jsonrpc: String,
    /// Method name for the notification
    pub method: String,
    /// Parameters for the notification
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcNotification {
    /// Create a new notification
    pub fn new(method: impl Into<String>) -> Self {
        Self {
            jsonrpc: JSONRPC_VERSION.to_string(),
            method: method.into(),
            params: None,
        }
    }

    /// Create a notification with parameters
    pub fn with_params(method: impl Into<String>, params: Value) -> Self {
        Self {
            jsonrpc: JSONRPC_VERSION.to_string(),
            method: method.into(),
            params: Some(params),
        }
    }

    /// Check if this is a known MCP notification type
    pub fn notification_type(&self) -> NotificationType {
        match self.method.as_str() {
            "notifications/initialized" => NotificationType::Initialized,
            "notifications/cancelled" => NotificationType::Cancelled,
            "notifications/progress" => NotificationType::Progress,
            "notifications/message" => NotificationType::Message,
            "notifications/resources/updated" => NotificationType::ResourcesUpdated,
            "notifications/resources/list_changed" => NotificationType::ResourcesListChanged,
            "notifications/tools/list_changed" => NotificationType::ToolsListChanged,
            "notifications/prompts/list_changed" => NotificationType::PromptsListChanged,
            "notifications/roots/list_changed" => NotificationType::RootsListChanged,
            _ => NotificationType::Unknown(self.method.clone()),
        }
    }
}

/// Known MCP notification types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotificationType {
    /// Sent by the client after initialization is complete
    Initialized,
    /// Sent to cancel a request
    Cancelled,
    /// Progress notification for long-running operations
    Progress,
    /// Logging message notification
    Message,
    /// Resource content updated notification
    ResourcesUpdated,
    /// Resource list changed notification
    ResourcesListChanged,
    /// Tool list changed notification
    ToolsListChanged,
    /// Prompt list changed notification
    PromptsListChanged,
    /// Roots list changed notification
    RootsListChanged,
    /// Unknown notification type
    Unknown(String),
}

impl std::fmt::Display for NotificationType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NotificationType::Initialized => write!(f, "notifications/initialized"),
            NotificationType::Cancelled => write!(f, "notifications/cancelled"),
            NotificationType::Progress => write!(f, "notifications/progress"),
            NotificationType::Message => write!(f, "notifications/message"),
            NotificationType::ResourcesUpdated => write!(f, "notifications/resources/updated"),
            NotificationType::ResourcesListChanged => {
                write!(f, "notifications/resources/list_changed")
            }
            NotificationType::ToolsListChanged => write!(f, "notifications/tools/list_changed"),
            NotificationType::PromptsListChanged => write!(f, "notifications/prompts/list_changed"),
            NotificationType::RootsListChanged => write!(f, "notifications/roots/list_changed"),
            NotificationType::Unknown(method) => write!(f, "{}", method),
        }
    }
}

/// JSON-RPC 2.0 Message (Request or Notification)
///
/// This enum allows parsing either a request (with id) or notification (without id)
/// from incoming JSON data.
#[derive(Debug, Clone)]
pub enum JsonRpcMessage {
    /// A request with an id (expects response)
    Request(JsonRpcRequest),
    /// A notification without id (no response expected)
    Notification(JsonRpcNotification),
}

impl JsonRpcMessage {
    /// Parse a JSON string into a JsonRpcMessage
    pub fn from_json(json: &str) -> Result<Self, JsonRpcError> {
        let raw: serde_json::Value = serde_json::from_str(json)
            .map_err(|e| JsonRpcError::parse_error(format!("Invalid JSON: {}", e)))?;

        if !raw.is_object() {
            return Err(JsonRpcError::invalid_request(
                "Message must be a JSON object",
            ));
        }

        let obj = raw.as_object().unwrap();

        if let Some(jsonrpc) = obj.get("jsonrpc") {
            if jsonrpc.as_str() != Some(JSONRPC_VERSION) {
                return Err(JsonRpcError::invalid_request(format!(
                    "Unsupported JSON-RPC version: {:?}",
                    jsonrpc
                )));
            }
        } else {
            return Err(JsonRpcError::invalid_request("Missing jsonrpc field"));
        }

        if !obj.contains_key("method") {
            return Err(JsonRpcError::invalid_request("Missing method field"));
        }

        if obj.contains_key("id") {
            let request: JsonRpcRequest = serde_json::from_value(raw)
                .map_err(|e| JsonRpcError::invalid_request(format!("Invalid request: {}", e)))?;
            Ok(JsonRpcMessage::Request(request))
        } else {
            let notification: JsonRpcNotification = serde_json::from_value(raw).map_err(|e| {
                JsonRpcError::invalid_request(format!("Invalid notification: {}", e))
            })?;
            Ok(JsonRpcMessage::Notification(notification))
        }
    }

    /// Check if this message is a notification
    pub fn is_notification(&self) -> bool {
        matches!(self, JsonRpcMessage::Notification(_))
    }

    /// Check if this message is a request
    pub fn is_request(&self) -> bool {
        matches!(self, JsonRpcMessage::Request(_))
    }

    /// Get the method name from the message
    pub fn method(&self) -> &str {
        match self {
            JsonRpcMessage::Request(req) => &req.method,
            JsonRpcMessage::Notification(notif) => &notif.method,
        }
    }

    /// Get the id if this is a request
    pub fn id(&self) -> Option<&Value> {
        match self {
            JsonRpcMessage::Request(req) => req.id.as_ref(),
            JsonRpcMessage::Notification(_) => None,
        }
    }
}

/// JSON-RPC 2.0 Response
///
/// A response can contain either a result or an error, but not both.
#[derive(Debug, Clone, Serialize)]
pub struct JsonRpcResponse {
    /// JSON-RPC version string, must be "2.0"
    pub jsonrpc: String,
    /// Identifier matching the original request
    pub id: Value,
    /// Result of the method call, if successful
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    /// Error information, if the method call failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

impl JsonRpcResponse {
    /// Create a successful response
    pub fn success(id: Value, result: Value) -> Self {
        Self {
            jsonrpc: JSONRPC_VERSION.to_string(),
            id,
            result: Some(result),
            error: None,
        }
    }

    /// Create a successful response with optional id
    pub fn success_opt(id: Option<Value>, result: Value) -> Self {
        Self::success(id.unwrap_or(Value::Null), result)
    }

    /// Create an error response
    pub fn error(id: Value, error: JsonRpcError) -> Self {
        Self {
            jsonrpc: JSONRPC_VERSION.to_string(),
            id,
            result: None,
            error: Some(error),
        }
    }

    /// Create an error response with optional id
    pub fn error_opt(id: Option<Value>, error: JsonRpcError) -> Self {
        Self::error(id.unwrap_or(Value::Null), error)
    }

    /// Create a response from a Result
    pub fn from_result(id: Value, result: Result<Value, JsonRpcError>) -> Self {
        match result {
            Ok(value) => Self::success(id, value),
            Err(err) => Self::error(id, err),
        }
    }

    /// Create a response from a Result with optional id
    pub fn from_result_opt(id: Option<Value>, result: Result<Value, JsonRpcError>) -> Self {
        Self::from_result(id.unwrap_or(Value::Null), result)
    }
}

/// JSON-RPC 2.0 Error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
    /// Error code indicating the type of failure
    pub code: i32,
    /// Human-readable description of the error
    pub message: String,
    /// Optional additional error details
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

impl JsonRpcError {
    /// Create a new JSON-RPC error with the given code and message
    pub fn new(code: i32, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            data: None,
        }
    }

    /// Create a new JSON-RPC error with additional data
    pub fn with_data(code: i32, message: impl Into<String>, data: Value) -> Self {
        Self {
            code,
            message: message.into(),
            data: Some(data),
        }
    }

    /// Create a parse error (code -32700)
    pub fn parse_error(msg: impl Into<String>) -> Self {
        Self::new(error_codes::PARSE_ERROR, msg)
    }

    /// Create an invalid request error (code -32600)
    pub fn invalid_request(msg: impl Into<String>) -> Self {
        Self::new(error_codes::INVALID_REQUEST, msg)
    }

    /// Create a method not found error (code -32601)
    pub fn method_not_found(method: String) -> Self {
        Self::with_data(
            error_codes::METHOD_NOT_FOUND,
            "Method not found",
            serde_json::json!({ "method": method }),
        )
    }

    /// Create an invalid params error (code -32602)
    pub fn invalid_params(msg: impl Into<String>) -> Self {
        Self::new(error_codes::INVALID_PARAMS, msg)
    }

    /// Create an invalid params error with a suggestion
    pub fn invalid_params_with_suggestion(
        msg: impl Into<String>,
        suggestion: impl Into<String>,
    ) -> Self {
        Self::with_data(
            error_codes::INVALID_PARAMS,
            msg,
            serde_json::json!({ "suggestion": suggestion.into() }),
        )
    }

    /// Create an internal error (code -32603)
    pub fn internal_error(msg: impl Into<String>) -> Self {
        Self::new(error_codes::INTERNAL_ERROR, msg)
    }

    /// Create a project not found error
    pub fn project_not_found(project: String) -> Self {
        Self::with_data(
            error_codes::PROJECT_NOT_FOUND,
            "Project not found",
            serde_json::json!({ "project": project }),
        )
    }

    /// Create a project not indexed error
    pub fn project_not_indexed(project: String) -> Self {
        Self::with_data(
            error_codes::PROJECT_NOT_INDEXED,
            "Project not indexed — call leindex_index or pass project_path to auto-index",
            serde_json::json!({
                "project": project,
                "suggestion": "Pass project_path to any tool to auto-index on first use, or call leindex_index explicitly.",
                "error_type": "project_not_indexed"
            }),
        )
    }

    /// Create an initialization failure error with multi-step remediation.
    pub fn init_failed(path: &str, inner: &str) -> Self {
        Self::with_data(
            error_codes::INIT_FAILED,
            format!(
                "Failed to initialize LeIndex for '{}'.\n\
                 Inner error: {}\n\n\
                 Remediation:\n\
                 1. Check write permissions on {}\n\
                 2. Set LEINDEX_HOME=/writable/path env var\n\
                 3. Delete .leindex/ directory and retry\n\
                 4. Check available disk space",
                path, inner, path
            ),
            serde_json::json!({
                "error_type": "init_failed",
                "inner_error": inner,
            }),
        )
    }

    /// Create an indexing failed error
    pub fn indexing_failed(msg: impl Into<String>) -> Self {
        let m = msg.into();
        Self::with_data(
            error_codes::INDEXING_FAILED,
            format!(
                "{}\n\n\
                 Remediation:\n\
                 1. Verify project_path is a valid directory with source files\n\
                 2. Try force_reindex=true to rebuild the index\n\
                 3. Delete .leindex/ directory and retry\n\
                 4. Check disk space and permissions",
                m
            ),
            serde_json::json!({
                "error_type": "indexing_failed",
            }),
        )
    }

    /// Create a search failed error
    pub fn search_failed(msg: impl Into<String>) -> Self {
        let m = msg.into();
        Self::with_data(
            error_codes::SEARCH_FAILED,
            format!(
                "{}\n\n\
                 Remediation:\n\
                 1. Ensure the project is indexed (call leindex_index)\n\
                 2. Try a simpler query (single term instead of complex pattern)\n\
                 3. Use leindex_grep_symbols for exact/regex pattern matching\n\
                 4. Try force_reindex=true if the index may be stale",
                m
            ),
            serde_json::json!({
                "error_type": "search_failed",
            }),
        )
    }

    /// Create a context expansion failed error
    pub fn context_expansion_failed(msg: impl Into<String>) -> Self {
        let m = msg.into();
        Self::with_data(
            error_codes::CONTEXT_EXPANSION_FAILED,
            format!(
                "{}\n\n\
                 Remediation:\n\
                 1. Check that the node_id exists in the indexed project\n\
                 2. Use leindex_grep_symbols to find valid symbol names\n\
                 3. Try force_reindex=true if the index may be stale",
                m
            ),
            serde_json::json!({
                "error_type": "context_expansion_failed",
            }),
        )
    }

    /// Create a memory limit exceeded error
    pub fn memory_limit_exceeded() -> Self {
        Self::with_data(
            error_codes::MEMORY_LIMIT_EXCEEDED,
            "Memory limit exceeded",
            serde_json::json!({
                "error_type": "memory_limit_exceeded",
                "suggestion": "Reduce token_budget, use pagination (offset/limit), or re-index with a smaller scope."
            }),
        )
    }
}

impl std::fmt::Display for JsonRpcError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}] {}", self.code, self.message)
    }
}

impl std::error::Error for JsonRpcError {}

/// Tool call parameters extracted from JSON-RPC request
#[derive(Debug, Clone, Deserialize)]
pub struct ToolCallParams {
    /// Name of the tool to be called
    pub name: String,
    /// Arguments for the tool call
    #[serde(default)]
    pub arguments: Value,
}

/// Progress event for long-running operations
///
/// These events are sent via SSE during indexing to provide
/// real-time feedback without arbitrary timeouts.
#[derive(Debug, Clone, Serialize)]
pub struct ProgressEvent {
    /// Event type: "progress" or "complete"
    #[serde(rename = "type")]
    pub event_type: String,

    /// Current stage of indexing
    pub stage: String,

    /// Current item count
    pub current: usize,

    /// Total items (0 if unknown)
    pub total: usize,

    /// Optional message with additional details

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,

    /// Timestamp in milliseconds
    pub timestamp_ms: u64,
}

impl ProgressEvent {
    /// Create a new progress event
    pub fn progress(
        stage: impl Into<String>,
        current: usize,
        total: usize,
        message: impl Into<String>,
    ) -> Self {
        Self {
            event_type: "progress".to_string(),
            stage: stage.into(),
            current,
            total,
            message: Some(message.into()),
            timestamp_ms: {
                let duration = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap();
                duration.as_millis() as u64
            },
        }
    }

    /// Create a completion event
    pub fn complete(stage: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            event_type: "complete".to_string(),
            stage: stage.into(),
            current: 0,
            total: 0,
            message: Some(message.into()),
            timestamp_ms: {
                let duration = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap();
                duration.as_millis() as u64
            },
        }
    }

    /// Create an error event
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            event_type: "error".to_string(),
            stage: "error".into(),
            current: 0,
            total: 0,
            message: Some(message.into()),
            timestamp_ms: {
                let duration = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap();
                duration.as_millis() as u64
            },
        }
    }
}

/// JSON-RPC Error Codes
///
/// Standard JSON-RPC 2.0 error codes are in the range -32700 to -32603.
/// Server-defined errors should be in the range -32099 to -32000.
pub mod error_codes {
    /// Invalid JSON was received by the server
    pub const PARSE_ERROR: i32 = -32700;

    /// The JSON sent is not a valid Request object
    pub const INVALID_REQUEST: i32 = -32600;

    /// The method does not exist / is not available
    pub const METHOD_NOT_FOUND: i32 = -32601;

    /// Invalid method parameter(s)
    pub const INVALID_PARAMS: i32 = -32602;

    /// Internal JSON-RPC error
    pub const INTERNAL_ERROR: i32 = -32603;

    // MCP-specific error codes (-32000 to -32099)

    /// Project directory not found
    pub const PROJECT_NOT_FOUND: i32 = -32001;

    /// Project exists but has not been indexed
    pub const PROJECT_NOT_INDEXED: i32 = -32002;

    /// Project indexing failed
    pub const INDEXING_FAILED: i32 = -32003;

    /// Search operation failed
    pub const SEARCH_FAILED: i32 = -32004;

    /// Context expansion failed
    pub const CONTEXT_EXPANSION_FAILED: i32 = -32005;

    /// Memory limit exceeded during operation
    pub const MEMORY_LIMIT_EXCEEDED: i32 = -32006;

    /// Index is stale (files changed since last index)
    pub const INDEX_STALE: i32 = -32007;

    /// Index initialization failed (directory creation, permissions, etc.)
    pub const INIT_FAILED: i32 = -32008;

    /// Filesystem error (permissions, disk space, etc.)
    pub const FILESYSTEM_ERROR: i32 = -32009;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_jsonrpc_request_valid() {
        let json = r#"{
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": {"name": "test", "arguments": {}}
        }"#;

        let req: JsonRpcRequest = serde_json::from_str(json).unwrap();
        assert!(req.validate().is_ok());
        assert_eq!(req.method, "tools/call");
        assert!(!req.is_notification());
        assert_eq!(req.id, Some(serde_json::json!(1)));
    }

    #[test]
    fn test_jsonrpc_request_notification() {
        let json = r#"{
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        }"#;

        let req: JsonRpcRequest = serde_json::from_str(json).unwrap();
        assert!(req.validate().is_ok());
        assert!(req.is_notification());
        assert!(req.id.is_none());
    }

    #[test]
    fn test_jsonrpc_notification_parsing() {
        let json = r#"{
            "jsonrpc": "2.0",
            "method": "notifications/initialized",
            "params": {}
        }"#;

        let notif: JsonRpcNotification = serde_json::from_str(json).unwrap();
        assert_eq!(notif.method, "notifications/initialized");
        assert_eq!(notif.notification_type(), NotificationType::Initialized);
    }

    #[test]
    fn test_jsonrpc_message_request() {
        let json = r#"{
            "jsonrpc": "2.0",
            "id": 42,
            "method": "tools/call",
            "params": {"name": "test"}
        }"#;

        let msg = JsonRpcMessage::from_json(json).unwrap();
        assert!(msg.is_request());
        assert!(!msg.is_notification());
        assert_eq!(msg.method(), "tools/call");
        assert_eq!(msg.id(), Some(&serde_json::json!(42)));
    }

    #[test]
    fn test_jsonrpc_message_notification() {
        let json = r#"{
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        }"#;

        let msg = JsonRpcMessage::from_json(json).unwrap();
        assert!(msg.is_notification());
        assert!(!msg.is_request());
        assert_eq!(msg.method(), "notifications/initialized");
        assert!(msg.id().is_none());
    }

    #[test]
    fn test_jsonrpc_request_invalid_version() {
        let json = r#"{
            "jsonrpc": "1.0",
            "id": 1,
            "method": "tools/call"
        }"#;

        let req: JsonRpcRequest = serde_json::from_str(json).unwrap();
        assert!(req.validate().is_err());
    }

    #[test]
    fn test_jsonrpc_message_invalid_version() {
        let json = r#"{
            "jsonrpc": "1.0",
            "id": 1,
            "method": "tools/call"
        }"#;

        let result = JsonRpcMessage::from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_jsonrpc_message_missing_method() {
        let json = r#"{
            "jsonrpc": "2.0",
            "id": 1
        }"#;

        let result = JsonRpcMessage::from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_jsonrpc_response_success() {
        let response =
            JsonRpcResponse::success(serde_json::json!(1), serde_json::json!({"result": "ok"}));

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"result\""));
        assert!(!json.contains("\"error\""));
    }

    #[test]
    fn test_jsonrpc_response_error() {
        let error = JsonRpcError::invalid_params("Missing required field");
        let response = JsonRpcResponse::error(serde_json::json!(1), error);

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"error\""));
        assert!(!json.contains("\"result\""));
    }

    #[test]
    fn test_jsonrpc_response_with_null_id() {
        let response =
            JsonRpcResponse::success(serde_json::Value::Null, serde_json::json!({"result": "ok"}));

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"id\":null"));
    }

    #[test]
    fn test_extract_tool_call() {
        let json = r#"{
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": {
                "name": "leindex_search",
                "arguments": {"query": "test"}
            }
        }"#;

        let req: JsonRpcRequest = serde_json::from_str(json).unwrap();
        let tool_call = req.extract_tool_call().unwrap();

        assert_eq!(tool_call.name, "leindex_search");
        assert_eq!(tool_call.arguments["query"], "test");
    }

    #[test]
    fn test_error_codes() {
        assert_eq!(error_codes::PARSE_ERROR, -32700);
        assert_eq!(error_codes::INVALID_REQUEST, -32600);
        assert_eq!(error_codes::METHOD_NOT_FOUND, -32601);
        assert_eq!(error_codes::INVALID_PARAMS, -32602);
        assert_eq!(error_codes::INTERNAL_ERROR, -32603);
        assert_eq!(error_codes::PROJECT_NOT_FOUND, -32001);
        assert_eq!(error_codes::PROJECT_NOT_INDEXED, -32002);
    }

    #[test]
    fn test_notification_types() {
        assert_eq!(
            JsonRpcNotification::new("notifications/initialized").notification_type(),
            NotificationType::Initialized
        );
        assert_eq!(
            JsonRpcNotification::new("notifications/cancelled").notification_type(),
            NotificationType::Cancelled
        );
        assert_eq!(
            JsonRpcNotification::new("notifications/progress").notification_type(),
            NotificationType::Progress
        );
        assert_eq!(
            JsonRpcNotification::new("notifications/message").notification_type(),
            NotificationType::Message
        );
        assert_eq!(
            JsonRpcNotification::new("notifications/resources/updated").notification_type(),
            NotificationType::ResourcesUpdated
        );
        assert_eq!(
            JsonRpcNotification::new("unknown/notification").notification_type(),
            NotificationType::Unknown("unknown/notification".to_string())
        );
    }

    #[test]
    fn test_backwards_compatibility() {
        // Ensure requests with id still work as before
        let json = r#"{
            "jsonrpc": "2.0",
            "id": "test-id-123",
            "method": "initialize",
            "params": {}
        }"#;

        let msg = JsonRpcMessage::from_json(json).unwrap();
        assert!(msg.is_request());

        if let JsonRpcMessage::Request(req) = msg {
            assert_eq!(req.id, Some(serde_json::json!("test-id-123")));
            assert!(!req.is_notification());
        } else {
            panic!("Expected request");
        }
    }
}