Coverage for mt_mcp/scopes.py: 100.00%

18 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 14:44 +0000

1"""Which tools a caller may see and call: role and client → tool tiers (SB-1302). 

2 

3Pure functions, no I/O. The tier filter is what `tools/list` shows and what 

4`tools/call` admits; it is not the only check. Tools that act on one team re-check 

5that team with auth.py (`can_manage_team`, `can_edit_match`) on every call. 

6""" 

7 

8from typing import Literal 

9 

10Tier = Literal["read", "my", "self", "team_write", "admin"] 

11 

12ALL_TIERS: frozenset[Tier] = frozenset({"read", "my", "self", "team_write", "admin"}) 

13 

14# Canonical role strings, as user_profiles stores them: club_manager is the 

15# underscore form, the others are hyphenated (see auth.INVITE_TYPE_TO_ROLE). 

16ROLE_TIERS: dict[str, frozenset[Tier]] = { 

17 "admin": ALL_TIERS, 

18 "club_manager": frozenset({"read", "my", "team_write"}), 

19 "team-manager": frozenset({"read", "my", "team_write"}), 

20 "team-player": frozenset({"read", "my", "self"}), 

21 "team-fan": frozenset({"read", "my"}), 

22 "club-fan": frozenset({"read", "my"}), 

23} 

24 

25# A role this table does not know (the legacy default "user", or anything new) 

26# can read and no more. 

27DEFAULT_TIERS: frozenset[Tier] = frozenset({"read"}) 

28 

29# Clients that ask for less than the caller's role allows. MT AI never gets 

30# writes or admin, whatever the role (mt2/ai.md "Auth"). 

31MT_AI_CLIENT = "mt-ai" 

32# sb-agents' mt-agent (SB-1493) answers Slack/Telegram users with no MT identity 

33# through a public API account: public data only, whatever that account's role. 

34SB_AGENTS_CLIENT = "sb-agents" 

35# The same mt-agent answering a signed-in MT user with a delegated token (SB-1529). 

36SB_AGENTS_USER_CLIENT = "sb-agents-user" 

37CLIENT_CAPS: dict[str, frozenset[Tier]] = { 

38 MT_AI_CLIENT: frozenset({"read", "my", "self"}), 

39 SB_AGENTS_CLIENT: frozenset({"read"}), 

40 SB_AGENTS_USER_CLIENT: frozenset({"read", "my", "self"}), 

41} 

42 

43# A delegated token's cap, whatever client header comes with it: the token itself 

44# says "sb-agents acting for this person", so dropping the header gains nothing. 

45DELEGATED_CAP: frozenset[Tier] = frozenset({"read", "my", "self"}) 

46 

47CLIENT_HEADER = "x-mt-client" 

48 

49# Clients named in metrics and logs. Naming one grants nothing; only CLIENT_CAPS 

50# changes what a client may use. 

51KNOWN_CLIENTS = frozenset({MT_AI_CLIENT, SB_AGENTS_CLIENT, SB_AGENTS_USER_CLIENT, "claude-code"}) 

52 

53 

54def allowed_tiers(role: str | None, client: str | None, delegated: bool = False) -> frozenset[Tier]: 

55 """The tiers a caller with this role, using this client, may use. 

56 

57 An unknown client name is treated as no cap: the header only ever narrows, 

58 so a caller gains nothing by inventing one. 

59 """ 

60 tiers = ROLE_TIERS.get(role or "", DEFAULT_TIERS) 

61 if delegated: 

62 tiers = tiers & DELEGATED_CAP 

63 cap = CLIENT_CAPS.get((client or "").strip().lower()) 

64 return tiers & cap if cap is not None else tiers