mcp-protocol-sdk 0.5.1

Production-ready Rust SDK for the Model Context Protocol (MCP) with multiple transport support
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
//! Tool system for MCP servers
//!
//! This module provides the abstraction for implementing and managing tools in MCP servers.
//! Tools are functions that can be called by clients to perform specific operations.
//! Enhanced with advanced parameter validation, type checking, and metadata support.

use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;
use std::time::Instant;

use crate::core::error::{McpError, McpResult};
use crate::core::tool_metadata::{
    CategoryFilter, EnhancedToolMetadata, ToolBehaviorHints, ToolCategory, ToolDeprecation,
};
use crate::core::validation::{ParameterValidator, ValidationConfig};
use crate::protocol::types::{ContentBlock, ToolInfo, ToolInputSchema, ToolResult};

/// Trait for implementing tool handlers
#[async_trait]
pub trait ToolHandler: Send + Sync {
    /// Execute the tool with the given arguments
    ///
    /// # Arguments
    /// * `arguments` - Tool arguments as key-value pairs
    ///
    /// # Returns
    /// Result containing the tool execution result or an error
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult>;
}

/// A registered tool with its handler, validation, and enhanced metadata
pub struct Tool {
    /// Information about the tool
    pub info: ToolInfo,
    /// Handler that implements the tool's functionality
    pub handler: Box<dyn ToolHandler>,
    /// Whether the tool is currently enabled
    pub enabled: bool,
    /// Parameter validator for input validation
    pub validator: Option<ParameterValidator>,
    /// Enhanced metadata for tool behavior, categorization, and performance
    pub enhanced_metadata: EnhancedToolMetadata,
}

impl Tool {
    /// Create a new tool with the given information and handler
    ///
    /// # Arguments
    /// * `name` - Name of the tool
    /// * `description` - Optional description of the tool
    /// * `input_schema` - JSON schema describing the input parameters
    /// * `handler` - Implementation of the tool's functionality
    pub fn new<H>(
        name: String,
        description: Option<String>,
        input_schema: Value,
        handler: H,
    ) -> Self
    where
        H: ToolHandler + 'static,
    {
        // Create validator from schema
        let validator = if input_schema.is_object() {
            Some(ParameterValidator::new(input_schema.clone()))
        } else {
            None
        };

        Self {
            info: ToolInfo {
                name,
                description,
                input_schema: ToolInputSchema {
                    schema_type: "object".to_string(),
                    properties: input_schema
                        .get("properties")
                        .and_then(|p| p.as_object())
                        .map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect()),
                    required: input_schema
                        .get("required")
                        .and_then(|r| r.as_array())
                        .map(|arr| {
                            arr.iter()
                                .filter_map(|v| v.as_str().map(String::from))
                                .collect()
                        }),
                    additional_properties: input_schema
                        .as_object()
                        .unwrap_or(&serde_json::Map::new())
                        .iter()
                        .filter(|(k, _)| !["type", "properties", "required"].contains(&k.as_str()))
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect(),
                },
                annotations: None,
                title: None,
                meta: None,
            },
            handler: Box::new(handler),
            enabled: true,
            validator,
            enhanced_metadata: EnhancedToolMetadata::new(),
        }
    }

    /// Create a new tool with custom validation configuration
    pub fn with_validation<H>(
        name: String,
        description: Option<String>,
        input_schema: Value,
        handler: H,
        validation_config: ValidationConfig,
    ) -> Self
    where
        H: ToolHandler + 'static,
    {
        let mut tool = Self::new(name, description, input_schema.clone(), handler);
        if input_schema.is_object() {
            tool.validator = Some(ParameterValidator::with_config(
                input_schema,
                validation_config,
            ));
        }
        tool
    }

    /// Enable the tool
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable the tool
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Check if the tool is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Execute the tool if it's enabled with parameter validation and performance tracking
    ///
    /// # Arguments
    /// * `arguments` - Tool arguments as key-value pairs
    ///
    /// # Returns
    /// Result containing the tool execution result or an error
    pub async fn call(&self, mut arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        if !self.enabled {
            return Err(McpError::validation(format!(
                "Tool '{}' is disabled",
                self.info.name
            )));
        }

        // Check for deprecation warning
        if let Some(warning) = self.enhanced_metadata.deprecation_warning() {
            eprintln!("Warning: {warning}");
        }

        // Validate and coerce parameters if validator is present
        if let Some(ref validator) = self.validator {
            validator.validate_and_coerce(&mut arguments).map_err(|e| {
                McpError::validation(format!(
                    "Tool '{}' parameter validation failed: {}",
                    self.info.name, e
                ))
            })?;
        }

        // Track execution time and outcome
        let start_time = Instant::now();
        let result = self.handler.call(arguments).await;
        let execution_time = start_time.elapsed();

        // Update performance metrics using interior mutability
        match &result {
            Ok(_) => self.enhanced_metadata.record_success(execution_time),
            Err(_) => self.enhanced_metadata.record_error(execution_time),
        }

        result
    }

    /// Execute the tool without validation or performance tracking (for advanced use cases)
    pub async fn call_unchecked(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        if !self.enabled {
            return Err(McpError::validation(format!(
                "Tool '{}' is disabled",
                self.info.name
            )));
        }

        self.handler.call(arguments).await
    }

    /// Validate parameters without executing the tool
    pub fn validate_parameters(&self, arguments: &mut HashMap<String, Value>) -> McpResult<()> {
        if let Some(ref validator) = self.validator {
            validator.validate_and_coerce(arguments).map_err(|e| {
                McpError::validation(format!(
                    "Tool '{}' parameter validation failed: {}",
                    self.info.name, e
                ))
            })
        } else {
            Ok(())
        }
    }

    // Enhanced Metadata Management Methods

    /// Set behavior hints for the tool
    pub fn set_behavior_hints(&mut self, hints: ToolBehaviorHints) {
        self.enhanced_metadata.behavior_hints = hints;
    }

    /// Get behavior hints for the tool
    pub fn behavior_hints(&self) -> &ToolBehaviorHints {
        &self.enhanced_metadata.behavior_hints
    }

    /// Set category for the tool
    pub fn set_category(&mut self, category: ToolCategory) {
        self.enhanced_metadata.category = Some(category);
    }

    /// Get category for the tool
    pub fn category(&self) -> Option<&ToolCategory> {
        self.enhanced_metadata.category.as_ref()
    }

    /// Set version for the tool
    pub fn set_version(&mut self, version: String) {
        self.enhanced_metadata.version = Some(version);
    }

    /// Get version of the tool
    pub fn version(&self) -> Option<&String> {
        self.enhanced_metadata.version.as_ref()
    }

    /// Set author for the tool
    pub fn set_author(&mut self, author: String) {
        self.enhanced_metadata.author = Some(author);
    }

    /// Get author of the tool
    pub fn author(&self) -> Option<&String> {
        self.enhanced_metadata.author.as_ref()
    }

    /// Mark tool as deprecated
    pub fn deprecate(&mut self, deprecation: ToolDeprecation) {
        self.enhanced_metadata.deprecation = Some(deprecation);
    }

    /// Check if tool is deprecated
    pub fn is_deprecated(&self) -> bool {
        self.enhanced_metadata.is_deprecated()
    }

    /// Get deprecation warning if tool is deprecated
    pub fn deprecation_warning(&self) -> Option<String> {
        self.enhanced_metadata.deprecation_warning()
    }

    /// Get performance metrics for the tool
    pub fn performance_metrics(&self) -> crate::core::tool_metadata::ToolPerformanceMetrics {
        self.enhanced_metadata.get_performance_snapshot()
    }

    /// Add custom metadata field
    pub fn add_custom_metadata(&mut self, key: String, value: serde_json::Value) {
        self.enhanced_metadata.custom.insert(key, value);
    }

    /// Get custom metadata field
    pub fn get_custom_metadata(&self, key: &str) -> Option<&serde_json::Value> {
        self.enhanced_metadata.custom.get(key)
    }

    /// Check if tool matches a category filter
    pub fn matches_category_filter(&self, filter: &CategoryFilter) -> bool {
        if let Some(ref category) = self.enhanced_metadata.category {
            category.matches_filter(filter)
        } else {
            // If no category set, only match empty filters
            filter.primary.is_none() && filter.secondary.is_none() && filter.tags.is_empty()
        }
    }

    /// Check if tool is suitable for caching based on behavior hints
    pub fn is_cacheable(&self) -> bool {
        self.enhanced_metadata
            .behavior_hints
            .cacheable
            .unwrap_or(false)
            || (self
                .enhanced_metadata
                .behavior_hints
                .read_only
                .unwrap_or(false)
                && self
                    .enhanced_metadata
                    .behavior_hints
                    .idempotent
                    .unwrap_or(false))
    }

    /// Check if tool is destructive
    pub fn is_destructive(&self) -> bool {
        self.enhanced_metadata
            .behavior_hints
            .destructive
            .unwrap_or(false)
    }

    /// Check if tool is read-only
    pub fn is_read_only(&self) -> bool {
        self.enhanced_metadata
            .behavior_hints
            .read_only
            .unwrap_or(false)
    }

    /// Check if tool is idempotent
    pub fn is_idempotent(&self) -> bool {
        self.enhanced_metadata
            .behavior_hints
            .idempotent
            .unwrap_or(false)
    }

    /// Check if tool requires authentication
    pub fn requires_auth(&self) -> bool {
        self.enhanced_metadata
            .behavior_hints
            .requires_auth
            .unwrap_or(false)
    }
}

