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
15 changes: 14 additions & 1 deletion src/late/mcp/middleware/missing_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
a live account on 2026-11-24). A 403 the token's scope already covers (a team
member with the group turned off, or a write scope that includes the read)
stays the API's own error: re-linking cannot fix it.

The steps key on the tool's first declared scope, not on all its x-oauth-scopes:
the spec lists the :read scope on writes too, and the API now refuses a write
to a token holding only the read, so a link with accounts:read but not
accounts:write would otherwise get a bare error.
"""

import json
Expand All @@ -28,6 +33,7 @@
"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",
"accounts:write": "create, change or remove your profiles and connected accounts",
}


Expand Down Expand Up @@ -56,6 +62,13 @@ def _granted_scopes() -> set[str] | None:
return set(scope.split()) if isinstance(scope, str) else None


def _holds(granted: set[str], needed: str) -> bool:
"""A :write scope includes its :read."""
return needed in granted or (
needed.endswith(":read") and f"{needed[: -len(':read')]}:write" in granted
)


def _refused_for_a_group(cause: BaseException | None) -> bool:
return (
isinstance(cause, LateAPIError)
Expand All @@ -81,7 +94,7 @@ async def on_call_tool(
if not reaching or not _refused_for_a_group(exc.__cause__):
raise
granted = _granted_scopes()
relink_helps = granted is not None and not granted.intersection(reaching)
relink_helps = granted is not None and not _holds(granted, reaching[0])
_log_group_refusal(
tool=context.message.name,
needed=reaching[0],
Expand Down
58 changes: 58 additions & 0 deletions tests/test_chatgpt_generated_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,64 @@ async def test_a_link_that_only_reads_messages_is_asked_to_relink_before_a_send(
assert "send messages" in text and "'messaging:write'" in text


ACCOUNTS_READ_ONLY = {
"error": "This connector token can only read the 'accounts' resource group. "
"Reconnect the integration and grant it write access to use this endpoint.",
"code": "insufficient_permissions",
"required_group": "accounts",
}


async def _create_profile(response: httpx.Response):
with respx.mock:
respx.post(url__startswith="https://api.test.com/v1/profiles").mock(
return_value=response
)
async with Client(chatgpt_mcp) as client:
return await client.call_tool(
"profiles_create_profile", {"name": "Client A"}, raise_on_error=False
)


async def test_a_link_that_only_reads_accounts_is_asked_to_relink_before_a_write(
monkeypatch,
):
# The spec lists accounts:read on the write too, which the link holds.
monkeypatch.setattr(
"late.mcp.middleware.missing_scope._granted_scopes", lambda: LINK_SCOPES
)

result = await _create_profile(httpx.Response(403, json=ACCOUNTS_READ_ONLY))

assert result.is_error
text = result.content[0].text
assert "'accounts:write'" in text
assert "Settings > Plugins" in text and "Reconnect" in text


@pytest.mark.parametrize(
"granted",
[
# The link holds the write scope: re-linking cannot fix it.
LINK_SCOPES | {"accounts:write"},
# An API key: no OAuth link to redo.
None,
],
)
async def test_a_write_refused_despite_its_scope_keeps_the_apis_own_error(
monkeypatch, granted
):
monkeypatch.setattr(
"late.mcp.middleware.missing_scope._granted_scopes", lambda: granted
)

result = await _create_profile(httpx.Response(403, json=ACCOUNTS_READ_ONLY))

assert result.is_error
assert "Settings > Plugins" not in result.content[0].text
assert "can only read the 'accounts' resource group" in result.content[0].text


async def test_each_group_refusal_logs_whether_it_asked_to_relink(monkeypatch, caplog):
token = AccessToken(
token="t",
Expand Down
Loading