Coverage for mcp/resources/iam_policies.py: 88%

20 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-15 15:07 +0000

1"""IAM policy resources (iam:// scheme) for the GCO MCP server.""" 

2 

3from pathlib import Path 

4 

5from server import mcp 

6 

7PROJECT_ROOT = Path(__file__).parent.parent.parent 

8IAM_POLICIES_DIR = PROJECT_ROOT / "docs" / "iam-policies" 

9 

10 

11@mcp.resource("iam://gco/policies/index") 

12def iam_policies_index() -> str: 

13 """List available IAM policy templates for GCO access control.""" 

14 lines = ["# IAM Policy Templates\n"] 

15 for f in sorted(IAM_POLICIES_DIR.glob("*.json")): 

16 lines.append(f"- `iam://gco/policies/{f.name}` — {f.stem}") 

17 readme = IAM_POLICIES_DIR / "README.md" 

18 if readme.is_file(): 18 ↛ 20line 18 didn't jump to line 20 because the condition on line 18 was always true

19 lines.append("\n- `iam://gco/policies/README.md` — policy documentation") 

20 return "\n".join(lines) 

21 

22 

23@mcp.resource("iam://gco/policies/{filename}") 

24def iam_policy_resource(filename: str) -> str: 

25 """Read an IAM policy template.""" 

26 path = IAM_POLICIES_DIR / filename 

27 if not path.is_file(): 27 ↛ 30line 27 didn't jump to line 30 because the condition on line 27 was always true

28 available = sorted(f.name for f in IAM_POLICIES_DIR.glob("*") if f.is_file()) 

29 return f"Policy '{filename}' not found. Available:\n" + "\n".join(available) 

30 return path.read_text()