Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/generate_mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"getTweet",
"getInstagramBusinessDiscovery",
"searchAdLibrary",
"searchAdTargeting",
"searchAdInterests",
"searchInstagramAudio",
"listTikTokCommercialMusic",
}

# Operations to SKIP (not useful for MCP)
Expand Down
81 changes: 80 additions & 1 deletion src/late/mcp/chatgpt_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,86 @@
"contacts_list_contacts",
)

CHATGPT_GENERATED_TOOLS = WAVE_1
# The next 70 reads by the same ranking, keeping to the scopes wave 1 already
# asks for (so a link that re-linked for wave 1 needs nothing new). Left out
# on review: X search (bills per tweet read), the Reddit feed (disabled
# platform), deprecated aliases and legacy analytics replaced by metrics,
# the raw request logs, a report reader whose creator is a write, and
# LinkedIn reactors (third-party personal data).
WAVE_2 = (
"lead_gen_list_leads",
"broadcasts_get_broadcast",
"lead_gen_count_leads",
"workflows_get_workflow",
"ad_campaigns_get_ad_campaign_details",
"instagram_list_instagram_stories",
"analytics_get_facebook_page_insights",
"ad_campaigns_get_ads_timeline",
"analytics_get_instagram_follower_history",
"profiles_get_profile",
"instagram_get_instagram_publishing_limit",
"analytics_get_you_tube_video_retention",
"mentions_list_inbox_mentions",
"accounts_get_tik_tok_creator_info",
"analytics_get_you_tube_channel_insights",
"queue_list_queue_slots",
"ad_creatives_get_ad_creative",
"phone_numbers_list_phone_numbers",
"ad_accounts_get_ad_account_live_entities",
"lead_gen_list_lead_forms",
"ad_accounts_get_ad_account_finance",
"ad_insights_get_campaign_analytics",
"analytics_get_you_tube_demographics",
"instagram_get_instagram_business_discovery",
"ad_targeting_search_ad_targeting",
"ad_creatives_list_ad_creatives",
"email_list_email_messages",
"messages_search_inbox_conversations",
"workflows_list_workflows",
"ad_campaigns_list_ad_sets",
"analytics_get_analytics_dashboard",
"analytics_get_linked_in_org_aggregate_analytics",
"contacts_get_contact",
"voice_list_voice_calls",
"ad_accounts_get_ads_activity_log",
"ad_audiences_list_ad_audiences",
"analytics_get_linked_in_aggregate_analytics",
"ad_accounts_list_ads_instagram_accounts",
"inbox_analytics_get_inbox_volume",
"tracking_tags_list_tracking_tags",
"broadcasts_list_broadcasts",
"ad_campaigns_list_ad_keywords",
"ad_accounts_list_ads_instagram_posts",
"analytics_get_google_business_search_keywords",
"ad_creatives_list_ad_videos",
"whatsapp_get_whats_app_template_by_id",
"inbox_analytics_get_inbox_response_time",
"accounts_get_account",
"ad_campaigns_get_ad_review",
"tracking_tags_get_tracking_tag_stats",
"account_groups_list_account_groups",
"ad_creatives_list_ad_images",
"lead_gen_get_lead_form",
"whatsapp_get_whats_app_business_profile",
"email_get_email_thread",
"inbox_analytics_get_inbox_source_breakdown",
"tracking_tags_get_tracking_tag",
"tracking_tags_get_ad_tracking_tags",
"gmb_verifications_get_google_business_verifications",
"whatsapp_list_whats_app_account_events",
"instagram_get_instagram_mentioned_comment",
"ad_accounts_list_high_demand_periods",
"gmb_services_get_google_business_services",
"ad_library_search_ad_library",
"inbox_analytics_get_inbox_heatmap",
"whatsapp_get_whats_app_template",
"accounts_list_tik_tok_commercial_music",
"instagram_search_instagram_audio",
"email_get_email_message",
"whatsapp_list_whats_app_group_chats",
)

CHATGPT_GENERATED_TOOLS = WAVE_1 + WAVE_2


