turboprop 0.1.2

Fast semantic code search and indexing tool
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
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//! MCP tools for semantic search
//!
//! Implements the search tool that exposes TurboProp's semantic search
//! capabilities via MCP protocol

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use tracing::{debug, info};

use crate::config::TurboPropConfig;
use crate::search::search_index_with_filters;

/// Strong type for search query strings
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct QueryString(String);

impl QueryString {
    /// Create a new query string
    pub fn new(query: String) -> Self {
        Self(query)
    }

    /// Get the inner string
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Get the length of the query
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the query is empty
    pub fn is_empty(&self) -> bool {
        self.0.trim().is_empty()
    }
}

impl From<String> for QueryString {
    fn from(query: String) -> Self {
        Self::new(query)
    }
}

impl AsRef<str> for QueryString {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

/// Strong type for result limits
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ResultLimit(usize);

impl ResultLimit {
    /// Create a new result limit
    pub fn new(limit: usize) -> Self {
        Self(limit)
    }

    /// Get the inner value
    pub fn get(&self) -> usize {
        self.0
    }

    /// Get the inner value
    pub fn value(&self) -> usize {
        self.0
    }
}

impl From<usize> for ResultLimit {
    fn from(limit: usize) -> Self {
        Self::new(limit)
    }
}

/// Strong type for context lines
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ContextLines(usize);

impl ContextLines {
    /// Create a new context lines value
    pub fn new(lines: usize) -> Self {
        Self(lines)
    }

    /// Get the inner value
    pub fn value(&self) -> usize {
        self.0
    }
}

impl From<usize> for ContextLines {
    fn from(lines: usize) -> Self {
        Self::new(lines)
    }
}

/// Strong type for similarity scores
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct SimilarityScore(f32);

impl SimilarityScore {
    /// Create a new similarity score
    pub fn new(score: f32) -> Self {
        Self(score)
    }

    /// Get the inner value
    pub fn get(&self) -> f32 {
        self.0
    }

    /// Get the inner value
    pub fn value(&self) -> f32 {
        self.0
    }

    /// Check if the score is within valid range (0.0 to 1.0)
    pub fn is_valid(&self) -> bool {
        (0.0..=1.0).contains(&self.0)
    }
}

impl From<f32> for SimilarityScore {
    fn from(score: f32) -> Self {
        Self::new(score)
    }
}

// Serialization and deserialization functions
use serde::{Deserializer, Serializer};

fn deserialize_query_string<'de, D>(deserializer: D) -> Result<QueryString, D::Error>
where
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    Ok(QueryString::from(s))
}

fn deserialize_result_limit<'de, D>(deserializer: D) -> Result<ResultLimit, D::Error>
where
    D: Deserializer<'de>,
{
    let n = usize::deserialize(deserializer)?;
    Ok(ResultLimit::from(n))
}

fn deserialize_optional_similarity_score<'de, D>(
    deserializer: D,
) -> Result<Option<SimilarityScore>, D::Error>
where
    D: Deserializer<'de>,
{
    let opt = Option::<f32>::deserialize(deserializer)?;
    Ok(opt.map(SimilarityScore::from))
}

fn deserialize_optional_context_lines<'de, D>(
    deserializer: D,
) -> Result<Option<ContextLines>, D::Error>
where
    D: Deserializer<'de>,
{
    let opt = Option::<usize>::deserialize(deserializer)?;
    Ok(opt.map(ContextLines::from))
}

fn serialize_similarity_score<S>(score: &SimilarityScore, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_f32(score.value())
}

/// Default result limit for search results
fn default_result_limit() -> ResultLimit {
    ResultLimit::new(10)
}

/// Parameters for the search tool
#[derive(Debug, Clone, Deserialize)]
pub struct SearchToolParams {
    /// Natural language search query (required)
    #[serde(deserialize_with = "deserialize_query_string")]
    pub query: QueryString,
    /// Maximum number of results to return
    #[serde(
        default = "default_result_limit",
        deserialize_with = "deserialize_result_limit"
    )]
    pub limit: ResultLimit,
    /// Minimum similarity threshold (0.0 to 1.0)
    #[serde(default, deserialize_with = "deserialize_optional_similarity_score")]
    pub threshold: Option<SimilarityScore>,
    /// Filter by file extension (e.g., ".rs", ".js", ".py")
    #[serde(default)]
    pub filetype: Option<String>,
    /// Glob pattern filter (e.g., "*.rs", "src/**/*.js")
    #[serde(default)]
    pub filter: Option<String>,
    /// Include file content in results
    #[serde(default = "default_include_content")]
    pub include_content: bool,
    /// Context lines around matches
    #[serde(default, deserialize_with = "deserialize_optional_context_lines")]
    pub context_lines: Option<ContextLines>,
}