impl std::fmt::Debug for Tool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Tool")
            .field("info", &self.info)
            .field("enabled", &self.enabled)
            .field("has_validator", &self.validator.is_some())
            .field("deprecated", &self.is_deprecated())
            .field("category", &self.enhanced_metadata.category)
            .field("version", &self.enhanced_metadata.version)
            .field("execution_count", &self.enhanced_metadata.execution_count())
            .field("success_rate", &self.enhanced_metadata.success_rate())
            .finish()
    }
}

/// Helper macro for creating tools with schema validation
///
/// # Examples
/// ```rust
/// use mcp_protocol_sdk::{tool, core::tool::ToolHandler};
/// use serde_json::json;
///
/// struct MyHandler;
/// #[async_trait::async_trait]
/// impl ToolHandler for MyHandler {
///     async fn call(&self, _args: std::collections::HashMap<String, serde_json::Value>) -> mcp_protocol_sdk::McpResult<mcp_protocol_sdk::protocol::types::ToolResult> {
///         // Implementation here
///         todo!()
///     }
/// }
///
/// let tool = tool!(
///     "my_tool",
///     "A sample tool",
///     json!({
///         "type": "object",
///         "properties": {
///             "input": { "type": "string" }
///         }
///     }),
///     MyHandler
/// );
/// ```
#[macro_export]
macro_rules! tool {
    ($name:expr_2021, $schema:expr_2021, $handler:expr_2021) => {
        $crate::core::tool::Tool::new($name.to_string(), None, $schema, $handler)
    };
    ($name:expr_2021, $description:expr_2021, $schema:expr_2021, $handler:expr_2021) => {
        $crate::core::tool::Tool::new(
            $name.to_string(),
            Some($description.to_string()),
            $schema,
            $handler,
        )
    };
}

// Common tool implementations

/// Simple echo tool for testing
pub struct EchoTool;

#[async_trait]
impl ToolHandler for EchoTool {
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        let message = arguments
            .get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Hello, World!");

        Ok(ToolResult {
            content: vec![ContentBlock::Text {
                text: message.to_string(),
                annotations: None,
                meta: None,
            }],
            is_error: None,
            structured_content: None,
            meta: None,
        })
    }
}