_SUMMARY_MAX = 400
Expand Down
8 changes: 4 additions & 4 deletions src/late/mcp/generated_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ def accounts_get_instagram_follow_status(
title="List trending commercial music",
readOnlyHint=True,
destructiveHint=False,
openWorldHint=False,
openWorldHint=True,
)
)
def accounts_list_tik_tok_commercial_music(
Expand Down Expand Up @@ -14855,7 +14855,7 @@ def ad_rules_list_ad_rule_runs(rule_id: str, limit: int = 20, skip: int = 0) ->
title="Search targeting interests",
readOnlyHint=True,
destructiveHint=False,
openWorldHint=False,
openWorldHint=True,
)
)
def ad_targeting_search_ad_interests(q: str, account_id: str) -> str:
Expand Down Expand Up @@ -14885,7 +14885,7 @@ def ad_targeting_search_ad_interests(q: str, account_id: str) -> str:
title="Search targeting options",
readOnlyHint=True,
destructiveHint=False,
openWorldHint=False,
openWorldHint=True,
)
)
def ad_targeting_search_ad_targeting(
Expand Down Expand Up @@ -41355,7 +41355,7 @@ def instagram_get_instagram_publishing_limit(account_id: str) -> str:
title="Search Instagram audio",
readOnlyHint=True,
destructiveHint=False,
openWorldHint=False,
openWorldHint=True,
)
)
def instagram_search_instagram_audio(
Expand Down
27 changes: 18 additions & 9 deletions src/late/mcp/middleware/missing_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@

_INSUFFICIENT_PERMISSIONS = "insufficient_permissions"

_SCOPE_PURPOSE = {
"messaging:read": "read your inbox, comments and messages",
"ads:read": "read your ad accounts, campaigns and results",
"contacts:read": "read your contacts and leads",
}


def relink_instructions(needed: str) -> str:
"""What the model tells the user when ChatGPT does not show its re-link prompt."""
purpose = _SCOPE_PURPOSE.get(needed, f"use this ({needed})")
return (
f"Zernio needs a new permission to {purpose}, which this ChatGPT connection "
f"has not granted yet ('{needed}'). Tell the user to reconnect Zernio in ChatGPT: "
"Settings > Plugins (called Apps in some versions) > Zernio > Reconnect, then "
"approve the new permission and ask again. Nothing was changed in their account."
)


def _granted_scopes() -> set[str] | None:
"""The OAuth scope of the calling token, or None for an API key."""
Expand Down Expand Up @@ -83,15 +100,7 @@ async def on_call_tool(
raise
needed = reaching[0]
return ToolResult(
content=[
TextContent(
type="text",
text=(
f"Zernio needs the '{needed}' permission for this, which this "
"ChatGPT connection was not granted. Reconnect Zernio to grant it."
),
)
],
content=[TextContent(type="text", text=relink_instructions(needed))],
meta={"mcp/www_authenticate": [self._challenge]},
is_error=True,
)
Expand Down
14 changes: 12 additions & 2 deletions tests/test_chatgpt_generated_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
LIVE_INSTRUCTIONS = (FIXTURES / "chatgpt_live_instructions.txt").read_text()
CONVERSATIONS = "https://api.test.com/v1/inbox/conversations"
LINK_SCOPES = {"posts:read", "posts:write", "accounts:read", "analytics:read"}
OPEN_WORLD_READS = {
"accounts_list_tik_tok_commercial_music",
"ad_library_search_ad_library",
"ad_targeting_search_ad_targeting",
"instagram_get_instagram_business_discovery",
"instagram_search_instagram_audio",
}
GROUP_DENIED = {
"error": "This connector token has the 'messages' resource group disabled.",
"code": "insufficient_permissions",
Expand Down Expand Up @@ -76,7 +83,8 @@ async def test_every_generated_tool_has_explicit_hints_and_its_oauth_scope():
hints = listed[name]["annotations"]
assert hints["readOnlyHint"] is True, name
assert hints["destructiveHint"] is False, name
assert hints["openWorldHint"] is False, name
# Reads of public catalogs and other accounts' content are open-world.
assert hints["openWorldHint"] is (name in OPEN_WORLD_READS), name
assert listed[name]["_meta"]["securitySchemes"] == [
{"type": "oauth2", "scopes": [TOOL_OAUTH_SCOPES[name][0]]}
], name
Expand Down Expand Up @@ -169,7 +177,9 @@ async def test_a_link_without_the_tools_scope_is_asked_to_relink(monkeypatch):
)
assert 'error="insufficient_scope"' in challenge
assert "messaging:read" in challenge
assert "messaging:read" in result.content[0].text
text = result.content[0].text
assert "read your inbox" in text
assert "Settings > Plugins" in text and "Reconnect" in text


async def test_each_group_refusal_logs_whether_it_asked_to_relink(monkeypatch, caplog):
Expand Down
Loading