/// Search result formatted for MCP
#[derive(Debug, Clone, Serialize)]
pub struct McpSearchResult {
    /// Relative file path
    pub file_path: String,
    /// Line number of the match
    pub line_number: usize,
    /// Similarity score (0.0 to 1.0)
    #[serde(serialize_with = "serialize_similarity_score")]
    pub similarity_score: SimilarityScore,
    /// Matched text content
    pub content: String,
    /// Context lines around the match (if requested)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<Vec<String>>,
    /// File type/extension
    pub file_type: String,
    /// Start and end positions in the chunk
    pub start_line: usize,
    pub end_line: usize,
}

/// Search tool execution result
#[derive(Debug, Clone, Serialize)]
pub struct SearchToolResult {
    /// Search results
    pub results: Vec<McpSearchResult>,
    /// Total number of results found (before limit)
    pub total_results: usize,
    /// Query execution time in milliseconds
    pub execution_time_ms: u64,
    /// Search parameters used
    pub query_info: SearchQueryInfo,
}

/// Information about the executed search query
#[derive(Debug, Clone, Serialize)]
pub struct SearchQueryInfo {
    /// Original query string
    pub query: String,
    /// Applied filters
    pub filters: SearchFilters,
    /// Limit applied
    pub limit: usize,
    /// Threshold used
    pub threshold: f32,
}

/// Applied search filters
#[derive(Debug, Clone, Serialize)]
pub struct SearchFilters {
    /// File type filter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filetype: Option<String>,
    /// Glob pattern filter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub glob_pattern: Option<String>,
}

/// Default for include_content
fn default_include_content() -> bool {
    true
}

// Keep the existing ToolExecutor trait and other types for compatibility
use crate::mcp::error::{McpError, McpResult};
use async_trait::async_trait;

/// Tool definition for MCP protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    /// Tool name
    pub name: String,
    /// Tool description
    pub description: String,
    /// Input schema (JSON Schema)
    pub input_schema: Value,
}

/// Tool call request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallRequest {
    /// Tool name to call
    pub name: String,
    /// Arguments for the tool
    pub arguments: HashMap<String, Value>,
}

/// Tool call response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallResponse {
    /// Whether the tool call was successful
    pub success: bool,
    /// Result data (if successful)
    pub content: Option<Value>,
    /// Error message (if failed)
    pub error: Option<String>,
}

/// Trait defining tool execution interface
#[async_trait]
pub trait ToolExecutor {
    /// Execute a tool with given arguments
    async fn execute(&self, request: ToolCallRequest) -> McpResult<ToolCallResponse>;

    /// Get tool definition
    fn definition(&self) -> ToolDefinition;

    /// Check if tool supports given arguments
    fn validate_arguments(&self, arguments: &HashMap<String, Value>) -> McpResult<()>;
}

/// Semantic search tool that integrates with TurboProp
pub struct SemanticSearchTool {
    /// Tool configuration
    config: SearchToolConfig,
    /// Path to the search index
    index_path: std::path::PathBuf,
    /// Repository path for relative paths
    repo_path: std::path::PathBuf,
    /// TurboProp configuration
    turboprop_config: TurboPropConfig,
    /// Whether this is a mock tool for testing
    is_mock: bool,
}

/// Configuration for semantic search tool
#[derive(Debug, Clone)]
pub struct SearchToolConfig {
    /// Default search limit
    pub default_limit: usize,
    /// Maximum search limit
    pub max_limit: usize,
    /// Default similarity threshold
    pub default_threshold: f32,
    /// Maximum query length in characters
    pub max_query_length: usize,
    /// Minimum similarity threshold
    pub min_threshold: f32,
    /// Maximum similarity threshold
    pub max_threshold: f32,
    /// Default context lines
    pub default_context_lines: usize,
    /// Maximum context lines
    pub max_context_lines: usize,
}

impl Default for SearchToolConfig {
    fn default() -> Self {
        Self {
            default_limit: 10,
            max_limit: 100,
            default_threshold: 0.3,
            max_query_length: 1000,
            min_threshold: 0.0,
            max_threshold: 1.0,
            default_context_lines: 0,
            max_context_lines: 10,
        }
    }
}