/// Tool for adding two numbers
pub struct AdditionTool;

#[async_trait]
impl ToolHandler for AdditionTool {
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        let a = arguments
            .get("a")
            .and_then(|v| v.as_f64())
            .ok_or_else(|| McpError::validation("Missing or invalid 'a' parameter"))?;

        let b = arguments
            .get("b")
            .and_then(|v| v.as_f64())
            .ok_or_else(|| McpError::validation("Missing or invalid 'b' parameter"))?;

        let result = a + b;

        Ok(ToolResult {
            content: vec![ContentBlock::Text {
                text: result.to_string(),
                annotations: None,
                meta: None,
            }],
            is_error: None,
            structured_content: None,
            meta: None,
        })
    }
}

/// Tool for getting current timestamp
pub struct TimestampTool;

#[async_trait]
impl ToolHandler for TimestampTool {
    async fn call(&self, _arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        use std::time::{SystemTime, UNIX_EPOCH};

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| McpError::internal(e.to_string()))?
            .as_secs();

        Ok(ToolResult {
            content: vec![ContentBlock::Text {
                text: timestamp.to_string(),
                annotations: None,
                meta: None,
            }],
            is_error: None,
            structured_content: None,
            meta: None,
        })
    }
}

/// Builder for creating tools with fluent API, advanced validation, and enhanced metadata
pub struct ToolBuilder {
    name: String,
    description: Option<String>,
    input_schema: Option<Value>,
    validation_config: Option<ValidationConfig>,
    title: Option<String>,
    behavior_hints: ToolBehaviorHints,
    category: Option<ToolCategory>,
    version: Option<String>,
    author: Option<String>,
    deprecation: Option<ToolDeprecation>,
    custom_metadata: HashMap<String, serde_json::Value>,
}

impl ToolBuilder {
    /// Create a new tool builder with the given name
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            description: None,
            input_schema: None,
            validation_config: None,
            title: None,
            behavior_hints: ToolBehaviorHints::new(),
            category: None,
            version: None,
            author: None,
            deprecation: None,
            custom_metadata: HashMap::new(),
        }
    }

    /// Set the tool description
    pub fn description<S: Into<String>>(mut self, description: S) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the tool title (for UI display)
    pub fn title<S: Into<String>>(mut self, title: S) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set the input schema
    pub fn schema(mut self, schema: Value) -> Self {
        self.input_schema = Some(schema);
        self
    }

    /// Set custom validation configuration
    pub fn validation_config(mut self, config: ValidationConfig) -> Self {
        self.validation_config = Some(config);
        self
    }

    /// Enable strict validation (no additional properties, strict types)
    pub fn strict_validation(mut self) -> Self {
        self.validation_config = Some(ValidationConfig {
            allow_additional: false,
            coerce_types: false,
            detailed_errors: true,
            max_string_length: Some(1000),
            max_array_length: Some(100),
            max_object_properties: Some(50),
        });
        self
    }

    /// Enable permissive validation (allow additional properties, type coercion)
    pub fn permissive_validation(mut self) -> Self {
        self.validation_config = Some(ValidationConfig {
            allow_additional: true,
            coerce_types: true,
            detailed_errors: false,
            max_string_length: None,
            max_array_length: None,
            max_object_properties: None,
        });
        self
    }

    // Enhanced Metadata Builder Methods

    /// Set behavior hints for the tool
    pub fn behavior_hints(mut self, hints: ToolBehaviorHints) -> Self {
        self.behavior_hints = hints;
        self
    }

    /// Mark tool as read-only
    pub fn read_only(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.read_only();
        self
    }

    /// Mark tool as destructive
    pub fn destructive(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.destructive();
        self
    }

    /// Mark tool as idempotent
    pub fn idempotent(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.idempotent();
        self
    }

    /// Mark tool as requiring authentication
    pub fn requires_auth(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.requires_auth();
        self
    }

    /// Mark tool as potentially long-running
    pub fn long_running(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.long_running();
        self
    }

    /// Mark tool as resource-intensive
    pub fn resource_intensive(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.resource_intensive();
        self
    }

    /// Mark tool results as cacheable
    pub fn cacheable(mut self) -> Self {
        self.behavior_hints = self.behavior_hints.cacheable();
        self
    }

    /// Set tool category
    pub fn category(mut self, category: ToolCategory) -> Self {
        self.category = Some(category);
        self
    }

    /// Set tool category with primary and secondary classification
    pub fn category_simple(mut self, primary: String, secondary: Option<String>) -> Self {
        let mut cat = ToolCategory::new(primary);
        if let Some(sec) = secondary {
            cat = cat.with_secondary(sec);
        }
        self.category = Some(cat);
        self
    }

    /// Add category tag
    pub fn tag(mut self, tag: String) -> Self {
        if let Some(ref mut category) = self.category {
            category.tags.insert(tag);
        } else {
            let mut cat = ToolCategory::new("general".to_string());
            cat.tags.insert(tag);
            self.category = Some(cat);
        }
        self
    }

    /// Set tool version
    pub fn version<S: Into<String>>(mut self, version: S) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Set tool author
    pub fn author<S: Into<String>>(mut self, author: S) -> Self {
        self.author = Some(author.into());
        self
    }

    /// Mark tool as deprecated
    pub fn deprecated(mut self, deprecation: ToolDeprecation) -> Self {
        self.deprecation = Some(deprecation);
        self
    }

    /// Mark tool as deprecated with simple reason
    pub fn deprecated_simple<S: Into<String>>(mut self, reason: S) -> Self {
        self.deprecation = Some(ToolDeprecation::new(reason.into()));
        self
    }

    /// Add custom metadata field
    pub fn custom_metadata<S: Into<String>>(mut self, key: S, value: serde_json::Value) -> Self {
        self.custom_metadata.insert(key.into(), value);
        self
    }

    /// Build the tool with the given handler
    pub fn build<H>(self, handler: H) -> McpResult<Tool>
    where
        H: ToolHandler + 'static,
    {
        let schema = self.input_schema.unwrap_or_else(|| {
            serde_json::json!({
                "type": "object",
                "properties": {},
                "additionalProperties": true
            })
        });

        let mut tool = if let Some(config) = self.validation_config {
            Tool::with_validation(self.name, self.description, schema, handler, config)
        } else {
            Tool::new(self.name, self.description, schema, handler)
        };

        // Set title if provided
        if let Some(title) = self.title {
            tool.info.title = Some(title);
        }

        // Apply enhanced metadata
        let mut enhanced_metadata =
            EnhancedToolMetadata::new().with_behavior_hints(self.behavior_hints);

        if let Some(category) = self.category {
            enhanced_metadata = enhanced_metadata.with_category(category);
        }

        if let Some(version) = self.version {
            enhanced_metadata = enhanced_metadata.with_version(version);
        }

        if let Some(author) = self.author {
            enhanced_metadata = enhanced_metadata.with_author(author);
        }

        if let Some(deprecation) = self.deprecation {
            enhanced_metadata = enhanced_metadata.deprecated(deprecation);
        }

        // Add custom metadata fields
        for (key, value) in self.custom_metadata {
            enhanced_metadata = enhanced_metadata.with_custom_field(key, value);
        }

        tool.enhanced_metadata = enhanced_metadata;

        Ok(tool)
    }

    /// Build the tool with validation chain - allows chaining parameter validation
    pub fn build_with_validation_chain<H>(
        self,
        handler: H,
        validation_fn: impl Fn(&mut HashMap<String, Value>) -> McpResult<()> + Send + Sync + 'static,
    ) -> McpResult<ValidationChainTool>
    where
        H: ToolHandler + 'static,
    {
        let tool = self.build(handler)?;
        Ok(ValidationChainTool {
            tool,
            custom_validator: Box::new(validation_fn),
        })
    }
}

