File size: 33,565 Bytes
d5c104e
 
 
 
 
 
 
 
 
 
 
 
 
 
5d8896b
 
d5c104e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import asyncio
import aiohttp
import logging
from dotenv import load_dotenv
from http.client import HTTPSConnection
from typing import List, Dict, Any
from datetime import datetime, timezone
from langchain.prompts import ChatPromptTemplate
from src.utils.api_key_manager import with_api_manager
from src.helpers.helper import remove_markdown

# Load environment variables from .env file
ENV_FILE_PATH = os.getenv("WRITABLE_DIR", "/tmp") + "/.env"
load_dotenv(ENV_FILE_PATH, override=True)

# Configure logging
logger = logging.getLogger(__name__)

# Client for interacting with the MCP service
class MCPClient:    
    def __init__(self):
        self.webhook_url = os.getenv("PIPEDREAM_WEBHOOK_URL")
        if not self.webhook_url:
            logger.warning("PIPEDREAM_WEBHOOK_URL not set in environment variables")
        
        # Set timeout for requests
        self.timeout = aiohttp.ClientTimeout(total=60)
        
    # Fetch app data from MCP service
    @with_api_manager()
    async def fetch_app_data(
        self, 
        provider: str, 
        services: List[str], 
        query: str,
        user_id: str,
        access_token: str,
        *,
        llm
    ) -> Dict[str, Any]:
        if not self.webhook_url:
            logger.error("Pipedream webhook URL not configured")
            return {"error": "Pipedream integration not configured"}
        
        # Add debugging
        print(f"=== MCP fetch_app_data called ===")
        print(f"Provider: {provider}")
        print(f"Services: {services}")
        print(f"Query: {query}")
        print(f"User ID: {user_id}")
        print(f"Access token exists: {bool(access_token)}")
        print(f"Access token length: {len(access_token) if access_token else 0}")
        print("==================================")
        
        # Check if token is None
        if not access_token:
            logger.error(f"No access token for {provider}! Cannot proceed.")
            return {"error": f"No authentication token for {provider}"}
        
        # Check if query contains previous context
        if query.startswith("This is the previous context of the conversation:"):
            prompt = \
