Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix: install MCP when Linux user systemd is down

Cursor Agentba3c1b3f3a79
diff --git a/scripts/install-mcp.py b/scripts/install-mcp.py
index 39192e01..1aa91f3f 100644
--- a/scripts/install-mcp.py
+++ b/scripts/install-mcp.py
@@ -183,6 +183,22 @@ def acquire_install_lock(lock_path, timeout=12.0):
time.sleep(0.1)
+def linux_user_systemd():
+ if not shutil.which('systemctl'):
+ return False
+ return run_managed(['systemctl', '--user', 'show-environment'], check=False).returncode == 0
+
+
+def start_detached(command, directory, token, log_path):
+ log_path.parent.mkdir(parents=True, exist_ok=True)
+ log = open(log_path, 'ab')
+ env = os.environ.copy()
+ env['GRAFF_MCP_TOKEN'] = token
+ subprocess.Popen(command, cwd=str(directory), env=env, stdin=subprocess.DEVNULL,
+ stdout=log, stderr=log, start_new_session=True)
+ log.close()
+
+
def service(home, executable, directory, port, token):
state = home / '.graff/mcp'
command = [str(executable), 'mcp', 'serve', '--http', '--port', str(port)]
@@ -199,7 +215,8 @@ def service(home, executable, directory, port, token):
run_managed(['launchctl', 'bootout', domain + '/dev.codegraff.mcp'], check=False)
run_managed(['launchctl', 'bootstrap', domain, str(target)])
run_managed(['launchctl', 'kickstart', '-k', domain + '/dev.codegraff.mcp'])
- elif sys.platform.startswith('linux') and shutil.which('systemctl'):
+ return
+ if sys.platform.startswith('linux'):
target = home / '.config/systemd/user/codegraff-mcp.service'
if target.exists() and MARKER not in target.read_text():
raise ValueError('Existing service is not managed by Codegraff')
@@ -208,11 +225,15 @@ def service(home, executable, directory, port, token):
'ExecStart=' + ' '.join(quote(part) for part in command) + '\n' +
'WorkingDirectory=' + quote(directory) + '\nEnvironment=GRAFF_MCP_TOKEN=' + token +
'\nRestart=on-failure\nRestartSec=10\n[Install]\nWantedBy=default.target\n')
- run_managed(['systemctl', '--user', 'daemon-reload'])
- run_managed(['systemctl', '--user', 'enable', '--now', 'codegraff-mcp.service'])
- run_managed(['systemctl', '--user', 'restart', 'codegraff-mcp.service'])
- else:
- raise ValueError('Automatic service setup needs macOS launchd or Linux user systemd')
+ if linux_user_systemd():
+ run_managed(['systemctl', '--user', 'daemon-reload'])
+ run_managed(['systemctl', '--user', 'enable', '--now', 'codegraff-mcp.service'])
+ run_managed(['systemctl', '--user', 'restart', 'codegraff-mcp.service'])
+ return
+ if sys.platform.startswith('linux'):
+ start_detached(command, directory, token, home / '.graff/mcp/service.log')
+ return
+ raise ValueError('Automatic service setup needs macOS launchd or Linux user systemd')
def main():
diff --git a/scripts/test-install-mcp.py b/scripts/test-install-mcp.py
index 8dd30a93..1631f1c6 100644
--- a/scripts/test-install-mcp.py
+++ b/scripts/test-install-mcp.py
@@ -156,5 +156,16 @@ class InstallTests(unittest.TestCase):
installer.run_managed(['launchctl', 'bootstrap', 'x'])
self.assertEqual(calls['n'], 3)
+ def test_linux_without_user_systemd_starts_detached(self):
+ with patch.object(installer.sys, 'platform', 'linux'), \
+ patch.object(installer.shutil, 'which', return_value='/usr/bin/systemctl'), \
+ patch.object(installer, 'linux_user_systemd', return_value=False), \
+ patch.object(installer.subprocess, 'Popen') as popen:
+ installer.service(self.home, Path('/usr/bin/graff'), self.home, 7720, self.token)
+ popen.assert_called_once()
+ unit = self.home / '.config/systemd/user/codegraff-mcp.service'
+ self.assertIn('Codegraff managed MCP service', unit.read_text())
+ self.assertIn('mcp', unit.read_text())
+
if __name__ == '__main__':
unittest.main()