/// Tool wrapper that supports custom validation chains
/// Type alias for validation function to reduce complexity
type ValidationFunction = Box<dyn Fn(&mut HashMap<String, Value>) -> McpResult<()> + Send + Sync>;

pub struct ValidationChainTool {
    tool: Tool,
    custom_validator: ValidationFunction,
}

#[async_trait]
impl ToolHandler for ValidationChainTool {
    async fn call(&self, mut arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        // Run custom validation first
        (self.custom_validator)(&mut arguments)?;

        // Then run the tool's built-in validation and execution
        self.tool.call(arguments).await
    }
}

// ============================================================================
// Enhanced Tool Creation Helpers and Macros
// ============================================================================

/// Create a validated tool with typed parameters
#[macro_export]
macro_rules! validated_tool {
    (
        name: $name:expr_2021,
        description: $desc:expr_2021,
        parameters: {
            $( $param_name:ident: $param_type:ident $( ( $( $constraint:ident: $value:expr_2021 ),* ) )? ),*
        },
        handler: $handler:expr_2021
    ) => {{
        use $crate::core::validation::{create_tool_schema, param_schema};

        let params = vec![
            $(
                {
                    let base_schema = param_schema!($param_type stringify!($param_name));
                    // Apply constraints if any
                    $(
                        // This would need more complex macro expansion for constraints
                        // For now, we'll use the base schema
                    )?
                    base_schema
                }
            ),*
        ];

        let required = vec![ $( stringify!($param_name) ),* ];
        let schema = create_tool_schema(params, required);

        $crate::core::tool::Tool::new(
            $name.to_string(),
            Some($desc.to_string()),
            schema,
            $handler
        )
    }};
}

/// Helper function to create a simple string parameter tool
pub fn create_string_tool<H>(
    name: &str,
    description: &str,
    param_name: &str,
    param_description: &str,
    handler: H,
) -> Tool
where
    H: ToolHandler + 'static,
{
    use serde_json::json;

    let schema = json!({
        "type": "object",
        "properties": {
            param_name: {
                "type": "string",
                "description": param_description
            }
        },
        "required": [param_name]
    });

    Tool::new(
        name.to_string(),
        Some(description.to_string()),
        schema,
        handler,
    )
}

/// Helper function to create a tool with multiple typed parameters
pub fn create_typed_tool<H>(
    name: &str,
    description: &str,
    parameters: Vec<(&str, &str, Value)>, // (name, description, schema)
    required: Vec<&str>,
    handler: H,
) -> Tool
where
    H: ToolHandler + 'static,
{
    use serde_json::{Map, json};

    let mut properties = Map::new();
    for (param_name, param_desc, param_schema) in parameters {
        let mut schema_with_desc = param_schema;
        if let Some(obj) = schema_with_desc.as_object_mut() {
            obj.insert("description".to_string(), json!(param_desc));
        }
        properties.insert(param_name.to_string(), schema_with_desc);
    }

    let schema = json!({
        "type": "object",
        "properties": properties,
        "required": required
    });

    Tool::new(
        name.to_string(),
        Some(description.to_string()),
        schema,
        handler,
    )
}

/// Trait for tools that can provide their own parameter validation
pub trait ValidatedToolHandler: ToolHandler {
    /// Get the JSON schema for this tool's parameters
    fn parameter_schema() -> Value;

    /// Get validation configuration for this tool
    fn validation_config() -> ValidationConfig {
        ValidationConfig::default()
    }

    /// Create a tool instance with built-in validation
    fn create_tool(name: String, description: Option<String>, handler: Self) -> Tool
    where
        Self: Sized + 'static,
    {
        Tool::with_validation(
            name,
            description,
            Self::parameter_schema(),
            handler,
            Self::validation_config(),
        )
    }
}

// ============================================================================
// Enhanced Built-in Tool Examples
// ============================================================================

/// Enhanced calculator tool with comprehensive validation
pub struct CalculatorTool;

