-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbluebubbles_server.py
More file actions
381 lines (294 loc) Β· 10.7 KB
/
bluebubbles_server.py
File metadata and controls
381 lines (294 loc) Β· 10.7 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
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
#!/usr/bin/env python3
"""
Simple BlueBubbles MCP Server - Interface for BlueBubbles iMessage API
"""
import os
import sys
import logging
from datetime import datetime, timezone
import httpx
import json
from mcp.server.fastmcp import FastMCP
from dateutil import parser
import uuid
# Configure logging to stderr
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
stream=sys.stderr
)
logger = logging.getLogger("bluebubbles-server")
# Initialize MCP server - NO PROMPT PARAMETER!
mcp = FastMCP("bluebubbles")
# Configuration
BLUEBUBBLES_URL = os.environ.get("BLUEBUBBLES_URL", "")
BLUEBUBBLES_PASSWORD = os.environ.get("BLUEBUBBLES_PASSWORD", "")
# === UTILITY FUNCTIONS ===
def get_base_url():
"""Get the properly formatted base URL for BlueBubbles API."""
if not BLUEBUBBLES_URL:
return ""
url = BLUEBUBBLES_URL.rstrip('/')
if not url.startswith('http'):
url = f"http://{url}"
return url
async def make_api_request(endpoint: str, method: str = "GET", data: dict = None):
"""Make a request to the BlueBubbles API."""
base_url = get_base_url()
if not base_url:
raise ValueError("BlueBubbles URL not configured")
url = f"{base_url}/api/v1/{endpoint.lstrip('/')}?password={BLUEBUBBLES_PASSWORD}"
headers = {}
async with httpx.AsyncClient() as client:
if method == "GET":
response = await client.get(url, headers=headers, timeout=30)
elif method == "POST":
response = await client.post(url, headers=headers, json=data, timeout=30)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
response.raise_for_status()
return response.json()
def format_message(msg):
"""Format a message object for display."""
text = msg.get('text', '').strip()
if not text:
text = "[No text content]"
sender = msg.get('handle', {}).get('address', 'Unknown')
date = msg.get('dateCreated', '')
is_from_me = msg.get('isFromMe', False)
if is_from_me:
sender = "Me"
try:
dt = parser.parse(date)
date_str = dt.strftime('%Y-%m-%d %H:%M')
except:
date_str = date
return f"[{date_str}] {sender}: {text}"
def format_chat(chat):
"""Format a chat object for display."""
display_name = chat.get('displayName', '')
chat_identifier = chat.get('chatIdentifier', '')
participant_count = len(chat.get('participants', []))
if display_name:
return f"{display_name} ({participant_count} participants)"
elif chat_identifier:
return f"{chat_identifier} ({participant_count} participants)"
else:
return f"Chat ({participant_count} participants)"
# === MCP TOOLS ===
@mcp.tool()
async def search_messages(query: str = "", chat_id: str = "", limit: str = "20") -> str:
"""Search for messages containing specific text across all chats or in a specific chat."""
logger.info(f"Searching messages with query: {query}, chat_id: {chat_id}, limit: {limit}")
if not query.strip():
return "β Error: Query text is required"
try:
limit_int = int(limit) if limit.strip() else 20
limit_int = min(limit_int, 1000)
params = {
"limit": limit_int,
"where": {
"statement": "message.text = :text",
"args": {
"text": query
}
}
}
if chat_id.strip():
params["chatGuid"] = chat_id
data = await make_api_request("message/query", "POST", params)
messages = data.get('data', [])
if not messages:
return f"π No messages found containing '{query}'"
result = f"π Found {len(messages)} message(s) containing '{query}':\n\n"
for msg in messages[:limit_int]:
result += format_message(msg) + "\n"
return result
except Exception as e:
logger.error(f"Error searching messages: {e}")
return f"β Error searching messages: {str(e)}"
@mcp.tool()
async def get_recent_messages(chat_id: str = "", limit: str = "10") -> str:
"""Get recent messages from all chats or a specific chat."""
logger.info(f"Getting recent messages for chat_id: {chat_id}, limit: {limit}")
try:
limit_int = int(limit) if limit.strip() else 10
limit_int = min(limit_int, 50)
if chat_id.strip():
# Get messages from specific chat
data = await make_api_request(f"chat/{chat_id}/message")
else:
# Get messages from all chats
data = await make_api_request(f"message/query")
messages = data.get('data', [])
if not messages:
return "π No recent messages found"
result = f"π Recent messages ({len(messages)} shown):\n\n"
for msg in messages:
result += format_message(msg) + "\n"
return result
except Exception as e:
logger.error(f"Error getting recent messages: {e}")
return f"β Error getting recent messages: {str(e)}"
#not sure this is anything
@mcp.tool()
async def list_chats(limit: str = "20") -> str:
"""List all available chats/conversations."""
logger.info(f"Listing chats with limit: {limit}")
try:
limit_int = int(limit) if limit.strip() else 20
limit_int = min(limit_int, 100)
data = await make_api_request(f"chat/query", "POST")
chats = data.get('data', [])
if not chats:
return "π No chats found"
result = f"π Available chats ({len(chats)} shown):\n\n"
for i, chat in enumerate(chats, 1):
chat_id = chat.get('guid', '')
formatted = format_chat(chat)
result += f"{i}. {formatted}\n ID: {chat_id}\n\n"
return result
except Exception as e:
logger.error(f"Error listing chats: {e}")
return f"β Error listing chats: {str(e)}"
@mcp.tool()
async def send_message(chat_id: str = "", message: str = "") -> str:
"""Send a message to a specific chat."""
logger.info(f"Sending message to chat_id: {chat_id}")
if not chat_id.strip():
return "β Error: Chat ID is required"
if not message.strip():
return "β Error: Message text is required"
try:
data = {
"chatGuid": chat_id,
"tempGuid": str(uuid.uuid4()),
"message": message
}
response = await make_api_request("message/text", "POST", data)
if response.get('status') == 200:
return f"β
Message sent successfully to {chat_id}"
else:
return f"β Failed to send message: {response.get('message', 'Unknown error')}"
except Exception as e:
logger.error(f"Error sending message: {e}")
return f"β Error sending message: {str(e)}"
@mcp.tool()
async def send_message_to_number(phone_number: str = "", message: str = "") -> str:
"""Send a message directly to a phone number or email address."""
logger.info(f"Sending message to number: {phone_number}")
if not phone_number.strip():
return "β Error: Phone number or email is required"
if not message.strip():
return "β Error: Message text is required"
try:
data = {
"addresses": [phone_number],
"message": message
}
response = await make_api_request("message/text/new", "POST", data)
if response.get('status') == 200:
return f"β
Message sent successfully to {phone_number}"
else:
return f"β Failed to send message: {response.get('message', 'Unknown error')}"
except Exception as e:
logger.error(f"Error sending message: {e}")
return f"β Error sending message: {str(e)}"
@mcp.tool()
async def get_contacts(limit: str = "50") -> str:
"""Get the list of contacts from BlueBubbles."""
logger.info(f"Getting contacts with limit: {limit}")
try:
limit_int = int(limit) if limit.strip() else 50
limit_int = min(limit_int, 200)
data = await make_api_request(f"contact")
contacts = data.get('data', [])
if not contacts:
return "π No contacts found"
result = f"π Contacts ({len(contacts)} shown):\n\n"
for contact in contacts:
first_name = contact.get('firstName', '')
last_name = contact.get('lastName', '')
name = f"{first_name} {last_name}".strip()
if not name:
name = "Unknown"
phones = contact.get('phoneNumbers', [])
emails = contact.get('emails', [])
result += f"β’ {name}\n"
for phone in phones:
result += f" π± {phone.get('address', '')}\n"
for email in emails:
result += f" π§ {email.get('address', '')}\n"
result += "\n"
return result
except Exception as e:
logger.error(f"Error getting contacts: {e}")
return f"β Error getting contacts: {str(e)}"
@mcp.tool()
async def mark_chat_read(chat_id: str = "") -> str:
"""Mark all messages in a chat as read."""
logger.info(f"Marking chat as read: {chat_id}")
if not chat_id.strip():
return "β Error: Chat ID is required"
try:
response = await make_api_request(f"chat/{chat_id}/read", "POST", {})
if response.get('status') == 200:
return f"β
Chat {chat_id} marked as read"
else:
return f"β Failed to mark chat as read: {response.get('message', 'Unknown error')}"
except Exception as e:
logger.error(f"Error marking chat as read: {e}")
return f"β Error marking chat as read: {str(e)}"
@mcp.tool()
async def get_server_info() -> str:
"""Get information about the BlueBubbles server."""
logger.info("Getting server info")
try:
data = await make_api_request("server/info")
info = data.get('data', {})
result = "π BlueBubbles Server Information:\n\n"
result += f"β’ OS Version: {info.get('os_version', 'Unknown')}\n"
result += f"β’ Server Version: {info.get('server_version', 'Unknown')}\n"
result += f"β’ Private API: {'Enabled' if info.get('private_api', False) else 'Disabled'}\n"
result += f"β’ Proxy Service: {info.get('proxy_service', 'Unknown')}\n"
return result
except Exception as e:
logger.error(f"Error getting server info: {e}")
return f"β Error getting server info: {str(e)}"
@mcp.tool()
async def get_chat_details(chat_id: str = "") -> str:
"""Get detailed information about a specific chat."""
logger.info(f"Getting details for chat: {chat_id}")
if not chat_id.strip():
return "β Error: Chat ID is required"
try:
data = await make_api_request(f"chat/{chat_id}")
chat = data.get('data', {})
if not chat:
return f"β Chat {chat_id} not found"
result = f"π Chat Details:\n\n"
result += f"β’ Display Name: {chat.get('displayName', 'N/A')}\n"
result += f"β’ Chat ID: {chat.get('guid', '')}\n"
result += f"β’ Is Group: {'Yes' if chat.get('isGroup', False) else 'No'}\n"
participants = chat.get('participants', [])
if participants:
result += f"\nπ₯ Participants ({len(participants)}):\n"
for p in participants:
address = p.get('address', 'Unknown')
result += f" β’ {address}\n"
return result
except Exception as e:
logger.error(f"Error getting chat details: {e}")
return f"β Error getting chat details: {str(e)}"
# === SERVER STARTUP ===
if __name__ == "__main__":
logger.info("Starting BlueBubbles MCP server...")
if not BLUEBUBBLES_URL:
logger.warning("BLUEBUBBLES_URL not set - server will need configuration")
if not BLUEBUBBLES_PASSWORD:
logger.warning("BLUEBUBBLES_PASSWORD not set - authentication may fail")
try:
mcp.run(transport='stdio')
except Exception as e:
logger.error(f"Server error: {e}", exc_info=True)
sys.exit(1)