File size: 3,293 Bytes
8bab08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# file: mcp/servers/email_server.py
#!/usr/bin/env python3
import json
import uuid
from datetime import datetime
from aiohttp import web

class EmailServer:
    """Email MCP server"""
    
    def __init__(self):
        self.threads = {}
        self.messages = []
    
    async def handle_rpc(self, request):
        data = await request.json()
        method = data.get("method")
        params = data.get("params", {})
        
        if method == "health":
            return web.json_response({"result": "ok"})
        
        elif method == "email.send":
            # Create message
            thread_id = str(uuid.uuid4())
            message_id = str(uuid.uuid4())
            
            # Get prospect_id from params, default to "unknown" if not provided
            prospect_id = params.get("prospect_id", "unknown")
            
            message = {
                "id": message_id,
                "thread_id": thread_id,
                "prospect_id": prospect_id,
                "direction": "outbound",
                "to": params["to"],
                "subject": params["subject"],
                "body": params["body"],
                "sent_at": datetime.utcnow().isoformat()
            }
            
            self.messages.append(message)
            
            if thread_id not in self.threads:
                self.threads[thread_id] = {
                    "id": thread_id,
                    "prospect_id": prospect_id,
                    "messages": []
                }
            self.threads[thread_id]["messages"].append(message)
            
            return web.json_response({
                "result": {
                    "thread_id": thread_id,
                    "message_id": message_id,
                    "prospect_id": prospect_id
                }
            })
        
        elif method == "email.thread":
            prospect_id = params.get("prospect_id")
            
            # Find thread for prospect
            for thread_id, thread_data in self.threads.items():
                if thread_data.get("prospect_id") == prospect_id:
                    return web.json_response({
                        "result": {
                            "id": thread_id,
                            "prospect_id": prospect_id,
                            "messages": thread_data["messages"]
                        }
                    })
            
            # Fallback to searching messages
            prospect_messages = [
                m for m in self.messages 
                if m.get("prospect_id") == prospect_id
            ]
            
            if prospect_messages:
                thread_id = prospect_messages[0]["thread_id"]
                return web.json_response({
                    "result": {
                        "id": thread_id,
                        "prospect_id": prospect_id,
                        "messages": prospect_messages
                    }
                })
            
            return web.json_response({"result": None})
        
        return web.json_response({"error": "Unknown method"}, status=400)

app = web.Application()
server = EmailServer()
app.router.add_post("/rpc", server.handle_rpc)

if __name__ == "__main__":
    web.run_app(app, port=9002)