-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleast_privilege_tool_scope.py
More file actions
80 lines (60 loc) · 2.47 KB
/
Copy pathleast_privilege_tool_scope.py
File metadata and controls
80 lines (60 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from langchain_anthropic import ChatAnthropic
from typing import TypedDict, List
class ScopedToolServer:
def __init__(self, allowed_tools: List[str]):
self.allowed_tools = allowed_tools
def call(self, tool: str, arg: str) -> str:
if tool not in self.allowed_tools:
raise PermissionError(
f"Tool '{tool}' is out of scope. Allowed: {self.allowed_tools}"
)
return f"[{tool}] executed with arg='{arg}'"
class ResearchAgent:
def __init__(self, llm, server: ScopedToolServer):
self.llm = llm
self.server = server
def run(self, query: str) -> str:
plan = self.llm.invoke(
f"You are a research agent. Given the query '{query}', "
"reply with only: TOOL: search ARG: <short keyword>"
)
line = plan.content.strip()
parts = line.split()
tool = parts[1] if len(parts) > 1 else "search"
arg = parts[3] if len(parts) > 3 else query
return self.server.call(tool, arg)
class ExecutionAgent:
def __init__(self, llm, server: ScopedToolServer):
self.llm = llm
self.server = server
def run(self, task: str) -> str:
plan = self.llm.invoke(
f"You are an execution agent. Given the task '{task}', "
"reply with only: TOOL: write ARG: <short description>"
)
line = plan.content.strip()
parts = line.split()
tool = parts[1] if len(parts) > 1 else "write"
arg = parts[3] if len(parts) > 3 else task
return self.server.call(tool, arg)
def main():
llm = ChatAnthropic(model="claude-haiku-4-5-20251001")
research_server = ScopedToolServer(["search", "read"])
execution_server = ScopedToolServer(["write", "run"])
research_agent = ResearchAgent(llm, research_server)
execution_agent = ExecutionAgent(llm, execution_server)
print("=== Least-Privilege Tool Scope Demo ===\n")
result = research_agent.run("latest AI papers")
print(f"Research agent (search): {result}")
try:
research_server.call("write", "malicious output")
except PermissionError as e:
print(f"Research agent blocked from 'write': {e}")
result = execution_agent.run("save report to disk")
print(f"Execution agent (write): {result}")
try:
execution_server.call("search", "sensitive data")
except PermissionError as e:
print(f"Execution agent blocked from 'search': {e}")
if __name__ == "__main__":
main()