impl SemanticSearchTool {
    /// Create a new semantic search tool
    pub fn new(
        index_path: std::path::PathBuf,
        repo_path: std::path::PathBuf,
        turboprop_config: TurboPropConfig,
    ) -> Self {
        Self::with_config(
            SearchToolConfig::default(),
            index_path,
            repo_path,
            turboprop_config,
        )
    }

    /// Create a new semantic search tool with custom configuration
    pub fn with_config(
        config: SearchToolConfig,
        index_path: std::path::PathBuf,
        repo_path: std::path::PathBuf,
        turboprop_config: TurboPropConfig,
    ) -> Self {
        let is_mock = index_path.to_string_lossy().contains("/tmp/test");
        Self {
            config,
            index_path,
            repo_path,
            turboprop_config,
            is_mock,
        }
    }

    /// Create a mock tool for testing
    pub fn new_mock() -> Self {
        use crate::config::TurboPropConfig;
        use std::path::PathBuf;

        Self {
            config: SearchToolConfig::default(),
            index_path: PathBuf::from("/tmp/test_index"),
            repo_path: PathBuf::from("/tmp/test_repo"),
            turboprop_config: TurboPropConfig::default(),
            is_mock: true,
        }
    }

    /// Create the JSON schema for search tool parameters
    fn create_input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": format!("Natural language search query (max {} characters)", self.config.max_query_length)
                },
                "limit": {
                    "type": "integer",
                    "description": format!("Maximum number of results to return (default: {}, max: {})",
                        self.config.default_limit, self.config.max_limit),
                    "default": self.config.default_limit,
                    "minimum": 1,
                    "maximum": self.config.max_limit
                },
                "threshold": {
                    "type": "number",
                    "description": format!("Minimum similarity threshold ({} to {}, default: use config value)",
                        self.config.min_threshold, self.config.max_threshold),
                    "minimum": self.config.min_threshold,
                    "maximum": self.config.max_threshold
                },
                "filetype": {
                    "type": "string",
                    "description": "Filter by file extension (e.g., '.rs', '.js', '.py', '.md')",
                    "pattern": "^\\.[a-zA-Z0-9]+$"
                },
                "filter": {
                    "type": "string",
                    "description": "Glob pattern filter (e.g., '*.rs', 'src/**/*.js', 'tests/**')"
                },
                "include_content": {
                    "type": "boolean",
                    "description": "Include full chunk content in results (default: true)",
                    "default": true
                },
                "context_lines": {
                    "type": "integer",
                    "description": format!("Number of context lines around matches (default: {}, max: {})",
                        self.config.default_context_lines, self.config.max_context_lines),
                    "default": self.config.default_context_lines,
                    "minimum": 0,
                    "maximum": self.config.max_context_lines
                }
            },
            "required": ["query"],
            "additionalProperties": false
        })
    }

    /// Execute the search with given parameters
    async fn execute_search(&self, search_params: SearchToolParams) -> anyhow::Result<Value> {
        let start_time = std::time::Instant::now();

        // Validate parameters
        self.validate_search_params(&search_params)?;

        debug!(
            "Executing search: query='{}', limit={}, threshold={:?}",
            search_params.query.as_str(),
            search_params.limit.value(),
            search_params.threshold.as_ref().map(|t| t.value())
        );

        // Use the existing search functionality directly
        let threshold = search_params
            .threshold
            .map(|t| t.value())
            .or(Some(self.turboprop_config.search.min_similarity));

        // Execute search using the enhanced function with filters
        let search_results = search_index_with_filters(
            &self.index_path,
            search_params.query.as_str(),
            Some(search_params.limit.value()),
            threshold,
            search_params.filetype.clone(),
            search_params.filter.clone(),
        )
        .await
        .context("Search execution failed")?;

        let execution_time = start_time.elapsed();

        // Convert results to MCP format
        let mcp_results = self
            .convert_results(
                search_results.clone(),
                search_params.include_content,
                search_params.context_lines.map(|c| c.value()).unwrap_or(0),
            )
            .await?;

        let total_results = search_results.len();

        // Create response
        let result = SearchToolResult {
            results: mcp_results,
            total_results,
            execution_time_ms: execution_time.as_millis() as u64,
            query_info: SearchQueryInfo {
                query: search_params.query.as_str().to_string(),
                filters: SearchFilters {
                    filetype: search_params.filetype,
                    glob_pattern: search_params.filter,
                },
                limit: search_params.limit.value(),
                threshold: threshold.unwrap_or(self.turboprop_config.search.min_similarity),
            },
        };

        info!(
            "Search completed: query='{}', results={}/{}, time={}ms",
            search_params.query.as_str(),
            result.results.len(),
            total_results,
            result.execution_time_ms
        );

        Ok(serde_json::to_value(result)?)
    }

    /// Validate search tool parameters
    fn validate_search_params(&self, params: &SearchToolParams) -> anyhow::Result<()> {
        // Validate query
        if params.query.is_empty() {
            anyhow::bail!("Query cannot be empty");
        }

        if params.query.len() > self.config.max_query_length {
            anyhow::bail!(
                "Query too long (max {} characters)",
                self.config.max_query_length
            );
        }

        // Validate limit
        if params.limit.value() > self.config.max_limit {
            anyhow::bail!("Limit too high (max {})", self.config.max_limit);
        }

        // Validate threshold
        if let Some(threshold) = &params.threshold {
            if !(self.config.min_threshold..=self.config.max_threshold).contains(&threshold.value())
            {
                anyhow::bail!(
                    "Threshold must be between {} and {}",
                    self.config.min_threshold,
                    self.config.max_threshold
                );
            }
        }

        // Validate filetype format
        if let Some(filetype) = &params.filetype {
            if !filetype.starts_with('.') || filetype.len() < 2 {
                anyhow::bail!("File type must start with '.' and have at least one character (e.g., '.rs', '.js')");
            }
        }

        // Validate context lines
        if let Some(context_lines) = &params.context_lines {
            if context_lines.value() > self.config.max_context_lines {
                anyhow::bail!(
                    "Context lines too high (max {})",
                    self.config.max_context_lines
                );
            }
        }

        Ok(())
    }

    /// Convert TurboProp search results to MCP format
    async fn convert_results(
        &self,
        results: Vec<crate::types::SearchResult>,
        include_content: bool,
        context_lines: usize,
    ) -> anyhow::Result<Vec<McpSearchResult>> {
        let mut mcp_results = Vec::new();

        for result in results {
            // Get relative path
            let relative_path = result
                .chunk
                .chunk
                .source_location
                .file_path
                .strip_prefix(&self.repo_path)
                .unwrap_or(&result.chunk.chunk.source_location.file_path)
                .to_string_lossy()
                .to_string();

            // Extract file extension
            let file_type = result
                .chunk
                .chunk
                .source_location
                .file_path
                .extension()
                .and_then(|ext| ext.to_str())
                .unwrap_or("unknown")
                .to_string();

            // Get content
            let content = if include_content {
                result.chunk.chunk.content.clone()
            } else {
                // Provide a preview of the content
                let preview_len = 200;
                if result.chunk.chunk.content.len() > preview_len {
                    format!("{}...", &result.chunk.chunk.content[..preview_len])
                } else {
                    result.chunk.chunk.content.clone()
                }
            };

            // Add context lines if requested
            let context = if context_lines > 0 {
                // For now, return None - context line extraction would require
                // reading the full file content, which we can implement in a future step
                None
            } else {
                None
            };

            let mcp_result = McpSearchResult {
                file_path: relative_path,
                line_number: result.chunk.chunk.source_location.start_line,
                similarity_score: SimilarityScore::from(result.similarity),
                content,
                context,
                file_type,
                start_line: result.chunk.chunk.source_location.start_line,
                end_line: result.chunk.chunk.source_location.end_line,
            };

            mcp_results.push(mcp_result);
        }

        Ok(mcp_results)
    }
}

