Skip to content

Meta Ads (Facebook/Instagram) MCP server for Claude, ChatGPT, Perplexity & Cursor — the Meta node of Pipeboard’s 5-platform family (+ Google, TikTok, Snap, Reddit). Hosted remote MCP, badged Meta Business Partner, free plan — no self-hosting required.

Add MCP Server Tool Decorators to Additional Functions

Yves Junqueira2809ea3e28c4
diff --git a/future_improvements.md b/future_improvements.md
index 4553817..598cf90 100644
--- a/future_improvements.md
+++ b/future_improvements.md
@@ -1,18 +1,5 @@
# Future Improvements for Meta Ads MCP
-## Critical Improvement: Add MCP Server Tool Decorators
-
-We recently refactored the codebase to modularize the functionality that was in meta_ads_generated.py, but we need to add the `@mcp_server.tool()` decorator to all relevant functions to ensure they're properly registered with the MCP server. We've already fixed this for the `get_ad_accounts` function, but this needs to be done for all other functions we want to expose as MCP tools.
-
-Pattern for tool function registration:
-```python
-@mcp_server.tool() # Required to register with MCP server
-@meta_api_tool # Handles authentication logic
-async def function_name(...):
- """Documentation..."""
- # Function code
-```
-
## Planned Improvement: Ad Set Update Confirmation Flow
### Overview
diff --git a/meta_ads_mcp/core/accounts.py b/meta_ads_mcp/core/accounts.py
index 45a00c3..35f99e3 100644
--- a/meta_ads_mcp/core/accounts.py
+++ b/meta_ads_mcp/core/accounts.py
@@ -28,6 +28,7 @@ async def get_ad_accounts(access_token: str = None, user_id: str = "me", limit:
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_account_info(access_token: str = None, account_id: str = None) -> str:
"""
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index d587375..371e294 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -9,8 +9,10 @@ from mcp.server.fastmcp import Image
from .api import meta_api_tool, make_api_request
from .accounts import get_ad_accounts
from .utils import download_image, try_multiple_download_methods, ad_creative_images
+from .server import mcp_server
+@mcp_server.tool()
@meta_api_tool
async def get_ads(access_token: str = None, account_id: str = None, limit: int = 10,
campaign_id: str = "", adset_id: str = "") -> str:
@@ -51,6 +53,7 @@ async def get_ads(access_token: str = None, account_id: str = None, limit: int =
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_ad_details(access_token: str = None, ad_id: str = None) -> str:
"""
@@ -73,6 +76,7 @@ async def get_ad_details(access_token: str = None, ad_id: str = None) -> str:
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_ad_creatives(access_token: str = None, ad_id: str = None) -> str:
"""
@@ -226,6 +230,7 @@ async def get_ad_creatives(access_token: str = None, ad_id: str = None) -> str:
return json.dumps(creative_data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_ad_image(access_token: str = None, ad_id: str = None) -> Image:
"""
diff --git a/meta_ads_mcp/core/adsets.py b/meta_ads_mcp/core/adsets.py
index 63e2eb9..e755a55 100644
--- a/meta_ads_mcp/core/adsets.py
+++ b/meta_ads_mcp/core/adsets.py
@@ -4,8 +4,10 @@ import json
from typing import Optional, Dict, Any, List
from .api import meta_api_tool, make_api_request
from .accounts import get_ad_accounts
+from .server import mcp_server
+@mcp_server.tool()
@meta_api_tool
async def get_adsets(access_token: str = None, account_id: str = None, limit: int = 10, campaign_id: str = "") -> str:
"""
@@ -41,6 +43,7 @@ async def get_adsets(access_token: str = None, account_id: str = None, limit: in
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_adset_details(access_token: str = None, adset_id: str = None) -> str:
"""
@@ -69,6 +72,7 @@ async def get_adset_details(access_token: str = None, adset_id: str = None) -> s
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def update_adset(access_token: str = None, adset_id: str = None,
bid_strategy: Optional[str] = None,
diff --git a/meta_ads_mcp/core/authentication.py b/meta_ads_mcp/core/authentication.py
index f409dab..017c2a8 100644
--- a/meta_ads_mcp/core/authentication.py
+++ b/meta_ads_mcp/core/authentication.py
@@ -3,9 +3,11 @@
import json
import asyncio
from .api import meta_api_tool
-from .auth import start_callback_server, auth_manager
+from .auth import start_callback_server, auth_manager, get_current_access_token
+from .server import mcp_server
+@mcp_server.tool()
@meta_api_tool
async def get_login_link(access_token: str = None) -> str:
"""
@@ -17,40 +19,22 @@ async def get_login_link(access_token: str = None) -> str:
Returns:
A clickable resource link for Meta authentication
"""
- # Check if we have a cached token
- cached_token = auth_manager.get_access_token()
- token_status = "No token" if not cached_token else "Valid token"
+ # The meta_api_tool decorator will handle all the authentication logic
+ # We just need to return a simple success message if we already have a token
- # If we already have a valid token and none was provided, just return success
- if cached_token and not access_token:
+ if access_token or await get_current_access_token():
+ cached_token = auth_manager.get_access_token()
return json.dumps({
"message": "Already authenticated",
- "token_status": token_status,
- "token_preview": cached_token[:10] + "...",
- "created_at": auth_manager.token_info.created_at,
- "expires_in": auth_manager.token_info.expires_in
+ "token_status": "Valid token",
+ "token_preview": cached_token[:10] + "..." if cached_token else "None",
+ "created_at": auth_manager.token_info.created_at if hasattr(auth_manager, "token_info") else None,
+ "expires_in": auth_manager.token_info.expires_in if hasattr(auth_manager, "token_info") else None
}, indent=2)
- # IMPORTANT: Start the callback server first by calling our helper function
- # This ensures the server is ready before we provide the URL to the user
- port = start_callback_server()
-
- # Generate direct login URL
- auth_manager.redirect_uri = f"http://localhost:{port}/callback" # Ensure port is set correctly
- login_url = auth_manager.get_auth_url()
-
- # Return a special format that helps the LLM format the response properly
- response = {
- "login_url": login_url,
- "token_status": token_status,
- "server_status": f"Callback server running on port {port}",
- "markdown_link": f"[Click here to authenticate with Meta Ads]({login_url})",
- "message": "IMPORTANT: Please use the Markdown link format in your response to allow the user to click it.",
- "instructions_for_llm": "You must present this link as clickable Markdown to the user using the markdown_link format provided.",
- "note": "After authenticating, the token will be automatically saved."
- }
-
- # Wait a moment to ensure the server is fully started
- await asyncio.sleep(1)
-
- return json.dumps(response, indent=2)
\ No newline at end of file
+ # If no token exists, the meta_api_tool decorator will handle authentication
+ # This return statement is essentially a fallback that won't normally be reached
+ return json.dumps({
+ "message": "Authentication required",
+ "note": "The auth flow should have been handled by the decorator"
+ }, indent=2)
\ No newline at end of file
diff --git a/meta_ads_mcp/core/campaigns.py b/meta_ads_mcp/core/campaigns.py
index 028b6e1..61d5357 100644
--- a/meta_ads_mcp/core/campaigns.py
+++ b/meta_ads_mcp/core/campaigns.py
@@ -4,8 +4,10 @@ import json
from typing import List, Optional
from .api import meta_api_tool, make_api_request
from .accounts import get_ad_accounts
+from .server import mcp_server
+@mcp_server.tool()
@meta_api_tool
async def get_campaigns(access_token: str = None, account_id: str = None, limit: int = 10, status_filter: str = "") -> str:
"""
@@ -41,6 +43,7 @@ async def get_campaigns(access_token: str = None, account_id: str = None, limit:
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def get_campaign_details(access_token: str = None, campaign_id: str = None) -> str:
"""
@@ -63,6 +66,7 @@ async def get_campaign_details(access_token: str = None, campaign_id: str = None
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def create_campaign(
access_token: str = None,
diff --git a/meta_ads_mcp/core/insights.py b/meta_ads_mcp/core/insights.py
index 33698ed..6d87300 100644
--- a/meta_ads_mcp/core/insights.py
+++ b/meta_ads_mcp/core/insights.py
@@ -4,10 +4,12 @@ import json
from typing import Optional
from .api import meta_api_tool, make_api_request
from .utils import download_image, try_multiple_download_methods, ad_creative_images, create_resource_from_image
+from .server import mcp_server
import base64
import datetime
+@mcp_server.tool()
@meta_api_tool
async def get_insights(access_token: str = None, object_id: str = None,
time_range: str = "maximum", breakdown: str = "",
@@ -40,6 +42,7 @@ async def get_insights(access_token: str = None, object_id: str = None,
return json.dumps(data, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def debug_image_download(access_token: str = None, url: str = "", ad_id: str = "") -> str:
"""
@@ -230,6 +233,7 @@ async def debug_image_download(access_token: str = None, url: str = "", ad_id: s
return json.dumps(results, indent=2)
+@mcp_server.tool()
@meta_api_tool
async def save_ad_image_via_api(access_token: str = None, ad_id: str = None) -> str:
"""