Skip to content

Facebook MCP server for automating posts, comment moderation, insights, and sentiment filtering.

Python

initial commit

Hagai Hen30156a2d1e90
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a35594d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.env
+.venv
+__pycache__
\ No newline at end of file
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..a0cb3c3
--- /dev/null
+++ b/config.py
@@ -0,0 +1,10 @@
+import os
+from dotenv import load_dotenv
+
+load_dotenv()
+
+# Facebook Graph API setup
+GRAPH_API_VERSION = "v22.0"
+PAGE_ACCESS_TOKEN = os.getenv("FACEBOOK_ACCESS_TOKEN")
+PAGE_ID = os.getenv("FACEBOOK_PAGE_ID")
+GRAPH_API_BASE_URL = f"https://graph.facebook.com/{GRAPH_API_VERSION}"
\ No newline at end of file
diff --git a/facebook_api.py b/facebook_api.py
new file mode 100644
index 0000000..1b4c01d
--- /dev/null
+++ b/facebook_api.py
@@ -0,0 +1,37 @@
+import requests
+from typing import Any
+from config import GRAPH_API_BASE_URL, PAGE_ID, PAGE_ACCESS_TOKEN
+
+
+class FacebookAPI:
+ # Generic Graph API request method
+ def _request(self, method: str, endpoint: str, params: dict[str, Any]) -> dict[str, Any]:
+ url = f"{GRAPH_API_BASE_URL}/{endpoint}"
+ params["access_token"] = PAGE_ACCESS_TOKEN
+ response = requests.request(method, url, params=params)
+ return response.json()
+
+ def post_message(self, message: str) -> dict[str, Any]:
+ return self._request("POST", f"{PAGE_ID}/feed", {"message": message})
+
+ def reply_to_comment(self, comment_id: str, message: str) -> dict[str, Any]:
+ return self._request("POST", f"{comment_id}/comments", {"message": message})
+
+ def get_posts(self) -> dict[str, Any]:
+ return self._request("GET", f"{PAGE_ID}/posts", {"fields": "id,message,created_time"})
+
+ def get_comments(self, post_id: str) -> dict[str, Any]:
+ return self._request("GET", f"{post_id}/comments", {"fields": "id,message,from,created_time"})
+
+ def delete_post(self, post_id: str) -> dict[str, Any]:
+ return self._request("DELETE", f"{post_id}", {})
+
+ def delete_comment(self, comment_id: str) -> dict[str, Any]:
+ return self._request("DELETE", f"{comment_id}", {})
+
+ def get_insights(self, post_id: str, metric: str, period: str = "lifetime") -> dict[str, Any]:
+ return self._request("GET", f"{post_id}/insights", {"metric": metric, "period": period})
+
+ def get_bulk_insights(self, post_id: str, metrics: list[str], period: str = "lifetime") -> dict[str, Any]:
+ metric_str = ",".join(metrics)
+ return self.get_insights(post_id, metric_str, period)
diff --git a/manager.py b/manager.py
new file mode 100644
index 0000000..dbc7e1e
--- /dev/null
+++ b/manager.py
@@ -0,0 +1,47 @@
+from typing import Any
+from facebook_api import FacebookAPI
+
+
+class Manager:
+ def __init__(self):
+ self.api = FacebookAPI()
+
+ def post_to_facebook(self, message: str) -> dict[str, Any]:
+ return self.api.post_message(message)
+
+ def reply_to_comment(self, post_id: str, comment_id: str, message: str) -> dict[str, Any]:
+ return self.api.reply_to_comment(comment_id, message)
+
+ def get_page_posts(self) -> dict[str, Any]:
+ return self.api.get_posts()
+
+ def get_post_comments(self, post_id: str) -> dict[str, Any]:
+ return self.api.get_comments(post_id)
+
+ def delete_post(self, post_id: str) -> dict[str, Any]:
+ return self.api.delete_post(post_id)
+
+ def delete_comment(self, comment_id: str) -> dict[str, Any]:
+ return self.api.delete_comment(comment_id)
+
+ def delete_comment_from_post(self, post_id: str, comment_id: str) -> dict[str, Any]:
+ return self.api.delete_comment(comment_id)
+
+ def filter_negative_comments(self, comments: dict[str, Any]) -> list[dict[str, Any]]:
+ keywords = ["bad", "terrible", "awful", "hate", "dislike", "problem", "issue"]
+ return [c for c in comments.get("data", []) if any(k in c.get("message", "").lower() for k in keywords)]
+
+ def get_number_of_comments(self, post_id: str) -> int:
+ return len(self.api.get_comments(post_id).get("data", []))
+
+ def get_number_of_likes(self, post_id: str) -> int:
+ return self.api._request("GET", post_id, {"fields": "likes.summary(true)"}).get("likes", {}).get("summary", {}).get("total_count", 0)
+
+ def get_post_insights(self, post_id: str) -> dict[str, Any]:
+ metrics = [
+ "post_impressions", "post_impressions_unique", "post_impressions_paid",
+ "post_impressions_organic", "post_engaged_users", "post_clicks",
+ "post_reactions_like_total", "post_reactions_love_total", "post_reactions_wow_total",
+ "post_reactions_haha_total", "post_reactions_sorry_total", "post_reactions_anger_total",
+ ]
+ return self.api.get_bulk_insights(post_id, metrics)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..779af07
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+mcp
+dotenv
+requests
\ No newline at end of file
diff --git a/server.py b/server.py
new file mode 100644
index 0000000..fe54aed
--- /dev/null
+++ b/server.py
@@ -0,0 +1,94 @@
+from mcp.server.fastmcp import FastMCP
+from manager import Manager
+from typing import Any
+
+mcp = FastMCP("Demo")
+manager = Manager()
+
+def post_to_facebook(message: str) -> dict[str, Any]:
+ """Create a new Facebook Page post with a text message.
+ Input: message (str)
+ Output: dict with post ID and creation status
+ """
+ return manager.post_to_facebook(message)
+
+def reply_to_comment(post_id: str, comment_id: str, message: str) -> dict[str, Any]:
+ """Reply to a specific comment on a Facebook post.
+ Input: post_id (str), comment_id (str), message (str)
+ Output: dict with reply creation status
+ """
+ return manager.reply_to_comment(post_id, comment_id, message)
+
+def get_page_posts() -> dict[str, Any]:
+ """Fetch the most recent posts on the Page.
+ Input: None
+ Output: dict with list of post objects and metadata
+ """
+ return manager.get_page_posts()
+
+def get_post_comments(post_id: str) -> dict[str, Any]:
+ """Retrieve all comments for a given post.
+ Input: post_id (str)
+ Output: dict with comment objects
+ """
+ return manager.get_post_comments(post_id)
+
+def delete_post(post_id: str) -> dict[str, Any]:
+ """Delete a specific post from the Facebook Page.
+ Input: post_id (str)
+ Output: dict with deletion result
+ """
+ return manager.delete_post(post_id)
+
+def delete_comment(comment_id: str) -> dict[str, Any]:
+ """Delete a specific comment from the Page.
+ Input: comment_id (str)
+ Output: dict with deletion result
+ """
+ return manager.delete_comment(comment_id)
+
+def delete_comment_from_post(post_id: str, comment_id: str) -> dict[str, Any]:
+ """Alias to delete a comment on a post.
+ Input: post_id (str), comment_id (str)
+ Output: dict with deletion result
+ """
+ return manager.delete_comment_from_post(post_id, comment_id)
+
+def filter_negative_comments(comments: dict[str, Any]) -> list[dict[str, Any]]:
+ """Filter comments for basic negative sentiment.
+ Input: comments (dict)
+ Output: list of flagged negative comments
+ """
+ return manager.filter_negative_comments(comments)
+
+def get_number_of_comments(post_id: str) -> int:
+ """Count the number of comments on a given post.
+ Input: post_id (str)
+ Output: integer count of comments
+ """
+ return manager.get_number_of_comments(post_id)
+
+def get_number_of_likes(post_id: str) -> int:
+ """Return the number of likes on a post.
+ Input: post_id (str)
+ Output: integer count of likes
+ """
+ return manager.get_number_of_likes(post_id)
+
+def get_post_insights(post_id: str) -> dict[str, Any]:
+ """Fetch all insights metrics (impressions, reactions, clicks, etc).
+ Input: post_id (str)
+ Output: dict with multiple metrics and their values
+ """
+ return manager.get_post_insights(post_id)
\ No newline at end of file