AkurAI Build
Menu

AkurAI-Build

public

Latest change 30e6b8c347048d64c2c048dfd05a8ded219d37f6 - Host authenticated Git repositories over Smart HTTP with repo host/sync CLI by Ólafur Búi Ólafsson

#!/usr/bin/env python3
import json, os, secrets, stat, subprocess, sys, tempfile, urllib.request
from pathlib import Path
if len(sys.argv) != 4: raise SystemExit("usage: configure-app-mcp.py NAME ENV_PREFIX PASSVAULT_FOLDER")
name, prefix, folder_text = sys.argv[1:]
folder_id=int(folder_text); token=secrets.token_urlsafe(48)
vault_env=Path.home()/'.config/omp/akurai-passvault-mcp.env'
if stat.S_IMODE(vault_env.stat().st_mode)!=0o600: raise SystemExit('PassVault environment must be mode 0600')
values={}
for line in vault_env.read_text().splitlines():
    if line and not line.startswith('#') and '=' in line:
        k,v=line.split('=',1); values[k]=v.strip().strip("'\"")
payload={"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"create_entry","arguments":{"name":f"{name} MCP token","username":"omp-mcp","password":token,"uri":"https://akurai-tasks.olibuijr.com/mcp","notes":"Bearer token for the AkurAI Tasks MCP endpoint","folderId":folder_id}}}
req=urllib.request.Request('https://akurai-passvault.olibuijr.com/mcp',data=json.dumps(payload).encode(),headers={'Authorization':'Bearer '+values['AKURAI_PASSVAULT_MCP_TOKEN'],'Content-Type':'application/json'})
with urllib.request.urlopen(req) as r:
    if 'error' in json.load(r): raise RuntimeError('PassVault rejected MCP token storage')
local=Path.home()/'.config/omp/akurai-tasks-mcp.env'; local.write_text(f'{prefix}_MCP_TOKEN={token}\n'); local.chmod(0o600)
with tempfile.NamedTemporaryFile('w',delete=False) as h: h.write(f'{prefix}_MCP_TOKEN={token}\n'); tmp=h.name
os.chmod(tmp,0o600)
cli=str(Path.home()/'.local/bin/akurai-ec2')
try:
    subprocess.run([cli,'ship',tmp,'/tmp/akurai-tasks-mcp.env'],check=True,stdout=subprocess.DEVNULL)
    cmd=f"sudo bash -c 'grep -v ^{prefix}_MCP_TOKEN= /etc/akurai-tasks/akurai-tasks.env > /tmp/tasks-env && cat /tmp/akurai-tasks-mcp.env >> /tmp/tasks-env && install -m 0640 -o root -g ubuntu /tmp/tasks-env /etc/akurai-tasks/akurai-tasks.env && rm -f /tmp/tasks-env /tmp/akurai-tasks-mcp.env' && sudo systemctl restart akurai-tasks.service"
    subprocess.run([cli,'ssh',cmd],check=True,stdout=subprocess.DEVNULL)
finally: os.unlink(tmp)
print('MCP token stored in PassVault and protected local/remote environments; service restarted.')