"""You are an expert at re-writing queries according to the context of the conversation.
Your task is to re-write the query to be more specific and relevant to the previous context provided below, but ONLY if necessary.

However, you MUST return the current query (and ONLY the current query) as it is if no changes are needed.
The decision whether to re-write the query or not is based on the previous context of the conversation.

Your output should be ONLY the re-written or current query, without any additional text or formatting.

{query}"""
            prompt_template = ChatPromptTemplate.from_template(prompt)
            messages = prompt_template.format_messages(query=query)
            response = await llm.ainvoke(messages)
            query = remove_markdown(response.content.strip())
      
        payload = {
            "provider": provider,
            "services": services,
            "query": query,
            "user_id": user_id,
            "token": access_token,
            "timestamp": datetime.now(timezone.utc).isoformat()
        }

        # Manually set headers
        headers = {'Content-Type': 'application/json'}
        
        print(f"Fetching {provider} data for services: {services}")
        print(f"Payload to send: {json.dumps({**payload, 'token': "REDACTED" if payload['token'] else None}, indent=2)}")

        # Configure the webhook URL
        host = self.webhook_url.replace("https://", "").replace("http://", "").strip()

        try:
            # Use HTTPSConnection for synchronous request
            conn = HTTPSConnection(host, timeout=self.timeout.total)
            conn.request("POST", "/", f"""{json.dumps(payload)}""", headers)
            response = conn.getresponse()
            print(f"Response status: {response.status}")
            print(f"Response headers: {dict(response.getheaders())}")
            if response.status == 200:
                try:
                    # Handle potential empty or null responses from Pipedream
                    data = json.loads(response.read().decode())
                    if data is None or (isinstance(data, dict) and not data):
                        logger.warning("Pipedream returned a null response.")
                        return {"error": "Received no data from the provider."}
                except json.JSONDecodeError:
                    logger.error("Pipedream returned a non-JSON or empty response.")
                    return {"error": "Invalid response from the provider."}
                # Check if any service within the data returned an auth error
                auth_error = False
                for service_key in data:
                    if isinstance(data[service_key], dict) and data[service_key].get('error'):
                        error_details = data[service_key].get('details', '')
                        if '401' in str(error_details) or 'authError' in str(error_details) or 'UNAUTHENTICATED' in str(error_details):
                            auth_error = True
                            break
                if auth_error:
                    logger.error(f"Authentication failed for {provider}")
                    return {"error": f"Authentication failed for {provider}. Please reconnect your account."}
                
                logger.info(f"Successfully fetched {provider} data")
                return data
            else:
                error_text = response.read().decode()
                logger.error(f"Pipedream request failed: {response.status} - {error_text}")
                return {"error": f"Failed to fetch data: {response.status} - {error_text}"}
        except asyncio.TimeoutError:
            logger.error("Pipedream request timed out")
            return {"error": "Request timed out. Please try again."}
        except aiohttp.ClientError as e:
            logger.error(f"Network error calling Pipedream: {str(e)}")
            return {"error": "Network error. Please check your connection."}
        except Exception as e:
            # Catching TypeError if data is None from response.json()
            logger.error(f"Unexpected error calling Pipedream: {str(e)}")
            return {"error": "An unexpected error occurred"}
    
    # Format the raw app data into a context string for LLM
    def format_as_context(self, provider: str, data: Dict[str, Any]) -> str:
        if not data or "error" in data:
            return ""
            
        context = f"\n[{provider.upper()} {"WORKSPACE" if provider == "slack" else "APP"} DATA]\n"
        context += "=" * 50 + "\n"
        
        # Format based on provider
        if provider == "google":
            context += self._format_google_data(data)
        elif provider == "microsoft":
            context += self._format_microsoft_data(data)
        elif provider == "slack":
            context += self._format_slack_data(data)
        else:
            context += f"Unknown provider: {provider}\n"
            
        context += "=" * 50 + "\n"
        return context
    
    # Helper methods to format data for Google apps
    def _format_google_data(self, data: Dict[str, Any]) -> str:
        formatted = ""
        
        # Google Drive
        if "drive" in data and isinstance(data["drive"], dict) and "files" in data["drive"]:
            formatted += "\nπŸ“ GOOGLE DRIVE FILES:\n"
            formatted += "-" * 30 + "\n"
            
            files = data["drive"]["files"]
            if not files:
                formatted += "No files found matching the query.\n"
            else:
                for i, file in enumerate(files[:10], 1):  # Limit to 10 files
                    formatted += f"\n{i}. File: {file.get('name', 'Unknown')}\n"
                    formatted += f"   Type: {file.get('mimeType', 'Unknown')}\n"
                    formatted += f"   Modified: {file.get('modifiedTime', 'Unknown')}\n"
                    
                    if file.get('webViewLink'):
                        formatted += f"   Link: {file['webViewLink']}\n"
                    
                    if file.get('content'):
                        content_preview = file['content'][:500]
                        if len(file['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"
                    
                    formatted += "\n"
        
        # Gmail
        if "gmail" in data and isinstance(data["gmail"], dict) and "messages" in data["gmail"]:
            formatted += "\nπŸ“§ GMAIL MESSAGES:\n"
            formatted += "-" * 30 + "\n"
            
            messages = data["gmail"]["messages"]
            if not messages:
                formatted += "No messages found matching the query.\n"
            else:
                for i, msg in enumerate(messages[:10], 1):
                    formatted += f"\n{i}. From: {msg.get('from', 'Unknown')}\n"
                    formatted += f"   Subject: {msg.get('subject', 'No subject')}\n"
                    
                    body_preview = msg.get('body', '')[:300]
                    if msg.get('body', '') and len(msg['body']) > 300:
                        body_preview += "..."
                    formatted += f"   Preview: {body_preview}\n"
        
        # Google Calendar
        if "calendar" in data and isinstance(data["calendar"], dict) and "events" in data["calendar"]:
            formatted += "\nπŸ“… GOOGLE CALENDAR EVENTS:\n"
            formatted += "-" * 30 + "\n"
            
            events = data["calendar"]["events"]
            if not events:
                formatted += "No calendar events found matching the query.\n"
            else:
                for i, event in enumerate(events[:10], 1):
                    formatted += f"\n{i}. Event: {event.get('summary', 'No title')}\n"
                    formatted += f"   Time: {event.get('start', 'Unknown')}\n"
                    
                    if event.get('location'):
                        formatted += f"   Location: {event['location']}\n"
                    
                    if event.get('description'):
                        desc_preview = event['description'][:200]
                        if len(event['description']) > 200:
                            desc_preview += "..."
                        formatted += f"   Description: {desc_preview}\n"
        
        # Google Docs
        if "docs" in data and isinstance(data["docs"], dict) and "docs" in data["docs"]:
            formatted += "\nπŸ“„ GOOGLE DOCS:\n"
            formatted += "-" * 30 + "\n"
            
            docs = data["docs"]["docs"]
            if not docs:
                formatted += "No documents found matching the query.\n"
            else:
                for i, doc in enumerate(docs[:5], 1):
                    formatted += f"\n{i}. Document: {doc.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {doc.get('modifiedTime', 'Unknown')}\n"
                    
                    if doc.get('content'):
                        content_preview = doc['content'][:500]
                        if len(doc['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"
        
        # Google Sheets
        if "sheets" in data and isinstance(data["sheets"], dict) and "sheets" in data["sheets"]:
            formatted += "\nπŸ“Š GOOGLE SHEETS:\n"
            formatted += "-" * 30 + "\n"
            
            sheets = data["sheets"]["sheets"]
            if not sheets:
                formatted += "No spreadsheets found matching the query.\n"
            else:
                for i, sheet in enumerate(sheets[:5], 1):
                    formatted += f"\n{i}. Spreadsheet: {sheet.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {sheet.get('modifiedTime', 'Unknown')}\n"
                    
                    if sheet.get('content'):
                        content_preview = sheet['content'][:300]
                        if len(sheet['content']) > 300:
                            content_preview += "..."
                        formatted += f"   Data Preview:\n   {content_preview}\n"
        
        # Google Tasks
        if "tasks" in data and isinstance(data["tasks"], dict) and "tasks" in data["tasks"]:
            formatted += "\nβœ… GOOGLE TASKS:\n"
            formatted += "-" * 30 + "\n"
            
            tasks = data["tasks"]["tasks"]
            if not tasks:
                formatted += "No tasks found matching the query.\n"
            else:
                for i, task in enumerate(tasks[:10], 1):
                    formatted += f"\n{i}. Task: {task.get('title', 'No title')}\n"
                    formatted += f"   List: {task.get('listTitle', 'Unknown')}\n"
                    formatted += f"   Status: {task.get('status', 'Unknown')}\n"
                    
                    if task.get('notes'):
                        formatted += f"   Notes: {task['notes'][:200]}...\n"
                    
                    if task.get('due'):
                        formatted += f"   Due: {task['due']}\n"
        
        # Add other Google services as needed
        
        return formatted
    
    # Helper methods to format data for Microsoft apps
    def _format_microsoft_data(self, data: Dict[str, Any]) -> str:
        formatted = ""

        # Word
        if "word" in data and isinstance(data["word"], dict) and "documents" in data["word"]:
            formatted += "\nπŸ“„ MICROSOFT WORD DOCUMENTS:\n"
            formatted += "-" * 30 + "\n"
            
            documents = data["word"]["documents"]
            if not documents:
                formatted += "No documents found matching the query.\n"
            else:
                for i, doc in enumerate(documents[:5], 1):
                    formatted += f"\n{i}. Document: {doc.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {doc.get('lastModifiedDateTime', 'Unknown')}\n"
                    
                    if doc.get('content'):
                        content_preview = doc['content'][:500]
                        if len(doc['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"

        # Excel
        if "excel" in data and isinstance(data["excel"], dict) and "workbooks" in data["excel"]:
            formatted += "\nπŸ“Š MICROSOFT EXCEL WORKBOOKS:\n"
            formatted += "-" * 30 + "\n"
            
            workbooks = data["excel"]["workbooks"]
            if not workbooks:
                formatted += "No workbooks found matching the query.\n"
            else:
                for i, wb in enumerate(workbooks[:5], 1):
                    formatted += f"\n{i}. Workbook: {wb.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {wb.get('lastModifiedDateTime', 'Unknown')}\n"
                    
                    if wb.get('content'):
                        content_preview = wb['content'][:500]
                        if len(wb['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"

        # PowerPoint
        if "powerpoint" in data and isinstance(data["powerpoint"], dict) and "presentations" in data["powerpoint"]:
            formatted += "\nπŸ“Š MICROSOFT POWERPOINT PRESENTATIONS:\n"
            formatted += "-" * 30 + "\n"

            presentations = data["powerpoint"]["presentations"]
            if not presentations:
                formatted += "No presentations found matching the query.\n"
            else:
                for i, pres in enumerate(presentations[:5], 1):
                    formatted += f"\n{i}. Presentation: {pres.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {pres.get('lastModifiedDateTime', 'Unknown')}\n"
                    
                    if pres.get('content'):
                        content_preview = pres['content'][:500]
                        if len(pres['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"
        
        # OneDrive/Files
        if "onedrive" in data and isinstance(data["onedrive"], dict) and "files" in data["onedrive"]:
            formatted += "\nπŸ“ ONEDRIVE FILES:\n"
            formatted += "-" * 30 + "\n"
            
            files = data["onedrive"]["files"]
            if not files:
                formatted += "No files found matching the query.\n"
            else:
                for i, file in enumerate(files[:10], 1):
                    formatted += f"\n{i}. File: {file.get('name', 'Unknown')}\n"
                    formatted += f"   Modified: {file.get('lastModified', 'Unknown')}\n"
                    
                    if file.get('webUrl'):
                        formatted += f"   URL: {file['webUrl']}\n"
                    
                    if file.get('content'):
                        content_preview = file['content'][:500]
                        if len(file['content']) > 500:
                            content_preview += "..."
                        formatted += f"   Content Preview:\n   {content_preview}\n"
        
        # Outlook
        if "outlook" in data and isinstance(data["outlook"], dict) and "messages" in data["outlook"]:
            formatted += "\nπŸ“§ OUTLOOK MESSAGES:\n"
            formatted += "-" * 30 + "\n"
            
            messages = data["outlook"]["messages"]
            if not messages:
                formatted += "No messages found matching the query.\n"
            else:
                for i, msg in enumerate(messages[:10], 1):
                    formatted += f"\n{i}. From: {msg.get('from', 'Unknown')}\n"
                    formatted += f"   Subject: {msg.get('subject', 'No subject')}\n"
                    
                    body_preview = msg.get('body', '')[:300]
                    if msg.get('body', '') and len(msg['body']) > 300:
                        body_preview += "..."
                    formatted += f"   Preview: {body_preview}\n"
        
        # OneNote
        if "onenote" in data and isinstance(data["onenote"], dict) and "pages" in data["onenote"]:
            formatted += "\nπŸ““ ONENOTE PAGES:\n"
            formatted += "-" * 30 + "\n"
            
            pages = data["onenote"]["pages"]
            if not pages:
                formatted += "No pages found matching the query.\n"
            else:
                for i, page in enumerate(pages[:10], 1):
                    formatted += f"\n{i}. Page: {page.get('title', 'Unknown')}\n"
                    formatted += f"   Section: {page.get('parentSection', 'Unknown')}\n"
                    formatted += f"   Modified: {page.get('lastModifiedDateTime', 'Unknown')}\n"
                    
                    if page.get('contentPreview'):
                        formatted += f"   Preview: {page['contentPreview'][:200]}...\n"
        
        # Microsoft To Do
        if "todo" in data and isinstance(data["todo"], dict) and "tasks" in data["todo"]:
            formatted += "\nβœ… MICROSOFT TO DO:\n"
            formatted += "-" * 30 + "\n"
            
            tasks = data["todo"]["tasks"]
            if not tasks:
                formatted += "No tasks found matching the query.\n"
            else:
                for i, task in enumerate(tasks[:10], 1):
                    formatted += f"\n{i}. Task: {task.get('title', 'No title')}\n"
                    formatted += f"   List: {task.get('listName', 'Unknown')}\n"
                    formatted += f"   Status: {'Completed' if task.get('isCompleted') else 'Pending'}\n"
                    
                    if task.get('body', {}).get('content'):
                        formatted += f"   Notes: {task['body']['content'][:200]}...\n"
                    
                    if task.get('dueDateTime'):
                        formatted += f"   Due: {task['dueDateTime']['dateTime']}\n"
        
        # Exchange Calendar
        if "exchange" in data and isinstance(data["exchange"], dict) and "events" in data["exchange"]:
            formatted += "\nπŸ“… EXCHANGE CALENDAR:\n"
            formatted += "-" * 30 + "\n"
            
            events = data["exchange"]["events"]
            if not events:
                formatted += "No calendar events found matching the query.\n"
            else:
                for i, event in enumerate(events[:10], 1):
                    formatted += f"\n{i}. Event: {event.get('subject', 'No subject')}\n"
                    formatted += f"   Start: {event.get('start', {}).get('dateTime', 'Unknown')}\n"
                    formatted += f"   End: {event.get('end', {}).get('dateTime', 'Unknown')}\n"
                    
                    if event.get('location', {}).get('displayName'):
                        formatted += f"   Location: {event['location']['displayName']}\n"
                    
                    if event.get('bodyPreview'):
                        formatted += f"   Preview: {event['bodyPreview'][:200]}...\n"
        
        return formatted
    
    # Helper methods to format data for Slack
    def _format_slack_data(self, data: Dict[str, Any]) -> str:
        formatted = ""
        
        # Workspace Info
        if "workspace" in data and isinstance(data["workspace"], dict):
            ws = data["workspace"]
            formatted += f"\nπŸ“ Workspace: {ws.get('name', 'Unknown')} ({ws.get('domain', 'N/A')})\n"
            if ws.get('enterprise_name'):
                formatted += f"   Enterprise: {ws['enterprise_name']}\n"
            formatted += "-" * 30 + "\n"
        
        # Users Summary
        if "users" in data and isinstance(data["users"], list):
            users = data["users"]
            formatted += f"\nπŸ‘₯ TEAM MEMBERS ({len(users)} total):\n"
            formatted += "-" * 30 + "\n"
            
            # Show admins/owners first
            admins = [u for u in users if u.get('is_admin') or u.get('is_owner')]
            if admins:
                formatted += "Leadership:\n"
                for admin in admins[:5]:
                    role = "Owner" if admin.get('is_primary_owner') else ("Admin" if admin.get('is_admin') else "Owner")
                    formatted += f"  β€’ {admin.get('real_name', admin.get('name', 'Unknown'))} (@{admin.get('name', '')}) - {role}\n"
                    if admin.get('title'):
                        formatted += f"    Title: {admin['title']}\n"
            
            # Show some regular members
            regular_users = [u for u in users if not (u.get('is_admin') or u.get('is_owner'))][:10]
            if regular_users:
                formatted += "\nTeam Members (sample):\n"
                for user in regular_users:
                    formatted += f"  β€’ {user.get('real_name', user.get('name', 'Unknown'))} (@{user.get('name', '')})"
                    if user.get('title'):
                        formatted += f" - {user['title']}"
                    if user.get('presence'):
                        formatted += f" [{user['presence']}]"
                    formatted += "\n"
        
        # Channels Overview
        if "channels" in data and isinstance(data["channels"], list):
            channels = data["channels"]
            formatted += f"\nπŸ“’ CHANNELS ({len(channels)} total):\n"
            formatted += "-" * 30 + "\n"
            
            # Group by type
            public_channels = [ch for ch in channels if not ch.get('is_private')]
            private_channels = [ch for ch in channels if ch.get('is_private')]
            
            if public_channels:
                formatted += f"Public Channels ({len(public_channels)}):\n"
                for ch in public_channels[:10]:
                    formatted += f"  β€’ #{ch.get('name', 'Unknown')}"
                    if ch.get('num_members'):
                        formatted += f" ({ch['num_members']} members)"
                    if ch.get('topic'):
                        formatted += f"\n    Topic: {ch['topic'][:50]}..."
                    formatted += "\n"
            
            if private_channels:
                formatted += f"\nPrivate Channels ({len(private_channels)}):\n"
                for ch in private_channels[:5]:
                    formatted += f"  β€’ πŸ”’ {ch.get('name', 'Unknown')}"
                    if ch.get('num_members'):
                        formatted += f" ({ch['num_members']} members)"
                    formatted += "\n"
            
            # Show recent messages from channels
            channels_with_messages = [ch for ch in channels if ch.get('recent_messages')]
            if channels_with_messages:
                formatted += "\nπŸ’¬ RECENT CHANNEL ACTIVITY:\n"
                formatted += "-" * 30 + "\n"
                for ch in channels_with_messages[:3]:
                    formatted += f"\n#{ch.get('name', 'Unknown')}:\n"
                    for msg in ch.get('recent_messages', [])[:3]:
                        formatted += f"  β€’ {msg.get('user', 'Unknown')}: {msg.get('text', '')[:100]}"
                        if msg.get('reply_count'):
                            formatted += f" (🧡 {msg['reply_count']} replies)"
                        formatted += "\n"
        
        # Search Results
        if "searchResults" in data and isinstance(data["searchResults"], dict):
            search = data["searchResults"]
            
            # Message search results
            if search.get("messages") and isinstance(search["messages"], list):
                messages = search["messages"]
                formatted += f"\nπŸ” MESSAGE SEARCH RESULTS ({len(messages)} found):\n"
                formatted += "-" * 30 + "\n"
                
                for i, msg in enumerate(messages[:10], 1):
                    formatted += f"\n{i}. User: {msg.get('user', 'Unknown')}\n"
                    formatted += f"   Channel: #{msg.get('channel', {}).get('name', 'Unknown')}\n"
                    formatted += f"   Message: {msg.get('text', '')[:200]}"
                    if len(msg.get('text', '')) > 200:
                        formatted += "..."
                    formatted += "\n"
                    
                    if msg.get('permalink'):
                        formatted += f"   Link: {msg['permalink']}\n"
                        
                    if msg.get('reactions'):
                        reactions_str = " ".join([f"{r['name']}:{r['count']}" for r in msg['reactions'][:5]])
                        formatted += f"   Reactions: {reactions_str}\n"
            
            # File search results
            if search.get("files") and isinstance(search["files"], list):
                files = search["files"]
                formatted += f"\nπŸ“Ž FILE SEARCH RESULTS ({len(files)} found):\n"
                formatted += "-" * 30 + "\n"
                
                for i, file in enumerate(files[:10], 1):
                    formatted += f"\n{i}. File: {file.get('name', 'Unknown')}\n"
                    formatted += f"   Type: {file.get('filetype', 'Unknown').upper()}\n"
                    formatted += f"   Size: {self._format_file_size(file.get('size', 0))}\n"
                    formatted += f"   Uploaded by: {file.get('user', 'Unknown')}\n"
                    
                    if file.get('channels'):
                        formatted += f"   Shared in: {len(file['channels'])} channel(s)\n"
        
        # Recent Files
        if "files" in data and isinstance(data["files"], list) and data["files"]:
            formatted += f"\nπŸ“‚ RECENT FILES ({len(data['files'])} shown):\n"
            formatted += "-" * 30 + "\n"
            
            for i, file in enumerate(data["files"][:10], 1):
                formatted += f"\n{i}. {file.get('name', 'Unknown')}"
                if file.get('title') and file['title'] != file.get('name'):
                    formatted += f" ({file['title']})"
                formatted += f"\n   Type: {file.get('filetype', 'Unknown').upper()}"
                formatted += f" | Size: {self._format_file_size(file.get('size', 0))}\n"
                
                if file.get('comments_count'):
                    formatted += f"   πŸ’¬ {file['comments_count']} comment(s)\n"
        
        # User Groups
        if "usergroups" in data and isinstance(data["usergroups"], list) and data["usergroups"]:
            formatted += f"\nπŸ‘₯ USER GROUPS ({len(data['usergroups'])} total):\n"
            formatted += "-" * 30 + "\n"
            
            for group in data["usergroups"][:10]:
                formatted += f"\nβ€’ {group.get('name', 'Unknown')} (@{group.get('handle', '')})\n"
                if group.get('description'):
                    formatted += f"  Description: {group['description'][:100]}"
                    if len(group['description']) > 100:
                        formatted += "..."
                    formatted += "\n"
                if group.get('user_count'):
                    formatted += f"  Members: {group['user_count']}\n"
        
        # Custom Emoji
        if "emoji" in data and isinstance(data["emoji"], dict) and data["emoji"]:
            emoji_list = list(data["emoji"].keys())
            formatted += f"\n😊 CUSTOM EMOJI ({len(emoji_list)} total):\n"
            formatted += "-" * 30 + "\n"
            
            # Show first 20 emoji
            sample_emoji = emoji_list[:20]
            formatted += "Sample: " + " ".join([f":{e}:" for e in sample_emoji])
            if len(emoji_list) > 20:
                formatted += f" ... and {len(emoji_list) - 20} more"
            formatted += "\n"
        
        # Starred Items
        if "stars" in data and isinstance(data.get("stars"), list) and data["stars"]:
            formatted += f"\n⭐ STARRED ITEMS ({len(data['stars'])} total):\n"
            formatted += "-" * 30 + "\n"
            
            # Group by type
            star_types = {}
            for star in data["stars"]:
                star_type = star.get('type', 'unknown')
                star_types[star_type] = star_types.get(star_type, 0) + 1
            
            for star_type, count in star_types.items():
                formatted += f"  β€’ {star_type.capitalize()}: {count}\n"
        
        # Reminders
        if "reminders" in data and isinstance(data.get("reminders"), list) and data["reminders"]:
            formatted += f"\n⏰ REMINDERS ({len(data['reminders'])} active):\n"
            formatted += "-" * 30 + "\n"
            
            for i, reminder in enumerate(data["reminders"][:5], 1):
                formatted += f"\n{i}. {reminder.get('text', 'No description')}\n"
                if reminder.get('recurring'):
                    formatted += "   πŸ”„ Recurring\n"
                if reminder.get('user') and reminder['user'] != reminder.get('creator'):
                    formatted += f"   For: {reminder['user']}\n"
        
        # Error handling
        if "error" in data:
            formatted += f"\n⚠️ ERROR: {data['error']}\n"
            if "partialResults" in data:
                formatted += "(Showing partial results above)\n"
        
        # Summary statistics
        formatted += "\n" + "=" * 50 + "\n"
        formatted += "πŸ“Š SUMMARY:\n"
        if "workspace" in data:
            formatted += f"β€’ Workspace: {data.get('workspace', {}).get('name', 'Unknown')}\n"
        if "users" in data:
            formatted += f"β€’ Total Users: {len(data.get('users', []))}\n"
        if "channels" in data:
            formatted += f"β€’ Total Channels: {len(data.get('channels', []))}\n"
        if "searchResults" in data:
            search = data["searchResults"]
            if search.get("messages"):
                formatted += f"β€’ Messages Found: {len(search.get('messages', []))}\n"
            if search.get("files"):
                formatted += f"β€’ Files Found: {len(search.get('files', []))}\n"
        if "emoji" in data and data["emoji"]:
            formatted += f"β€’ Custom Emoji: {len(data.get('emoji', {}))}\n"
        
        return formatted
    
    # Helper method to format file sizes
    def _format_file_size(self, size_bytes: int) -> str:
        if size_bytes < 1024:
            return f"{size_bytes} B"
        elif size_bytes < 1024 * 1024:
            return f"{size_bytes / 1024:.1f} KB"
        elif size_bytes < 1024 * 1024 * 1024:
            return f"{size_bytes / (1024 * 1024):.1f} MB"
        else:
            return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB"
        
if __name__ == "__main__":
    # Example usage
    client = MCPClient()
    print("\n\n-----Fetching app data...------\n\n")
    print(asyncio.run(client.fetch_app_data(
        provider="slack",
        services=[],
        query="give me a detailed summary of my slack workspace",
        user_id="b45c4267-a357-4dff-9c7e-c421d8c8b659",
        access_token="xoxp-9146298917605-9146298924117-9248981137408-2bd4faadc77fb2c47d7b5adde7a70eaf"
    )))