#[async_trait]
impl ToolHandler for CalculatorTool {
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        let operation = arguments
            .get("operation")
            .and_then(|v| v.as_str())
            .ok_or_else(|| McpError::validation("Missing 'operation' parameter"))?;

        let a = arguments
            .get("a")
            .and_then(|v| v.as_f64())
            .ok_or_else(|| McpError::validation("Missing or invalid 'a' parameter"))?;

        let b = arguments
            .get("b")
            .and_then(|v| v.as_f64())
            .ok_or_else(|| McpError::validation("Missing or invalid 'b' parameter"))?;

        let result = match operation {
            "add" => a + b,
            "subtract" => a - b,
            "multiply" => a * b,
            "divide" => {
                if b == 0.0 {
                    return Ok(ToolResult {
                        content: vec![ContentBlock::Text {
                            text: "Error: Division by zero".to_string(),
                            annotations: None,
                            meta: None,
                        }],
                        is_error: Some(true),
                        structured_content: Some(serde_json::json!({
                            "error": "division_by_zero",
                            "message": "Cannot divide by zero"
                        })),
                        meta: None,
                    });
                }
                a / b
            }
            _ => {
                return Err(McpError::validation(format!(
                    "Unsupported operation: {operation}"
                )));
            }
        };

        Ok(ToolResult {
            content: vec![ContentBlock::Text {
                text: result.to_string(),
                annotations: None,
                meta: None,
            }],
            is_error: None,
            structured_content: Some(serde_json::json!({
                "operation": operation,
                "operands": [a, b],
                "result": result
            })),
            meta: None,
        })
    }
}

impl ValidatedToolHandler for CalculatorTool {
    fn parameter_schema() -> Value {
        use crate::core::validation::create_tool_schema;
        use crate::param_schema;

        create_tool_schema(
            vec![
                param_schema!(enum "operation", values: ["add", "subtract", "multiply", "divide"]),
                param_schema!(number "a", min: -1000000, max: 1000000),
                param_schema!(number "b", min: -1000000, max: 1000000),
            ],
            vec!["operation", "a", "b"],
        )
    }

    fn validation_config() -> ValidationConfig {
        ValidationConfig {
            allow_additional: false,
            coerce_types: true,
            detailed_errors: true,
            max_string_length: Some(20),
            max_array_length: Some(10),
            max_object_properties: Some(10),
        }
    }
}

/// Text processing tool with advanced string validation
pub struct TextProcessorTool;

#[async_trait]
impl ToolHandler for TextProcessorTool {
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        let text = arguments
            .get("text")
            .and_then(|v| v.as_str())
            .ok_or_else(|| McpError::validation("Missing 'text' parameter"))?;

        let operation = arguments
            .get("operation")
            .and_then(|v| v.as_str())
            .unwrap_or("uppercase");

        let result = match operation {
            "uppercase" => text.to_uppercase(),
            "lowercase" => text.to_lowercase(),
            "reverse" => text.chars().rev().collect(),
            "word_count" => text.split_whitespace().count().to_string(),
            "char_count" => text.len().to_string(),
            _ => {
                return Err(McpError::validation(format!(
                    "Unsupported operation: {operation}"
                )));
            }
        };

        Ok(ToolResult {
            content: vec![ContentBlock::Text {
                text: result.clone(),
                annotations: None,
                meta: None,
            }],
            is_error: None,
            structured_content: Some(serde_json::json!({
                "original_text": text,
                "operation": operation,
                "result": result,
                "length": text.len()
            })),
            meta: None,
        })
    }
}

