Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/macos_mcp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import logging
import os
from pathlib import Path
import plistlib
import secrets
import shutil
import socket
Expand Down Expand Up @@ -936,28 +937,15 @@ def _resolve_program() -> list[str]:
def _build_plist(program_args: list[str]) -> str:
log_out = CONFIG_DIR / "server.log"
log_err = CONFIG_DIR / "server.error.log"
args_xml = "\n".join(f" <string>{a}</string>" for a in program_args)
return f"""\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>{_AGENT_LABEL}</string>
<key>ProgramArguments</key>
<array>
{args_xml}
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>{log_out}</string>
<key>StandardErrorPath</key>
<string>{log_err}</string>
</dict>
</plist>"""
payload = {
"Label": _AGENT_LABEL,
"ProgramArguments": program_args,
"RunAtLoad": True,
"KeepAlive": True,
"StandardOutPath": str(log_out),
"StandardErrorPath": str(log_err),
}
return plistlib.dumps(payload, fmt=plistlib.FMT_XML, sort_keys=False).decode("utf-8")


def _launchctl(*args: str) -> subprocess.CompletedProcess:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_launchd_plist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import plistlib

import macos_mcp.__main__ as server


def test_build_plist_escapes_special_characters(mocker, tmp_path):
config_dir = tmp_path / "config & logs <local>"
mocker.patch.object(server, "CONFIG_DIR", config_dir)
args = ["/tmp/tool & helper", "serve", "--label=<local>", "a>b"]

rendered = server._build_plist(args)
parsed = plistlib.loads(rendered.encode("utf-8"))

assert parsed == {
"Label": "com.macos-mcp.server",
"ProgramArguments": args,
"RunAtLoad": True,
"KeepAlive": True,
"StandardOutPath": str(config_dir / "server.log"),
"StandardErrorPath": str(config_dir / "server.error.log"),
}