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
63 changes: 53 additions & 10 deletions src/late/mcp/chatgpt_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
# 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).
# the raw request logs, a report reader whose creator is a write (listed with
# it in wave 4), and LinkedIn reactors (third-party personal data).
WAVE_2 = (
"lead_gen_list_leads",
"broadcasts_get_broadcast",
Expand Down Expand Up @@ -162,8 +162,7 @@
# broadcasts, managed messaging). Held back: creating and activating a
# workflow (the API checks a workflow's sending nodes against API keys only,
# so an OAuth link without messaging:write could install them) and updating a
# contact (its only scope, automations:write, also opens webhooks). Deletes and
# ad spend are a later wave.
# contact (its only scope, automations:write, also opens webhooks).
WAVE_3_EFFECTS = {
"messages_send_inbox_message": _REACHES_OUT,
"comments_send_private_reply_to_comment": _REACHES_OUT,
Expand All @@ -186,12 +185,27 @@
}
WAVE_3 = tuple(WAVE_3_EFFECTS)

CHATGPT_GENERATED_TOOLS = WAVE_1 + WAVE_2 + WAVE_3
WRITE_EFFECTS = WAVE_3_EFFECTS
# Spend and deletes, all destructive: creating an ad and pausing or resuming a
# campaign move real ad budget, disconnecting an account and hiding or deleting
# a comment remove what is there, and the insights report is marked destructive
# on the product owner's call. Its reader rides along so a report can be read.
WAVE_4_EFFECTS = {
"ad_campaigns_create_standalone_ad": _OVERWRITES_IN_PUBLIC,
"ad_campaigns_update_ad_campaign_status": _OVERWRITES_IN_PUBLIC,
"accounts_delete_account": _OVERWRITES,
"ad_insights_create_ad_insights_report": _OVERWRITES,
"comments_hide_inbox_comment": _OVERWRITES_IN_PUBLIC,
"comments_delete_inbox_comment": _OVERWRITES_IN_PUBLIC,
}
WAVE_4 = (*WAVE_4_EFFECTS, "ad_insights_get_ad_insights_report")

CHATGPT_GENERATED_TOOLS = WAVE_1 + WAVE_2 + WAVE_3 + WAVE_4
WRITE_EFFECTS = WAVE_3_EFFECTS | WAVE_4_EFFECTS

# OpenAI's scan held these writes on their generated text alone ("needs
# further review"); each lead says who the call reaches and when nothing goes
# out yet, which the generated summary leaves implicit.
# Each lead says who a write reaches, what it spends or removes, and when
# nothing goes out yet, which the generated summary leaves implicit. OpenAI's
# scan held the first three on the generated text alone ("needs further
# review").
DESCRIPTION_LEADS = {
"broadcasts_create_broadcast": (
"Creates a draft only: nothing is sent to anyone. Sending is a separate "
Expand All @@ -209,9 +223,34 @@
"message or call that person, or update the workflow's webhooks and CRM, "
"so confirm with the user before calling."
),
"ad_campaigns_create_standalone_ad": (
"Spends the user's ad budget: anything the call does not create PAUSED "
"(status, or campaign_status / ad_set_status / ad_status) goes live and "
"starts spending once the platform approves it. Confirm the ad account, "
"budget, schedule, targeting and creative with the user before calling; "
"validate_only (Meta, TikTok, ChatGPT ads, Google Performance Max and "
"Demand Gen) checks it first without creating anything."
),
"ad_campaigns_update_ad_campaign_status": (
"Resuming restarts spending the user's ad budget and pausing stops "
"delivery (Trade Desk: pause only). Confirm the campaign and the new "
"status with the user before calling."
),
"accounts_delete_account": (
"Disconnects the social account: its analytics history is deleted at once, "
"and after a 1-hour grace window the account and its scheduled posts are "
"removed for good (reconnecting it at zernio.com within that hour restores "
"them). Confirm the account with the user before calling."
),
}


# In a live test ChatGPT passed the user ID from get_profile as profile_id and
# got a 404; the generated docs never say where the ID comes from.
_ARGUMENT_SOURCES = {
"profile_id": "A Zernio profile ID from profiles_list_profiles, not the user ID.",
}

_SUMMARY_MAX = 400
_PARAM_MAX = 100
_ARGS_HEADING = re.compile(r"\n[ \t]*Args:\n")
Expand All @@ -230,12 +269,16 @@ def _argument_lines(args: str) -> list[str]:
return []
indent = matches[0][1]
return [
f" {m[2]}: {_clip(m[3] or '', _PARAM_MAX)}".rstrip(": ")
f" {m[2]}: {_with_source(m[2], _clip(m[3] or '', _PARAM_MAX))}".rstrip(": ")
for m in matches
if m[1] == indent
]


def _with_source(argument: str, doc: str) -> str:
return " ".join(part for part in (_ARGUMENT_SOURCES.get(argument), doc) if part)


def concise_description(doc: str) -> str:
"""A generated tool's docstring cut to its first paragraph and one line per argument.

Expand Down
1 change: 1 addition & 0 deletions src/late/mcp/middleware/missing_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ads:read": "read your ad accounts, campaigns and results",
"contacts:read": "read your contacts and leads",
"messaging:write": "send messages, replies and broadcasts for you",
"ads:write": "create ads and pause or resume campaigns, which spends your ad budget",
}


Expand Down
30 changes: 19 additions & 11 deletions tests/test_chatgpt_generated_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
DESCRIPTION_LEADS,
WAVE_1,
WAVE_2,
WAVE_3,
WRITE_EFFECTS,
_hints_for,
concise_description,
Expand Down Expand Up @@ -113,6 +112,10 @@ async def test_every_generated_tool_has_explicit_hints_and_its_oauth_scope():
("workflows_trigger_api_call_workflow", True, True),
("posts_edit_post", True, True),
("accounts_reply_to_google_business_review", True, True),
("ad_campaigns_create_standalone_ad", True, True),
("ad_campaigns_update_ad_campaign_status", True, True),
("accounts_delete_account", True, False),
("comments_delete_inbox_comment", True, True),
],
)
async def test_a_write_says_whether_it_overwrites_and_whether_it_reaches_out(
Expand All @@ -134,7 +137,7 @@ async def test_every_listed_write_has_its_effects_spelled_out():
if not listed[name]["annotations"]["readOnlyHint"]
}

assert writes == set(WAVE_3)
assert writes == set(WRITE_EFFECTS)
for name in writes:
hints = listed[name]["annotations"]
effects = WRITE_EFFECTS[name]
Expand All @@ -161,14 +164,11 @@ def test_advertises_exactly_the_scopes_the_listed_tools_need():
]
assert set(CHATGPT_SCOPES) == LINK_SCOPES | needed
assert {"messaging:read", "ads:read", "contacts:read"} <= set(CHATGPT_SCOPES)
assert {"messaging:write", "accounts:write"} <= set(CHATGPT_SCOPES)
# automations:write also opens webhooks; ads:write and money:write spend.
assert not {
"automations:write",
"ads:write",
"money:write",
"commerce:write",
} & set(CHATGPT_SCOPES)
assert {"messaging:write", "accounts:write", "ads:write"} <= set(CHATGPT_SCOPES)
# automations:write also opens webhooks; money:write moves balance.
assert not {"automations:write", "money:write", "commerce:write"} & set(
CHATGPT_SCOPES
)


async def test_a_held_write_says_who_it_reaches_right_after_its_title():
Expand All @@ -181,6 +181,14 @@ async def test_a_held_write_says_who_it_reaches_right_after_its_title():
assert DESCRIPTION_LEADS.keys() <= set(CHATGPT_GENERATED_TOOLS)


async def test_profile_id_says_where_the_id_comes_from():
listed = await _listed()

description = listed["comment_automations_create_comment_automation"]["description"]

assert "profile_id: A Zernio profile ID from profiles_list_profiles" in description


def test_a_write_listed_without_its_effects_is_refused():
generated = ToolAnnotations(readOnlyHint=False, destructiveHint=True)

Expand All @@ -191,7 +199,7 @@ def test_a_write_listed_without_its_effects_is_refused():
async def test_the_whole_listing_stays_within_budget():
listed = await _listed()

assert len(json.dumps(list(listed.values()))) < 300_000
assert len(json.dumps(list(listed.values()))) < 340_000


def test_a_description_keeps_its_first_paragraph_and_one_line_per_argument():
Expand Down
Loading