From 8a1873f7c1019931faaf0f38aadd268159bae8fd Mon Sep 17 00:00:00 2001 From: Oscar Perez Date: Tue, 24 Nov 2026 12:21:16 +0100 Subject: [PATCH] fix(mcp): ChatGPT links missing a tool's write scope are told to reconnect Key the reconnect steps on the tool's first declared scope instead of on all its x-oauth-scopes, so a link holding accounts:read but not accounts:write gets the steps when the API refuses a profile or account write. A :write scope still counts as holding its :read. Co-Authored-By: Claude Opus 5.5 --- src/late/mcp/middleware/missing_scope.py | 15 +++++- tests/test_chatgpt_generated_tools.py | 58 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/late/mcp/middleware/missing_scope.py b/src/late/mcp/middleware/missing_scope.py index 73314ab6..2ae4b0ab 100644 --- a/src/late/mcp/middleware/missing_scope.py +++ b/src/late/mcp/middleware/missing_scope.py @@ -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 @@ -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", } @@ -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) @@ -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], diff --git a/tests/test_chatgpt_generated_tools.py b/tests/test_chatgpt_generated_tools.py index 8a41a1aa..e6eb5202 100644 --- a/tests/test_chatgpt_generated_tools.py +++ b/tests/test_chatgpt_generated_tools.py @@ -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",