#[async_trait]
impl ToolExecutor for SemanticSearchTool {
    async fn execute(&self, request: ToolCallRequest) -> McpResult<ToolCallResponse> {
        debug!(
            "Executing semantic search tool with arguments: {:?}",
            request.arguments
        );

        // Validate arguments first
        self.validate_arguments(&request.arguments)?;

        // Return mock results when in test mode
        if self.is_mock {
            let mock_result = json!({
                "results": [
                    {
                        "file_path": "src/main.rs",
                        "line_number": 10,
                        "similarity_score": 0.85,
                        "content": "fn main() { println!(\"Hello, world!\"); }",
                        "file_type": "rs",
                        "start_line": 10,
                        "end_line": 10
                    }
                ],
                "total_results": 1,
                "execution_time_ms": 50,
                "query_info": {
                    "query": request.arguments.get("query").unwrap_or(&json!("test")).as_str().unwrap_or("test"),
                    "filters": {},
                    "limit": 10,
                    "threshold": 0.3
                }
            });

            return Ok(ToolCallResponse {
                success: true,
                content: Some(mock_result),
                error: None,
            });
        }

        // Convert arguments to search params
        let search_params: SearchToolParams =
            serde_json::from_value(serde_json::to_value(&request.arguments).map_err(|e| {
                McpError::tool_execution(
                    &request.name,
                    format!("Failed to serialize arguments: {}", e),
                )
            })?)
            .map_err(|e| {
                McpError::tool_execution(&request.name, format!("Failed to parse arguments: {}", e))
            })?;

        // Execute search directly
        match self.execute_search(search_params).await {
            Ok(result) => Ok(ToolCallResponse {
                success: true,
                content: Some(result),
                error: None,
            }),
            Err(e) => Ok(ToolCallResponse {
                success: false,
                content: None,
                error: Some(e.to_string()),
            }),
        }
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "semantic_search".to_string(),
            description: "Semantic search across the indexed codebase using natural language queries. Supports filtering by file type, glob patterns, and similarity thresholds.".to_string(),
            input_schema: self.create_input_schema(),
        }
    }

    fn validate_arguments(&self, arguments: &HashMap<String, Value>) -> McpResult<()> {
        // Check required query parameter
        if !arguments.contains_key("query") {
            return Err(McpError::tool_execution(
                "semantic_search",
                "Missing required 'query' parameter",
            ));
        }

        // Validate query is a string
        if !arguments.get("query").unwrap().is_string() {
            return Err(McpError::tool_execution(
                "semantic_search",
                "'query' parameter must be a string",
            ));
        }

        // Validate limit if provided
        if let Some(limit_value) = arguments.get("limit") {
            if let Some(limit) = limit_value.as_u64() {
                if limit == 0 || limit > self.config.max_limit as u64 {
                    return Err(McpError::tool_execution(
                        "semantic_search",
                        format!("'limit' must be between 1 and {}", self.config.max_limit),
                    ));
                }
            } else {
                return Err(McpError::tool_execution(
                    "semantic_search",
                    "'limit' parameter must be an integer",
                ));
            }
        }

        // Validate threshold if provided
        if let Some(threshold_value) = arguments.get("threshold") {
            if let Some(threshold) = threshold_value.as_f64() {
                if !(self.config.min_threshold as f64..=self.config.max_threshold as f64)
                    .contains(&threshold)
                {
                    return Err(McpError::tool_execution(
                        "semantic_search",
                        format!(
                            "'threshold' must be between {} and {}",
                            self.config.min_threshold, self.config.max_threshold
                        ),
                    ));
                }
            } else {
                return Err(McpError::tool_execution(
                    "semantic_search",
                    "'threshold' parameter must be a number",
                ));
            }
        }

        Ok(())
    }
}

