mcp_rust_examples 0.1.0

Complete educational resource for learning MCP (Model Context Protocol) development with Rust - 20 comprehensive examples from beginner to enterprise level
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
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
# Model Context Protocol (MCP) in Rust: Complete Tutorial

This tutorial provides a comprehensive guide to building MCP (Model Context Protocol) servers in Rust, progressing from simple examples to production-ready enterprise applications.

## Table of Contents

1. [Introduction to MCP]#introduction-to-mcp
2. [Getting Started]#getting-started
3. [Basic Examples (1-5)]#basic-examples
4. [Intermediate Examples (6-10)]#intermediate-examples
5. [Advanced Examples (11-15)]#advanced-examples
6. [Enterprise Examples (16-20)]#enterprise-examples
7. [Best Practices]#best-practices
8. [Production Deployment]#production-deployment

## Introduction to MCP

The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to LLMs (Large Language Models). It enables AI-powered tools and workflows by solving the M×N integration problem through a unified protocol.

### Key MCP Concepts

- **Tools**: Functions that LLMs can call to perform actions
- **Resources**: Data sources that LLMs can access (documents, databases, APIs)
- **Prompts**: Reusable prompt templates
- **Sampling**: LLM text generation capabilities

### Why Rust for MCP?

Rust provides several advantages for MCP server development:

- **Memory Safety**: Prevents common bugs without garbage collection overhead
- **Performance**: Comparable to C/C++ with high-level ergonomics
- **Concurrency**: Excellent async/await support with Tokio
- **Type Safety**: Catches errors at compile time
- **Ecosystem**: Rich crate ecosystem for JSON, HTTP, databases, etc.

### External Learning Resources

**Official MCP Documentation:**
- [MCP Official Website]https://modelcontextprotocol.io - Core concepts and overview
- [MCP Specification]https://spec.modelcontextprotocol.io - Complete protocol specification
- [MCP GitHub Repository]https://github.com/modelcontextprotocol - Official implementations and examples

