forked from rhel-lightspeed/linux-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_script.py
More file actions
551 lines (466 loc) · 19.5 KB
/
run_script.py
File metadata and controls
551 lines (466 loc) · 19.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import logging
import secrets
import shlex
import typing as t
from dataclasses import asdict
from dataclasses import dataclass
from fastmcp import Context
from fastmcp.apps import AppConfig
from fastmcp.exceptions import ToolError
from fastmcp.tools import ToolResult
from mcp.types import ContentBlock
from mcp.types import TextContent
from mcp.types import ToolAnnotations
# from pydantic import BaseModel
from pydantic import BaseModel
from pydantic import Field
from linux_mcp_server.audit import log_tool_call
from linux_mcp_server.config import CONFIG
from linux_mcp_server.connection.ssh import execute_command
from linux_mcp_server.gatekeeper import check_run_script
from linux_mcp_server.gatekeeper import GatekeeperStatus
from linux_mcp_server.mcp_app import RUN_SCRIPT_APP_URI
from linux_mcp_server.mcp_app import use_mcp_app_for_client
from linux_mcp_server.server import mcp
from linux_mcp_server.utils.decorators import disallow_local_execution_in_containers
from linux_mcp_server.utils.types import Host
logger = logging.getLogger("linux-mcp-server")
ExecutionState = t.Literal[
"waiting-approval", "success", "failure", "executing", "rejected-user", "rejected-gatekeeper"
]
# This would make more sense as a StrEnum, but that generates a schema with
# the enum in $defs, which deeply confuses (at least) the combination of
# Claude Sonnet 4.5 and Claude Desktop - the model knows that it's supposed
# to use "python" or "bash" and passes it in the call, but the UI sees `null`
# instead.
#
# Future versions of FastMCP may automatically dereference references
# (https://github.com/jlowin/fastmcp/pulls/2814) but that doesn't currently
# happen as of v2.14.5 (https://github.com/jlowin/fastmcp/issues/3153).
#
ScriptType = t.Literal["python", "bash"]
SCRIPT_TYPE_PYTHON = "python"
SCRIPT_TYPE_BASH = "bash"
@dataclass()
class ScriptDetails:
state: ExecutionState
description: str
script: str
script_type: ScriptType
host: Host
readonly: bool
@property
def needs_confirmation(self) -> bool:
return not self.readonly or CONFIG.always_confirm_scripts
# TODO: Might need a cleanup mechanism to limit the maximum number of scripts we can store
class ScriptStore:
"""
Stores script execution state across the async approval lifecycle.
When a script execution is requested, approval happens asynchronously via MCP app UI.
This store maps request IDs to their execution details (script, host, state) so we can
retrieve and execute the script after user approval, even though the original request
context is no longer available. It also preserve execution state (waiting-approval, executing,
success, failure, rejected) for the MCP app UI.
"""
def __init__(self):
self._scripts: dict[str, ScriptDetails] = {}
def add_script(
self,
description: str,
script: str,
script_type: ScriptType,
host: Host,
readonly: bool,
) -> str:
"""
Add a new script to the store and generate a unique ID for it.
This method stores a script with its metadata and assigns it an initial state of
"waiting-approval". The generated ID can be used to retrieve or update the script later.
Args:
description: Human-readable description of what the script does.
script: The script content to execute (Python or Bash code).
script_type: The type of script, either "python" or "bash".
host: The target host where the script will be executed.
readonly: Whether the script only reads and does not modify the system.
Returns:
A unique URL-safe token (16 bytes) identifying this script execution.
"""
id = secrets.token_urlsafe(16)
self._scripts[id] = ScriptDetails(
state="waiting-approval",
description=description,
script=script,
script_type=script_type,
host=host,
readonly=readonly,
)
return id
def get_script_details(self, id: str) -> ScriptDetails:
"""
Retrieve the full details of a stored script by its ID.
Args:
id: The unique identifier for the script, as returned by add_script().
Returns:
A ScriptDetails object containing the script's state, content, type, and target host.
Raises:
KeyError: If no script exists with the given ID.
"""
return self._scripts[id]
def set_script_state(self, id: str, new_state: ExecutionState):
"""
Update the execution state of a stored script.
This method is used to track script lifecycle transitions, such as moving from
"waiting-approval" to "executing" to "success" or "failure".
Args:
id: The unique identifier for the script.
new_state: The new execution state. Valid states are:
- "waiting-approval": Script is pending user approval
- "executing": Script is currently running
- "success": Script completed successfully
- "failure": Script execution failed
- "rejected-user": User rejected the script
- "rejected-gatekeeper": Script was rejected by policy checks
Raises:
KeyError: If no script exists with the given ID.
"""
self._scripts[id].state = new_state
script_store = ScriptStore()
BASH_STRICT_PREAMBLE = "set -euo pipefail; "
SYSTEMD_RUN_ARGS = [
"--quiet",
"--pipe",
"--collect",
"--wait",
"--property=WorkingDirectory=/tmp",
"--property=PrivateTmp=true",
"--property=NoNewPrivileges=true",
]
SYSTEMD_RUN_READONLY_ARGS = [
"--property=ReadOnlyPaths=/",
"--property=RestrictAddressFamilies=AF_UNIX",
]
SYSTEMD_RUN_COMMAND = "/usr/bin/sudo /usr/bin/systemd-run {args}"
# Wrapper for run_script_readonly: use sudo+systemd-run when available, else run script
# directly. Template uses {script_type} and {script}; script is escaped via shlex.quote().
WRAPPER_TEMPLATE = """\
set -euo pipefail
SCRIPT={script}
if command -v sudo >/dev/null 2>&1 && command -v systemd-run >/dev/null 2>&1 && sudo -l whoami >/dev/null 2>&1; then
exec {systemd_run_command} {script_type} -c "$SCRIPT"
else
exec {script_type} -c "$SCRIPT"
fi
"""
RUN_SCRIPT_COMMON_DESCRIPTION = """\
A bash script should be used for simple operations that can be expressed cleanly
as a few shell commands, but a Python script should be used if complex processing
is needed. Bash scripts are run with strict mode (set -euo pipefail) applied by
the invocation, so handle expected non-zero exit codes in the script (e.g. with
`|| true`) where needed.
Write short, simple scripts that are easy to review - do not include unnecessary
complexity such as elaborate logging or handling unlikely corner cases.
"""
RUN_SCRIPT_INTERACTIVE_DESCRIPTION = (
"""
Run a script that modifies the system. The user will be asked for approval interactively.
"""
+ RUN_SCRIPT_COMMON_DESCRIPTION
)
class RunScriptInteractiveResult(BaseModel):
id: str
status: GatekeeperStatus
detail: str
# class UserInfo(BaseModel):
# name: str
# age: int
#
def _wrap_script(script_type: ScriptType, script: str) -> list[str]:
"""Wrap a script in a wrapper script that uses sudo+systemd-run when available, else run script directly."""
wrapper_script = WRAPPER_TEMPLATE.format(
systemd_run_command=SYSTEMD_RUN_COMMAND.format(args=" ".join(SYSTEMD_RUN_ARGS)),
script_type=script_type,
script=shlex.quote((BASH_STRICT_PREAMBLE + script) if script_type == SCRIPT_TYPE_BASH else script),
)
return ["bash", "-c", wrapper_script]
@dataclass
class ExecuteScriptResult:
state: t.Literal["success", "failure"]
output: str
@mcp.tool(
tags={"run_script", "mcp_apps_only", "hidden_from_model"},
description="Execute a script; this is only available to the our mcp-app",
app=AppConfig(visibility=["app"]),
)
@log_tool_call
@disallow_local_execution_in_containers
async def execute_script(
id: t.Annotated[str, Field(description="The associated ID of the script to be executed")],
) -> ToolResult:
script_details = script_store.get_script_details(id)
command = _wrap_script(script_details.script_type, script_details.script)
script_store.set_script_state(id, "executing")
content: list[ContentBlock] = []
try:
returncode, stdout, stderr = await execute_command(command, host=script_details.host)
except Exception:
script_store.set_script_state(id, "failure")
raise
if returncode == 0:
script_store.set_script_state(id, "success")
output = stdout if isinstance(stdout, str) else stdout.decode("utf-8", errors="replace")
content.append(TextContent(type="text", text=output))
result = ExecuteScriptResult("success", output)
else:
script_store.set_script_state(id, "failure")
output = f"Error executing script: return code {returncode}, stderr: {stderr}"
content.append(TextContent(type="text", text=output))
result = ExecuteScriptResult("failure", output)
return ToolResult(content=content, structured_content=asdict(result))
@mcp.tool(
tags={"run_script", "mcp_apps_only", "hidden_from_model"},
description="Reject a script; this is only available to the our mcp-app",
app=AppConfig(visibility=["app"]),
)
@log_tool_call
@disallow_local_execution_in_containers
async def reject_script(
id: t.Annotated[str, Field(description="The associated ID of the script to be rejected")],
):
script_store.set_script_state(id, "rejected-user")
@mcp.tool(
tags={"run_script", "mcp_apps_only"},
title="Run a script with interactive user confirmation",
description=RUN_SCRIPT_INTERACTIVE_DESCRIPTION,
annotations=ToolAnnotations(destructiveHint=True),
output_schema=RunScriptInteractiveResult.model_json_schema(),
app=AppConfig(resourceUri=RUN_SCRIPT_APP_URI),
)
@log_tool_call
@disallow_local_execution_in_containers
async def run_script_interactive(
ctx: Context,
description: t.Annotated[
str,
Field(
description="Description of what the script does - e.g. 'Modify file permissions on nginx.conf to fix startup errors.'"
),
],
script_type: t.Annotated[
ScriptType,
Field(description="The type of script to run (python or bash)."),
],
script: t.Annotated[
str,
Field(description="The script to run."),
],
readonly: t.Annotated[bool, Field(description="Should be true if the script does not modify the system.")],
token: t.Annotated[str, Field(description="The token returned by the validate_script tool.")],
host: Host = None,
) -> ToolResult:
script_details = script_store.get_script_details(token)
# Verify that this script requires confirmation
if not script_details.needs_confirmation:
raise ToolError("This script does not require confirmation. Use run_script instead of run_script_interactive.")
# Check if the passed parameters match the stored script details
new_details = ScriptDetails(
state="waiting-approval",
description=description,
script_type=script_type,
script=script,
host=host,
readonly=readonly,
)
result_id = token
if new_details != script_details:
# Parameters don't match - revalidate and create a new script store entry
gatekeeper_result = check_run_script(
description,
script_type,
(BASH_STRICT_PREAMBLE + script) if script_type == SCRIPT_TYPE_BASH else script,
readonly=readonly,
)
if gatekeeper_result.status != GatekeeperStatus.OK:
script_store.set_script_state(token, "rejected-gatekeeper")
raise ToolError(gatekeeper_result.description)
# Create new script store entry with the updated parameters
result_id = script_store.add_script(description, script, script_type, host, readonly)
content: list[ContentBlock] = [
TextContent(
type="text",
text="The user has been asked for approval; please respond nothing to the user yet; the final result will be provided as a separate message later.",
)
]
structured_content_obj = RunScriptInteractiveResult(id=result_id, status=GatekeeperStatus.OK, detail="")
return ToolResult(content=content, structured_content=structured_content_obj.model_dump())
@mcp.tool(
tags={"run_script", "mcp_apps_only", "hidden_from_model"},
title="Get the execution state with request ID",
description="Get the execution state with request ID",
app=AppConfig(visibility=["app"]),
)
@log_tool_call
@disallow_local_execution_in_containers
async def get_execution_state(id: str):
script_detail = script_store.get_script_details(id)
return {"state": script_detail.state}
def _pick_execution_tool(needs_confirmation: bool):
if not needs_confirmation:
return "run_script"
elif use_mcp_app_for_client():
return "run_script_interactive"
else:
return "run_script_with_confirmation"
@mcp.tool(
tags={"run_script"},
title="Validate a script",
description="Request validation of a script from the gatekeeper. The tool will return a unique token that must be included in the run_script tool call.",
annotations=ToolAnnotations(readOnlyHint=True),
)
@log_tool_call
@disallow_local_execution_in_containers
async def validate_script(
ctx: Context,
description: t.Annotated[
str,
Field(
description="Description of what the script does - e.g. 'Modify file permissions on nginx.conf to fix startup errors.'"
),
],
script_type: t.Annotated[
ScriptType,
Field(description="The type of script to run (python or bash)."),
],
script: t.Annotated[
str,
Field(description="The script to run."),
],
host: Host = None,
readonly: t.Annotated[bool, Field(description="Should be true if the script does not modify the system.")] = True,
) -> ToolResult:
gatekeeper_result = check_run_script(
description,
script_type,
(BASH_STRICT_PREAMBLE + script) if script_type == SCRIPT_TYPE_BASH else script,
readonly=readonly,
)
id = script_store.add_script(description, script, script_type, host, readonly)
script_details = script_store.get_script_details(id)
if gatekeeper_result.status != GatekeeperStatus.OK:
script_store.set_script_state(id, "rejected-gatekeeper")
raise ToolError(gatekeeper_result.description)
result = ToolResult(
content=[
TextContent(
type="text",
text=f"Script passed gatekeeper validation and is stored with ID {id}. Please use {_pick_execution_tool(script_details.needs_confirmation)} to execute the validated script.",
)
],
structured_content={
"token": id,
"needs_confirmation": script_details.needs_confirmation,
},
)
return result
@mcp.tool(
tags={"run_script"},
title="Run a script",
description="Call this tool to run a previously validated script. Use this when validate_script returned needs_confirmation: false.",
annotations=ToolAnnotations(readOnlyHint=True),
)
@log_tool_call
@disallow_local_execution_in_containers
async def run_script(
ctx: Context,
token: t.Annotated[str, Field(description="The token returned by the validate_script tool.")],
) -> str:
script_details = script_store.get_script_details(token)
# Verify that this script doesn't require confirmation
if script_details.needs_confirmation:
raise ToolError(f"This script requires confirmation. Use {_pick_execution_tool(True)} instead of run_script.")
script_store.set_script_state(token, "executing")
try:
command = _wrap_script(script_details.script_type, script_details.script)
returncode, stdout, stderr = await execute_command(command, host=script_details.host)
except Exception:
script_store.set_script_state(token, "failure")
raise
if returncode == 0:
script_store.set_script_state(token, "success")
return stdout if isinstance(stdout, str) else stdout.decode("utf-8", errors="replace")
else:
script_store.set_script_state(token, "failure")
return f"Error executing script: return code {returncode}, stderr: {stderr}"
@mcp.tool(
tags={"run_script", "mcp_apps_exclude"},
title="Run a script with confirmation",
description="Call this tool to run a previously validated script that modifies the system. Use this when validate_script returned needs_confirmation: true. The parameters must match those passed to validate_script.",
annotations=ToolAnnotations(destructiveHint=True),
)
@log_tool_call
@disallow_local_execution_in_containers
async def run_script_with_confirmation(
ctx: Context,
description: t.Annotated[
str,
Field(
description="Description of what the script does - e.g. 'Modify file permissions on nginx.conf to fix startup errors.'"
),
],
script_type: t.Annotated[
ScriptType,
Field(description="The type of script to run (python or bash)."),
],
script: t.Annotated[
str,
Field(description="The script to run."),
],
readonly: t.Annotated[bool, Field(description="Should be true if the script does not modify the system.")],
token: t.Annotated[str, Field(description="The token returned by the validate_script tool.")],
host: Host = None,
) -> str:
script_details = script_store.get_script_details(token)
# Verify that this script requires confirmation
if not script_details.needs_confirmation:
raise ToolError(
"This script does not require confirmation. Use run_script instead of run_script_with_confirmation."
)
# Verify the retrieved script details match the incoming parameters
new_details = ScriptDetails(
state="waiting-approval",
description=description,
script_type=script_type,
script=script,
host=host,
readonly=readonly,
)
details_changed = new_details != script_details
execute_details = new_details if details_changed else script_details
if details_changed:
# Revalidate the script again; this is a convenience for the user to avoid
# potentially having to double-approve the same script.
gatekeeper_result = check_run_script(
description,
script_type,
(BASH_STRICT_PREAMBLE + script) if script_type == SCRIPT_TYPE_BASH else script,
readonly=readonly,
)
if gatekeeper_result.status != GatekeeperStatus.OK:
script_store.set_script_state(token, "rejected-gatekeeper")
raise ToolError(gatekeeper_result.description)
else:
script_store.set_script_state(token, "executing")
try:
command = _wrap_script(execute_details.script_type, execute_details.script)
returncode, stdout, stderr = await execute_command(command, host=execute_details.host)
except Exception:
if not details_changed:
script_store.set_script_state(token, "failure")
raise
if returncode == 0:
if not details_changed:
script_store.set_script_state(token, "success")
return stdout if isinstance(stdout, str) else stdout.decode("utf-8", errors="replace")
else:
if not details_changed:
script_store.set_script_state(token, "failure")
return f"Error executing script: return code {returncode}, stderr: {stderr}"