diff --git a/scripts/generate_mcp_tools.py b/scripts/generate_mcp_tools.py index f8597a58..67fda26d 100644 --- a/scripts/generate_mcp_tools.py +++ b/scripts/generate_mcp_tools.py @@ -135,6 +135,10 @@ "getTweet", "getInstagramBusinessDiscovery", "searchAdLibrary", + "searchAdTargeting", + "searchAdInterests", + "searchInstagramAudio", + "listTikTokCommercialMusic", } # Operations to SKIP (not useful for MCP) diff --git a/src/late/mcp/chatgpt_tools.py b/src/late/mcp/chatgpt_tools.py index 88e02d29..309b6bdf 100644 --- a/src/late/mcp/chatgpt_tools.py +++ b/src/late/mcp/chatgpt_tools.py @@ -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 diff --git a/src/late/mcp/generated_tools.py b/src/late/mcp/generated_tools.py index 3ddcb9b8..df314461 100644 --- a/src/late/mcp/generated_tools.py +++ b/src/late/mcp/generated_tools.py @@ -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( @@ -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: @@ -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( @@ -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( diff --git a/src/late/mcp/middleware/missing_scope.py b/src/late/mcp/middleware/missing_scope.py index a060647e..cf0f42ab 100644 --- a/src/late/mcp/middleware/missing_scope.py +++ b/src/late/mcp/middleware/missing_scope.py @@ -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.""" @@ -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, ) diff --git a/tests/test_chatgpt_generated_tools.py b/tests/test_chatgpt_generated_tools.py index 0ea2fa77..cc12502d 100644 --- a/tests/test_chatgpt_generated_tools.py +++ b/tests/test_chatgpt_generated_tools.py @@ -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", @@ -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 @@ -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):