**Rust MCP Toolkit (`rmcp`):**
- [A Coder's Guide to the Official Rust MCP Toolkit]https://hackmd.io/@Hamze/S1tlKZP0kx - Comprehensive guide to `rmcp` 
- [Official Rust MCP SDK]https://github.com/modelcontextprotocol/rust-sdk - Official Rust implementation
- [rmcp Documentation]https://docs.rs/rmcp - API reference and examples

**Rust Learning Resources:**
- [The Rust Programming Language]https://doc.rust-lang.org/book/ - Official Rust book
- [Rust by Example]https://doc.rust-lang.org/rust-by-example/ - Learn Rust through examples
- [Tokio Tutorial]https://tokio.rs/tokio/tutorial - Async programming in Rust

## Getting Started

### Prerequisites

- Rust 1.70+ installed
- Basic understanding of Rust async programming
- Familiarity with JSON-RPC concepts

### Project Setup

```bash
# Clone the tutorial repository
git clone <repository-url>
cd mcp-rust-tutorial

# Run any example
cargo run --bin example_01_hello_world
```

### Dependencies

Key dependencies used throughout the tutorial:

```toml
[dependencies]
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
uuid = { version = "1.0", features = ["v4", "serde"] }
chrono = { version = "0.4", features = ["serde"] }
```

### External Learning Resources

**Project Setup and Cargo:**
- [The Cargo Book]https://doc.rust-lang.org/cargo/ - Complete guide to Rust's package manager
- [Managing Dependencies]https://doc.rust-lang.org/cargo/guide/dependencies.html - How to add and manage crates
- [Cargo.toml Reference]https://doc.rust-lang.org/cargo/reference/manifest.html - Complete manifest file reference

**Essential Crates Documentation:**
- [Tokio Documentation]https://docs.rs/tokio - Async runtime and utilities
- [Serde Documentation]https://serde.rs/ - Serialization/deserialization framework
- [Tracing Documentation]https://docs.rs/tracing - Application-level tracing framework
- [UUID Documentation]https://docs.rs/uuid - Unique identifier generation
- [Chrono Documentation]https://docs.rs/chrono - Date and time handling

## Basic Examples

### Example 1: Hello World MCP Server

The simplest possible MCP server with a single greeting tool.

**Key Concepts:**
- Basic MCP server structure
- Tool definitions with JSON schema
- Simple JSON-RPC message handling

**Features Demonstrated:**
- Single tool (`greeting`) with parameter validation
- Basic error handling
- JSON schema for input validation

**Rust Concepts Explained:**

**1. Struct Definition and Implementation Blocks**
```rust
pub struct HelloWorldServer;  // Unit struct - no fields needed

impl HelloWorldServer {  // Implementation block for methods
    // Methods go here
}
```
- **Unit Struct**: `HelloWorldServer` is a unit struct (no fields), perfect for stateless servers
- **`pub` Keyword**: Makes the struct public, allowing external access
- **Implementation Block**: `impl` defines methods associated with the struct

**2. Vector Creation and Initialization**
```rust
pub fn list_tools(&self) -> Vec<Tool> {
    vec![Tool { /* ... */ }]  // vec! macro creates a vector
}
```
- **`vec!` Macro**: Creates a vector with initial elements
- **Return Type**: `Vec<Tool>` specifies a vector of Tool structs
- **`&self` Parameter**: Immutable reference to the struct instance

**3. Serde JSON Integration**
```rust
use serde::{Deserialize, Serialize};  // Import traits

#[derive(Serialize, Deserialize, Debug)]  // Derive macros
pub struct GreetingRequest {
    pub name: String,
}
```
- **Derive Macros**: Automatically implement traits for serialization
- **Serialize/Deserialize**: Convert Rust structs to/from JSON
- **Debug Trait**: Enables printing with `{:?}` format

**4. JSON Schema with serde_json::json! Macro**
```rust
input_schema: serde_json::json!({
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "description": "The name of the person to greet"
        }
    },
    "required": ["name"]
}),
```
- **`json!` Macro**: Creates JSON values at compile time
- **Type Safety**: Rust ensures the JSON structure is valid
- **Schema Definition**: Defines expected input structure for tools

**5. String Handling**
```rust
name: "greeting".to_string(),  // Convert &str to String
```
- **String vs &str**: `String` is owned, `&str` is borrowed
- **`.to_string()`**: Converts string literals to owned String types

**Real Code from Example:**

**1. Data Structures - The Foundation**
```rust
// Step 1: Define request/response structures for type safety
#[derive(Serialize, Deserialize, Debug)]
pub struct GreetingRequest {
    pub name: String,  // The parameter clients will send
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GreetingResponse {
    pub message: String,  // The formatted response we'll return
}
```
*Pedagogical Note: This demonstrates Rust's approach to data modeling. Instead of working with raw JSON, we define strongly-typed structures that the compiler can validate. The `derive` macros automatically implement serialization/deserialization.*

**2. Server Structure - Clean Architecture**
```rust
// Step 2: Create the server handler (stateless in this simple example)
pub struct HelloWorldServer;

impl HelloWorldServer {
    pub fn new() -> Self {
        Self  // Unit struct constructor
    }

    // Define what tools this server provides
    pub fn list_tools(&self) -> Vec<Tool> {
        vec![Tool {
            name: "greeting".to_string(),
            description: "Generate a personalized greeting message".to_string(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "The name of the person to greet"
                    }
                },
                "required": ["name"]
            }),
        }]
    }
}
```
*Pedagogical Note: This shows the MCP pattern of separating concerns - the server structure holds state (none in this case), while methods handle specific protocol operations.*

**3. Tool Implementation - Business Logic**
```rust
// Step 3: Implement the actual tool logic
pub fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
    match name {
        "greeting" => {
            // Parse incoming JSON into our typed structure
            let request: GreetingRequest = serde_json::from_value(arguments)
                .map_err(|e| format!("Failed to parse arguments: {}", e))?;

            // Execute business logic (create greeting)
            let response = GreetingResponse {
                message: format!("Hello, {}! Welcome to MCP with Rust!", request.name),
            };

            // Convert back to JSON for MCP protocol
            serde_json::to_value(response)
                .map_err(|e| format!("Failed to serialize response: {}", e))
        }
        _ => Err(format!("Unknown tool: {}", name)),
    }
}
```
*Pedagogical Note: This demonstrates the complete data flow: JSON → Rust struct → business logic → Rust struct → JSON. The `?` operator provides clean error propagation.*

**4. Async Main Function - Program Entry Point**
```rust
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging for debugging
    tracing_subscriber::fmt::init();

    println!("🚀 Starting Hello World MCP Server");
    println!("📝 Available tools: greeting");
    
    let server = HelloWorldServer::new();
    
    // Simple JSON-RPC message loop (production would use rmcp crate)
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    
    let stdin = stdin();
    let mut stdout = stdout();
    let mut reader = BufReader::new(stdin);
    let mut line = String::new();

    loop {
        line.clear();
        match reader.read_line(&mut line).await {
            Ok(0) => break, // EOF
            Ok(_) => {
                // Process JSON-RPC message...
            }
            Err(e) => {
                eprintln!("Error reading input: {}", e);
                break;
            }
        }
    }
    
    Ok(())
}
```
*Pedagogical Note: The `#[tokio::main]` attribute transforms the main function into an async runtime. This example shows basic I/O handling, though real MCP servers would use the rmcp crate for protocol handling.*

**Run Example:**
```bash
cargo run --bin example_01_hello_world
```

### External Learning Resources

**Basic Rust Concepts:**
- [Structs and Methods]https://doc.rust-lang.org/book/ch05-00-structs.html - Rust Book chapter on structs
- [Error Handling]https://doc.rust-lang.org/book/ch09-00-error-handling.html - Result types and error propagation
- [Traits]https://doc.rust-lang.org/book/ch10-02-traits.html - Defining shared behavior
- [Derive Macros]https://doc.rust-lang.org/reference/derive.html - Automatic trait implementations

**JSON and Serialization:**
- [Serde Tutorial]https://serde.rs/derive.html - Working with derive macros
- [JSON Schema]https://json-schema.org/ - Understanding JSON schema validation
- [Working with JSON in Rust]https://blog.logrocket.com/json-and-rust-why-serde_json-is-the-top-choice/ - Practical JSON handling

**MCP Protocol Basics:**
- [JSON-RPC 2.0 Specification]https://www.jsonrpc.org/specification - Understanding the underlying protocol
- [MCP Client-Server Communication]https://hackmd.io/@Hamze/S1tlKZP0kx#Building-a-Client-That-Talks-to-Servers - How clients communicate with servers

### Example 2: Calculator with Error Handling

Building upon the hello world example with mathematical operations and comprehensive error handling.

**Key Concepts:**
- Multiple operations in a single tool
- Custom error types
- Parameter validation
- Robust error handling patterns

**Features Demonstrated:**
- Mathematical operations (add, subtract, multiply, divide)
- Division by zero protection
- Input validation and error messages

**Rust Concepts Explained:**

**1. Result Type and Error Handling**
```rust
fn perform_calculation(&self, request: &CalculatorRequest) -> Result<f64, CalculatorError> {
    // Result<T, E> represents either success (Ok(T)) or failure (Err(E))
}
```
- **Result<T, E>**: Rust's way of handling fallible operations
- **Ok(value)**: Success case containing the result
- **Err(error)**: Failure case containing error information
- **No Exceptions**: Rust uses Result instead of exceptions

**2. Pattern Matching with match**
```rust
match request.operation.as_str() {
    "divide" => { /* division logic */ },
    "add" => { /* addition logic */ },
    _ => { /* default case */ }
}
```
- **Pattern Matching**: Exhaustive checking of all possible values
- **String Patterns**: Matching on string values
- **Wildcard `_`**: Catches all unmatched cases

**3. Custom Error Types**
```rust
#[derive(Debug)]
pub enum CalculatorError {
    DivisionByZero,
    UnsupportedOperation(String),
}

impl std::fmt::Display for CalculatorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CalculatorError::DivisionByZero => write!(f, "Division by zero is not allowed"),
            CalculatorError::UnsupportedOperation(op) => write!(f, "Unsupported operation: {}", op),
        }
    }
}
```
- **Enum**: Defines a type with multiple variants
- **Associated Data**: `UnsupportedOperation(String)` carries additional data
- **Trait Implementation**: `Display` trait for user-friendly error messages

**4. Conditional Logic and Early Return**
```rust
if request.b == 0.0 {
    Err(CalculatorError::DivisionByZero)  // Early return with error
} else {
    Ok(request.a / request.b)  // Success case
}
```
- **Early Return**: Return immediately on error conditions
- **Type Safety**: Compiler ensures all paths return Result type

**5. Method Parameters and References**
```rust
fn perform_calculation(&self, request: &CalculatorRequest) -> Result<f64, CalculatorError>
//                     ^self    ^borrowed reference
```
- **`&self`**: Immutable reference to the struct instance
- **`&CalculatorRequest`**: Borrowed reference to avoid ownership transfer
- **Borrowing**: Access data without taking ownership

**6. Floating Point Operations**
```rust
request.a / request.b  // f64 division
request.a + request.b  // f64 addition
```
- **f64 Type**: 64-bit floating point numbers
- **Arithmetic Operators**: Standard mathematical operations
- **IEEE 754**: Rust follows IEEE floating point standards

**Real Code from Example:**

**1. Structured Request/Response Types**
```rust
// Define the calculator request structure with multiple parameters
#[derive(Serialize, Deserialize, Debug)]
pub struct CalculatorRequest {
    pub operation: String,  // The mathematical operation to perform
    pub a: f64,            // First number in the calculation
    pub b: f64,            // Second number in the calculation
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CalculatorResponse {
    pub result: f64,                    // The result of the calculation
    pub operation_performed: String,    // The operation that was performed (for confirmation)
}
```
*Pedagogical Note: Notice how we model the domain with precise types. The `f64` type ensures floating-point arithmetic, while the `operation_performed` field provides useful feedback to clients.*

**2. Custom Error Types - Production Pattern**
```rust
// Custom error type for calculator-specific errors
#[derive(Debug)]
pub enum CalculatorError {
    DivisionByZero,
    UnsupportedOperation(String),
}

impl std::fmt::Display for CalculatorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CalculatorError::DivisionByZero => write!(f, "Division by zero is not allowed"),
            CalculatorError::UnsupportedOperation(op) => write!(f, "Unsupported operation: {}", op),
        }
    }
}

impl std::error::Error for CalculatorError {}
```
*Pedagogical Note: This demonstrates Rust's idiomatic error handling. By implementing `Display` and `Error` traits, our custom errors integrate seamlessly with Rust's error ecosystem.*

**3. Core Business Logic with Validation**
```rust
// Private method to perform the actual calculation
fn perform_calculation(&self, request: &CalculatorRequest) -> Result<f64, CalculatorError> {
    match request.operation.as_str() {
        "add" => Ok(request.a + request.b),
        "subtract" => Ok(request.a - request.b),
        "multiply" => Ok(request.a * request.b),
        "divide" => {
            // Validate that we're not dividing by zero
            if request.b == 0.0 {
                Err(CalculatorError::DivisionByZero)
            } else {
                Ok(request.a / request.b)
            }
        }
        _ => Err(CalculatorError::UnsupportedOperation(
            request.operation.clone(),
        )),
    }
}
```
*Pedagogical Note: This shows defensive programming - we validate inputs and handle edge cases explicitly. The match expression ensures all operations are handled, with the `_` wildcard catching invalid operations.*

**4. Tool Definition with Schema Validation**
```rust
pub fn list_tools(&self) -> Vec<Tool> {
    vec![Tool {
        name: "calculator".to_string(),
        description: "Perform basic arithmetic operations (add, subtract, multiply, divide)".to_string(),
        input_schema: serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "description": "The operation to perform",
                    "enum": ["add", "subtract", "multiply", "divide"]  // Constrains valid values
                },
                "a": {
                    "type": "number",
                    "description": "First number"
                },
                "b": {
                    "type": "number", 
                    "description": "Second number"
                }
            },
            "required": ["operation", "a", "b"]  // All parameters are mandatory
        }),
    }]
}
```
*Pedagogical Note: The JSON schema provides client-side validation. The `enum` constraint limits operations to valid values, and `required` ensures all parameters are provided.*

**5. Complete Tool Call Handler**
```rust
pub fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
    match name {
        "calculator" => {
            // Parse the request
            let request: CalculatorRequest = serde_json::from_value(arguments)
                .map_err(|e| format!("Failed to parse arguments: {}", e))?;

            // Perform the calculation
            let result = self
                .perform_calculation(&request)
                .map_err(|e| e.to_string())?;  // Convert our custom error to string

            // Create the response
            let response = CalculatorResponse {
                result,
                operation_performed: format!(
                    "{} {} {}", 
                    request.a, request.operation, request.b
                ),
            };

            serde_json::to_value(response)
                .map_err(|e| format!("Failed to serialize response: {}", e))
        }
        _ => Err(format!("Unknown tool: {}", name)),
    }
}
```
*Pedagogical Note: This shows the complete error handling pipeline: JSON parsing errors, business logic errors, and serialization errors are all handled gracefully using the `?` operator for clean error propagation.*

**Run Example:**
```bash
cargo run --bin example_02_calculator
```

### External Learning Resources

**Advanced Error Handling:**
- [Error Handling Patterns in Rust]https://doc.rust-lang.org/book/ch09-00-error-handling.html - Comprehensive error handling guide
- [Custom Error Types]https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/define_error_type.html - Creating your own error types
- [The ? Operator]https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.html - Error propagation shorthand
- [thiserror crate]https://docs.rs/thiserror - Convenient derive macros for error types

**Pattern Matching:**
- [Pattern Matching]https://doc.rust-lang.org/book/ch06-00-enums.html - Enums and pattern matching
- [Match Expressions]https://doc.rust-lang.org/reference/expressions/match-expr.html - Detailed match syntax
- [Advanced Patterns]https://doc.rust-lang.org/book/ch18-00-patterns.html - Pattern matching techniques

**Floating Point Arithmetic:**
- [Floating-Point Arithmetic]https://doc.rust-lang.org/reference/types/numeric.html#floating-point-types - f32 and f64 types
- [IEEE 754 Standard]https://en.wikipedia.org/wiki/IEEE_754 - Understanding floating-point representation
- [Rust Numeric Types]https://doc.rust-lang.org/book/ch03-02-data-types.html#numeric-types - All numeric types in Rust

### Example 3: Text Processor with Multiple Tools

Demonstrates organizing multiple related tools within a single MCP server.

**Key Concepts:**
- Multiple tools in one server
- Text transformation operations
- Tool organization patterns

**Features Demonstrated:**
- Text transformations (uppercase, lowercase, reverse, capitalize)
- Text analysis (word count, character analysis)
- Multiple tool management

**Rust Concepts Explained:**

**1. String Methods and Transformations**
```rust
text.to_uppercase()     // Creates new String with uppercase characters
text.to_lowercase()     // Creates new String with lowercase characters
text.trim().to_string() // Removes whitespace and converts to owned String
```
- **String Methods**: Built-in methods for string manipulation
- **Ownership**: These methods create new String instances
- **Method Chaining**: Can chain multiple string operations

**2. Iterator Patterns and Functional Programming**
```rust
text.chars().rev().collect()  // Reverse characters using iterators
text.split_whitespace()       // Split into words
    .map(|word| capitalize_word(word))  // Transform each word
    .collect::<Vec<_>>()       // Collect into vector
    .join(" ")                 // Join back with spaces
```
- **Iterators**: Lazy evaluation for efficient processing
- **Functional Style**: Transform data through method chains
- **Closures**: `|word|` is a closure (anonymous function)
- **Collect**: Materialize iterator results into collections

**3. Character Processing**
```rust
text.chars().any(|c| c.is_uppercase())  // Check if any char is uppercase
text.chars().any(|c| c.is_numeric())    // Check if any char is numeric
text.chars().count()                    // Count characters
```
- **Character Iterator**: `.chars()` creates iterator over Unicode scalar values
- **Predicate Functions**: `.any()` tests conditions across elements
- **Unicode Support**: Full Unicode character support

**4. String Splitting and Processing**
```rust
text.split_whitespace().count()  // Count words
text.lines().count()             // Count lines
```
- **Split Methods**: Various ways to break strings apart
- **Whitespace Handling**: Automatic handling of spaces, tabs, newlines

**5. Complex String Manipulation**
```rust
fn capitalize_words(&self, text: &str) -> String {
    text.split_whitespace()
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}
```
- **Option Handling**: `chars.next()` returns `Option<char>`
- **Pattern Matching**: Handle Some/None cases safely
- **String Concatenation**: Using `+` operator for strings
- **Type Annotations**: `collect::<String>()` specifies collection type

**Run Example:**
```bash
cargo run --bin example_03_text_processor
```

### External Learning Resources

**String Processing and Iterators:**
- [String Types in Rust]https://doc.rust-lang.org/book/ch08-02-strings.html - String vs &str and UTF-8 handling
- [Iterator Trait]https://doc.rust-lang.org/book/ch13-02-iterators.html - Working with iterators
- [Functional Programming in Rust]https://doc.rust-lang.org/book/ch13-00-functional-features.html - Closures and iterators
- [Processing Collections]https://doc.rust-lang.org/rust-by-example/fn/closures/iter.html - Practical iterator examples

**Unicode and Character Processing:**
- [Unicode in Rust]https://doc.rust-lang.org/reference/tokens.html#unicode-escapes - Unicode support
- [UTF-8 Everywhere]http://utf8everywhere.org/ - Understanding UTF-8 encoding
- [char type]https://doc.rust-lang.org/std/primitive.char.html - Unicode scalar values

**Functional Programming Patterns:**
- [Closures]https://doc.rust-lang.org/book/ch13-01-closures.html - Anonymous functions
- [Higher-Order Functions]https://doc.rust-lang.org/rust-by-example/fn/hof.html - Functions that operate on functions
- [Method Chaining]https://doc.rust-lang.org/book/ch13-02-iterators.html#methods-that-consume-the-iterator - Building processing pipelines

### Example 4: Weather Service

*Implementation details would be based on the actual example_04 file*

### Example 5: Resource Provider

Demonstrates MCP resources for providing document access to LLMs.

**Key Concepts:**
- MCP Resources (not just tools)
- Document storage and retrieval
- Search functionality
- URI-based resource identification

**Features Demonstrated:**
- Document collection with metadata
- Search by title, content, author, and tags
- Resource URIs (`document://doc_id`)
- Resource reading by URI

**Rust Concepts Explained:**

**1. HashMap for Data Storage**
```rust
use std::collections::HashMap;

struct ResourceProviderServer {
    documents: HashMap<String, Document>,  // Key-value storage
}
```
- **HashMap<K, V>**: Hash table for O(1) average lookups
- **Generic Types**: `K` is key type, `V` is value type
- **Ownership**: HashMap owns its key-value pairs

**2. Iterator Transformations**
```rust
self.documents
    .values()           // Iterator over values only
    .map(|doc| Resource { /* transform */ })  // Transform each document
    .collect()          // Materialize into Vec
```
- **`.values()`**: Iterate over HashMap values, ignoring keys
- **`.map()`**: Transform each element using a closure
- **Lazy Evaluation**: Operations are deferred until `.collect()`

**3. Option Types and Pattern Matching**
```rust
name: Some(doc.title.clone()),          // Wrap in Some variant
description: Some(format!("...")),      // Option<String>
mime_type: Some("text/plain".to_string()),
```
- **Option<T>**: Represents optional values (Some(T) or None)
- **Some(value)**: Present value variant
- **Explicit Optionality**: Rust forces handling of missing values

**4. String Formatting and Interpolation**
```rust
format!("document://{}", doc.id)        // String interpolation
format!("Document by {} - Tags: {}", 
    doc.author, doc.tags.join(", "))    // Multiple parameters
```
- **`format!` Macro**: Type-safe string formatting
- **`{}` Placeholders**: Positional parameter substitution
- **Display Trait**: Uses `Display` implementation for formatting

**5. Vector Operations**
```rust
doc.tags.join(", ")  // Join vector elements with separator
```
- **`.join()`**: Concatenate vector elements with delimiter
- **String Collections**: Working with `Vec<String>`

**6. Cloning for Ownership**
```rust
name: Some(doc.title.clone()),  // Clone the string
```
- **`.clone()`**: Create owned copy of data
- **Ownership Transfer**: Move vs clone for memory management
- **Trade-offs**: Cloning uses memory but avoids borrowing issues

**7. Struct Field Access**
```rust
Resource {
    uri: format!("document://{}", doc.id),
    name: Some(doc.title.clone()),
    description: Some(format!("Document by {} - Tags: {}", 
        doc.author, doc.tags.join(", "))),
    mime_type: Some("text/plain".to_string()),
}
```
- **Struct Literals**: Creating instances with named fields
- **Field Access**: Using dot notation to access struct fields
- **Constructor Pattern**: Building complex objects step by step

**Real Code from Example:**

**1. Rich Domain Models - Document Structure**
```rust
// Structure representing a comprehensive document resource
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Document {
    pub id: String,
    pub title: String,
    pub content: String,
    pub author: String,
    pub created_at: String,
    pub tags: Vec<String>,
}

// MCP Resource representation
#[derive(Serialize, Deserialize, Debug)]
pub struct Resource {
    pub uri: String,              // Unique identifier (e.g., "document://doc1")
    pub name: Option<String>,     // Human-readable name
    pub description: Option<String>, // Detailed description
    pub mime_type: Option<String>,   // Content type
}
```
*Pedagogical Note: Notice the rich metadata model. Documents have structured information, while Resources provide the MCP protocol interface. The `Clone` trait allows documents to be duplicated when needed.*

**2. Server with In-Memory Storage**
```rust
pub struct ResourceProviderServer {
    // In-memory document storage for this example
    // In a real application, this might be a database connection
    documents: HashMap<String, Document>,
}

impl ResourceProviderServer {
    pub fn new() -> Self {
        let mut documents = HashMap::new();

        // Add comprehensive sample documents for testing
        documents.insert("doc1".to_string(), Document {
            id: "doc1".to_string(),
            title: "Introduction to Model Context Protocol".to_string(),
            content: "The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to LLMs...".to_string(),
            author: "MCP Team".to_string(),
            created_at: "2024-01-01T00:00:00Z".to_string(),
            tags: vec!["MCP".to_string(), "Protocol".to_string(), "AI".to_string()],
        });
        // ... more documents

        Self { documents }
    }
}
```
*Pedagogical Note: This shows a common pattern - initializing with sample data for testing. The `HashMap` provides O(1) lookups by document ID.*

**3. Resource Listing - Iterator Transformation**
```rust
// List all available resources
pub fn list_resources(&self) -> Vec<Resource> {
    self.documents
        .values()                    // Iterator over HashMap values
        .map(|doc| Resource {        // Transform each Document into Resource
            uri: format!("document://{}", doc.id),
            name: Some(doc.title.clone()),
            description: Some(format!(
                "Document by {} - Tags: {}", 
                doc.author, 
                doc.tags.join(", ")    // Join vector elements with separator
            )),
            mime_type: Some("text/plain".to_string()),
        })
        .collect()                   // Materialize iterator into Vec
}
```
*Pedagogical Note: This demonstrates functional programming in Rust. The transformation pipeline efficiently converts internal document representation to MCP resource format.*

**4. Resource Reading with URI Parsing**
```rust
// Read a specific resource by URI
pub fn read_resource(&self, uri: &str) -> Result<Value, String> {
    // Parse the URI to extract the document ID
    if let Some(doc_id) = uri.strip_prefix("document://") {
        if let Some(document) = self.documents.get(doc_id) {
            // Return the document content as a resource
            Ok(serde_json::json!({
                "contents": [{
                    "uri": uri,
                    "mimeType": "text/plain",
                    "text": document.content
                }]
            }))
        } else {
            Err(format!("Document not found: {}", doc_id))
        }
    } else {
        Err(format!("Invalid document URI: {}", uri))
    }
}
```
*Pedagogical Note: This shows URI parsing and validation. The `strip_prefix` method safely extracts the document ID, with proper error handling for malformed URIs.*

**5. Advanced Search Implementation**
```rust
// Helper method to search documents by query
fn search_documents(&self, query: &str, limit: Option<usize>) -> Vec<&Document> {
    let query_lower = query.to_lowercase();
    let mut matches: Vec<&Document> = self
        .documents
        .values()
        .filter(|doc| {
            doc.title.to_lowercase().contains(&query_lower)
                || doc.content.to_lowercase().contains(&query_lower)
                || doc.author.to_lowercase().contains(&query_lower)
                || doc.tags.iter().any(|tag| tag.to_lowercase().contains(&query_lower))
        })
        .collect();

    // Sort by relevance (simple scoring based on title matches)
    matches.sort_by(|a, b| {
        let a_score = if a.title.to_lowercase().contains(&query_lower) { 2 } else { 0 }
                    + if a.tags.iter().any(|tag| tag.to_lowercase().contains(&query_lower)) { 1 } else { 0 };
        let b_score = if b.title.to_lowercase().contains(&query_lower) { 2 } else { 0 }
                    + if b.tags.iter().any(|tag| tag.to_lowercase().contains(&query_lower)) { 1 } else { 0 };
        b_score.cmp(&a_score)  // Sort by highest score first
    });

    if let Some(limit) = limit {
        matches.into_iter().take(limit).collect()
    } else {
        matches
    }
}
```
*Pedagogical Note: This demonstrates advanced iterator usage with filtering, scoring, and sorting. The search algorithm combines multiple fields with relevance scoring - title matches score higher than tag matches.*

**6. Tool Integration with Resources**
```rust
pub fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
    match name {
        "search_documents" => {
            let request: SearchRequest = serde_json::from_value(arguments)
                .map_err(|e| format!("Failed to parse arguments: {}", e))?;

            let matches = self.search_documents(&request.query, request.limit);

            let response = SearchResponse {
                total_count: matches.len(),
                matches: matches
                    .into_iter()
                    .map(|doc| DocumentSummary {
                        id: doc.id.clone(),
                        title: doc.title.clone(),
                        author: doc.author.clone(),
                        uri: format!("document://{}", doc.id),
                        tags: doc.tags.clone(),
                    })
                    .collect(),
            };

            serde_json::to_value(response)
                .map_err(|e| format!("Failed to serialize response: {}", e))
        }
        _ => Err(format!("Unknown tool: {}", name)),
    }
}
```
*Pedagogical Note: This shows how tools and resources work together. The search tool returns resource URIs that can then be read using the resource protocol.*

**Run Example:**
```bash
cargo run --bin example_05_resource_provider
```

### External Learning Resources

**Data Structures and Collections:**
- [HashMap]https://doc.rust-lang.org/std/collections/struct.HashMap.html - Key-value storage
- [Collections]https://doc.rust-lang.org/book/ch08-00-common-collections.html - Vectors, strings, and hash maps
- [Vec]https://doc.rust-lang.org/std/vec/struct.Vec.html - Dynamic arrays
- [BTreeMap vs HashMap]https://doc.rust-lang.org/std/collections/index.html#when-should-you-use-which-collection - Choosing the right collection

**Option Types and Error Handling:**
- [Option Type]https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#the-option-enum-and-its-advantages-over-null-values - Handling missing values
- [Result and Option]https://doc.rust-lang.org/rust-by-example/error/option_unwrap.html - Practical examples
- [Combinators]https://doc.rust-lang.org/rust-by-example/error/option_unwrap/map.html - map, and_then, unwrap_or

**Search Algorithms and String Processing:**
- [String Searching]https://doc.rust-lang.org/std/primitive.str.html#method.contains - Built-in string search methods
- [Sorting]https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_by - Custom sorting with closures
- [Search Algorithms]https://en.wikipedia.org/wiki/String-searching_algorithm - Understanding search techniques

**MCP Resources:**
- [MCP Resources Specification]https://spec.modelcontextprotocol.io/specification/server/resources/ - Official resource protocol
- [URI Schemes]https://tools.ietf.org/html/rfc3986 - Understanding URI structure
- [MIME Types]https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types - Content type specification

## Intermediate Examples

### Example 6: Configurable Server

Production-ready configuration management using files, environment variables, and command-line arguments.

**Key Concepts:**
- Multi-source configuration (files, env vars, CLI args)
- Tool enablement/disablement
- Runtime parameter configuration
- Configuration validation

**Features Demonstrated:**
- JSON configuration files
- Environment variable overrides
- Command-line argument processing
- Feature flags for tools

**Rust Concepts Explained:**

**1. Environment Variable Access**
```rust
use std::env;

if let Ok(server_name) = env::var("MCP_SERVER_NAME") {
    config.server_name = server_name;
}
```
- **`std::env`**: Standard library module for environment access
- **`env::var()`**: Returns `Result<String, VarError>`
- **`if let` Pattern**: Convenient pattern matching for Result types
- **Error Handling**: Gracefully handle missing environment variables

**2. Command Line Argument Processing**
```rust
let args: Vec<String> = env::args().collect();
for i in 0..args.len() {
    match args[i].as_str() {
        "--server-name" if i + 1 < args.len() => {
            config.server_name = args[i + 1].clone();
        }
        _ => {}
    }
}
```
- **`env::args()`**: Iterator over command line arguments
- **`.collect()`**: Convert iterator to Vec
- **Index Bounds Checking**: Ensure safe array access
- **Pattern Guards**: `if` conditions in match arms

**3. Configuration Structure with Defaults**
```rust
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ServerConfig {
    pub server_name: String,
    pub enabled_tools: Vec<String>,
    pub tool_configs: HashMap<String, ToolConfig>,
}

impl Default for ServerConfig {
    fn default() -> Self {
        // Set sensible defaults
    }
}
```
- **Derive Macros**: Multiple traits derived automatically
- **Clone Trait**: Enable config copying
- **Default Trait**: Provide sensible default values
- **Nested Structures**: Complex configuration hierarchies

**4. File I/O and JSON Parsing**
```rust
if let Ok(config_content) = std::fs::read_to_string("server_config.json") {
    if let Ok(file_config) = serde_json::from_str::<ServerConfig>(&config_content) {
        config = file_config;
    }
}
```
- **File Operations**: Reading entire files to strings
- **Nested Error Handling**: Multiple fallible operations
- **Type Annotations**: `from_str::<ServerConfig>` specifies target type
- **Graceful Degradation**: Continue with defaults if file missing

**5. Configuration Validation**
```rust
fn validate_configuration(config: &ServerConfig) -> Result<(), Box<dyn std::error::Error>> {
    if config.server_name.is_empty() {
        return Err("Server name cannot be empty".into());
    }
    // More validation...
    Ok(())
}
```
- **Trait Objects**: `Box<dyn std::error::Error>` for any error type
- **Early Return**: Validation with immediate error reporting
- **`.into()`**: Convert string literals to error types

**6. Feature Flags and Conditional Logic**
```rust
for tool_name in &config.enabled_tools {
    if let Some(tool_config) = self.get_tool_config(tool_name) {
        if !tool_config.enabled {
            continue;  // Skip disabled tools
        }
        // Process enabled tool
    }
}
```
- **Reference Iteration**: `&config.enabled_tools` borrows the vector
- **Option Handling**: Safe access to potentially missing values
- **Control Flow**: `continue` to skip loop iterations

**Configuration Priority:**
1. Command-line arguments (highest)
2. Environment variables
3. Configuration files
4. Default values (lowest)

**Run Example:**
```bash
# With environment variables
export MCP_SERVER_NAME="My Custom Server"
export MCP_MAX_CONNECTIONS=50
cargo run --bin example_06_configurable_server

# With command line args
cargo run --bin example_06_configurable_server -- --server-name "CLI Server"
```

### External Learning Resources

**Configuration Management:**
- [Environment Variables]https://doc.rust-lang.org/std/env/index.html - std::env module documentation
- [Configuration Patterns]https://rust-unofficial.github.io/patterns/patterns/creational/builder.html - Builder pattern for configuration
- [config crate]https://docs.rs/config - Popular configuration library
- [clap crate]https://docs.rs/clap - Command line argument parsing

**File I/O and JSON Parsing:**
- [File I/O]https://doc.rust-lang.org/book/ch12-02-reading-a-file.html - Reading and writing files
- [std::fs]https://doc.rust-lang.org/std/fs/index.html - File system operations
- [JSON Processing]https://docs.serde.rs/serde_json/ - Working with JSON data
- [Path Handling]https://doc.rust-lang.org/std/path/index.html - Cross-platform path manipulation

**Default Trait and Initialization:**
- [Default Trait]https://doc.rust-lang.org/std/default/trait.Default.html - Providing default values
- [Derive Default]https://doc.rust-lang.org/reference/derive.html#default - Automatic default implementations
- [Builder Pattern]https://rust-unofficial.github.io/patterns/patterns/creational/builder.html - Constructing complex objects

**Production Configuration:**
- [The Twelve-Factor App]https://12factor.net/config - Configuration best practices
- [dotenv crate]https://docs.rs/dotenv - Loading environment variables from files
- [figment crate]https://docs.rs/figment - Advanced configuration management

### Example 7: File Operations

Secure file system operations with comprehensive safety controls.

**Key Concepts:**
- Path validation and sanitization
- Directory traversal prevention
- File size limits
- Extension filtering
- Permission management

**Features Demonstrated:**
- Safe file reading/writing
- Directory listing with metadata
- File information retrieval
- Configurable security policies

**Security Features:**
- Allowed directory restrictions
- File extension whitelisting
- Path canonicalization
- Size limit enforcement
- Read-only mode support

**Run Example:**
```bash
cargo run --bin example_07_file_operations
```

### Example 8: HTTP Client

*Implementation details would be based on the actual example_08 file*

### Example 9: Database Integration

SQLite database integration with connection pooling and migrations.

**Key Concepts:**
- Database connection pooling
- SQL migrations
- CRUD operations
- Prepared statements
- Error handling for database operations

**Features Demonstrated:**
- User management (create, read, update, delete)
- Database migrations
- Search with pagination
- Connection pool management
- Operation logging

**Rust Concepts Explained:**

**1. Async/Await Programming**
```rust
async fn create_user(&self, arguments: Value) -> Result<Value, String> {
    // async function returns Future<Output = Result<Value, String>>
    let result = sqlx::query_as::<_, (i64,)>(...)
        .await?;  // await the future and propagate errors
}
```
- **`async fn`**: Declares an asynchronous function
- **`await`**: Suspends execution until future completes
- **Non-blocking**: Other tasks can run while waiting for I/O
- **Error Propagation**: `?` operator works with async functions

**2. Connection Pooling with SqlitePool**
```rust
use sqlx::SqlitePool;

pub struct DatabaseServer {
    pool: SqlitePool,  // Shared connection pool
}

let pool = SqlitePool::connect("sqlite:./data/example.db").await?;
```
- **Connection Pooling**: Reuse database connections efficiently
- **Async Operations**: All database calls are async
- **Resource Management**: Pool handles connection lifecycle
- **Concurrent Access**: Multiple tasks can share the pool safely

**3. Prepared Statements and Parameter Binding**
```rust
sqlx::query_as::<_, (i64,)>(
    "INSERT INTO users (name, email, age) VALUES (?, ?, ?) RETURNING id"
)
.bind(&request.name)    // Bind first parameter
.bind(&request.email)   // Bind second parameter
.bind(request.age)      // Bind third parameter
```
- **SQL Injection Protection**: Parameters are safely escaped
- **Type Safety**: Compile-time checking of SQL types
- **Parameter Binding**: `.bind()` method for each placeholder
- **Query Compilation**: Statements are prepared once and reused

**4. Tuple Destructuring and Type Annotations**
```rust
sqlx::query_as::<_, (i64,)>(...)  // Returns tuple with single i64
let result = result.0;            // Extract the ID from tuple
```
- **Type Hints**: `<_, (i64,)>` specifies return type
- **Tuple Types**: `(i64,)` is a single-element tuple
- **Destructuring**: Access tuple elements by index

**5. Error Handling with map_err**
```rust
.fetch_one(&self.pool)
.await
.map_err(|e| format!("Failed to create user: {}", e))?;
```
- **Error Transformation**: Convert database errors to strings
- **`.map_err()`**: Transform error type while preserving success value
- **Error Context**: Add meaningful error messages
- **Chaining**: Combine with `?` operator for propagation

**6. JSON Deserialization**
```rust
let request: CreateUserRequest = serde_json::from_value(arguments)?;
```
- **Type Inference**: Rust infers the target type from annotation
- **Automatic Validation**: Serde validates JSON structure
- **Error Propagation**: `?` converts serde errors to function error type

**7. Database Migrations**
```rust
sqlx::query(
    r#"
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT UNIQUE NOT NULL,
        created_at TEXT NOT NULL DEFAULT (datetime('now'))
    )
    "#,
)
.execute(&self.pool)
.await?;
```
- **Raw String Literals**: `r#"..."#` preserves formatting and escaping
- **DDL Operations**: Data Definition Language for schema changes
- **Idempotent Migrations**: `IF NOT EXISTS` for safe re-runs

**Real Code from Example:**

**1. Database Configuration and Connection Setup**
```rust
use sqlx::{Sqlite, SqlitePool};

#[derive(Debug, Serialize, Deserialize)]
pub struct DatabaseConfig {
    pub database_url: String,
    pub max_connections: u32,
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            database_url: "sqlite:./data/example.db".to_string(),
            max_connections: 10,
        }
    }
}

pub struct DatabaseServer {
    pool: SqlitePool,  // Connection pool for efficient database access
    config: DatabaseConfig,
}
```
*Pedagogical Note: Connection pooling is crucial for database performance. SQLx provides async connection pooling out of the box, allowing multiple concurrent database operations.*

**2. Async Database Initialization with Migrations**
```rust
impl DatabaseServer {
    pub async fn new(config: DatabaseConfig) -> Result<Self, sqlx::Error> {
        // Create the data directory if it doesn't exist
        if let Some(parent) = std::path::Path::new(&config.database_url.replace("sqlite:", "")).parent() {
            std::fs::create_dir_all(parent).map_err(|e| sqlx::Error::Io(e))?;
        }

        // Create connection pool with configuration
        let pool = SqlitePool::connect_with(
            sqlx::sqlite::SqliteConnectOptions::new()
                .filename(config.database_url.replace("sqlite:", ""))
                .create_if_missing(true)
                .journal_mode(sqlx::sqlite::SqliteJournalMode::Wal),
        )
        .await?;

        let server = Self { pool, config };

        // Run database migrations
        server.run_migrations().await?;

        Ok(server)
    }

    async fn run_migrations(&self) -> Result<(), sqlx::Error> {
        // Create users table if it doesn't exist
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                email TEXT UNIQUE NOT NULL,
                age INTEGER,
                created_at TEXT NOT NULL DEFAULT (datetime('now'))
            )
            "#,
        )
        .execute(&self.pool)
        .await?;

        println!("✅ Database migrations completed successfully");
        Ok(())
    }
}
```
*Pedagogical Note: This shows production-ready database setup with migrations, WAL mode for better concurrency, and proper error handling. The `create_if_missing` option simplifies deployment.*

**3. Type-Safe Database Operations**
```rust
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateUserRequest {
    pub name: String,
    pub email: String,
    pub age: Option<i32>,
}

#[derive(Serialize, Deserialize, Debug, sqlx::FromRow)]
pub struct User {
    pub id: i64,
    pub name: String,
    pub email: String,
    pub age: Option<i32>,
    pub created_at: String,
}

async fn create_user(&self, arguments: Value) -> Result<Value, String> {
    let request: CreateUserRequest = serde_json::from_value(arguments)
        .map_err(|e| format!("Failed to parse arguments: {}", e))?;

    // Insert user and return the generated ID
    let result = sqlx::query_as::<_, (i64,)>(
        "INSERT INTO users (name, email, age) VALUES (?, ?, ?) RETURNING id"
    )
    .bind(&request.name)
    .bind(&request.email)
    .bind(request.age)
    .fetch_one(&self.pool)
    .await
    .map_err(|e| format!("Failed to create user: {}", e))?;

    let user_id = result.0;

    // Fetch the complete user record
    let user = sqlx::query_as::<_, User>(
        "SELECT id, name, email, age, created_at FROM users WHERE id = ?"
    )
    .bind(user_id)
    .fetch_one(&self.pool)
    .await
    .map_err(|e| format!("Failed to fetch created user: {}", e))?;

    serde_json::to_value(&user)
        .map_err(|e| format!("Failed to serialize user: {}", e))
}
```
*Pedagogical Note: The `sqlx::FromRow` derive macro automatically maps SQL rows to Rust structs. Parameter binding prevents SQL injection attacks while maintaining type safety.*

**4. Advanced Query with Pagination**
```rust
#[derive(Serialize, Deserialize, Debug)]
pub struct SearchUsersRequest {
    pub query: Option<String>,
    pub page: Option<u32>,
    pub page_size: Option<u32>,
}

async fn search_users(&self, arguments: Value) -> Result<Value, String> {
    let request: SearchUsersRequest = serde_json::from_value(arguments)?;
    
    let page = request.page.unwrap_or(1);
    let page_size = request.page_size.unwrap_or(10).min(100); // Limit max page size
    let offset = (page - 1) * page_size;

    let users = if let Some(query) = &request.query {
        // Search with filtering
        sqlx::query_as::<_, User>(
            "SELECT id, name, email, age, created_at FROM users 
             WHERE name LIKE ? OR email LIKE ? 
             ORDER BY created_at DESC 
             LIMIT ? OFFSET ?"
        )
        .bind(format!("%{}%", query))
        .bind(format!("%{}%", query))
        .bind(page_size as i32)
        .bind(offset as i32)
        .fetch_all(&self.pool)
        .await
    } else {
        // Get all users with pagination
        sqlx::query_as::<_, User>(
            "SELECT id, name, email, age, created_at FROM users 
             ORDER BY created_at DESC 
             LIMIT ? OFFSET ?"
        )
        .bind(page_size as i32)
        .bind(offset as i32)
        .fetch_all(&self.pool)
        .await
    }
    .map_err(|e| format!("Failed to search users: {}", e))?;

    // Get total count for pagination
    let total_count: (i64,) = if let Some(query) = &request.query {
        sqlx::query_as(
            "SELECT COUNT(*) FROM users WHERE name LIKE ? OR email LIKE ?"
        )
        .bind(format!("%{}%", query))
        .bind(format!("%{}%", query))
        .fetch_one(&self.pool)
        .await
    } else {
        sqlx::query_as("SELECT COUNT(*) FROM users")
            .fetch_one(&self.pool)
            .await
    }
    .map_err(|e| format!("Failed to count users: {}", e))?;

    let response = serde_json::json!({
        "users": users,
        "page": page,
        "page_size": page_size,
        "total_count": total_count.0,
        "total_pages": (total_count.0 as f64 / page_size as f64).ceil() as u32
    });

    Ok(response)
}
```
*Pedagogical Note: This demonstrates real-world database patterns: pagination, search filtering, and count queries. The LIKE operator provides simple text searching, while LIMIT/OFFSET handles pagination.*

**5. Transaction Support for Data Integrity**
```rust
async fn update_user(&self, arguments: Value) -> Result<Value, String> {
    let request: UpdateUserRequest = serde_json::from_value(arguments)?;
    
    // Start a database transaction
    let mut tx = self.pool.begin().await
        .map_err(|e| format!("Failed to start transaction: {}", e))?;

    // Update the user within the transaction
    let rows_affected = sqlx::query(
        "UPDATE users SET name = ?, email = ?, age = ? WHERE id = ?"
    )
    .bind(&request.name)
    .bind(&request.email)
    .bind(request.age)
    .bind(request.id)
    .execute(&mut *tx)
    .await
    .map_err(|e| format!("Failed to update user: {}", e))?
    .rows_affected();

    if rows_affected == 0 {
        // Rollback transaction if user not found
        tx.rollback().await
            .map_err(|e| format!("Failed to rollback transaction: {}", e))?;
        return Err("User not found".to_string());
    }

    // Fetch the updated user
    let user = sqlx::query_as::<_, User>(
        "SELECT id, name, email, age, created_at FROM users WHERE id = ?"
    )
    .bind(request.id)
    .fetch_one(&mut *tx)
    .await
    .map_err(|e| format!("Failed to fetch updated user: {}", e))?;

    // Commit the transaction
    tx.commit().await
        .map_err(|e| format!("Failed to commit transaction: {}", e))?;

    serde_json::to_value(&user)
        .map_err(|e| format!("Failed to serialize user: {}", e))
}
```
*Pedagogical Note: Transactions ensure data integrity. If any operation fails, the entire transaction is rolled back. The `mut *tx` syntax allows using the transaction reference with SQLx queries.*

**Run Example:**
```bash
cargo run --bin example_09_database
```

### External Learning Resources

**Database Programming in Rust:**
- [SQLx Book]https://github.com/launchbadge/sqlx/blob/main/README.md - Comprehensive async SQL toolkit
- [Database Best Practices]https://github.com/launchbadge/sqlx/blob/main/FAQ.md - SQLx FAQ and patterns
- [Async Programming Book]https://rust-lang.github.io/async-book/ - Complete async programming guide
- [diesel crate]https://diesel.rs/ - Alternative ORM approach

**SQL and Database Concepts:**
- [SQLite Tutorial]https://www.sqlitetutorial.net/ - SQLite-specific features and syntax
- [SQL Injection Prevention]https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html - Security best practices
- [Database Transactions]https://en.wikipedia.org/wiki/Database_transaction - ACID properties and isolation
- [Connection Pooling]https://en.wikipedia.org/wiki/Connection_pool - Understanding connection management

**Async Rust and Tokio:**
- [Tokio Tutorial]https://tokio.rs/tokio/tutorial - Complete async runtime guide
- [Async/Await in Rust]https://blog.rust-lang.org/2019/11/07/Async-await-stable.html - Official announcement and explanation
- [Futures and Streams]https://rust-lang.github.io/async-book/02_execution/01_chapter.html - Core async concepts
- [Error Handling in Async]https://rust-lang.github.io/async-book/07_workarounds/01_chapter.html - Async-specific error patterns

**Production Database Patterns:**
- [Database Migrations]https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/README.md - Schema versioning with SQLx CLI
- [Query Performance]https://use-the-index-luke.com/ - SQL performance tuning
- [Connection Pool Tuning]https://docs.rs/sqlx/latest/sqlx/pool/struct.PoolOptions.html - Optimizing connection pools

### Example 10: WebSocket Server

*Implementation details would be based on the actual example_10 file*

## Advanced Examples

### Example 11: Monitoring and Metrics

Comprehensive system monitoring with metrics collection, health checks, and alerting.

**Key Concepts:**
- Real-time metrics collection
- Health check orchestration
- Threshold-based alerting
- Historical data management
- Time-series data handling

**Features Demonstrated:**
- System metrics (CPU, memory, disk, network)
- Service health checks
- Alert management (creation, filtering, clearing)
- Metrics history with circular buffering
- Configurable alert thresholds

**Monitoring Capabilities:**
- CPU and memory usage tracking
- Network activity monitoring
- Service availability checks
- Alert threshold configuration
- Historical trend analysis

**Run Example:**
```bash
cargo run --bin example_11_monitoring
```

### Example 12: Task Queue System

Async background task processing with priority queues and worker management.

**Key Concepts:**
- Priority-based task scheduling
- Background worker processes
- Channel-based communication
- Graceful shutdown handling
- Task retry mechanisms

**Features Demonstrated:**
- Task prioritization (Low, Normal, High, Critical)
- Async task execution
- Worker lifecycle management
- Task status tracking
- Error handling and logging

**Rust Concepts Explained:**

**1. Generic Functions with Trait Bounds**
```rust
pub async fn add_task<F>(
    &self,
    priority: TaskPriority,
    task: F,
    description: String,
) -> Result<u64, String>
where
    F: Fn() -> Result<String, String> + Send + 'static,
```
- **Generic Parameters**: `<F>` introduces a type parameter
- **Trait Bounds**: `Fn() + Send + 'static` constrains the type
- **Closure Traits**: `Fn()` means the closure can be called multiple times
- **Thread Safety**: `Send` allows moving between threads
- **Lifetime**: `'static` means no borrowed references with shorter lifetimes

**2. Channels for Inter-Task Communication**
```rust
use tokio::sync::mpsc;

let (sender, mut receiver) = mpsc::unbounded_channel::<TaskItem>();
```
- **Multi-Producer Single-Consumer**: Multiple senders, one receiver
- **Unbounded Channel**: No limit on queued messages
- **Generic Channel**: `<TaskItem>` specifies message type
- **Async Communication**: Non-blocking message passing

**3. Enums with Ordering**
```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TaskPriority {
    Low = 1,
    Normal = 2,
    High = 3,
    Critical = 4,
}
```
- **Discriminant Values**: Explicit numeric values for ordering
- **Derive Traits**: Automatic comparison implementation
- **Ordering**: `PartialOrd` and `Ord` enable sorting
- **Copy Semantics**: Lightweight enum copying

**4. Boxed Closures and Dynamic Dispatch**
```rust
type TaskFunction = Box<dyn Fn() -> Result<String, String> + Send>;

pub struct TaskItem {
    task: TaskFunction,  // Dynamically dispatched function
}
```
- **Trait Objects**: `dyn Fn()` for runtime polymorphism
- **Heap Allocation**: `Box<>` stores closures on the heap
- **Type Aliases**: `type TaskFunction` creates readable type names
- **Send Trait**: Ensures thread safety for cross-thread transfer

**5. Background Task Spawning**
```rust
tokio::spawn(async move {
    let mut buffer = VecDeque::new();
    
    while let Some(task) = receiver.recv().await {
        buffer.push_back(task);
        buffer.sort_by(|a, b| b.priority.cmp(&a.priority));
        // Process tasks...
    }
});
```
- **Task Spawning**: `tokio::spawn` creates concurrent task
- **Move Semantics**: `async move` transfers ownership into closure
- **Deque Operations**: `VecDeque` for efficient queue operations
- **Custom Sorting**: Priority-based task ordering

**6. Error Handling in Channels**
```rust
self.sender.send(task_item)
    .map_err(|_| "Task queue is shut down".to_string())?;
```
- **Channel Errors**: Send fails when receiver is dropped
- **Error Mapping**: Convert channel error to string
- **Graceful Degradation**: Meaningful error messages

**7. Async Control Flow**
```rust
while let Some(task) = receiver.recv().await {
    // Process each task as it arrives
    let task_id = task.id;
    
    // Execute the task and handle the result
    match (task.task)() {
        Ok(result) => println!("Task {} completed: {}", task_id, result),
        Err(error) => println!("Task {} failed: {}", task_id, error),
    }
}
```
- **Async Loops**: `while let` with `.await` for stream processing
- **Pattern Matching**: Handle task execution results
- **Non-blocking**: Other tasks can run during await points

**Real Code from Example:**

**1. Type System for Async Tasks**
```rust
// Type alias for task functions
// This represents a task that can be executed asynchronously
// Tasks are boxed functions that return a Result
type Task = Box<dyn Fn() -> Result<String, String> + Send + 'static>;

// Enum: TaskPriority with explicit ordering
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TaskPriority {
    Low = 1,
    Normal = 2,
    High = 3,
    Critical = 4,
}

// Task container with metadata
pub struct TaskItem {
    id: u64,
    priority: TaskPriority,
    task: Task,
    description: String,
}
```
*Pedagogical Note: The type alias makes complex types readable. The `Send + 'static` bounds ensure tasks can be moved between threads safely. The `Ord` trait enables priority-based sorting.*

**2. Queue Structure with Channel Communication**
```rust
use tokio::sync::{mpsc, Mutex, Notify};
use std::collections::VecDeque;

pub struct TaskQueue {
    sender: mpsc::UnboundedSender<TaskItem>,    // Send tasks to worker
    shutdown_notify: Arc<Notify>,                // Coordinate shutdown
    next_task_id: Arc<Mutex<u64>>,              // Thread-safe ID generation
}

impl TaskQueue {
    pub fn new() -> Self {
        // Create an unbounded channel for task communication
        let (sender, receiver) = mpsc::unbounded_channel::<TaskItem>();
        
        // Create a notification mechanism for graceful shutdown
        let shutdown_notify = Arc::new(Notify::new());
        let shutdown_notify_worker = shutdown_notify.clone();
        
        // Initialize the task ID counter
        let next_task_id = Arc::new(Mutex::new(1u64));

        // Spawn the background worker task
        tokio::spawn(async move {
            Self::worker_loop(receiver, shutdown_notify_worker).await;
        });

        Self { sender, shutdown_notify, next_task_id }
    }
}
```
*Pedagogical Note: This shows the async ecosystem in action. `mpsc::unbounded_channel` provides async communication, `Notify` enables graceful shutdown signaling, and `Arc<Mutex<>>` provides thread-safe shared state.*

**3. Advanced Async Task Management**
```rust
pub async fn add_task<F>(
    &self,
    priority: TaskPriority,
    task: F,
    description: String,
) -> Result<u64, String>
where
    F: Fn() -> Result<String, String> + Send + 'static,
{
    // Generate a unique ID for this task
    let mut next_id = self.next_task_id.lock().await;
    let task_id = *next_id;
    *next_id += 1;
    drop(next_id); // Release the lock early

    // Create the task item
    let task_item = TaskItem::new(task_id, priority, Box::new(task), description.clone());

    // Send the task to the worker
    match self.sender.send(task_item) {
        Ok(_) => {
            info!("Queued task {}: {} (priority: {:?})", task_id, description, priority);
            Ok(task_id)
        }
        Err(_) => {
            error!("Failed to queue task: worker has shut down");
            Err("Task queue is shut down".to_string())
        }
    }
}
```
*Pedagogical Note: Notice the careful lock management - we acquire the mutex, increment the counter, then immediately drop the lock to minimize contention. The channel send operation can fail if the receiver is dropped.*

**4. Priority-Based Worker Loop**
```rust
async fn worker_loop(
    mut receiver: mpsc::UnboundedReceiver<TaskItem>,
    shutdown_notify: Arc<Notify>,
) {
    // Use a priority queue to ensure high-priority tasks are executed first
    let mut task_buffer: VecDeque<TaskItem> = VecDeque::new();

    info!("Task queue worker started");

    loop {
        // Use tokio::select! to handle both incoming tasks and shutdown signals
        tokio::select! {
            // Handle incoming tasks
            task_option = receiver.recv() => {
                match task_option {
                    Some(task) => {
                        // Insert the task in priority order
                        Self::insert_task_by_priority(&mut task_buffer, task);
                        
                        // Process all available tasks in the buffer
                        Self::process_task_buffer(&mut task_buffer).await;
                    }
                    None => {
                        // Channel closed, no more tasks will arrive
                        warn!("Task channel closed, worker shutting down");
                        break;
                    }
                }
            }
            
            // Handle shutdown signal
            _ = shutdown_notify.notified() => {
                info!("Shutdown signal received, processing remaining tasks");
                
                // Process any remaining tasks
                Self::process_task_buffer(&mut task_buffer).await;
                
                // Process any remaining tasks in the channel
                while let Ok(task) = receiver.try_recv() {
                    Self::insert_task_by_priority(&mut task_buffer, task);
                }
                Self::process_task_buffer(&mut task_buffer).await;
                
                info!("Worker shutdown complete");
                break;
            }
        }
    }
}
```
*Pedagogical Note: `tokio::select!` is crucial for async programming - it allows handling multiple async operations concurrently. The worker processes tasks in priority order and handles graceful shutdown.*

**5. Priority Queue Implementation**
```rust
// Insert a task into the buffer maintaining priority order
fn insert_task_by_priority(buffer: &mut VecDeque<TaskItem>, task: TaskItem) {
    // Find the correct position to insert the task based on priority
    let insert_position = buffer
        .iter()
        .position(|existing_task| existing_task.priority < task.priority)
        .unwrap_or(buffer.len());

    buffer.insert(insert_position, task);
}

// Process all tasks currently in the buffer
async fn process_task_buffer(buffer: &mut VecDeque<TaskItem>) {
    while let Some(task) = buffer.pop_front() {
        let task_id = task.id;

        // Execute the task and handle the result
        match task.execute() {
            Ok(result) => {
                info!("Task {} completed successfully: {}", task_id, result);
            }
            Err(error) => {
                error!("Task {} failed: {}", task_id, error);
            }
        }

        // Add a small delay between tasks to prevent overwhelming the system
        sleep(Duration::from_millis(10)).await;
    }
}
```
*Pedagogical Note: This shows manual priority queue implementation using `VecDeque`. Tasks are inserted in priority order and processed sequentially. The small delay prevents CPU saturation.*

**6. Sample Task Creation and Usage**
```rust
// Create a sample task function for demonstration
fn create_sample_task(
    task_name: String,
    work_duration_ms: u64,
    should_fail: bool,
) -> Box<dyn Fn() -> Result<String, String> + Send + 'static> {
    Box::new(move || {
        // Simulate some work
        std::thread::sleep(Duration::from_millis(work_duration_ms));

        if should_fail {
            Err(format!("Task '{}' failed as requested", task_name))
        } else {
            Ok(format!("Task '{}' completed after {}ms", task_name, work_duration_ms))
        }
    })
}

// Usage in main function
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let task_queue = TaskQueue::new();

    // Add a high-priority task
    task_queue.add_task(
        TaskPriority::High,
        create_sample_task("High Priority Task".to_string(), 100, false),
        "Critical system maintenance".to_string(),
    ).await?;

    // Add a critical priority task (should be processed first)
    task_queue.add_task(
        TaskPriority::Critical,
        create_sample_task("Critical Task".to_string(), 75, false),
        "Emergency response".to_string(),
    ).await?;

    Ok(())
}
```
*Pedagogical Note: This demonstrates closure creation with move semantics. The `move` keyword transfers ownership of variables into the closure, making it `'static`. The higher-level API makes task creation simple.*

**Run Example:**
```bash
cargo run --bin example_12_task_queue
```

### External Learning Resources

**Advanced Async Programming:**
- [Tokio Select Macro]https://tokio.rs/tokio/tutorial/select - Concurrent async operations
- [Channels and Message Passing]https://tokio.rs/tokio/tutorial/channels - Inter-task communication
- [Spawning Tasks]https://tokio.rs/tokio/tutorial/spawning - Background task management
- [Graceful Shutdown]https://tokio.rs/tokio/topics/shutdown - Clean application termination

**Concurrency Patterns:**
- [Actor Model]https://ryhl.io/blog/actors-with-tokio/ - Message-passing concurrency
- [Work Queue Pattern]https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem - Producer-consumer problem
- [Priority Queues]https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html - Efficient priority handling
- [Background Jobs]https://github.com/tokio-rs/tokio/discussions/3858 - Task queue design patterns

**Generic Programming and Trait Bounds:**
- [Generics]https://doc.rust-lang.org/book/ch10-01-syntax.html - Generic types and functions
- [Trait Bounds]https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters - Constraining generic types
- [Send and Sync]https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html - Thread safety traits
- [Lifetime Parameters]https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html - Managing references

**Production Task Queue Systems:**
- [sidekiq.rs]https://github.com/film42/sidekiq-rs - Background job processing
- [faktory crate]https://docs.rs/faktory - Language-agnostic job server
- [celery-rs]https://github.com/rusty-celery/rusty-celery - Distributed task queue
- [Queue Design Patterns]https://aws.amazon.com/builders-library/avoiding-insurmountable-queue-backlogs/ - Scalable queue architecture

### Example 13: Authentication Service

Production-ready authentication with JWT tokens, password hashing, and session management.

**Key Concepts:**
- Secure password hashing (SHA-256 for demo, use bcrypt/Argon2 in production)
- JWT-like token management
- Role-based access control
- Account lockout protection
- Session lifecycle management

**Features Demonstrated:**
- User registration with validation
- Secure authentication
- Token generation and validation
- Account lockout after failed attempts
- Role-based permissions

**Rust Concepts Explained:**

**1. Thread-Safe Shared State**
```rust
use std::sync::{Arc, RwLock};

pub struct AuthService {
    users: Arc<RwLock<HashMap<String, User>>>,
    active_tokens: Arc<RwLock<HashMap<String, TokenInfo>>>,
}
```
- **Arc (Atomically Reference Counted)**: Enables shared ownership across threads
- **RwLock**: Multiple readers OR single writer lock
- **Thread Safety**: Safe concurrent access to shared data
- **Interior Mutability**: Modify data behind shared references

**2. Enums for Type Safety**
```rust
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UserRole {
    Admin,
    User,
    Guest,
}

#[derive(Debug)]
pub enum AuthError {
    UserNotFound,
    InvalidPassword,
    AccountLocked,
    TokenExpired,
}
```
- **Type Safety**: Compile-time guarantees for valid values
- **Pattern Matching**: Exhaustive handling of all cases
- **Serialization**: Convert to/from JSON with serde
- **Error Variants**: Structured error handling

**3. DateTime Handling with Chrono**
```rust
use chrono::{DateTime, Duration, Utc};

expires_at: Utc::now() + Duration::hours(24),
last_login: Some(Utc::now()),
```
- **UTC Timestamps**: Timezone-aware datetime handling
- **Duration Arithmetic**: Add/subtract time periods
- **Type Safety**: Compile-time checks for datetime operations
- **Serialization**: JSON-compatible datetime formats

**4. Password Hashing and Security**
```rust
use sha2::{Digest, Sha256};

fn hash_password(password: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(password.as_bytes());
    hex::encode(hasher.finalize())
}
```
- **Cryptographic Hashing**: One-way password transformation
- **Byte Operations**: Convert strings to bytes for hashing
- **Hex Encoding**: Convert binary hash to string representation
- **Security Note**: Use bcrypt/Argon2 in production

**5. Token Generation and Validation**
```rust
fn generate_token(&self) -> String {
    format!("{}_{}", 
        Uuid::new_v4().to_string().replace('-', ""),
        Utc::now().timestamp()
    )
}

fn is_token_valid(&self, token: &str) -> bool {
    if let Ok(tokens) = self.active_tokens.read() {
        if let Some(token_info) = tokens.get(token) {
            return token_info.expires_at > Utc::now();
        }
    }
    false
}
```
- **UUID Generation**: Cryptographically secure random IDs
- **String Manipulation**: Format and modify token strings
- **Lock Acquisition**: Safe access to shared data
- **Nested Pattern Matching**: Handle multiple Option/Result types

**6. Account Lockout Logic**
```rust
if user.failed_login_attempts >= 3 {
    if let Some(lockout_until) = user.locked_until {
        if Utc::now() < lockout_until {
            return Err(AuthError::AccountLocked);
        }
    }
}
```
- **Conditional Logic**: Complex security rule implementation
- **Option Handling**: Safe access to potentially missing values
- **Time Comparisons**: Validate lockout periods
- **Early Return**: Immediate rejection of locked accounts

**7. RwLock Usage Patterns**
```rust
// Reading data
let users = self.users.read().unwrap();
if let Some(user) = users.get(username) { /* ... */ }

// Writing data
let mut users = self.users.write().unwrap();
users.insert(username.to_string(), new_user);
```
- **Read Lock**: Multiple concurrent readers
- **Write Lock**: Exclusive access for modifications
- **Lock Acquisition**: `.read()` and `.write()` methods
- **Panic Handling**: `.unwrap()` for lock poisoning (use proper error handling in production)

**Security Features:**
- Password strength validation
- Failed attempt tracking
- Account lockout mechanism
- Token expiration handling
- Secure session management

**Run Example:**
```bash
cargo run --bin example_13_auth_service
```

### External Learning Resources

**Security and Cryptography:**
- [Password Hashing]https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html - OWASP password storage guidelines
- [bcrypt crate]https://docs.rs/bcrypt - Secure password hashing (recommended over SHA-256)
- [argon2 crate]https://docs.rs/argon2 - Modern password hashing algorithm
- [jsonwebtoken crate]https://docs.rs/jsonwebtoken - JWT implementation for Rust

**Thread Safety and Concurrency:**
- [Arc and Rc]https://doc.rust-lang.org/book/ch15-04-rc.html - Reference counting smart pointers
- [Mutex and RwLock]https://doc.rust-lang.org/book/ch16-03-shared-state.html - Shared state concurrency
- [Interior Mutability]https://doc.rust-lang.org/book/ch15-05-interior-mutability.html - Mutating immutable values
- [Thread Safety]https://doc.rust-lang.org/nomicon/send-and-sync.html - Send and Sync traits

**Authentication Patterns:**
- [JWT Best Practices]https://auth0.com/blog/a-look-at-the-latest-draft-for-jwt-bcp/ - JSON Web Token security
- [Session Management]https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html - Secure session handling
- [Rate Limiting]https://en.wikipedia.org/wiki/Rate_limiting - Preventing brute force attacks
- [OAuth 2.0]https://oauth.net/2/ - Industry standard authorization

**DateTime and Temporal Logic:**
- [Chrono Documentation]https://docs.rs/chrono - Date and time handling
- [Time Zones]https://en.wikipedia.org/wiki/Time_zone - Understanding temporal complexity
- [UTC vs Local Time]https://stackoverflow.com/questions/19654578/when-to-use-utc-vs-local-time - Best practices for timestamps

**Production Authentication:**
- [OWASP Authentication Guide]https://owasp.org/www-project-cheat-sheets/ - Comprehensive security cheat sheets
- [Auth0 Blog]https://auth0.com/blog/tech/ - Authentication and security articles
- [Security Headers]https://securityheaders.com/ - HTTP security best practices

### Example 14: Notification Service

Multi-channel notification system with templates and delivery tracking.

**Key Concepts:**
- Multi-channel delivery (Email, SMS, Webhook, Push)
- Template-based messaging
- Subscription management
- Delivery tracking and retry logic
- Background worker processes

**Features Demonstrated:**
- Notification templates with variables
- User subscription management
- Multi-channel delivery
- Delivery status tracking
- Retry mechanisms for failed deliveries

**Supported Channels:**
- Email notifications
- SMS messaging
- Webhook callbacks
- Push notifications
- In-app notifications

**Run Example:**
```bash
cargo run --bin example_14_notification_service
```

### Example 15: Data Pipeline

ETL (Extract, Transform, Load) pipeline with data processing and transformation.

**Key Concepts:**
- Data transformation pipelines
- ETL operations
- Data validation
- Pipeline composition
- Error handling in data flows

**Features Demonstrated:**
- Data record processing
- Transformation operations (filter, map, enrich)
- Pipeline chaining
- Error tracking and statistics
- Data validation

**Transformation Types:**
- Filtering by field values
- Mathematical transformations
- Data enrichment
- Statistical operations

**Run Example:**
```bash
cargo run --bin example_15_data_pipeline
```

## Enterprise Examples

### Example 16: Search Service

Full-text search engine with indexing and relevance scoring.

**Key Concepts:**
- Document indexing
- Full-text search
- Relevance scoring
- Search result ranking
- Index management

**Features Demonstrated:**
- Document indexing with metadata
- Word-based search indexing
- Tag-based searching
- Relevance scoring algorithms
- Search result pagination

**Run Example:**
```bash
cargo run --bin example_16_search_service
```

### Example 17: Blockchain Integration

Blockchain concepts including blocks, transactions, and proof-of-work.

**Key Concepts:**
- Blockchain data structures
- Transaction management
- Hash-based security
- Proof-of-work mining
- Chain validation

**Features Demonstrated:**
- Block creation and mining
- Transaction processing
- Hash calculation with SHA-256
- Proof-of-work algorithm
- Balance tracking

**Run Example:**
```bash
cargo run --bin example_17_blockchain_integration
```

### Example 18: ML Model Server

Machine learning model serving with inference capabilities.

**Key Concepts:**
- Model management
- Inference pipelines
- Batch prediction
- Model versioning
- Performance tracking

**Features Demonstrated:**
- Model registration and activation
- Single and batch predictions
- Model versioning
- Inference statistics
- Model lifecycle management

**Run Example:**
```bash
cargo run --bin example_18_ml_model_server
```

### Example 19: Microservice Gateway

Service mesh gateway with routing and load balancing.

**Key Concepts:**
- Service discovery
- Load balancing strategies
- Request routing
- Health checking
- Gateway patterns

**Features Demonstrated:**
- Service registration
- Round-robin load balancing
- Route mapping
- Health status monitoring
- Request/response tracking

**Load Balancing Strategies:**
- Round Robin
- Weighted Round Robin
- Random selection

**Run Example:**
```bash
cargo run --bin example_19_microservice_gateway
```

### Example 20: Enterprise Server

Complete enterprise application combining authentication, monitoring, caching, and APIs.

**Key Concepts:**
- Enterprise architecture patterns
- Component integration
- Caching strategies
- API management
- Production patterns

**Features Demonstrated:**
- User management with sessions
- Multi-layer caching
- API endpoint routing
- Metrics collection
- Enterprise security patterns

**Rust Concepts Explained:**

**1. Complex Generic Structures**
```rust
pub struct Cache<T: Clone> {
    entries: Arc<RwLock<HashMap<String, CacheEntry<T>>>>,
}

struct CacheEntry<T> {
    value: T,
    expires_at: DateTime<Utc>,
}
```
- **Generic Structures**: `<T: Clone>` makes cache work with any cloneable type
- **Trait Bounds**: `Clone` constraint enables value duplication
- **Nested Generics**: `CacheEntry<T>` uses the same generic parameter
- **Composition**: Complex data structures built from simpler ones

**2. Advanced Arc and RwLock Patterns**
```rust
pub struct EnterpriseServer {
    users: Arc<RwLock<HashMap<Uuid, User>>>,
    sessions: Arc<RwLock<HashMap<Uuid, Session>>>,
    user_cache: Cache<User>,
    metrics: Arc<RwLock<Metrics>>,
}
```
- **Multiple Shared Resources**: Each field is independently lockable
- **Lock Granularity**: Fine-grained locking prevents contention
- **Type Composition**: Combine primitive and custom types
- **Resource Management**: Automatic cleanup with RAII

**3. TTL (Time-To-Live) Implementation**
```rust
pub fn get(&self, key: &str) -> Option<T> {
    if let Ok(entries) = self.entries.read() {
        if let Some(entry) = entries.get(key) {
            if Utc::now() < entry.expires_at {
                return Some(entry.value.clone());
            }
        }
    }
    None
}
```
- **Expiration Logic**: Check current time against expiration
- **Automatic Cleanup**: Expired entries are ignored
- **Clone Semantics**: Return owned copies of cached values
- **Thread Safety**: Multiple readers can access cache concurrently

**4. Builder Pattern and Fluent APIs**
```rust
impl<T: Clone> Cache<T> {
    pub fn new() -> Self { /* ... */ }
    
    pub fn set(&self, key: String, value: T, ttl_seconds: u64) {
        let expires_at = Utc::now() + Duration::seconds(ttl_seconds as i64);
        // Insert with expiration...
    }
}
```
- **Method Chaining**: Fluent interface design
- **Duration Calculations**: Compute expiration times
- **Type Conversion**: Safe casting between numeric types
- **Immutable API**: Methods take `&self` for concurrent access

**5. Metrics Collection and Aggregation**
```rust
#[derive(Debug, Default)]
pub struct Metrics {
    request_count: u64,
    total_response_time: u64,
    active_sessions: u32,
    cache_hits: u64,
    cache_misses: u64,
}

impl Metrics {
    pub fn average_response_time(&self) -> f64 {
        if self.request_count > 0 {
            self.total_response_time as f64 / self.request_count as f64
        } else {
            0.0
        }
    }
}
```
- **Default Trait**: Zero-initialized metrics
- **Numeric Calculations**: Average computation with division
- **Type Casting**: Convert integers to floating point
- **Division by Zero**: Safe handling of edge cases

**6. Session Management**
```rust
pub fn create_session(&self, user_id: Uuid) -> Result<Uuid, String> {
    let session = Session {
        id: Uuid::new_v4(),
        user_id,
        created_at: Utc::now(),
        expires_at: Utc::now() + Duration::hours(24),
        last_accessed: Utc::now(),
    };
    
    if let Ok(mut sessions) = self.sessions.write() {
        sessions.insert(session.id, session);
        Ok(session.id)
    } else {
        Err("Failed to create session".to_string())
    }
}
```
- **Struct Initialization**: Named field syntax
- **UUID Generation**: Unique session identifiers
- **Duration Arithmetic**: Session expiration calculation
- **Error Handling**: Graceful failure with meaningful messages

**7. Advanced Pattern Matching**
```rust
match request.path.as_str() {
    "/api/users" => match request.method.as_str() {
        "GET" => self.list_users(&request),
        "POST" => self.create_user(&request),
        _ => self.method_not_allowed(),
    },
    path if path.starts_with("/api/users/") => {
        let user_id = path.strip_prefix("/api/users/").unwrap();
        self.get_user(user_id, &request)
    },
    _ => self.not_found(),
}
```
- **Nested Matching**: Match patterns inside match arms
- **Pattern Guards**: `if` conditions in match arms
- **String Methods**: `.starts_with()` and `.strip_prefix()`
- **Route Parsing**: Extract parameters from URL paths

**Enterprise Features:**
- Session-based authentication
- TTL-based caching
- API rate limiting concepts
- Comprehensive monitoring
- Production-ready error handling

**Run Example:**
```bash
cargo run --bin example_20_enterprise_server
```

## Rust Concepts Summary

The examples in this tutorial demonstrate a comprehensive range of Rust language features and patterns essential for MCP development:

### **Core Language Features**

**1. Ownership and Borrowing**
- **Ownership Transfer**: Moving values between functions
- **Borrowing**: `&` for immutable references, `&mut` for mutable references
- **Lifetimes**: Ensuring references are valid (`'static` lifetime)
- **RAII**: Automatic resource cleanup when values go out of scope

**2. Type System and Safety**
- **Strong Typing**: Compile-time type checking prevents runtime errors
- **Option<T>**: Safe handling of potentially missing values
- **Result<T, E>**: Explicit error handling without exceptions
- **Enums**: Type-safe unions with pattern matching
- **Generics**: Code reuse with type parameters and trait bounds

**3. Pattern Matching**
- **match Expressions**: Exhaustive handling of all cases
- **if let**: Convenient pattern matching for single cases
- **while let**: Pattern matching in loops
- **Pattern Guards**: Additional conditions in match arms

### **Memory Management**

**1. Smart Pointers**
- **Box<T>**: Heap allocation for owned data
- **Arc<T>**: Atomic reference counting for shared ownership
- **Rc<T>**: Reference counting for single-threaded sharing

**2. Interior Mutability**
- **RwLock<T>**: Multiple readers or single writer
- **Mutex<T>**: Mutual exclusion for shared mutable state
- **Cell<T>** and **RefCell<T>**: Single-threaded interior mutability

### **Concurrency and Async Programming**

**1. Async/Await**
- **async fn**: Asynchronous function declarations
- **await**: Suspending execution for async operations
- **Future**: Lazy computations that can be awaited
- **Tokio**: Async runtime for network and I/O operations

**2. Channels and Communication**
- **mpsc**: Multi-producer, single-consumer channels
- **unbounded_channel**: No limit on queued messages
- **Message Passing**: Safe inter-task communication

**3. Task Management**
- **tokio::spawn**: Creating concurrent tasks
- **Background Workers**: Long-running async tasks
- **Graceful Shutdown**: Coordinated task termination

### **Error Handling Patterns**

**1. Error Propagation**
- **? Operator**: Automatic error propagation
- **map_err()**: Transform error types
- **unwrap_or()**: Provide default values
- **Early Return**: Exit functions on error conditions

**2. Custom Error Types**
- **Error Enums**: Structured error variants
- **Display Trait**: User-friendly error messages
- **Error Trait**: Standard error handling interface
- **Nested Errors**: Wrapping underlying errors

### **Functional Programming**

**1. Iterators**
- **Iterator Trait**: Lazy sequence processing
- **map()**: Transform elements
- **filter()**: Select elements
- **collect()**: Materialize results
- **fold()** and **reduce()**: Aggregation operations

**2. Closures**
- **Fn Traits**: Different closure types (Fn, FnMut, FnOnce)
- **Capture**: Borrowing or moving variables into closures
- **Generic Functions**: Accept closures as parameters

### **Serialization and Data Handling**

**1. Serde Integration**
- **Serialize/Deserialize**: Automatic JSON conversion
- **Derive Macros**: Code generation for traits
- **Field Attributes**: Control serialization behavior
- **Custom Serialization**: Manual implementation when needed

**2. Data Structures**
- **HashMap**: Key-value storage with O(1) lookup
- **Vec**: Dynamic arrays with growth
- **VecDeque**: Double-ended queues
- **BTreeMap**: Ordered key-value storage

### **String and Text Processing**

**1. String Types**
- **String**: Owned, mutable UTF-8 text
- **&str**: Borrowed string slices
- **String Conversion**: `.to_string()`, `.into()`, format!()
- **Unicode Support**: Full UTF-8 character handling

**2. Text Operations**
- **Splitting**: `.split()`, `.split_whitespace()`, `.lines()`
- **Transformation**: `.to_uppercase()`, `.to_lowercase()`, `.trim()`
- **Searching**: `.contains()`, `.starts_with()`, `.find()`
- **Formatting**: `format!()` macro with type safety

### **Database and I/O**

**1. SQLx Integration**
- **Connection Pooling**: Efficient database resource management
- **Prepared Statements**: SQL injection prevention
- **Async Database**: Non-blocking database operations
- **Type Safety**: Compile-time SQL type checking

**2. File Operations**
- **Path Handling**: Safe file system navigation
- **Error Handling**: Graceful I/O failure handling
- **Async I/O**: Non-blocking file operations

### **Security and Validation**

**1. Input Validation**
- **JSON Schema**: Structure validation
- **Type Checking**: Compile-time safety
- **Sanitization**: Safe string processing
- **Bounds Checking**: Array access safety

**2. Cryptography**
- **Hashing**: Password security (SHA-256, recommend bcrypt/Argon2)
- **Random Generation**: Secure UUID creation
- **Token Management**: Session and authentication tokens

### **Production Patterns**

**1. Configuration Management**
- **Environment Variables**: Runtime configuration
- **Config Files**: Structured settings
- **Defaults**: Fallback values
- **Validation**: Configuration checking

**2. Monitoring and Metrics**
- **Structured Logging**: Tracing integration
- **Performance Metrics**: Request timing and counting
- **Health Checks**: Service status monitoring
- **Resource Tracking**: Memory and CPU usage

**3. Testing and Quality**
- **Unit Tests**: Function-level testing
- **Integration Tests**: Component interaction testing
- **Property Testing**: Random input validation
- **Benchmarking**: Performance measurement

This comprehensive coverage of Rust concepts provides a solid foundation for building robust, safe, and efficient MCP servers. Each concept builds upon the others to create production-ready applications.

## Best Practices

### Error Handling

1. **Use Custom Error Types**: Define specific error types for different failure modes
2. **Propagate Errors Properly**: Use `?` operator and `Result` types consistently
3. **Provide Meaningful Messages**: Include context in error messages
4. **Log Errors Appropriately**: Use structured logging for debugging

```rust
#[derive(Debug)]
pub enum McpError {
    ValidationError(String),
    DatabaseError(String),
    AuthenticationError(String),
    InternalError(String),
}

impl std::fmt::Display for McpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            McpError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
            McpError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
            // ... other variants
        }
    }
}
```

### External Learning Resources

**Error Handling Best Practices:**
- [Error Handling in Rust]https://blog.burntsushi.net/rust-error-handling/ - Comprehensive error handling guide
- [thiserror and anyhow]https://nick.groenen.me/posts/rust-error-handling/ - Modern error handling crates
- [Error Handling Patterns]https://rust-unofficial.github.io/patterns/patterns/behavioural/strategy.html - Common error patterns
- [Failure vs thiserror vs anyhow]https://blog.yoshuawuyts.com/error-handling-survey/ - Comparing error handling approaches

### Security

1. **Input Validation**: Always validate inputs with proper schemas
2. **Path Safety**: Use path canonicalization to prevent directory traversal
3. **Rate Limiting**: Implement rate limiting for production deployments
4. **Authentication**: Use proper authentication mechanisms
5. **Secure Defaults**: Default to secure configurations

### External Learning Resources

**Security Best Practices:**
- [OWASP Top 10]https://owasp.org/www-project-top-ten/ - Most critical security risks
- [Rust Security Book]https://rust-secure-code.github.io/ - Security-focused Rust programming
- [Input Validation]https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html - Comprehensive input validation guide
- [Path Traversal Prevention]https://owasp.org/www-community/attacks/Path_Traversal - Understanding path traversal attacks

**Cryptography and Authentication:**
- [RustCrypto]https://github.com/RustCrypto - Cryptographic algorithms in Rust
- [Security Audit Tools]https://github.com/rust-secure-code/safety-dance - Rust security tools
- [Secure Coding Guidelines]https://anssi-fr.github.io/rust-guide/ - French cybersecurity agency Rust guide

### Performance

1. **Async/Await**: Use async programming for I/O operations
2. **Connection Pooling**: Pool database and HTTP connections
3. **Caching**: Implement appropriate caching strategies
4. **Resource Limits**: Set memory and connection limits
5. **Monitoring**: Monitor performance metrics

### External Learning Resources

**Performance Optimization:**
- [The Rust Performance Book]https://nnethercote.github.io/perf-book/ - Comprehensive performance guide
- [Benchmarking in Rust]https://doc.rust-lang.org/1.7.0/book/benchmark-tests.html - Measuring performance
- [Profiling Rust Applications]https://nnethercote.github.io/perf-book/profiling.html - Performance profiling tools
- [criterion.rs]https://docs.rs/criterion - Statistical benchmarking

**Memory Management:**
- [Rust Memory Management]https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html - Ownership and borrowing
- [Zero-Cost Abstractions]https://blog.rust-lang.org/2015/05/11/traits.html - Rust's performance philosophy
- [Memory Profiling]https://blog.rust-lang.org/inside-rust/2020/02/25/intro-rustc-self-profile.html - Memory usage analysis

**Async Performance:**
- [Tokio Performance]https://tokio.rs/tokio/topics/tracing - Async performance monitoring
- [async-std Performance]https://book.async.rs/ - Alternative async runtime
- [Async Performance Patterns]https://ryhl.io/blog/async-what-is-blocking/ - Common async pitfalls

### Code Organization

1. **Modular Design**: Separate concerns into different modules
2. **Configuration**: Make servers configurable for different environments
3. **Testing**: Write comprehensive unit and integration tests
4. **Documentation**: Document APIs and configuration options
5. **Logging**: Use structured logging throughout

## Production Deployment

### Configuration Management

Use hierarchical configuration with environment-specific overrides:

```rust
// Load configuration in order of priority
pub fn load_config() -> Result<Config, ConfigError> {
    let mut config = Config::default();
    
    // 1. Load from config file
    if let Ok(config_path) = env::var("CONFIG_FILE") {
        config = Config::from_file(&config_path)?;
    }
    
    // 2. Override with environment variables
    config.override_from_env()?;
    
    // 3. Override with command line arguments
    config.override_from_args()?;
    
    Ok(config)
}
```

### Monitoring and Observability

1. **Structured Logging**: Use JSON logging for production
2. **Metrics Collection**: Collect performance and business metrics
3. **Health Checks**: Implement comprehensive health checks
4. **Distributed Tracing**: Use tracing for request correlation
5. **Alerting**: Set up alerts for critical issues

### Deployment Patterns

1. **Containerization**: Use Docker for consistent deployments
2. **Service Mesh**: Consider service mesh for microservices
3. **Load Balancing**: Distribute load across multiple instances
4. **Blue-Green Deployment**: Enable zero-downtime deployments
5. **Circuit Breakers**: Implement circuit breakers for resilience

### Testing Strategy

```bash
# Run all tests
cargo test

# Run specific example tests
cargo test --bin example_01_hello_world

# Run integration tests
cargo test --test integration

# Check code quality
cargo clippy
cargo fmt --check
```

### Performance Testing

```bash
# Run benchmarks
cargo bench

# Memory usage profiling
cargo run --release --bin example_11_monitoring

# Load testing with external tools
# (use tools like wrk, bombardier, or custom load tests)
```

## Conclusion

This tutorial demonstrates a complete progression from simple MCP servers to production-ready enterprise applications. Each example builds upon previous concepts while introducing new patterns and best practices.

The examples cover:

- **Fundamentals**: Basic MCP concepts and server structure
- **Intermediate**: Configuration, file operations, database integration
- **Advanced**: Monitoring, task queues, authentication, notifications
- **Enterprise**: Search, blockchain, ML, microservices, complete applications

For production use, focus on:
- Security best practices
- Comprehensive error handling
- Performance optimization
- Monitoring and observability
- Testing and validation

The complete source code for all examples is available in the repository, with each example being a fully functional MCP server that can be run and extended.

### External Learning Resources

**Production Deployment:**
- [Deployment Strategies]https://deployment-strategies.github.io/ - Various deployment patterns
- [Docker and Rust]https://github.com/LukeMathWalker/cargo-chef - Efficient Docker builds for Rust
- [Kubernetes for Rust]https://github.com/kube-rs/kube-rs - Kubernetes integration
- [Health Checks]https://microservices.io/patterns/observability/health-check-api.html - Service health monitoring

**Monitoring and Observability:**
- [Observability in Rust]https://www.lpalmieri.com/posts/2020-09-27-zero-to-production-4-are-we-observable-yet/ - Comprehensive observability guide
- [OpenTelemetry Rust]https://github.com/open-telemetry/opentelemetry-rust - Distributed tracing
- [Metrics Collection]https://prometheus.io/docs/instrumenting/writing_clientlibs/ - Prometheus metrics patterns
- [Structured Logging]https://docs.rs/tracing - Advanced logging with tracing

**Load Testing and Benchmarking:**
- [Load Testing]https://github.com/wg/wrk - HTTP benchmarking tool
- [Artillery]https://artillery.io/ - Modern load testing toolkit
- [Benchmarking Guide]https://nnethercote.github.io/perf-book/benchmarking.html - Performance measurement best practices

## Additional Resources

### Official Documentation
- [MCP Official Specification]https://spec.modelcontextprotocol.io/ - Complete protocol specification
- [Rust Async Programming]https://rust-lang.github.io/async-book/ - Comprehensive async guide
- [Tokio Documentation]https://tokio.rs/ - Async runtime documentation
- [Serde JSON Guide]https://serde.rs/ - Serialization framework
- [SQLx Documentation]https://docs.rs/sqlx/ - Async SQL toolkit

### Community Resources
- [A Coder's Guide to the Official Rust MCP Toolkit]https://hackmd.io/@Hamze/S1tlKZP0kx - Comprehensive `rmcp` guide
- [Rust Learning Resources]https://github.com/ctjhoa/rust-learning - Curated learning materials
- [Awesome Rust]https://github.com/rust-unofficial/awesome-rust - Community-curated Rust resources
- [This Week in Rust]https://this-week-in-rust.org/ - Weekly Rust newsletter

### Tools and Development
- [Rust Analyzer]https://rust-analyzer.github.io/ - Language server for IDEs
- [Clippy]https://github.com/rust-lang/rust-clippy - Linting tool
- [rustfmt]https://github.com/rust-lang/rustfmt - Code formatting
- [cargo-audit]https://github.com/RustSec/rustsec/tree/main/cargo-audit - Security vulnerability scanner

## Contributing

Contributions to improve examples, add new patterns, or enhance documentation are welcome. Please follow Rust best practices and include tests for new functionality.

### How to Contribute
- Fork the repository and create a feature branch
- Follow the established code style and documentation patterns
- Add tests for new functionality
- Update the tutorial documentation if needed
- Submit a pull request with a clear description of changes