/// MCP tools registry that manages available tools
pub struct Tools {
    /// Available tools
    tools: HashMap<String, Box<dyn ToolExecutor + Send + Sync>>,
}

impl Tools {
    /// Create a new tools registry with default tools
    pub fn new() -> Self {
        let mut tools: HashMap<String, Box<dyn ToolExecutor + Send + Sync>> = HashMap::new();

        // Register semantic_search tool for tests
        if cfg!(test) {
            let search_tool = SemanticSearchTool::new_mock();
            tools.insert("semantic_search".to_string(), Box::new(search_tool));
        }

        Self { tools }
    }

    /// Create a new tools registry with default semantic search tool for integration tests
    pub fn new_for_integration_tests() -> Self {
        let mut tools: HashMap<String, Box<dyn ToolExecutor + Send + Sync>> = HashMap::new();

        // Always create a mock search tool for integration testing
        let search_tool = SemanticSearchTool::new_mock();
        tools.insert("semantic_search".to_string(), Box::new(search_tool));

        Self { tools }
    }

    /// Create a new tools registry with enhanced search tool
    pub fn with_search_tool(
        index_path: std::path::PathBuf,
        repo_path: std::path::PathBuf,
        turboprop_config: TurboPropConfig,
    ) -> Self {
        let mut tools: HashMap<String, Box<dyn ToolExecutor + Send + Sync>> = HashMap::new();

        // Register search tool
        let search_tool = SemanticSearchTool::new(index_path, repo_path, turboprop_config);
        tools.insert("semantic_search".to_string(), Box::new(search_tool));

        Self { tools }
    }

    /// Register a new tool
    pub fn register_tool(&mut self, tool: Box<dyn ToolExecutor + Send + Sync>) {
        let definition = tool.definition();
        self.tools.insert(definition.name.clone(), tool);
    }

