File size: 35,377 Bytes
2ec0d39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Sample MCP Database/CRM Server
Demonstrates database operations, data management, and business logic integration
"""

import asyncio
import json
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional
import sqlite3
import uuid
from dataclasses import dataclass, asdict
from enum import Enum
import structlog

# Configure structured logging
logging.basicConfig(level=logging.INFO)
logger = structlog.get_logger()


class CustomerStatus(Enum):
    """Customer status enumeration."""
    ACTIVE = "active"
    INACTIVE = "inactive"
    PENDING = "pending"
    SUSPENDED = "suspended"


@dataclass
class Customer:
    """Customer data model."""
    id: str
    name: str
    email: str
    phone: Optional[str]
    status: CustomerStatus
    created_at: str
    last_contact: Optional[str]
    tags: List[str]
    notes: str
    lifetime_value: float = 0.0


@dataclass
class Lead:
    """Lead data model."""
    id: str
    name: str
    email: str
    phone: Optional[str]
    source: str
    status: str
    score: int
    assigned_to: Optional[str]
    created_at: str
    notes: str


@dataclass
class Opportunity:
    """Opportunity data model."""
    id: str
    customer_id: str
    title: str
    value: float
    probability: int
    stage: str
    close_date: Optional[str]
    assigned_to: Optional[str]
    created_at: str
    notes: str


class DatabaseManager:
    """Database manager for CRM operations."""

    def __init__(self, db_path: str = "crm_database.db"):
        self.db_path = db_path
        self._initialize_database()

    def _initialize_database(self):
        """Initialize the database with required tables."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            
            # Create customers table
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS customers (
                    id TEXT PRIMARY KEY,
                    name TEXT NOT NULL,
                    email TEXT UNIQUE NOT NULL,
                    phone TEXT,
                    status TEXT NOT NULL,
                    created_at TEXT NOT NULL,
                    last_contact TEXT,
                    tags TEXT,
                    notes TEXT DEFAULT '',
                    lifetime_value REAL DEFAULT 0.0
                )
            """)
            
            # Create leads table
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS leads (
                    id TEXT PRIMARY KEY,
                    name TEXT NOT NULL,
                    email TEXT UNIQUE NOT NULL,
                    phone TEXT,
                    source TEXT NOT NULL,
                    status TEXT NOT NULL,
                    score INTEGER DEFAULT 0,
                    assigned_to TEXT,
                    created_at TEXT NOT NULL,
                    notes TEXT DEFAULT ''
                )
            """)
            
            # Create opportunities table
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS opportunities (
                    id TEXT PRIMARY KEY,
                    customer_id TEXT NOT NULL,
                    title TEXT NOT NULL,
                    value REAL NOT NULL,
                    probability INTEGER DEFAULT 0,
                    stage TEXT NOT NULL,
                    close_date TEXT,
                    assigned_to TEXT,
                    created_at TEXT NOT NULL,
                    notes TEXT DEFAULT '',
                    FOREIGN KEY (customer_id) REFERENCES customers (id)
                )
            """)
            
            conn.commit()
            logger.info("Database initialized successfully")

    async def add_customer(self, customer_data: Dict[str, Any]) -> Customer:
        """Add a new customer to the database."""
        customer_id = str(uuid.uuid4())
        customer = Customer(
            id=customer_id,
            name=customer_data["name"],
            email=customer_data["email"],
            phone=customer_data.get("phone"),
            status=CustomerStatus(customer_data.get("status", "active")),
            created_at=datetime.utcnow().isoformat(),
            last_contact=None,
            tags=customer_data.get("tags", []),
            notes=customer_data.get("notes", ""),
            lifetime_value=customer_data.get("lifetime_value", 0.0)
        )
        
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO customers (id, name, email, phone, status, created_at, last_contact, tags, notes, lifetime_value)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, (
                customer.id, customer.name, customer.email, customer.phone,
                customer.status.value, customer.created_at, customer.last_contact,
                json.dumps(customer.tags), customer.notes, customer.lifetime_value
            ))
            conn.commit()
        
        logger.info("Customer added", customer_id=customer_id, email=customer.email)
        return customer

    async def get_customer(self, customer_id: str) -> Optional[Customer]:
        """Get customer by ID."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM customers WHERE id = ?", (customer_id,))
            row = cursor.fetchone()
            
            if row:
                return Customer(
                    id=row[0], name=row[1], email=row[2], phone=row[3],
                    status=CustomerStatus(row[4]), created_at=row[5],
                    last_contact=row[6], tags=json.loads(row[7] or "[]"),
                    notes=row[8], lifetime_value=row[9]
                )
        return None

    async def search_customers(self, query: str, limit: int = 10) -> List[Customer]:
        """Search customers by name or email."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                SELECT * FROM customers 
                WHERE name LIKE ? OR email LIKE ? 
                LIMIT ?
            """, (f"%{query}%", f"%{query}%", limit))
            
            customers = []
            for row in cursor.fetchall():
                customers.append(Customer(
                    id=row[0], name=row[1], email=row[2], phone=row[3],
                    status=CustomerStatus(row[4]), created_at=row[5],
                    last_contact=row[6], tags=json.loads(row[7] or "[]"),
                    notes=row[8], lifetime_value=row[9]
                ))
        return customers

    async def add_lead(self, lead_data: Dict[str, Any]) -> Lead:
        """Add a new lead."""
        lead_id = str(uuid.uuid4())
        lead = Lead(
            id=lead_id,
            name=lead_data["name"],
            email=lead_data["email"],
            phone=lead_data.get("phone"),
            source=lead_data["source"],
            status=lead_data.get("status", "new"),
            score=lead_data.get("score", 0),
            assigned_to=lead_data.get("assigned_to"),
            created_at=datetime.utcnow().isoformat(),
            notes=lead_data.get("notes", "")
        )
        
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO leads (id, name, email, phone, source, status, score, assigned_to, created_at, notes)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, (
                lead.id, lead.name, lead.email, lead.phone, lead.source,
                lead.status, lead.score, lead.assigned_to, lead.created_at, lead.notes
            ))
            conn.commit()
        
        logger.info("Lead added", lead_id=lead_id, email=lead.email)
        return lead

    async def get_leads_by_status(self, status: str, limit: int = 20) -> List[Lead]:
        """Get leads by status."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM leads WHERE status = ? LIMIT ?", (status, limit))
            
            leads = []
            for row in cursor.fetchall():
                leads.append(Lead(
                    id=row[0], name=row[1], email=row[2], phone=row[3],
                    source=row[4], status=row[5], score=row[6],
                    assigned_to=row[7], created_at=row[8], notes=row[9]
                ))
        return leads

    async def add_opportunity(self, opportunity_data: Dict[str, Any]) -> Opportunity:
        """Add a new opportunity."""
        opp_id = str(uuid.uuid4())
        opportunity = Opportunity(
            id=opp_id,
            customer_id=opportunity_data["customer_id"],
            title=opportunity_data["title"],
            value=opportunity_data["value"],
            probability=opportunity_data.get("probability", 0),
            stage=opportunity_data.get("stage", "prospecting"),
            close_date=opportunity_data.get("close_date"),
            assigned_to=opportunity_data.get("assigned_to"),
            created_at=datetime.utcnow().isoformat(),
            notes=opportunity_data.get("notes", "")
        )
        
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO opportunities (id, customer_id, title, value, probability, stage, close_date, assigned_to, created_at, notes)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, (
                opportunity.id, opportunity.customer_id, opportunity.title,
                opportunity.value, opportunity.probability, opportunity.stage,
                opportunity.close_date, opportunity.assigned_to, opportunity.created_at, opportunity.notes
            ))
            conn.commit()
        
        logger.info("Opportunity added", opportunity_id=opp_id, value=opportunity.value)
        return opportunity

    async def get_sales_pipeline(self, limit: int = 50) -> List[Dict[str, Any]]:
        """Get sales pipeline with customer information."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                SELECT o.id, o.title, o.value, o.probability, o.stage, o.close_date,
                       c.name as customer_name, c.email as customer_email,
                       o.assigned_to, o.created_at, o.notes
                FROM opportunities o
                JOIN customers c ON o.customer_id = c.id
                ORDER BY o.created_at DESC
                LIMIT ?
            """, (limit,))
            
            pipeline = []
            for row in cursor.fetchall():
                pipeline.append({
                    "opportunity_id": row[0],
                    "title": row[1],
                    "value": row[2],
                    "probability": row[3],
                    "stage": row[4],
                    "close_date": row[5],
                    "customer_name": row[6],
                    "customer_email": row[7],
                    "assigned_to": row[8],
                    "created_at": row[9],
                    "notes": row[10]
                })
        return pipeline

    async def get_metrics(self) -> Dict[str, Any]:
        """Get CRM metrics and analytics."""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            
            # Customer metrics
            cursor.execute("SELECT COUNT(*) FROM customers WHERE status = 'active'")
            active_customers = cursor.fetchone()[0]
            
            cursor.execute("SELECT COUNT(*) FROM customers")
            total_customers = cursor.fetchone()[0]
            
            # Lead metrics
            cursor.execute("SELECT COUNT(*) FROM leads WHERE status = 'new'")
            new_leads = cursor.fetchone()[0]
            
            cursor.execute("SELECT COUNT(*) FROM leads WHERE created_at >= ?", 
                          [(datetime.utcnow() - timedelta(days=30)).isoformat()])
            leads_this_month = cursor.fetchone()[0]
            
            # Opportunity metrics
            cursor.execute("SELECT SUM(value) FROM opportunities WHERE stage NOT IN ('won', 'lost')")
            pipeline_value = cursor.fetchone()[0] or 0
            
            cursor.execute("SELECT SUM(value) FROM opportunities WHERE stage = 'won'")
            won_value = cursor.fetchone()[0] or 0
            
            cursor.execute("SELECT SUM(value) FROM opportunities WHERE stage = 'won' AND close_date >= ?",
                          [(datetime.utcnow() - timedelta(days=30)).isoformat()])
            won_value_this_month = cursor.fetchone()[0] or 0
            
            return {
                "customers": {
                    "active": active_customers,
                    "total": total_customers,
                    "conversion_rate": round((active_customers / max(total_customers, 1)) * 100, 2)
                },
                "leads": {
                    "new": new_leads,
                    "this_month": leads_this_month
                },
                "opportunities": {
                    "pipeline_value": pipeline_value,
                    "won_value": won_value,
                    "won_value_this_month": won_value_this_month
                }
            }


class CRMMCPServer:
    """Model Context Protocol CRM Server implementation."""

    def __init__(self, port: int = 8002):
        self.port = port
        self.db = DatabaseManager()
        self.tools = {
            "add_customer": {
                "name": "add_customer",
                "description": "Add a new customer to the CRM system",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "Customer name",
                            "minLength": 1,
                            "maxLength": 200
                        },
                        "email": {
                            "type": "string",
                            "description": "Customer email address",
                            "format": "email"
                        },
                        "phone": {
                            "type": "string",
                            "description": "Customer phone number",
                            "pattern": "^[+]?[\\d\\s\\-\\(\\)]+$"
                        },
                        "status": {
                            "type": "string",
                            "enum": ["active", "inactive", "pending", "suspended"],
                            "default": "active"
                        },
                        "tags": {
                            "type": "array",
                            "items": {"type": "string"},
                            "description": "Customer tags for categorization"
                        },
                        "notes": {
                            "type": "string",
                            "description": "Additional notes about the customer",
                            "maxLength": 1000
                        },
                        "lifetime_value": {
                            "type": "number",
                            "description": "Customer lifetime value in USD",
                            "minimum": 0
                        }
                    },
                    "required": ["name", "email"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "string"},
                        "name": {"type": "string"},
                        "email": {"type": "string"},
                        "phone": {"type": "string"},
                        "status": {"type": "string"},
                        "created_at": {"type": "string"},
                        "tags": {"type": "array", "items": {"type": "string"}},
                        "notes": {"type": "string"},
                        "lifetime_value": {"type": "number"}
                    }
                },
                "examples": [
                    {
                        "name": "John Doe",
                        "email": "john.doe@example.com",
                        "phone": "+1-555-0123",
                        "status": "active",
                        "tags": ["enterprise", "priority"],
                        "lifetime_value": 50000.0
                    }
                ],
                "tags": ["crm", "customer", "add", "create"]
            },
            "get_customer": {
                "name": "get_customer",
                "description": "Get customer information by customer ID",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "customer_id": {
                            "type": "string",
                            "description": "Customer UUID",
                            "format": "uuid"
                        }
                    },
                    "required": ["customer_id"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "string"},
                        "name": {"type": "string"},
                        "email": {"type": "string"},
                        "phone": {"type": "string"},
                        "status": {"type": "string"},
                        "created_at": {"type": "string"},
                        "last_contact": {"type": "string"},
                        "tags": {"type": "array", "items": {"type": "string"}},
                        "notes": {"type": "string"},
                        "lifetime_value": {"type": "number"}
                    }
                },
                "examples": [
                    {"customer_id": "123e4567-e89b-12d3-a456-426614174000"}
                ],
                "tags": ["crm", "customer", "get", "retrieve"]
            },
            "search_customers": {
                "name": "search_customers",
                "description": "Search customers by name or email",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "Search query for name or email",
                            "minLength": 1,
                            "maxLength": 100
                        },
                        "limit": {
                            "type": "integer",
                            "description": "Maximum number of results",
                            "minimum": 1,
                            "maximum": 100,
                            "default": 10
                        }
                    },
                    "required": ["query"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "customers": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "id": {"type": "string"},
                                    "name": {"type": "string"},
                                    "email": {"type": "string"},
                                    "phone": {"type": "string"},
                                    "status": {"type": "string"},
                                    "created_at": {"type": "string"},
                                    "tags": {"type": "array", "items": {"type": "string"}},
                                    "notes": {"type": "string"},
                                    "lifetime_value": {"type": "number"}
                                }
                            }
                        }
                    }
                },
                "examples": [
                    {"query": "john", "limit": 5}
                ],
                "tags": ["crm", "customer", "search", "find"]
            },
            "add_lead": {
                "name": "add_lead",
                "description": "Add a new sales lead",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string", "description": "Lead name"},
                        "email": {"type": "string", "format": "email"},
                        "phone": {"type": "string"},
                        "source": {
                            "type": "string",
                            "description": "Lead source",
                            "enum": ["website", "referral", "social", "email", "phone", "event"]
                        },
                        "status": {
                            "type": "string",
                            "enum": ["new", "contacted", "qualified", "unqualified"],
                            "default": "new"
                        },
                        "score": {
                            "type": "integer",
                            "description": "Lead score (1-100)",
                            "minimum": 1,
                            "maximum": 100
                        },
                        "assigned_to": {"type": "string", "description": "Assigned sales rep"},
                        "notes": {"type": "string", "description": "Additional notes"}
                    },
                    "required": ["name", "email", "source"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "string"},
                        "name": {"type": "string"},
                        "email": {"type": "string"},
                        "phone": {"type": "string"},
                        "source": {"type": "string"},
                        "status": {"type": "string"},
                        "score": {"type": "integer"},
                        "assigned_to": {"type": "string"},
                        "created_at": {"type": "string"},
                        "notes": {"type": "string"}
                    }
                },
                "examples": [
                    {
                        "name": "Jane Smith",
                        "email": "jane.smith@company.com",
                        "source": "website",
                        "score": 85
                    }
                ],
                "tags": ["crm", "lead", "add", "sales"]
            },
            "get_sales_pipeline": {
                "name": "get_sales_pipeline",
                "description": "Get sales pipeline with opportunity details",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "limit": {
                            "type": "integer",
                            "description": "Maximum number of opportunities to return",
                            "minimum": 1,
                            "maximum": 200,
                            "default": 50
                        }
                    }
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "pipeline": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "opportunity_id": {"type": "string"},
                                    "title": {"type": "string"},
                                    "value": {"type": "number"},
                                    "probability": {"type": "integer"},
                                    "stage": {"type": "string"},
                                    "close_date": {"type": "string"},
                                    "customer_name": {"type": "string"},
                                    "customer_email": {"type": "string"},
                                    "assigned_to": {"type": "string"},
                                    "created_at": {"type": "string"},
                                    "notes": {"type": "string"}
                                }
                            }
                        }
                    }
                },
                "examples": [
                    {"limit": 20}
                ],
                "tags": ["crm", "sales", "pipeline", "opportunities"]
            },
            "get_crm_metrics": {
                "name": "get_crm_metrics",
                "description": "Get CRM metrics and analytics dashboard",
                "inputSchema": {
                    "type": "object",
                    "properties": {}
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "customers": {
                            "type": "object",
                            "properties": {
                                "active": {"type": "integer"},
                                "total": {"type": "integer"},
                                "conversion_rate": {"type": "number"}
                            }
                        },
                        "leads": {
                            "type": "object",
                            "properties": {
                                "new": {"type": "integer"},
                                "this_month": {"type": "integer"}
                            }
                        },
                        "opportunities": {
                            "type": "object",
                            "properties": {
                                "pipeline_value": {"type": "number"},
                                "won_value": {"type": "number"},
                                "won_value_this_month": {"type": "number"}
                            }
                        }
                    }
                },
                "examples": [{}],
                "tags": ["crm", "metrics", "analytics", "dashboard"]
            }
        }

    async def handle_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
        """Handle incoming MCP requests."""
        method = request.get("method")
        params = request.get("params", {})
        request_id = request.get("id")

        try:
            if method == "tools/list":
                return await self._handle_tools_list(request_id)
            elif method == "tools/call":
                return await self._handle_tools_call(request_id, params)
            elif method == "initialize":
                return await self._handle_initialize(request_id, params)
            else:
                return self._error_response(request_id, -32601, "Method not found")

        except Exception as e:
            logger.error("Request handling failed", method=method, error=str(e))
            return self._error_response(request_id, -32603, f"Internal error: {str(e)}")

    async def _handle_tools_list(self, request_id: str) -> Dict[str, Any]:
        """Handle tools/list request."""
        tools_list = []
        for tool_name, tool_info in self.tools.items():
            tools_list.append({
                "name": tool_info["name"],
                "description": tool_info["description"],
                "inputSchema": tool_info["inputSchema"],
                "outputSchema": tool_info.get("outputSchema"),
                "examples": tool_info.get("examples", []),
                "tags": tool_info.get("tags", [])
            })

        return {
            "jsonrpc": "2.0",
            "id": request_id,
            "result": {
                "tools": tools_list
            }
        }

    async def _handle_tools_call(self, request_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Handle tools/call request."""
        tool_name = params.get("name")
        arguments = params.get("arguments", {})

        if tool_name not in self.tools:
            return self._error_response(request_id, -32601, f"Tool '{tool_name}' not found")

        # Execute the tool
        try:
            if tool_name == "add_customer":
                result = await self._add_customer(arguments)
            elif tool_name == "get_customer":
                result = await self._get_customer(arguments)
            elif tool_name == "search_customers":
                result = await self._search_customers(arguments)
            elif tool_name == "add_lead":
                result = await self._add_lead(arguments)
            elif tool_name == "get_sales_pipeline":
                result = await self._get_sales_pipeline(arguments)
            elif tool_name == "get_crm_metrics":
                result = await self._get_crm_metrics(arguments)
            else:
                return self._error_response(request_id, -32601, f"Tool '{tool_name}' not implemented")

            return {
                "jsonrpc": "2.0",
                "id": request_id,
                "result": {
                    "content": [
                        {
                            "type": "text",
                            "text": json.dumps(result, indent=2)
                        }
                    ]
                }
            }

        except Exception as e:
            logger.error("Tool execution failed", tool=tool_name, error=str(e))
            return self._error_response(request_id, -32603, f"Tool execution failed: {str(e)}")

    async def _handle_initialize(self, request_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Handle initialize request."""
        return {
            "jsonrpc": "2.0",
            "id": request_id,
            "result": {
                "protocolVersion": "2024-11-05",
                "capabilities": {
                    "tools": {}
                },
                "serverInfo": {
                    "name": "CRM MCP Server",
                    "version": "1.0.0",
                    "description": "Provides customer relationship management and sales operations"
                }
            }
        }

    async def _add_customer(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute add_customer tool."""
        return asdict(await self.db.add_customer(arguments))

    async def _get_customer(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute get_customer tool."""
        customer_id = arguments.get("customer_id")
        if not customer_id:
            raise ValueError("customer_id is required")

        customer = await self.db.get_customer(customer_id)
        if not customer:
            raise ValueError(f"Customer not found: {customer_id}")

        return asdict(customer)

    async def _search_customers(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute search_customers tool."""
        query = arguments.get("query")
        limit = arguments.get("limit", 10)

        if not query:
            raise ValueError("query is required")

        customers = await self.db.search_customers(query, limit)
        return {
            "customers": [asdict(customer) for customer in customers]
        }

    async def _add_lead(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute add_lead tool."""
        return asdict(await self.db.add_lead(arguments))

    async def _get_sales_pipeline(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute get_sales_pipeline tool."""
        limit = arguments.get("limit", 50)
        pipeline = await self.db.get_sales_pipeline(limit)
        return {"pipeline": pipeline}

    async def _get_crm_metrics(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """Execute get_crm_metrics tool."""
        return await self.db.get_metrics()

    def _error_response(self, request_id: str, code: int, message: str) -> Dict[str, Any]:
        """Create error response."""
        return {
            "jsonrpc": "2.0",
            "id": request_id,
            "error": {
                "code": code,
                "message": message
            }
        }

    async def start_server(self):
        """Start the CRM MCP server."""
        from aiohttp import web, ClientTimeout
        import aiohttp_cors

        # Create aiohttp application
        app = web.Application()
        
        # Add CORS support
        cors = aiohttp_cors.setup(app, defaults={
            "*": aiohttp_cors.ResourceOptions(
                allow_credentials=True,
                expose_headers="*",
                allow_headers="*",
                allow_methods="*"
            )
        })

        # Add MCP endpoint
        async def mcp_handler(request):
            try:
                data = await request.json()
                result = await self.handle_request(data)
                return web.json_response(result)
            except json.JSONDecodeError:
                return web.json_response({
                    "jsonrpc": "2.0",
                    "id": None,
                    "error": {"code": -32700, "message": "Parse error"}
                }, status=400)
            except Exception as e:
                logger.error("Request processing failed", error=str(e))
                return web.json_response({
                    "jsonrpc": "2.0",
                    "id": None,
                    "error": {"code": -32603, "message": "Internal error"}
                }, status=500)

        # Add routes
        app.router.add_post('/mcp', mcp_handler)
        app.router.add_get('/health', self._health_check)

        # Configure CORS for all routes
        for route in list(app.router.routes()):
            cors.add(route)

        # Start server
        runner = web.AppRunner(app)
        await runner.setup()
        
        site = web.TCPSite(runner, 'localhost', self.port)
        await site.start()
        
        logger.info(f"CRM MCP Server started on port {self.port}")
        return runner


    async def _health_check(self, request):
        """Health check endpoint."""
        return web.json_response({
            "status": "healthy",
            "service": "CRM MCP Server",
            "version": "1.0.0",
            "timestamp": datetime.utcnow().isoformat()
        })

    async def stop_server(self, runner):
        """Stop the CRM MCP server."""
        await runner.cleanup()


async def main():
    """Main entry point for the CRM server."""
    server = CRMMCPServer()
    runner = await server.start_server()
    
    try:
        # Keep server running
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        logger.info("Shutting down CRM server...")
        await server.stop_server(runner)


if __name__ == "__main__":
    asyncio.run(main())