-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
234 lines (200 loc) · 8.04 KB
/
Copy pathclient.py
File metadata and controls
234 lines (200 loc) · 8.04 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Claude SDK Client Configuration
===============================
Functions for creating and configuring the Claude Agent SDK client.
"""
import json
import os
import sys
from pathlib import Path
from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
from claude_agent_sdk.types import HookMatcher
from security import bash_security_hook
def get_project_mcp_servers(project_dir: Path) -> dict:
"""
Load project-specific MCP server configuration.
Looks for .claude/mcp_servers.json in the project directory.
Returns empty dict if file doesn't exist or is invalid.
Args:
project_dir: The project directory to check
Returns:
Dictionary of MCP server configurations
"""
mcp_config_path = project_dir / ".claude" / "mcp_servers.json"
if not mcp_config_path.exists():
return {}
try:
with open(mcp_config_path, "r") as f:
config = json.load(f)
# Set cwd for each server to project directory (needed for artisan)
for server_name, server_config in config.items():
if "cwd" not in server_config:
server_config["cwd"] = str(project_dir.resolve())
print(f"[MCP] Loaded project MCP servers: {list(config.keys())}")
return config
except (json.JSONDecodeError, OSError) as e:
print(f"[MCP] Warning: Could not load MCP config from {mcp_config_path}: {e}")
return {}
# Feature MCP tools for feature/test management
FEATURE_MCP_TOOLS = [
"mcp__features__feature_get_stats",
"mcp__features__feature_get_next",
"mcp__features__feature_get_for_regression",
"mcp__features__feature_mark_passing",
"mcp__features__feature_skip",
"mcp__features__feature_create_bulk",
# Enhancement discovery tools
"mcp__features__feature_get_all_categories",
"mcp__features__feature_get_summary",
"mcp__features__feature_search",
]
# Laravel Boost MCP tools for Laravel development (all 16 tools)
LARAVEL_BOOST_TOOLS = [
"mcp__laravel-boost__application-info",
"mcp__laravel-boost__browser-logs",
"mcp__laravel-boost__database-connections",
"mcp__laravel-boost__database-query",
"mcp__laravel-boost__database-schema",
"mcp__laravel-boost__get-absolute-url",
"mcp__laravel-boost__get-config",
"mcp__laravel-boost__last-error",
"mcp__laravel-boost__list-artisan-commands",
"mcp__laravel-boost__list-available-config-keys",
"mcp__laravel-boost__list-available-env-vars",
"mcp__laravel-boost__list-routes",
"mcp__laravel-boost__read-log-entries",
"mcp__laravel-boost__report-feedback",
"mcp__laravel-boost__search-docs",
"mcp__laravel-boost__tinker",
]
# Playwright MCP tools for browser automation
PLAYWRIGHT_TOOLS = [
# Core navigation & screenshots
"mcp__playwright__browser_navigate",
"mcp__playwright__browser_navigate_back",
"mcp__playwright__browser_take_screenshot",
"mcp__playwright__browser_snapshot",
# Element interaction
"mcp__playwright__browser_click",
"mcp__playwright__browser_type",
"mcp__playwright__browser_fill_form",
"mcp__playwright__browser_select_option",
"mcp__playwright__browser_hover",
"mcp__playwright__browser_drag",
"mcp__playwright__browser_press_key",
# JavaScript & debugging
"mcp__playwright__browser_evaluate",
"mcp__playwright__browser_run_code",
"mcp__playwright__browser_console_messages",
"mcp__playwright__browser_network_requests",
# Browser management
"mcp__playwright__browser_close",
"mcp__playwright__browser_resize",
"mcp__playwright__browser_tabs",
"mcp__playwright__browser_wait_for",
"mcp__playwright__browser_handle_dialog",
"mcp__playwright__browser_file_upload",
"mcp__playwright__browser_install",
]
# Built-in tools
BUILTIN_TOOLS = [
"Read",
"Write",
"Edit",
"Glob",
"Grep",
"Bash",
]
def create_client(project_dir: Path, model: str):
"""
Create a Claude Agent SDK client with multi-layered security.
Args:
project_dir: Directory for the project
model: Claude model to use
Returns:
Configured ClaudeSDKClient (from claude_agent_sdk)
Security layers (defense in depth):
1. Sandbox - OS-level bash command isolation prevents filesystem escape
2. Permissions - File operations restricted to project_dir only
3. Security hooks - Bash commands validated against an allowlist
(see security.py for ALLOWED_COMMANDS)
Note: Authentication is handled by start.bat/start.sh before this runs.
The Claude SDK auto-detects credentials from ~/.claude/.credentials.json
"""
# Create comprehensive security settings
# Note: Using relative paths ("./**") restricts access to project directory
# since cwd is set to project_dir
security_settings = {
"sandbox": {"enabled": True, "autoAllowBashIfSandboxed": True},
"permissions": {
"defaultMode": "acceptEdits", # Auto-approve edits within allowed directories
"allow": [
# Allow all file operations within the project directory
"Read(./**)",
"Write(./**)",
"Edit(./**)",
"Glob(./**)",
"Grep(./**)",
# Bash permission granted here, but actual commands are validated
# by the bash_security_hook (see security.py for allowed commands)
"Bash(*)",
# Allow Playwright MCP tools for browser automation
*PLAYWRIGHT_TOOLS,
# Allow Feature MCP tools for feature management
*FEATURE_MCP_TOOLS,
# Allow Laravel Boost MCP tools for Laravel development
*LARAVEL_BOOST_TOOLS,
],
},
}
# Ensure project directory exists before creating settings file
project_dir.mkdir(parents=True, exist_ok=True)
# Write settings to a file in the project directory
settings_file = project_dir / ".claude_settings.json"
with open(settings_file, "w") as f:
json.dump(security_settings, f, indent=2)
# Base MCP servers (always included)
base_mcp_servers = {
"playwright": {"command": "npx", "args": ["@playwright/mcp@latest", "--viewport-size", "1280x720"]},
"features": {
"command": sys.executable, # Use the same Python that's running this script
"args": ["-m", "mcp_server.feature_mcp"],
"env": {
"PROJECT_DIR": str(project_dir.resolve()),
"PYTHONPATH": str(Path(__file__).parent.resolve()),
},
},
}
# Load and merge project-specific MCP servers (e.g., Laravel Boost)
project_mcp_servers = get_project_mcp_servers(project_dir)
mcp_servers = {**base_mcp_servers, **project_mcp_servers}
print(f"Created security settings at {settings_file}")
print(" - Sandbox enabled (OS-level bash isolation)")
print(f" - Filesystem restricted to: {project_dir.resolve()}")
print(" - Bash commands restricted to allowlist (see security.py)")
print(f" - MCP servers: {', '.join(mcp_servers.keys())}")
print(" - Project settings enabled (skills, commands, CLAUDE.md)")
print()
return ClaudeSDKClient(
options=ClaudeAgentOptions(
model=model,
system_prompt="You are an expert full-stack developer building a production-quality web application.",
setting_sources=["project"], # Enable skills, commands, and CLAUDE.md from project dir
max_buffer_size=10 * 1024 * 1024, # 10MB for large Playwright screenshots
allowed_tools=[
*BUILTIN_TOOLS,
*PLAYWRIGHT_TOOLS,
*FEATURE_MCP_TOOLS,
*LARAVEL_BOOST_TOOLS,
],
mcp_servers=mcp_servers,
hooks={
"PreToolUse": [
HookMatcher(matcher="Bash", hooks=[bash_security_hook]),
],
},
max_turns=1000,
cwd=str(project_dir.resolve()),
settings=str(settings_file.resolve()), # Use absolute path
)
)