    /// Get all available tool definitions
    pub fn list_tools(&self) -> Vec<ToolDefinition> {
        self.tools.values().map(|tool| tool.definition()).collect()
    }

    /// Execute a tool by name
    pub async fn execute_tool(&self, request: ToolCallRequest) -> Result<ToolCallResponse> {
        let tool = self
            .tools
            .get(&request.name)
            .ok_or_else(|| anyhow::anyhow!("Tool not found: {}", request.name))?;

        tool.execute(request)
            .await
            .map_err(|e| anyhow::anyhow!("Tool execution error: {}", e))
    }

    /// Check if a tool exists
    pub fn has_tool(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }
}

impl Default for Tools {
    fn default() -> Self {
        Self::new()
    }
}

/// Simple SearchTool that wraps the semantic search functionality for MCP server
#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_search_tool_creation() {
        let tool = SemanticSearchTool::new_mock();
        let definition = tool.definition();

        assert_eq!(definition.name, "semantic_search");
        assert!(!definition.description.is_empty());
        assert!(definition.input_schema.is_object());
    }

    #[test]
    fn test_search_params_deserialization() {
        let params = json!({
            "query": "test function",
            "limit": 5,
            "threshold": 0.7,
            "filetype": ".rs"
        });

        let search_params: SearchToolParams = serde_json::from_value(params).unwrap();

        assert_eq!(search_params.query.as_str(), "test function");
        assert_eq!(search_params.limit.value(), 5);
        assert_eq!(
            search_params.threshold.as_ref().map(|t| t.value()),
            Some(0.7)
        );
        assert_eq!(search_params.filetype, Some(".rs".to_string()));
    }

    #[test]
    fn test_search_params_defaults() {
        let params = json!({
            "query": "test function"
        });

        let search_params: SearchToolParams = serde_json::from_value(params).unwrap();

        assert_eq!(search_params.query.as_str(), "test function");
        assert_eq!(search_params.limit.value(), 10); // default
        assert_eq!(search_params.threshold, None);
        assert_eq!(search_params.filetype, None);
        assert!(search_params.include_content); // default true
    }

    #[test]
    fn test_parameter_validation() {
        let tool = SemanticSearchTool::new_mock();

        // Valid parameters
        let valid_params = SearchToolParams {
            query: QueryString::from("test".to_string()),
            limit: ResultLimit::from(10),
            threshold: Some(SimilarityScore::from(0.5)),
            filetype: Some(".rs".to_string()),
            filter: None,
            include_content: true,
            context_lines: Some(ContextLines::from(2)),
        };
        assert!(tool.validate_search_params(&valid_params).is_ok());

        // Empty query
        let empty_query = SearchToolParams {
            query: QueryString::from("".to_string()),
            limit: ResultLimit::from(10),
            threshold: None,
            filetype: None,
            filter: None,
            include_content: true,
            context_lines: None,
        };
        assert!(tool.validate_search_params(&empty_query).is_err());

        // Invalid threshold
        let invalid_threshold = SearchToolParams {
            query: QueryString::from("test".to_string()),
            limit: ResultLimit::from(10),
            threshold: Some(SimilarityScore::from(1.5)),
            filetype: None,
            filter: None,
            include_content: true,
            context_lines: None,
        };
        assert!(tool.validate_search_params(&invalid_threshold).is_err());

        // Invalid filetype
        let invalid_filetype = SearchToolParams {
            query: QueryString::from("test".to_string()),
            limit: ResultLimit::from(10),
            threshold: None,
            filetype: Some("rs".to_string()), // missing dot
            filter: None,
            include_content: true,
            context_lines: None,
        };
        assert!(tool.validate_search_params(&invalid_filetype).is_err());
    }

    #[test]
    fn test_input_schema_structure() {
        let tool = SemanticSearchTool::new_mock();
        let definition = tool.definition();
        let schema = &definition.input_schema;

        // Check required properties
        assert!(schema["properties"]["query"].is_object());
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&json!("query")));

        // Check optional properties
        assert!(schema["properties"]["limit"].is_object());
        assert!(schema["properties"]["threshold"].is_object());
        assert!(schema["properties"]["filetype"].is_object());
        assert!(schema["properties"]["filter"].is_object());
    }

    #[test]
    fn test_tools_registry() {
        let tools = Tools::new();

        // Should have semantic_search tool for testing
        assert!(tools.has_tool("semantic_search"));

        // List tools
        let tool_list = tools.list_tools();
        assert!(!tool_list.is_empty());
    }
}