impl ValidatedToolHandler for TextProcessorTool {
    fn parameter_schema() -> Value {
        use crate::core::validation::create_tool_schema;
        use crate::param_schema;

        create_tool_schema(
            vec![
                param_schema!(string "text", min: 1, max: 10000),
                param_schema!(enum "operation", values: ["uppercase", "lowercase", "reverse", "word_count", "char_count"]),
            ],
            vec!["text"],
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Content;
    use serde_json::json;

    #[tokio::test]
    async fn test_echo_tool() {
        let tool = EchoTool;
        let mut args = HashMap::new();
        args.insert("message".to_string(), json!("test message"));

        let result = tool.call(args).await.unwrap();
        match &result.content[0] {
            Content::Text { text, .. } => assert_eq!(text, "test message"),
            _ => panic!("Expected text content"),
        }
    }

    #[tokio::test]
    async fn test_addition_tool() {
        let tool = AdditionTool;
        let mut args = HashMap::new();
        args.insert("a".to_string(), json!(5.0));
        args.insert("b".to_string(), json!(3.0));

        let result = tool.call(args).await.unwrap();
        match &result.content[0] {
            Content::Text { text, .. } => assert_eq!(text, "8"),
            _ => panic!("Expected text content"),
        }
    }

    #[test]
    fn test_tool_creation() {
        let tool = Tool::new(
            "test_tool".to_string(),
            Some("Test tool".to_string()),
            json!({"type": "object"}),
            EchoTool,
        );

        assert_eq!(tool.info.name, "test_tool");
        assert_eq!(tool.info.description, Some("Test tool".to_string()));
        assert!(tool.is_enabled());
    }

    #[test]
    fn test_tool_enable_disable() {
        let mut tool = Tool::new(
            "test_tool".to_string(),
            None,
            json!({"type": "object"}),
            EchoTool,
        );

        assert!(tool.is_enabled());

        tool.disable();
        assert!(!tool.is_enabled());

        tool.enable();
        assert!(tool.is_enabled());
    }

    #[tokio::test]
    async fn test_disabled_tool() {
        let mut tool = Tool::new(
            "test_tool".to_string(),
            None,
            json!({"type": "object"}),
            EchoTool,
        );

        tool.disable();

        let result = tool.call(HashMap::new()).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            McpError::Validation(msg) => assert!(msg.contains("disabled")),
            _ => panic!("Expected validation error"),
        }
    }

    #[test]
    fn test_tool_builder() {
        let tool = ToolBuilder::new("test")
            .description("A test tool")
            .schema(json!({"type": "object", "properties": {"x": {"type": "number"}}}))
            .build(EchoTool)
            .unwrap();

        assert_eq!(tool.info.name, "test");
        assert_eq!(tool.info.description, Some("A test tool".to_string()));
        assert!(tool.validator.is_some());
    }

    #[test]
    fn test_enhanced_tool_builder() {
        let tool = ToolBuilder::new("enhanced_test")
            .title("Enhanced Test Tool")
            .description("A test tool with enhanced features")
            .strict_validation()
            .schema(json!({
                "type": "object",
                "properties": {
                    "name": {"type": "string", "minLength": 2},
                    "age": {"type": "integer", "minimum": 0}
                },
                "required": ["name"]
            }))
            .build(EchoTool)
            .unwrap();

        assert_eq!(tool.info.name, "enhanced_test");
        assert_eq!(tool.info.title, Some("Enhanced Test Tool".to_string()));
        assert!(tool.validator.is_some());
    }

    #[tokio::test]
    async fn test_parameter_validation() {
        let schema = json!({
            "type": "object",
            "properties": {
                "name": {"type": "string", "minLength": 2},
                "age": {"type": "integer", "minimum": 0, "maximum": 150}
            },
            "required": ["name", "age"]
        });

        let tool = Tool::new(
            "validation_test".to_string(),
            Some("Test validation".to_string()),
            schema,
            EchoTool,
        );

        // Valid parameters
        let mut valid_args = HashMap::new();
        valid_args.insert("name".to_string(), json!("Alice"));
        valid_args.insert("age".to_string(), json!(25));
        assert!(tool.validate_parameters(&mut valid_args).is_ok());

        // Missing required parameter
        let mut invalid_args = HashMap::new();
        invalid_args.insert("name".to_string(), json!("Bob"));
        assert!(tool.validate_parameters(&mut invalid_args).is_err());

        // Invalid parameter type with coercion
        let mut coercible_args = HashMap::new();
        coercible_args.insert("name".to_string(), json!("Charlie"));
        coercible_args.insert("age".to_string(), json!("30")); // String that can be coerced to number
        assert!(tool.validate_parameters(&mut coercible_args).is_ok());
        // After validation, should be coerced to number
        assert_eq!(coercible_args.get("age").unwrap().as_i64(), Some(30));
    }

    #[tokio::test]
    async fn test_calculator_tool() {
        let tool = CalculatorTool::create_tool(
            "calculator".to_string(),
            Some("Advanced calculator".to_string()),
            CalculatorTool,
        );

        // Test addition
        let mut args = HashMap::new();
        args.insert("operation".to_string(), json!("add"));
        args.insert("a".to_string(), json!(5));
        args.insert("b".to_string(), json!(3));

        let result = tool.call(args).await.unwrap();
        assert_eq!(
            result.content[0],
            ContentBlock::Text {
                text: "8".to_string(),
                annotations: None,
                meta: None,
            }
        );
        assert!(result.structured_content.is_some());

        // Test division by zero
        let mut args = HashMap::new();
        args.insert("operation".to_string(), json!("divide"));
        args.insert("a".to_string(), json!(10));
        args.insert("b".to_string(), json!(0));

        let result = tool.call(args).await.unwrap();
        assert_eq!(result.is_error, Some(true));
        if let ContentBlock::Text { text, .. } = &result.content[0] {
            assert!(text.contains("Division by zero"));
        } else {
            panic!("Expected text content");
        }
    }

    #[tokio::test]
    async fn test_text_processor_tool() {
        let tool = TextProcessorTool::create_tool(
            "text_processor".to_string(),
            Some("Text processing utility".to_string()),
            TextProcessorTool,
        );

        // Test uppercase
        let mut args = HashMap::new();
        args.insert("text".to_string(), json!("hello world"));
        args.insert("operation".to_string(), json!("uppercase"));

        let result = tool.call(args.clone()).await.unwrap();
        assert_eq!(
            result.content[0],
            ContentBlock::Text {
                text: "HELLO WORLD".to_string(),
                annotations: None,
                meta: None,
            }
        );

        // Test word count
        args.insert("operation".to_string(), json!("word_count"));
        let result = tool.call(args).await.unwrap();
        assert_eq!(
            result.content[0],
            ContentBlock::Text {
                text: "2".to_string(),
                annotations: None,
                meta: None,
            }
        );
    }

    #[test]
    fn test_create_typed_tool() {
        let tool = create_typed_tool(
            "typed_test",
            "A typed parameter test tool",
            vec![
                (
                    "username",
                    "User's name",
                    json!({"type": "string", "minLength": 3}),
                ),
                (
                    "age",
                    "User's age",
                    json!({"type": "integer", "minimum": 0}),
                ),
                (
                    "active",
                    "Whether user is active",
                    json!({"type": "boolean"}),
                ),
            ],
            vec!["username", "age"],
            EchoTool,
        );

        assert_eq!(tool.info.name, "typed_test");
        assert!(tool.validator.is_some());

        // Check that schema was built correctly
        let schema = &tool.info.input_schema;
        assert!(schema.properties.is_some());
        let props = schema.properties.as_ref().unwrap();
        assert!(props.contains_key("username"));
        assert!(props.contains_key("age"));
        assert!(props.contains_key("active"));
    }

    #[test]
    fn test_validation_config_options() {
        // Test strict validation
        let strict_tool = ToolBuilder::new("strict")
            .strict_validation()
            .build(EchoTool)
            .unwrap();
        assert!(strict_tool.validator.is_some());

        // Test permissive validation
        let permissive_tool = ToolBuilder::new("permissive")
            .permissive_validation()
            .build(EchoTool)
            .unwrap();
        assert!(permissive_tool.validator.is_some());
    }
}

// ============================================================================
// Extension Trait for Better Ergonomics
// ============================================================================

/// Extension trait for HashMap to make parameter extraction easier
pub trait ParameterExt {
    /// Extract a required string parameter
    fn get_string(&self, key: &str) -> McpResult<&str>;

