-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_social_server.py
More file actions
1119 lines (873 loc) · 50.7 KB
/
Copy pathmcp_social_server.py
File metadata and controls
1119 lines (873 loc) · 50.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
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Social Media Management Suite MCP Server
A Model Context Protocol server that provides comprehensive social media management tools for Puch AI.
Enhanced with AI-powered content creation, audience insights, and competitor analysis.
"""
import os
import json
import asyncio
import uuid
from datetime import datetime, timedelta
from typing import Annotated, List, Dict, Any
from dotenv import load_dotenv
from fastmcp import FastMCP
from fastmcp.server.auth.providers.bearer import BearerAuthProvider, RSAKeyPair
from pydantic import Field
import requests
import re
# Load environment variables
load_dotenv()
# Import our enhanced utility modules
try:
from utils.data_manager import data_manager
from utils.social_apis import social_api_manager
from utils.hashtag_engine import hashtag_engine
from utils.content_creator import content_creator
from utils.audience_insights import audience_insights
from utils.competitor_analysis import competitor_analysis
UTILS_AVAILABLE = True
except ImportError as e:
print(f"⚠️ Warning: Some utility modules not available: {e}")
print("📝 Basic functionality will still work with mock data")
UTILS_AVAILABLE = False
# Authentication setup
auth_token = os.getenv("AUTH_TOKEN", "social_mcp_token_2024")
my_number = os.getenv("MY_NUMBER", "919876543210")
# Custom Bearer Auth Provider - Simply check if the token matches
auth_provider = None # We'll use a simpler approach without custom auth provider
# Initialize MCP server
mcp = FastMCP("Social Media Management Suite")
# API keys (optional - will use mock data if not provided)
twitter_bearer_token = os.getenv("TWITTER_BEARER_TOKEN", "")
openai_api_key = os.getenv("OPENAI_API_KEY", "")
ritekit_api_key = os.getenv("RITEKIT_API_KEY", "")
# Data file paths
SCHEDULED_POSTS_FILE = "data/scheduled_posts.json"
ANALYTICS_CACHE_FILE = "data/analytics_cache.json"
TRENDS_CACHE_FILE = "data/trends_cache.json"
# Utility functions
def load_json_data(filename):
"""Load data from JSON file, return empty list if file doesn't exist."""
try:
with open(filename, 'r') as f:
return json.load(f)
except FileNotFoundError:
return []
def save_json_data(filename, data):
"""Save data to JSON file."""
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
def extract_keywords(text):
"""Extract keywords from text for hashtag generation."""
# Remove special characters and split into words
words = re.findall(r'\b[a-zA-Z]{3,}\b', text.lower())
# Filter out common stop words
stop_words = {'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'had', 'her', 'was', 'one', 'our', 'out', 'day', 'get', 'has', 'him', 'his', 'how', 'its', 'may', 'new', 'now', 'old', 'see', 'two', 'who', 'boy', 'did', 'she', 'use', 'way', 'will', 'with'}
keywords = [word for word in words if word not in stop_words and len(word) > 3]
# Return unique keywords, limited to top 10
return list(dict.fromkeys(keywords))[:10]
def generate_hashtags_rule_based(content, platform="twitter", count=5):
"""Generate hashtags using rule-based approach."""
keywords = extract_keywords(content)
# Platform-specific hashtag databases
hashtag_suggestions = {
"twitter": {
"business": ["#business", "#entrepreneur", "#startup", "#success", "#marketing"],
"technology": ["#tech", "#innovation", "#AI", "#digital", "#future"],
"lifestyle": ["#lifestyle", "#motivation", "#inspiration", "#wellness", "#mindset"],
"social": ["#socialmedia", "#content", "#engagement", "#community", "#brand"]
},
"instagram": {
"business": ["#businessowner", "#entrepreneurlife", "#hustle", "#businesstips", "#success"],
"technology": ["#technology", "#innovation", "#techlife", "#digital", "#startup"],
"lifestyle": ["#lifestyleblogger", "#dailylife", "#inspiration", "#motivation", "#wellness"],
"social": ["#socialmediamarketing", "#contentcreator", "#influencer", "#brand", "#marketing"]
},
"linkedin": {
"business": ["#business", "#leadership", "#professional", "#career", "#networking"],
"technology": ["#technology", "#innovation", "#digitaltransformation", "#AI", "#tech"],
"lifestyle": ["#worklifebalance", "#productivity", "#growth", "#development", "#success"],
"social": ["#socialmedia", "#marketing", "#branding", "#content", "#strategy"]
}
}
# Determine category based on keywords
category = "business" # default
tech_keywords = ["tech", "digital", "software", "app", "code", "data", "ai", "machine", "learning"]
lifestyle_keywords = ["life", "health", "fitness", "food", "travel", "style", "home"]
social_keywords = ["social", "media", "content", "post", "share", "follow", "like"]
if any(keyword in keywords for keyword in tech_keywords):
category = "technology"
elif any(keyword in keywords for keyword in lifestyle_keywords):
category = "lifestyle"
elif any(keyword in keywords for keyword in social_keywords):
category = "social"
# Get platform-specific hashtags
platform_hashtags = hashtag_suggestions.get(platform, hashtag_suggestions["twitter"])
category_hashtags = platform_hashtags.get(category, platform_hashtags["business"])
# Generate hashtags from keywords
keyword_hashtags = [f"#{keyword}" for keyword in keywords[:3]]
# Combine and return unique hashtags
all_hashtags = keyword_hashtags + category_hashtags
unique_hashtags = list(dict.fromkeys(all_hashtags))
return unique_hashtags[:count]
def get_mock_analytics(platform, timeframe="7d"):
"""Generate mock analytics data for demo purposes."""
import random
base_metrics = {
"twitter": {"engagement": 150, "reach": 2500, "impressions": 5000, "followers": 1200},
"instagram": {"engagement": 300, "reach": 4000, "impressions": 8000, "followers": 2500},
"facebook": {"engagement": 200, "reach": 3000, "impressions": 6000, "followers": 1800},
"linkedin": {"engagement": 100, "reach": 1500, "impressions": 3000, "followers": 800}
}
# Add some randomness to make it realistic
metrics = base_metrics.get(platform, base_metrics["twitter"]).copy()
for key in metrics:
variation = random.uniform(0.8, 1.2) # ±20% variation
metrics[key] = int(metrics[key] * variation)
# Adjust based on timeframe
if timeframe == "30d":
for key in metrics:
if key != "followers":
metrics[key] *= 4
elif timeframe == "90d":
for key in metrics:
if key != "followers":
metrics[key] *= 12
return metrics
def get_mock_trending_topics(platform="general", category="all"):
"""Generate mock trending topics for demo purposes."""
trending_topics = {
"technology": [
"Artificial Intelligence", "Machine Learning", "Blockchain", "Cybersecurity",
"Cloud Computing", "IoT", "5G Technology", "Quantum Computing"
],
"business": [
"Digital Marketing", "Remote Work", "Startup Funding", "E-commerce",
"Sustainability", "Leadership", "Innovation", "Entrepreneurship"
],
"entertainment": [
"Streaming Services", "Gaming", "Virtual Reality", "Social Media Trends",
"Content Creation", "Influencer Marketing", "Digital Art", "NFTs"
],
"sports": [
"Olympics", "World Cup", "NBA Finals", "Super Bowl", "Tennis Championships",
"Formula 1", "Cricket World Cup", "Sports Analytics"
]
}
if category == "all":
# Return mix from all categories
all_topics = []
for topics in trending_topics.values():
all_topics.extend(topics[:2]) # Take 2 from each category
return all_topics[:10]
else:
return trending_topics.get(category, trending_topics["technology"])[:10]
# MCP Tools
@mcp.tool(description="Schedule a post across multiple social media platforms")
async def schedule_post(
content: Annotated[str, Field(description="Post content/text to schedule")],
platforms: Annotated[str, Field(description="Comma-separated platforms: twitter,facebook,instagram,linkedin")],
schedule_time: Annotated[str, Field(description="Schedule time in YYYY-MM-DD HH:MM format (24-hour)")],
media_url: Annotated[str, Field(description="Optional media URL to attach (image/video)", default="")] = ""
) -> str:
"""Schedule a post to be published across multiple social media platforms."""
try:
# Validate schedule time
schedule_datetime = datetime.strptime(schedule_time, "%Y-%m-%d %H:%M")
if schedule_datetime <= datetime.now():
return "❌ Error: Schedule time must be in the future"
# Validate platforms
valid_platforms = ["twitter", "facebook", "instagram", "linkedin"]
platform_list = [p.strip().lower() for p in platforms.split(",")]
invalid_platforms = [p for p in platform_list if p not in valid_platforms]
if invalid_platforms:
return f"❌ Error: Invalid platforms: {', '.join(invalid_platforms)}. Valid options: {', '.join(valid_platforms)}"
# Load existing scheduled posts
scheduled_posts = load_json_data(SCHEDULED_POSTS_FILE)
# Create new scheduled post entry
post_id = str(uuid.uuid4())[:8]
scheduled_post = {
"id": post_id,
"content": content,
"platforms": platform_list,
"schedule_time": schedule_time,
"media_url": media_url,
"status": "scheduled",
"created_at": datetime.now().isoformat(),
"posted_at": None
}
# Add to scheduled posts
scheduled_posts.append(scheduled_post)
save_json_data(SCHEDULED_POSTS_FILE, scheduled_posts)
# Calculate time until posting
time_until = schedule_datetime - datetime.now()
days = time_until.days
hours, remainder = divmod(time_until.seconds, 3600)
minutes, _ = divmod(remainder, 60)
time_str = []
if days > 0:
time_str.append(f"{days} day{'s' if days != 1 else ''}")
if hours > 0:
time_str.append(f"{hours} hour{'s' if hours != 1 else ''}")
if minutes > 0:
time_str.append(f"{minutes} minute{'s' if minutes != 1 else ''}")
time_until_str = ", ".join(time_str) if time_str else "less than a minute"
return f"""✅ Post scheduled successfully!
📝 Content: "{content[:50]}{'...' if len(content) > 50 else ''}"
📱 Platforms: {', '.join(platform_list)}
⏰ Scheduled for: {schedule_time}
🕐 Time until posting: {time_until_str}
🆔 Post ID: {post_id}
Note: This is a demo implementation. In production, this would integrate with actual social media APIs to post automatically."""
except ValueError:
return "❌ Error: Invalid date format. Please use YYYY-MM-DD HH:MM format (e.g., 2024-12-25 14:30)"
@mcp.tool(description="Generate relevant hashtags for social media content")
async def generate_hashtags(
content: Annotated[str, Field(description="Post content to analyze for hashtag generation")],
platform: Annotated[str, Field(description="Target platform: twitter, instagram, linkedin, facebook", default="twitter")] = "twitter",
count: Annotated[int, Field(description="Number of hashtags to generate (1-20)", default=5)] = 5
) -> str:
"""Generate relevant hashtags for social media content using AI and rule-based approaches."""
if count < 1 or count > 20:
return "❌ Error: Count must be between 1 and 20"
valid_platforms = ["twitter", "instagram", "linkedin", "facebook"]
if platform.lower() not in valid_platforms:
return f"❌ Error: Invalid platform. Valid options: {', '.join(valid_platforms)}"
platform = platform.lower()
try:
# Try AI-powered hashtag generation first (if OpenAI API key is available)
if openai_api_key:
try:
from openai import OpenAI
client = OpenAI(api_key=openai_api_key)
prompt = f"""Generate {count} relevant and popular hashtags for this {platform} post:
"{content}"
Requirements:
- Return only hashtags, one per line
- Include the # symbol
- Make them relevant to the content
- Consider {platform} best practices
- Mix popular and niche hashtags"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
temperature=0.7
)
ai_hashtags = response.choices[0].message.content.strip().split('\n')
ai_hashtags = [tag.strip() for tag in ai_hashtags if tag.strip().startswith('#')]
if ai_hashtags:
hashtag_analysis = f"""🤖 AI-Generated Hashtags for {platform.title()}:
{chr(10).join(ai_hashtags[:count])}
📊 Hashtag Strategy:
• Mix of popular and niche hashtags
• Platform-optimized for {platform}
• Content-relevant keywords included
• Designed to maximize reach and engagement
💡 Tips:
• Copy and paste these hashtags into your post
• Consider spacing them throughout your content
• Monitor performance to refine future hashtag strategies"""
return hashtag_analysis
except Exception as e:
# Fall back to rule-based if AI fails
pass
# Rule-based hashtag generation (fallback or primary method)
rule_hashtags = generate_hashtags_rule_based(content, platform, count)
# Add platform-specific advice
platform_tips = {
"twitter": "• Keep hashtags concise and relevant\n• Use 1-3 hashtags per tweet\n• Mix trending and niche hashtags",
"instagram": "• Use up to 30 hashtags for maximum reach\n• Mix popular and niche hashtags\n• Place hashtags in comments or at end of caption",
"linkedin": "• Use 3-5 professional hashtags\n• Focus on industry-relevant tags\n• Avoid overly casual hashtags",
"facebook": "• Use 1-2 hashtags sparingly\n• Focus on branded or campaign hashtags\n• Hashtags are less important on Facebook"
}
result = f"""📱 Generated Hashtags for {platform.title()}:
{chr(10).join(rule_hashtags)}
💡 {platform.title()} Best Practices:
{platform_tips[platform]}
🔍 Keywords extracted from your content:
{', '.join(extract_keywords(content)[:5])}
Note: These hashtags are generated using content analysis. For best results, research current trending hashtags in your niche."""
return result
except Exception as e:
return f"❌ Error generating hashtags: {str(e)}"
@mcp.tool(description="Get analytics and engagement metrics for social media accounts")
async def get_analytics(
platform: Annotated[str, Field(description="Platform to analyze: twitter, facebook, instagram, linkedin, all")],
timeframe: Annotated[str, Field(description="Timeframe: 7d, 30d, 90d", default="7d")] = "7d",
metric_type: Annotated[str, Field(description="Metric type: engagement, reach, impressions, followers, all", default="all")] = "all"
) -> str:
"""Get analytics and engagement metrics for social media accounts."""
valid_platforms = ["twitter", "facebook", "instagram", "linkedin", "all"]
valid_timeframes = ["7d", "30d", "90d"]
valid_metrics = ["engagement", "reach", "impressions", "followers", "all"]
if platform.lower() not in valid_platforms:
return f"❌ Error: Invalid platform. Valid options: {', '.join(valid_platforms)}"
if timeframe not in valid_timeframes:
return f"❌ Error: Invalid timeframe. Valid options: {', '.join(valid_timeframes)}"
if metric_type not in valid_metrics:
return f"❌ Error: Invalid metric type. Valid options: {', '.join(valid_metrics)}"
try:
# Load cached analytics or generate mock data
analytics_cache = load_json_data(ANALYTICS_CACHE_FILE)
if platform.lower() == "all":
platforms_to_analyze = ["twitter", "facebook", "instagram", "linkedin"]
else:
platforms_to_analyze = [platform.lower()]
analytics_report = f"📊 SOCIAL MEDIA ANALYTICS REPORT\n"
analytics_report += f"📅 Timeframe: Last {timeframe}\n"
analytics_report += f"📈 Metrics: {metric_type.title()}\n\n"
total_engagement = 0
total_reach = 0
total_impressions = 0
total_followers = 0
for plt in platforms_to_analyze:
# Get mock analytics data (in production, this would call actual APIs)
metrics = get_mock_analytics(plt, timeframe)
analytics_report += f"📱 {plt.upper()}:\n"
if metric_type == "all" or metric_type == "engagement":
analytics_report += f" 💬 Engagement: {metrics['engagement']:,}\n"
total_engagement += metrics['engagement']
if metric_type == "all" or metric_type == "reach":
analytics_report += f" 👥 Reach: {metrics['reach']:,}\n"
total_reach += metrics['reach']
if metric_type == "all" or metric_type == "impressions":
analytics_report += f" 👁️ Impressions: {metrics['impressions']:,}\n"
total_impressions += metrics['impressions']
if metric_type == "all" or metric_type == "followers":
analytics_report += f" 👤 Followers: {metrics['followers']:,}\n"
total_followers += metrics['followers']
# Calculate engagement rate
if metrics['impressions'] > 0:
engagement_rate = (metrics['engagement'] / metrics['impressions']) * 100
analytics_report += f" 📊 Engagement Rate: {engagement_rate:.2f}%\n"
analytics_report += "\n"
# Add totals if analyzing multiple platforms
if len(platforms_to_analyze) > 1:
analytics_report += "🎯 TOTAL ACROSS ALL PLATFORMS:\n"
if metric_type == "all" or metric_type == "engagement":
analytics_report += f" 💬 Total Engagement: {total_engagement:,}\n"
if metric_type == "all" or metric_type == "reach":
analytics_report += f" 👥 Total Reach: {total_reach:,}\n"
if metric_type == "all" or metric_type == "impressions":
analytics_report += f" 👁️ Total Impressions: {total_impressions:,}\n"
if metric_type == "all" or metric_type == "followers":
analytics_report += f" 👤 Total Followers: {total_followers:,}\n"
if total_impressions > 0:
overall_engagement_rate = (total_engagement / total_impressions) * 100
analytics_report += f" 📊 Overall Engagement Rate: {overall_engagement_rate:.2f}%\n"
# Add insights and recommendations
analytics_report += "\n💡 INSIGHTS & RECOMMENDATIONS:\n"
if total_engagement > 0:
if total_impressions > 0:
engagement_rate = (total_engagement / total_impressions) * 100
if engagement_rate > 3:
analytics_report += "✅ Great engagement rate! Your content resonates well with your audience.\n"
elif engagement_rate > 1:
analytics_report += "📈 Good engagement rate. Consider experimenting with different content types.\n"
else:
analytics_report += "📊 Low engagement rate. Try more interactive content and better timing.\n"
analytics_report += "🎯 Focus on your best-performing platforms for maximum ROI.\n"
analytics_report += "📅 Post consistently during peak engagement hours.\n"
analytics_report += "🔄 Engage with your audience to build stronger relationships.\n"
analytics_report += "\nNote: This is demo data. In production, this would connect to actual social media APIs for real-time analytics."
return analytics_report
except Exception as e:
return f"❌ Error retrieving analytics: {str(e)}"
@mcp.tool(description="Track trending topics and suggest content ideas")
async def get_trending_topics(
platform: Annotated[str, Field(description="Platform to check trends: twitter, instagram, general", default="general")] = "general",
category: Annotated[str, Field(description="Category: technology, business, entertainment, sports, all", default="all")] = "all",
location: Annotated[str, Field(description="Location for localized trends: US, UK, IN, global", default="global")] = "global"
) -> str:
"""Track trending topics and suggest content ideas based on current trends."""
valid_platforms = ["twitter", "instagram", "general"]
valid_categories = ["technology", "business", "entertainment", "sports", "all"]
valid_locations = ["US", "UK", "IN", "global"]
if platform.lower() not in valid_platforms:
return f"❌ Error: Invalid platform. Valid options: {', '.join(valid_platforms)}"
if category.lower() not in valid_categories:
return f"❌ Error: Invalid category. Valid options: {', '.join(valid_categories)}"
if location.upper() not in valid_locations:
return f"❌ Error: Invalid location. Valid options: {', '.join(valid_locations)}"
try:
# Get trending topics (mock data for demo)
trending_topics = get_mock_trending_topics(platform, category.lower())
trends_report = f"🔥 TRENDING TOPICS REPORT\n"
trends_report += f"📱 Platform: {platform.title()}\n"
trends_report += f"📂 Category: {category.title()}\n"
trends_report += f"🌍 Location: {location.upper()}\n\n"
trends_report += "📈 CURRENT TRENDING TOPICS:\n"
for i, topic in enumerate(trending_topics, 1):
trends_report += f"{i:2d}. {topic}\n"
# Generate content suggestions based on trends
trends_report += "\n💡 CONTENT IDEAS BASED ON TRENDS:\n"
content_suggestions = [
f"Share your perspective on {trending_topics[0] if trending_topics else 'current trends'}",
f"Create a how-to guide related to {trending_topics[1] if len(trending_topics) > 1 else 'trending topics'}",
f"Start a discussion about {trending_topics[2] if len(trending_topics) > 2 else 'industry trends'}",
"Share behind-the-scenes content related to trending topics",
"Create a poll asking your audience about their opinions on current trends"
]
for i, suggestion in enumerate(content_suggestions, 1):
trends_report += f"{i}. {suggestion}\n"
# Add platform-specific recommendations
platform_recommendations = {
"twitter": [
"Join trending conversations with thoughtful replies",
"Use trending hashtags in your tweets",
"Share quick takes on breaking news",
"Retweet with added commentary"
],
"instagram": [
"Create visually appealing posts about trending topics",
"Use trending hashtags in your posts",
"Share Stories with trending stickers",
"Create Reels about popular trends"
],
"general": [
"Adapt trending topics to your niche",
"Create educational content around trends",
"Share your unique perspective on popular topics",
"Engage with trending conversations authentically"
]
}
trends_report += f"\n🎯 {platform.upper()} STRATEGY TIPS:\n"
for tip in platform_recommendations.get(platform.lower(), platform_recommendations["general"]):
trends_report += f"• {tip}\n"
# Add timing recommendations
trends_report += "\n⏰ TIMING RECOMMENDATIONS:\n"
trends_report += "• Post about trends while they're still hot (within 24-48 hours)\n"
trends_report += "• Monitor trend velocity - some trends peak quickly\n"
trends_report += "• Plan content calendar around predictable trends (holidays, events)\n"
trends_report += "• Set up alerts for trends in your industry\n"
trends_report += "\nNote: This is demo data. In production, this would connect to real-time trend APIs for current trending topics."
return trends_report
except Exception as e:
return f"❌ Error retrieving trending topics: {str(e)}"
@mcp.tool(description="View and manage scheduled posts")
async def manage_scheduled_posts(
action: Annotated[str, Field(description="Action: list, cancel, modify", default="list")] = "list",
post_id: Annotated[str, Field(description="Post ID for cancel/modify actions", default="")] = ""
) -> str:
"""View and manage scheduled social media posts."""
valid_actions = ["list", "cancel", "modify"]
if action.lower() not in valid_actions:
return f"❌ Error: Invalid action. Valid options: {', '.join(valid_actions)}"
try:
scheduled_posts = load_json_data(SCHEDULED_POSTS_FILE)
if action.lower() == "list":
if not scheduled_posts:
return "📅 No scheduled posts found. Use the schedule_post tool to create your first scheduled post!"
# Filter active scheduled posts
active_posts = [post for post in scheduled_posts if post["status"] == "scheduled"]
if not active_posts:
return "📅 No active scheduled posts. All posts have been published or cancelled."
result = "📅 SCHEDULED POSTS:\n\n"
for post in active_posts:
schedule_time = datetime.strptime(post["schedule_time"], "%Y-%m-%d %H:%M")
time_until = schedule_time - datetime.now()
if time_until.total_seconds() > 0:
days = time_until.days
hours, remainder = divmod(time_until.seconds, 3600)
minutes, _ = divmod(remainder, 60)
time_str = []
if days > 0:
time_str.append(f"{days}d")
if hours > 0:
time_str.append(f"{hours}h")
if minutes > 0:
time_str.append(f"{minutes}m")
time_until_str = " ".join(time_str) if time_str else "<1m"
status_emoji = "⏰"
else:
time_until_str = "OVERDUE"
status_emoji = "⚠️"
result += f"{status_emoji} Post ID: {post['id']}\n"
result += f"📝 Content: \"{post['content'][:60]}{'...' if len(post['content']) > 60 else ''}\"\n"
result += f"📱 Platforms: {', '.join(post['platforms'])}\n"
result += f"⏰ Scheduled: {post['schedule_time']}\n"
result += f"🕐 Time until posting: {time_until_str}\n"
if post.get('media_url'):
result += f"📎 Media: {post['media_url']}\n"
result += "\n"
result += "💡 Use manage_scheduled_posts with action='cancel' and post_id to cancel a post."
return result
elif action.lower() == "cancel":
if not post_id:
return "❌ Error: Post ID is required for cancel action"
# Find and cancel the post
post_found = False
for post in scheduled_posts:
if post["id"] == post_id and post["status"] == "scheduled":
post["status"] = "cancelled"
post["cancelled_at"] = datetime.now().isoformat()
post_found = True
break
if post_found:
save_json_data(SCHEDULED_POSTS_FILE, scheduled_posts)
return f"✅ Post {post_id} has been cancelled successfully."
else:
return f"❌ Error: No scheduled post found with ID {post_id}"
elif action.lower() == "modify":
return "🚧 Modify functionality is not yet implemented. Please cancel the existing post and create a new one with updated details."
except Exception as e:
return f"❌ Error managing scheduled posts: {str(e)}"
# Validation tool (required by Puch AI)
@mcp.tool
async def validate() -> str:
"""Validate authentication and return the user's phone number."""
# Return the phone number in the exact format {country_code}{number}
return my_number
# Enhanced AI-powered tools using our utility modules
@mcp.tool(description="Create AI-powered content suggestions for social media")
async def create_content_suggestion(
platform: Annotated[str, Field(description="Target platform: twitter, instagram, facebook, linkedin")],
content_type: Annotated[str, Field(description="Content type: engagement, promotional, informative, trending")],
topic: Annotated[str, Field(description="Topic or theme for the content")]
) -> str:
"""Generate AI-powered content suggestions optimized for specific platforms."""
if not UTILS_AVAILABLE:
return "❌ Enhanced content creation requires utility modules to be properly installed"
try:
result = await content_creator.get_content_suggestion(platform, content_type, topic)
if "error" in result:
return f"❌ Error: {result['error']}"
response = f"🤖 AI CONTENT SUGGESTIONS for {platform.upper()}\n"
response += f"📝 Type: {content_type.title()}\n"
response += f"🎯 Topic: {topic}\n\n"
if isinstance(result.get("suggestions"), list):
for i, suggestion in enumerate(result["suggestions"], 1):
response += f"{i}. {suggestion}\n\n"
else:
response += f"{result.get('suggestions', 'No suggestions available')}\n\n"
response += f"🔮 Generated by: {result.get('generated_by', 'AI').upper()}\n"
response += "💡 Tip: Customize these suggestions to match your brand voice!"
return response
except Exception as e:
return f"❌ Error creating content suggestion: {str(e)}"
@mcp.tool(description="Generate a content calendar for social media planning")
async def create_content_calendar(
platform: Annotated[str, Field(description="Target platform: twitter, instagram, facebook, linkedin")],
days: Annotated[int, Field(description="Number of days to plan (1-30)", default=7)] = 7,
focus_topics: Annotated[str, Field(description="Comma-separated topics to focus on", default="")] = ""
) -> str:
"""Generate a strategic content calendar for social media planning."""
if not UTILS_AVAILABLE:
return "❌ Enhanced content calendar requires utility modules to be properly installed"
if days < 1 or days > 30:
return "❌ Error: Days must be between 1 and 30"
try:
topics_list = [topic.strip() for topic in focus_topics.split(",") if topic.strip()] if focus_topics else None
result = await content_creator.create_content_calendar(platform, days, topics_list)
if "error" in result:
return f"❌ Error: {result['error']}"
response = f"📅 CONTENT CALENDAR for {platform.upper()}\n"
response += f"⏰ Duration: {days} days\n"
if topics_list:
response += f"🎯 Focus Topics: {', '.join(topics_list)}\n"
response += "\n"
if "calendar" in result:
for day_item in result["calendar"]:
if isinstance(day_item, dict):
if "ai_suggestion" in day_item:
response += f"📅 Day {day_item['day']}:\n{day_item['ai_suggestion']}\n\n"
else:
response += f"📅 Day {day_item['day']}:\n"
response += f" 📝 Type: {day_item.get('content_type', 'N/A')}\n"
response += f" 🎯 Topic: {day_item.get('topic', 'N/A')}\n"
response += f" 💡 Suggestion: {day_item.get('suggested_post', 'N/A')}\n"
response += f" ⏰ Best Time: {day_item.get('best_time', 'N/A')}\n\n"
response += f"🔮 Generated by: {result.get('generated_by', 'AI').upper()}\n"
response += "💡 Tip: Adapt these suggestions to your brand and current events!"
return response
except Exception as e:
return f"❌ Error creating content calendar: {str(e)}"
@mcp.tool(description="Get detailed audience insights and demographics")
async def get_audience_insights(
platform: Annotated[str, Field(description="Platform to analyze: twitter, facebook, instagram, linkedin")],
insight_type: Annotated[str, Field(description="Type: demographics, growth, engagement, report", default="report")] = "report"
) -> str:
"""Get comprehensive audience insights including demographics, growth, and engagement patterns."""
if not UTILS_AVAILABLE:
return "❌ Enhanced audience insights require utility modules to be properly installed"
valid_platforms = ["twitter", "facebook", "instagram", "linkedin"]
if platform.lower() not in valid_platforms:
return f"❌ Error: Invalid platform. Valid options: {', '.join(valid_platforms)}"
valid_types = ["demographics", "growth", "engagement", "report"]
if insight_type.lower() not in valid_types:
return f"❌ Error: Invalid insight type. Valid options: {', '.join(valid_types)}"
try:
if insight_type.lower() == "demographics":
result = await audience_insights.get_audience_demographics(platform)
elif insight_type.lower() == "growth":
result = await audience_insights.get_follower_growth(platform)
elif insight_type.lower() == "engagement":
result = await audience_insights.get_engagement_metrics(platform)
else: # report
result = await audience_insights.generate_audience_report(platform)
if "error" in result:
return f"❌ Error: {result['error']}"
response = f"👥 AUDIENCE INSIGHTS for {platform.upper()}\n"
response += f"📊 Analysis Type: {insight_type.title()}\n\n"
if insight_type.lower() == "demographics":
demographics = result.get("demographics", {})
if "age_groups" in demographics:
response += "🎂 AGE DISTRIBUTION:\n"
for age, percentage in demographics["age_groups"].items():
response += f" {age}: {percentage}%\n"
response += "\n"
if "gender" in demographics:
response += "👤 GENDER DISTRIBUTION:\n"
for gender, percentage in demographics["gender"].items():
response += f" {gender.title()}: {percentage}%\n"
response += "\n"
if "locations" in demographics:
response += "🌍 TOP LOCATIONS:\n"
for location, percentage in list(demographics["locations"].items())[:5]:
response += f" {location}: {percentage}%\n"
response += "\n"
elif insight_type.lower() == "growth":
growth = result.get("follower_growth", {})
response += f"📈 FOLLOWER GROWTH ({result.get('period_days', 30)} days):\n"
response += f" Current Followers: {result.get('follower_count', 0):,}\n"
response += f" Total Growth: {growth.get('total', 0):,}\n"
response += f" Growth Rate: {growth.get('percentage', 0)}%\n"
response += f" Avg Daily Growth: {growth.get('average_daily', 0)}\n\n"
elif insight_type.lower() == "engagement":
metrics = result.get("average_metrics", {})
response += f"💬 ENGAGEMENT METRICS ({result.get('period_days', 7)} days):\n"
response += f" Total Posts: {result.get('total_posts', 0)}\n"
response += f" Avg Likes: {metrics.get('likes', 0)}\n"
response += f" Avg Comments: {metrics.get('comments', 0)}\n"
response += f" Avg Shares: {metrics.get('shares', 0)}\n"
response += f" Engagement Rate: {metrics.get('engagement_rate', 0)}%\n\n"
top_posts = result.get("top_performing_posts", [])
if top_posts:
response += "🏆 TOP PERFORMING POSTS:\n"
for post in top_posts[:3]:
response += f" 📝 {post.get('post_id', 'N/A')} - {post.get('metrics', {}).get('engagement_rate', 0)}% engagement\n"
response += "\n"
else: # Full report
response += "📋 COMPREHENSIVE AUDIENCE REPORT:\n\n"
# Add summary from each section
if "follower_growth" in result:
growth = result["follower_growth"].get("follower_growth", {})
response += f"📈 Current Followers: {result['follower_growth'].get('follower_count', 0):,}\n"
response += f"📊 30-day Growth: {growth.get('total', 0):,} ({growth.get('percentage', 0)}%)\n\n"
if "insights" in result and result["insights"]:
response += "🤖 AI INSIGHTS:\n"
response += f"{result['insights']}\n\n"
response += "💡 Use these insights to optimize your content strategy and posting schedule!"
return response
except Exception as e:
return f"❌ Error getting audience insights: {str(e)}"
@mcp.tool(description="Add and analyze competitors")
async def manage_competitors(
action: Annotated[str, Field(description="Action: add, remove, list, analyze, compare")],
competitor_name: Annotated[str, Field(description="Competitor name", default="")] = "",
platforms: Annotated[str, Field(description="Platforms in format 'twitter:@handle,instagram:@handle'", default="")] = "",
competitors_to_compare: Annotated[str, Field(description="Comma-separated competitor names for comparison", default="")] = ""
) -> str:
"""Manage competitor tracking and analysis for social media intelligence."""
if not UTILS_AVAILABLE:
return "❌ Enhanced competitor analysis requires utility modules to be properly installed"
valid_actions = ["add", "remove", "list", "analyze", "compare"]
if action.lower() not in valid_actions:
return f"❌ Error: Invalid action. Valid options: {', '.join(valid_actions)}"
try:
if action.lower() == "add":
if not competitor_name or not platforms:
return "❌ Error: Both competitor_name and platforms are required for add action"
# Parse platforms string
platform_dict = {}
for platform_info in platforms.split(","):
if ":" in platform_info:
platform, handle = platform_info.split(":", 1)
platform_dict[platform.strip()] = handle.strip()
if not platform_dict:
return "❌ Error: Invalid platforms format. Use 'twitter:@handle,instagram:@handle'"
result = await competitor_analysis.add_competitor(competitor_name, platform_dict)
if "error" in result:
return f"❌ Error: {result['error']}"
return f"✅ {result.get('message', 'Competitor added successfully')}"
elif action.lower() == "remove":
if not competitor_name:
return "❌ Error: Competitor name is required for remove action"
result = await competitor_analysis.remove_competitor(competitor_name)
if "error" in result:
return f"❌ Error: {result['error']}"
return f"✅ {result.get('message', 'Competitor removed successfully')}"
elif action.lower() == "list":
result = await competitor_analysis.list_competitors()
if "error" in result:
return f"❌ Error: {result['error']}"
competitors = result.get("competitors", [])
if not competitors:
return "📋 No competitors are currently being tracked. Use action='add' to start monitoring competitors."
response = f"📋 TRACKED COMPETITORS ({result.get('total', 0)}):\n\n"
for competitor in competitors:
response += f"🏢 {competitor['name']}\n"
response += f" 📱 Platforms: {', '.join(competitor['platforms'].keys())}\n"
response += f" 📅 Added: {competitor['added_on'][:10]}\n"
if competitor.get('last_analyzed'):
response += f" 🔍 Last Analyzed: {competitor['last_analyzed'][:10]}\n"
response += "\n"
return response
elif action.lower() == "analyze":
if not competitor_name:
return "❌ Error: Competitor name is required for analyze action"
result = await competitor_analysis.analyze_competitor_strategy(competitor_name)
if "error" in result:
return f"❌ Error: {result['error']}"
response = f"🔍 COMPETITOR ANALYSIS: {competitor_name}\n"
response += f"📱 Platforms: {', '.join(result.get('platforms', {}).keys())}\n\n"
analysis = result.get("analysis", {})
if "content_strategy" in analysis:
content = analysis["content_strategy"]
response += "📝 CONTENT STRATEGY:\n"
if "post_types" in content:
response += " 📊 Post Types:\n"
for post_type, percentage in content["post_types"].items():
response += f" {post_type.title()}: {percentage}%\n"
if "top_topics" in content:
response += " 🎯 Top Topics:\n"
for topic, percentage in content["top_topics"].items():
response += f" {topic.title()}: {percentage}%\n"
response += "\n"
if "ai_insights" in analysis and analysis["ai_insights"]:
response += "🤖 AI INSIGHTS:\n"
response += f"{analysis['ai_insights']}\n\n"
response += "💡 Use these insights to identify opportunities and differentiate your strategy!"
return response
elif action.lower() == "compare":
if not competitors_to_compare:
return "❌ Error: competitors_to_compare is required for compare action"
competitor_names = [name.strip() for name in competitors_to_compare.split(",")]
if len(competitor_names) < 2:
return "❌ Error: At least 2 competitors are required for comparison"
result = await competitor_analysis.compare_competitors(competitor_names)
if "error" in result:
return f"❌ Error: {result['error']}"
response = f"🆚 COMPETITOR COMPARISON\n"
response += f"📊 Comparing: {', '.join(competitor_names)}\n\n"
comparison = result.get("comparison", {})
if "followers" in comparison:
response += "👥 FOLLOWER COUNT:\n"
for name, count in comparison["followers"].items():
response += f" {name}: {count:,}\n"
response += "\n"
if "engagement" in comparison:
response += "💬 ENGAGEMENT RATE:\n"
for name, rate in comparison["engagement"].items():
response += f" {name}: {rate}%\n"
response += "\n"
if "posting_frequency" in comparison:
response += "📅 POSTING FREQUENCY (per day):\n"
for name, freq in comparison["posting_frequency"].items():
response += f" {name}: {freq}\n"
response += "\n"
return response
except Exception as e:
return f"❌ Error managing competitors: {str(e)}"
@mcp.tool(description="Generate advanced hashtags using AI and trending analysis")
async def generate_advanced_hashtags(
content: Annotated[str, Field(description="Post content to analyze for hashtag generation")],
platform: Annotated[str, Field(description="Target platform: twitter, instagram, linkedin, facebook", default="twitter")] = "twitter",
count: Annotated[int, Field(description="Number of hashtags to generate (1-30)", default=10)] = 10,