From e03e85ae9361fa91330963d72a084cb3ef63b7cd Mon Sep 17 00:00:00 2001 From: S2D2 Date: Tue, 10 Nov 2026 20:46:31 +0000 Subject: [PATCH 1/3] fix: keep malformed arguments and account entries inside the error hierarchy A tool call whose arguments dict has a non-string key reached header sanitising and failed with an AttributeError on key.startswith(). It is not a JSON object, so it now raises ToolArgumentsError with the existing message, before any request. Account discovery read each GET /accounts entry with .get() and took any truthy id, so a non-dict entry raised AttributeError and a numeric id became an account id. fetch_accounts() already skips such entries when it records end users and providers; discovery now does the same. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com> --- stackone_ai/tools.py | 3 ++- stackone_ai/toolset.py | 14 +++++++++----- tests/test_fetch_tools.py | 15 +++++++++++++++ tests/test_models.py | 6 ++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/stackone_ai/tools.py b/stackone_ai/tools.py index 486b6aa..4f04369 100644 --- a/stackone_ai/tools.py +++ b/stackone_ai/tools.py @@ -1072,7 +1072,8 @@ def _parse_arguments(self, arguments: str | JsonDict | None) -> JsonDict: ) from exc else: parsed = arguments - if not isinstance(parsed, dict): + # A non-string key is not JSON either, and header sanitising would crash on it. + if not isinstance(parsed, dict) or not all(isinstance(key, str) for key in parsed): raise ToolArgumentsError(f"Tool arguments for {_json_text(self.name)} must be a JSON object") return dict(parsed) diff --git a/stackone_ai/toolset.py b/stackone_ai/toolset.py index 0780659..2987f95 100644 --- a/stackone_ai/toolset.py +++ b/stackone_ai/toolset.py @@ -1108,15 +1108,21 @@ def _discover_account_ids(self) -> list[str]: def _fetch_active_account_ids(self) -> list[str]: generation = self._cache_generation accounts = self.fetch_accounts() + # Skipped the way fetch_accounts() skips them when recording end users and providers. + records = [a for a in accounts if isinstance(a, dict)] - active = [a["id"] for a in accounts if a.get("status") == "active" and a.get("id")] + active = [ + a["id"] + for a in records + if a.get("status") == "active" and isinstance(a.get("id"), str) and a["id"] + ] if not active: if not accounts: raise ToolsetConfigError( "This API key has no linked accounts. Link one in the StackOne " "dashboard, or pass an account id explicitly." ) - listed = ", ".join(f"{a.get('provider')} ({a.get('status')})" for a in accounts) + listed = ", ".join(f"{a.get('provider')} ({a.get('status')})" for a in records) raise ToolsetConfigError( f"None of this API key's {len(accounts)} linked accounts are active: {listed}. " "Re-link them in the StackOne dashboard, or pass an account id explicitly." @@ -1125,9 +1131,7 @@ def _fetch_active_account_ids(self) -> list[str]: # Each non-shared account belongs to a single end user: discovering it would put one # end user's tools in front of whoever uses this toolset. if not self._include_non_shared: - skipped = sorted( - {a["id"] for a in accounts if a.get("id") in active and a.get("shared") is False} - ) + skipped = sorted({a["id"] for a in records if a.get("id") in active and a.get("shared") is False}) if skipped and len(skipped) == len(set(active)): raise ToolsetConfigError( f"None of this API key's {len(skipped)} active account(s) are shared: each " diff --git a/tests/test_fetch_tools.py b/tests/test_fetch_tools.py index e849270..a020ebe 100644 --- a/tests/test_fetch_tools.py +++ b/tests/test_fetch_tools.py @@ -944,6 +944,21 @@ def test_inactive_accounts_are_skipped(self, mcp_mock_server: str): toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server) assert toolset._discover_account_ids() == ["default"] + def test_malformed_account_entries_are_skipped(self, monkeypatch): + """A non-dict entry or a non-string id failed with an AttributeError, or became an id.""" + toolset = StackOneToolSet(api_key="test-key") + listed: list[Any] = ["junk", None, {"id": 5, "status": "active"}] + monkeypatch.setattr( + toolset, "fetch_accounts", lambda: [*listed, {"id": "acc", "provider": "p", "status": "active"}] + ) + assert toolset._discover_account_ids() == ["acc"] + + def test_only_malformed_account_entries_say_none_are_active(self, monkeypatch): + toolset = StackOneToolSet(api_key="test-key") + monkeypatch.setattr(toolset, "fetch_accounts", lambda: ["junk", {"id": 5, "status": "active"}]) + with pytest.raises(ToolsetConfigError, match="None of this API key's 2 linked accounts are active"): + toolset._discover_account_ids() + def test_a_discovery_in_flight_during_a_clear_is_not_cached(self, monkeypatch): """An account list fetched before clear_catalog_cache() must not outlive it.""" toolset = StackOneToolSet(api_key="test-key") diff --git a/tests/test_models.py b/tests/test_models.py index db28134..b77c0d0 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -12,6 +12,7 @@ ExecuteConfig, StackOneAPIError, StackOneError, + ToolArgumentsError, ToolParameters, ) @@ -329,6 +330,11 @@ def test_non_dict_arguments(self): with pytest.raises(ValueError, match=r'^Tool arguments for "[^"]+" must be a JSON object$'): _mcp_tool().execute("[1, 2, 3]") + def test_non_string_keys_are_not_a_json_object(self): + """A key JSON cannot name failed in header sanitising with an AttributeError.""" + with pytest.raises(ToolArgumentsError, match=r'^Tool arguments for "[^"]+" must be a JSON object$'): + _mcp_tool().execute({1: "x"}) # type: ignore[dict-item] + @given(invalid_json=invalid_json_strategy) @settings(max_examples=50) def test_invalid_json_arguments_pbt(self, invalid_json: str): From cd284d56cf9934fef75c6e1a31708a3cc6cd5260 Mon Sep 17 00:00:00 2001 From: S2D2 Date: Wed, 11 Nov 2026 00:34:55 +0000 Subject: [PATCH 2/3] refactor(tests): state the malformed-input cases as present-tense contracts The two docstrings narrated the old failure ("failed with an AttributeError"). They now say what the code guarantees. No behaviour change. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com> --- tests/test_fetch_tools.py | 2 +- tests/test_models.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_fetch_tools.py b/tests/test_fetch_tools.py index a020ebe..5af6df0 100644 --- a/tests/test_fetch_tools.py +++ b/tests/test_fetch_tools.py @@ -945,7 +945,7 @@ def test_inactive_accounts_are_skipped(self, mcp_mock_server: str): assert toolset._discover_account_ids() == ["default"] def test_malformed_account_entries_are_skipped(self, monkeypatch): - """A non-dict entry or a non-string id failed with an AttributeError, or became an id.""" + """An entry that is not an object, or whose id is not a string, is not an account.""" toolset = StackOneToolSet(api_key="test-key") listed: list[Any] = ["junk", None, {"id": 5, "status": "active"}] monkeypatch.setattr( diff --git a/tests/test_models.py b/tests/test_models.py index b77c0d0..1971d21 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -331,7 +331,7 @@ def test_non_dict_arguments(self): _mcp_tool().execute("[1, 2, 3]") def test_non_string_keys_are_not_a_json_object(self): - """A key JSON cannot name failed in header sanitising with an AttributeError.""" + """Header sanitising calls string methods on every key, so a non-string key is refused up front.""" with pytest.raises(ToolArgumentsError, match=r'^Tool arguments for "[^"]+" must be a JSON object$'): _mcp_tool().execute({1: "x"}) # type: ignore[dict-item] From f27f68324dd46b6e7a6d2d5d35441ffd3aefde22 Mon Sep 17 00:00:00 2001 From: S2D2 Date: Wed, 11 Nov 2026 00:48:51 +0000 Subject: [PATCH 3/3] fix: count only account records and name the string-key rule A list of entries that are not objects now reads as no linked accounts, and the inactive count leaves those entries out. toolset.execute() refuses non-string argument keys with ToolsetConfigError, as its docstring promises, rather than letting the inner execute tool raise under its own name, and the error says the keys must be strings. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com> --- stackone_ai/tools.py | 6 ++++-- stackone_ai/toolset.py | 6 ++++-- tests/test_fetch_tools.py | 18 +++++++++++++++--- tests/test_models.py | 4 ++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/stackone_ai/tools.py b/stackone_ai/tools.py index 4f04369..779bf93 100644 --- a/stackone_ai/tools.py +++ b/stackone_ai/tools.py @@ -1072,9 +1072,11 @@ def _parse_arguments(self, arguments: str | JsonDict | None) -> JsonDict: ) from exc else: parsed = arguments - # A non-string key is not JSON either, and header sanitising would crash on it. - if not isinstance(parsed, dict) or not all(isinstance(key, str) for key in parsed): + if not isinstance(parsed, dict): raise ToolArgumentsError(f"Tool arguments for {_json_text(self.name)} must be a JSON object") + # Header sanitising calls string methods on every top-level key. + if not all(isinstance(key, str) for key in parsed): + raise ToolArgumentsError(f"Tool arguments for {_json_text(self.name)} must have string keys") return dict(parsed) def _declared_headers(self) -> tuple[set[str] | None, set[str], bool]: diff --git a/stackone_ai/toolset.py b/stackone_ai/toolset.py index 2987f95..403883f 100644 --- a/stackone_ai/toolset.py +++ b/stackone_ai/toolset.py @@ -746,6 +746,8 @@ def execute( raise ToolsetConfigError(f"action_id must be a non-empty string, got {_json_text(action_id)}") if arguments is not None and not isinstance(arguments, dict): raise ToolsetConfigError(f"arguments must be a JSON object, got {_json_type(arguments)}") + if arguments is not None and not all(isinstance(key, str) for key in arguments): + raise ToolsetConfigError("arguments must have string keys") if session_id is not None and (not isinstance(session_id, str) or not session_id): raise ToolsetConfigError(f"session_id must be a non-empty string, got {_json_text(session_id)}") @@ -1117,14 +1119,14 @@ def _fetch_active_account_ids(self) -> list[str]: if a.get("status") == "active" and isinstance(a.get("id"), str) and a["id"] ] if not active: - if not accounts: + if not records: raise ToolsetConfigError( "This API key has no linked accounts. Link one in the StackOne " "dashboard, or pass an account id explicitly." ) listed = ", ".join(f"{a.get('provider')} ({a.get('status')})" for a in records) raise ToolsetConfigError( - f"None of this API key's {len(accounts)} linked accounts are active: {listed}. " + f"None of this API key's {len(records)} linked accounts are active: {listed}. " "Re-link them in the StackOne dashboard, or pass an account id explicitly." ) diff --git a/tests/test_fetch_tools.py b/tests/test_fetch_tools.py index 5af6df0..b9e1de7 100644 --- a/tests/test_fetch_tools.py +++ b/tests/test_fetch_tools.py @@ -953,10 +953,20 @@ def test_malformed_account_entries_are_skipped(self, monkeypatch): ) assert toolset._discover_account_ids() == ["acc"] - def test_only_malformed_account_entries_say_none_are_active(self, monkeypatch): + def test_only_malformed_account_entries_count_as_no_accounts(self, monkeypatch): toolset = StackOneToolSet(api_key="test-key") - monkeypatch.setattr(toolset, "fetch_accounts", lambda: ["junk", {"id": 5, "status": "active"}]) - with pytest.raises(ToolsetConfigError, match="None of this API key's 2 linked accounts are active"): + monkeypatch.setattr(toolset, "fetch_accounts", lambda: ["junk", None]) + with pytest.raises(ToolsetConfigError, match="This API key has no linked accounts"): + toolset._discover_account_ids() + + def test_a_malformed_entry_is_not_counted_as_an_inactive_account(self, monkeypatch): + toolset = StackOneToolSet(api_key="test-key") + monkeypatch.setattr( + toolset, "fetch_accounts", lambda: ["junk", {"provider": "p", "status": "revoked"}] + ) + with pytest.raises( + ToolsetConfigError, match=r"None of this API key's 1 linked accounts are active: p \(revoked\)\." + ): toolset._discover_account_ids() def test_a_discovery_in_flight_during_a_clear_is_not_cached(self, monkeypatch): @@ -1745,6 +1755,8 @@ def explode(*_args, **_kwargs): toolset = StackOneToolSet(api_key="test-key", account_id="acc1") with pytest.raises(ToolsetConfigError, match="JSON object"): toolset.execute("linear_list_issues", [1, 2, 3]) + with pytest.raises(ToolsetConfigError, match="string keys"): + toolset.execute("linear_list_issues", {1: "x"}) # type: ignore[dict-item] def test_fetch_accounts_rejects_a_non_list_body(self, monkeypatch): """list(dict) yields the keys, which blew up much later as an AttributeError.""" diff --git a/tests/test_models.py b/tests/test_models.py index 1971d21..19a3fb9 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -330,9 +330,9 @@ def test_non_dict_arguments(self): with pytest.raises(ValueError, match=r'^Tool arguments for "[^"]+" must be a JSON object$'): _mcp_tool().execute("[1, 2, 3]") - def test_non_string_keys_are_not_a_json_object(self): + def test_non_string_keys_are_refused(self): """Header sanitising calls string methods on every key, so a non-string key is refused up front.""" - with pytest.raises(ToolArgumentsError, match=r'^Tool arguments for "[^"]+" must be a JSON object$'): + with pytest.raises(ToolArgumentsError, match=r'^Tool arguments for "[^"]+" must have string keys$'): _mcp_tool().execute({1: "x"}) # type: ignore[dict-item] @given(invalid_json=invalid_json_strategy)