    /// Extract an optional string parameter
    fn get_optional_string(&self, key: &str) -> Option<&str>;

    /// Extract a required number parameter
    fn get_number(&self, key: &str) -> McpResult<f64>;

    /// Extract an optional number parameter
    fn get_optional_number(&self, key: &str) -> Option<f64>;

    /// Extract a required integer parameter
    fn get_integer(&self, key: &str) -> McpResult<i64>;

    /// Extract an optional integer parameter
    fn get_optional_integer(&self, key: &str) -> Option<i64>;

    /// Extract a required boolean parameter
    fn get_boolean(&self, key: &str) -> McpResult<bool>;

    /// Extract an optional boolean parameter
    fn get_optional_boolean(&self, key: &str) -> Option<bool>;
}

impl ParameterExt for HashMap<String, Value> {
    fn get_string(&self, key: &str) -> McpResult<&str> {
        self.get(key).and_then(|v| v.as_str()).ok_or_else(|| {
            McpError::validation(format!("Missing or invalid string parameter: {key}"))
        })
    }

    fn get_optional_string(&self, key: &str) -> Option<&str> {
        self.get(key).and_then(|v| v.as_str())
    }

    fn get_number(&self, key: &str) -> McpResult<f64> {
        self.get(key).and_then(|v| v.as_f64()).ok_or_else(|| {
            McpError::validation(format!("Missing or invalid number parameter: {key}"))
        })
    }

    fn get_optional_number(&self, key: &str) -> Option<f64> {
        self.get(key).and_then(|v| v.as_f64())
    }

    fn get_integer(&self, key: &str) -> McpResult<i64> {
        self.get(key).and_then(|v| v.as_i64()).ok_or_else(|| {
            McpError::validation(format!("Missing or invalid integer parameter: {key}"))
        })
    }

    fn get_optional_integer(&self, key: &str) -> Option<i64> {
        self.get(key).and_then(|v| v.as_i64())
    }

    fn get_boolean(&self, key: &str) -> McpResult<bool> {
        self.get(key).and_then(|v| v.as_bool()).ok_or_else(|| {
            McpError::validation(format!("Missing or invalid boolean parameter: {key}"))
        })
    }

    fn get_optional_boolean(&self, key: &str) -> Option<bool> {
        self.get(key).and_then(|v| v.as_bool())
    }
}

#[cfg(test)]
mod enhanced_tests {
    use super::*;
    use crate::core::tool_metadata::*;
    use crate::prelude::ToolHandler;
    use std::time::Duration;
    use tokio;

    // Test handler for basic tool functionality
    struct TestHandler {
        result: String,
        should_fail: bool,
    }

    #[async_trait]
    impl ToolHandler for TestHandler {
        async fn call(&self, _arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
            if self.should_fail {
                Err(McpError::validation("Test error".to_string()))
            } else {
                Ok(ToolResult {
                    content: vec![ContentBlock::Text {
                        text: self.result.clone(),
                        annotations: None,
                        meta: None,
                    }],
                    is_error: None,
                    structured_content: None,
                    meta: None,
                })
            }
        }
    }

    #[tokio::test]
    async fn test_enhanced_tool_builder() {
        let handler = TestHandler {
            result: "test result".to_string(),
            should_fail: false,
        };

        let tool = ToolBuilder::new("test_tool")
            .description("A test tool")
            .title("Test Tool")
            .version("1.0.0")
            .author("Test Author")
            .read_only()
            .idempotent()
            .cacheable()
            .category_simple("data".to_string(), Some("analysis".to_string()))
            .tag("testing".to_string())
            .tag("utility".to_string())
            .custom_metadata("priority".to_string(), serde_json::Value::from("high"))
            .build(handler)
            .expect("Failed to build tool");

        assert_eq!(tool.info.name, "test_tool");
        assert_eq!(tool.info.description, Some("A test tool".to_string()));
        assert_eq!(tool.info.title, Some("Test Tool".to_string()));
        assert_eq!(tool.version(), Some(&"1.0.0".to_string()));
        assert_eq!(tool.author(), Some(&"Test Author".to_string()));
        assert!(tool.is_read_only());
        assert!(tool.is_idempotent());
        assert!(tool.is_cacheable());
        assert!(!tool.is_destructive());
        assert!(!tool.requires_auth());

        let category = tool.category().unwrap();
        assert_eq!(category.primary, "data");
        assert_eq!(category.secondary, Some("analysis".to_string()));
        assert!(category.tags.contains("testing"));
        assert!(category.tags.contains("utility"));

        let custom_priority = tool.get_custom_metadata("priority");
        assert_eq!(custom_priority, Some(&serde_json::Value::from("high")));
    }

    #[tokio::test]
    async fn test_performance_tracking() {
        let handler = TestHandler {
            result: "success".to_string(),
            should_fail: false,
        };

        let tool = ToolBuilder::new("performance_test")
            .build(handler)
            .expect("Failed to build tool");

        // Initial state
        let metrics = tool.performance_metrics();
        assert_eq!(metrics.execution_count, 0);
        assert_eq!(metrics.success_count, 0);
        assert_eq!(metrics.error_count, 0);

        // Execute tool successfully
        let result = tool.call(HashMap::new()).await;
        assert!(result.is_ok());

        // Check updated metrics
        let metrics = tool.performance_metrics();
        assert_eq!(metrics.execution_count, 1);
        assert_eq!(metrics.success_count, 1);
        assert_eq!(metrics.error_count, 0);
        assert_eq!(metrics.success_rate, 100.0);
        assert!(metrics.average_execution_time > Duration::from_nanos(0));
    }

