-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·337 lines (286 loc) · 12.3 KB
/
Copy pathmain.py
File metadata and controls
executable file
·337 lines (286 loc) · 12.3 KB
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
#!/usr/bin/env python3
"""Beeper MCP Server - Read-only access to Beeper messages."""
import asyncio
import json
import logging
import sys
from pathlib import Path
from typing import Any, Dict, List
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
from beeper_reader import BeeperReader
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class BeeperMCPServer:
"""MCP server for Beeper message access."""
def __init__(self):
self.server = Server("beeper-mcp")
self.config = self._load_config()
self.reader = BeeperReader(self.config)
# Register tools
self._register_tools()
def _load_config(self) -> Dict[str, Any]:
"""Load configuration from file or use defaults."""
config_path = Path(__file__).parent / "config.json"
if config_path.exists():
try:
with open(config_path, 'r') as f:
config = json.load(f)
logger.info("Loaded configuration from config.json")
return config
except Exception as e:
logger.error(f"Error loading config: {e}, using defaults")
# Default configuration
return {
"database_paths": [
"~/Library/Application Support/Beeper",
"~/.config/Beeper",
"~/Library/Application Support/Element",
"~/.config/Element"
],
"max_results": 50,
"log_level": "INFO"
}
def _register_tools(self):
"""Register MCP tools."""
@self.server.list_tools()
async def list_tools() -> List[Tool]:
return [
Tool(
name="list_conversations",
description="List recent conversations with metadata",
inputSchema={
"type": "object",
"properties": {
"limit": {
"type": "integer",
"description": "Maximum number of conversations to return",
"default": 20,
"minimum": 1,
"maximum": self.config.get('max_results', 50)
}
}
}
),
Tool(
name="read_messages",
description="Read recent messages from a specific conversation",
inputSchema={
"type": "object",
"properties": {
"conversation_id": {
"type": "string",
"description": "ID of the conversation to read messages from"
},
"limit": {
"type": "integer",
"description": "Maximum number of messages to return",
"default": 20,
"minimum": 1,
"maximum": self.config.get('max_results', 50)
}
},
"required": ["conversation_id"]
}
),
Tool(
name="search_messages",
description="Search for messages containing specific text across all conversations",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query text"
},
"limit": {
"type": "integer",
"description": "Maximum number of results to return",
"default": 20,
"minimum": 1,
"maximum": self.config.get('max_results', 50)
}
},
"required": ["query"]
}
)
]
@self.server.call_tool()
async def call_tool(name: str, arguments: Dict[str, Any]) -> List[TextContent]:
try:
if name == "list_conversations":
return await self._list_conversations(arguments)
elif name == "read_messages":
return await self._read_messages(arguments)
elif name == "search_messages":
return await self._search_messages(arguments)
else:
raise ValueError(f"Unknown tool: {name}")
except Exception as e:
logger.error(f"Error calling tool {name}: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def _list_conversations(self, arguments: Dict[str, Any]) -> List[TextContent]:
"""Handle list_conversations tool call."""
limit = arguments.get('limit', 20)
try:
conversations = await asyncio.to_thread(
self.reader.list_conversations, limit
)
if not conversations:
return [TextContent(
type="text",
text="No conversations found. Please ensure Beeper is installed and has been used."
)]
# Format output
output_lines = [f"Found {len(conversations)} conversation(s):\n"]
for i, conv in enumerate(conversations, 1):
lines = [
f"{i}. {conv['title']}",
f" ID: {conv['id']}",
f" Source: {conv.get('source', 'unknown')}"
]
if conv.get('last_activity'):
lines.append(f" Last activity: {conv['last_activity']}")
if conv.get('participant_count'):
lines.append(f" Participants: {conv['participant_count']}")
if conv.get('last_message_preview'):
preview = conv['last_message_preview']
if len(preview) > 50:
preview = preview[:50] + "..."
lines.append(f" Last message: {preview}")
output_lines.extend(lines)
output_lines.append("") # Empty line between conversations
return [TextContent(
type="text",
text="\n".join(output_lines)
)]
except Exception as e:
logger.error(f"Error listing conversations: {e}")
return [TextContent(
type="text",
text=f"Error listing conversations: {str(e)}"
)]
async def _read_messages(self, arguments: Dict[str, Any]) -> List[TextContent]:
"""Handle read_messages tool call."""
conversation_id = arguments.get('conversation_id')
limit = arguments.get('limit', 20)
if not conversation_id:
return [TextContent(
type="text",
text="Error: conversation_id is required"
)]
try:
messages = await asyncio.to_thread(
self.reader.read_messages, conversation_id, limit
)
if not messages:
return [TextContent(
type="text",
text=f"No messages found in conversation '{conversation_id}'"
)]
# Format output
output_lines = [f"Messages from conversation '{conversation_id}':\n"]
for msg in messages:
timestamp = msg.get('timestamp', 'Unknown time')
sender = msg.get('sender', 'Unknown sender')
content = msg.get('content', '')
# Format message
output_lines.append(f"[{timestamp}] {sender}:")
output_lines.append(f" {content}")
output_lines.append("") # Empty line between messages
return [TextContent(
type="text",
text="\n".join(output_lines)
)]
except Exception as e:
logger.error(f"Error reading messages: {e}")
return [TextContent(
type="text",
text=f"Error reading messages: {str(e)}"
)]
async def _search_messages(self, arguments: Dict[str, Any]) -> List[TextContent]:
"""Handle search_messages tool call."""
query = arguments.get('query')
limit = arguments.get('limit', 20)
if not query:
return [TextContent(
type="text",
text="Error: query is required"
)]
try:
results = await asyncio.to_thread(
self.reader.search_messages, query, limit
)
if not results:
return [TextContent(
type="text",
text=f"No messages found matching '{query}'"
)]
# Format output
output_lines = [f"Found {len(results)} message(s) matching '{query}':\n"]
for i, msg in enumerate(results, 1):
conv_title = msg.get('conversation_title', 'Unknown conversation')
timestamp = msg.get('timestamp', 'Unknown time')
sender = msg.get('sender', 'Unknown sender')
content = msg.get('content', '')
# Highlight search term in content (simple approach)
if query.lower() in content.lower():
start = content.lower().find(query.lower())
end = start + len(query)
# Show context around match
context_start = max(0, start - 30)
context_end = min(len(content), end + 30)
snippet = content[context_start:context_end]
if context_start > 0:
snippet = "..." + snippet
if context_end < len(content):
snippet = snippet + "..."
else:
snippet = content[:80] + "..." if len(content) > 80 else content
output_lines.extend([
f"{i}. In '{conv_title}'",
f" [{timestamp}] {sender}:",
f" {snippet}",
""
])
return [TextContent(
type="text",
text="\n".join(output_lines)
)]
except Exception as e:
logger.error(f"Error searching messages: {e}")
return [TextContent(
type="text",
text=f"Error searching messages: {str(e)}"
)]
async def run(self):
"""Run the MCP server."""
try:
logger.info("Starting Beeper MCP server...")
async with stdio_server() as (read_stream, write_stream):
await self.server.run(
read_stream,
write_stream,
self.server.create_initialization_options()
)
except KeyboardInterrupt:
logger.info("Server stopped by user")
except Exception as e:
logger.error(f"Server error: {e}")
raise
finally:
self.reader.close()
async def main():
"""Main entry point."""
server = BeeperMCPServer()
await server.run()
if __name__ == "__main__":
asyncio.run(main())