    #[tokio::test]
    async fn test_performance_tracking_with_errors() {
        let handler = TestHandler {
            result: "".to_string(),
            should_fail: true,
        };

        let tool = ToolBuilder::new("error_test")
            .build(handler)
            .expect("Failed to build tool");

        // Execute tool with error
        let result = tool.call(HashMap::new()).await;
        assert!(result.is_err());

        // Check error metrics
        let metrics = tool.performance_metrics();
        assert_eq!(metrics.execution_count, 1);
        assert_eq!(metrics.success_count, 0);
        assert_eq!(metrics.error_count, 1);
        assert_eq!(metrics.success_rate, 0.0);
    }

    #[tokio::test]
    async fn test_deprecation_warning() {
        let handler = TestHandler {
            result: "deprecated result".to_string(),
            should_fail: false,
        };

        let deprecation = ToolDeprecation::new("This tool is outdated".to_string())
            .with_replacement("new_tool".to_string())
            .with_severity(DeprecationSeverity::High);

        let tool = ToolBuilder::new("deprecated_tool")
            .deprecated(deprecation)
            .build(handler)
            .expect("Failed to build tool");

        assert!(tool.is_deprecated());
        let warning = tool.deprecation_warning().unwrap();
        assert!(warning.contains("deprecated"));
        assert!(warning.contains("outdated"));
        assert!(warning.contains("new_tool"));
    }

    #[tokio::test]
    async fn test_category_filtering() {
        let category = ToolCategory::new("file".to_string())
            .with_secondary("read".to_string())
            .with_tag("filesystem".to_string())
            .with_tag("utility".to_string());

        let handler = TestHandler {
            result: "filtered result".to_string(),
            should_fail: false,
        };

        let tool = ToolBuilder::new("filterable_tool")
            .category(category)
            .build(handler)
            .expect("Failed to build tool");

        // Test primary category filter
        let filter = CategoryFilter::new().with_primary("file".to_string());
        assert!(tool.matches_category_filter(&filter));

        let filter = CategoryFilter::new().with_primary("network".to_string());
        assert!(!tool.matches_category_filter(&filter));

        // Test tag filter
        let filter = CategoryFilter::new().with_tag("filesystem".to_string());
        assert!(tool.matches_category_filter(&filter));

        let filter = CategoryFilter::new().with_tag("nonexistent".to_string());
        assert!(!tool.matches_category_filter(&filter));

        // Test secondary category filter
        let filter = CategoryFilter::new().with_secondary("read".to_string());
        assert!(tool.matches_category_filter(&filter));

        let filter = CategoryFilter::new().with_secondary("write".to_string());
        assert!(!tool.matches_category_filter(&filter));
    }

    #[tokio::test]
    async fn test_behavior_hints() {
        let hints = ToolBehaviorHints::new()
            .read_only()
            .idempotent()
            .cacheable()
            .requires_auth()
            .long_running()
            .resource_intensive();

        let handler = TestHandler {
            result: "hints result".to_string(),
            should_fail: false,
        };

        let tool = ToolBuilder::new("hints_tool")
            .behavior_hints(hints)
            .build(handler)
            .expect("Failed to build tool");

        assert!(tool.is_read_only());
        assert!(tool.is_idempotent());
        assert!(tool.is_cacheable());
        assert!(tool.requires_auth());
        assert!(!tool.is_destructive());

        let behavior_hints = tool.behavior_hints();
        assert_eq!(behavior_hints.read_only, Some(true));
        assert_eq!(behavior_hints.idempotent, Some(true));
        assert_eq!(behavior_hints.cacheable, Some(true));
        assert_eq!(behavior_hints.requires_auth, Some(true));
        assert_eq!(behavior_hints.long_running, Some(true));
        assert_eq!(behavior_hints.resource_intensive, Some(true));
        assert_eq!(behavior_hints.destructive, None);
    }

    #[tokio::test]
    async fn test_tool_enabling_disabling() {
        let handler = TestHandler {
            result: "enabled result".to_string(),
            should_fail: false,
        };

        let mut tool = ToolBuilder::new("enable_test")
            .build(handler)
            .expect("Failed to build tool");

        assert!(tool.is_enabled());

        // Disable tool
        tool.disable();
        assert!(!tool.is_enabled());

        // Try to call disabled tool
        let result = tool.call(HashMap::new()).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("disabled"));

        // Re-enable tool
        tool.enable();
        assert!(tool.is_enabled());

        // Should work again
        let result = tool.call(HashMap::new()).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_custom_metadata() {
        let handler = TestHandler {
            result: "metadata result".to_string(),
            should_fail: false,
        };

        let mut tool = ToolBuilder::new("metadata_tool")
            .custom_metadata("priority".to_string(), serde_json::Value::from("high"))
            .custom_metadata("team".to_string(), serde_json::Value::from("backend"))
            .build(handler)
            .expect("Failed to build tool");

        assert_eq!(
            tool.get_custom_metadata("priority"),
            Some(&serde_json::Value::from("high"))
        );
        assert_eq!(
            tool.get_custom_metadata("team"),
            Some(&serde_json::Value::from("backend"))
        );
        assert_eq!(tool.get_custom_metadata("nonexistent"), None);

        // Add metadata after creation
        tool.add_custom_metadata(
            "environment".to_string(),
            serde_json::Value::from("production"),
        );
        assert_eq!(
            tool.get_custom_metadata("environment"),
            Some(&serde_json::Value::from("production"))
        );
    }

    #[test]
    fn test_tool_debug_format() {
        let handler = TestHandler {
            result: "debug result".to_string(),
            should_fail: false,
        };

        let tool = ToolBuilder::new("debug_tool")
            .version("2.0.0")
            .category_simple("debug".to_string(), None)
            .build(handler)
            .expect("Failed to build tool");

        let debug_str = format!("{tool:?}");
        assert!(debug_str.contains("debug_tool"));
        assert!(debug_str.contains("enabled"));
        assert!(debug_str.contains("execution_count"));
        assert!(debug_str.contains("success_rate"));
    }
}