MCP Admin
Console remoto do Protheus via REST: server info, logs, listagem do RPO, SQL arbitrario, shell remoto via StartJob+WaitRun. Audit completo + bloqueio do catastrofico (DROP DATABASE/SHUTDOWN).
Assinatura: GET/POST /McpAdmin/admin?action=serverinfo|readfile|listdir|sources|source|env|threads|logs|sql|shell
Retorna: JsonObject (varia por action)
O McpAdmin.prw e o "sudo" da familia MCP server. Console remoto pra ops, SRE e incident response. Privilegiado por design — requer auth obrigatoria via MV_MCPADMK.
Actions disponiveis (GET)
| Action | O que faz |
|---|---|
serverinfo | Build, OS, threads, environment, FWCodEmp, paths (RootPath, RpoDb) |
readfile | Le qualquer arquivo do servidor (tail 512KB). Param: ?file=<path> |
listdir | Lista diretorio. Param: ?path=<dir>&filter=*.log |
sources | Lista fontes do RPO. Param: ?filter=MATA |
source | Le codigo-fonte do RPO. Param: ?file=MATA030.prw |
env | Configuracoes do appserver.ini. Params: ?section=GENERAL&key=MaxStringSize |
threads | Total de threads ativas + info |
logs | Lista logs em /log/ ou le um (param file) |
processes | Info do ambiente (server, build, threads, FWCodEmp, dbDriver, OS) |
Actions disponiveis (POST)
| Action | Body | O que faz |
|---|---|---|
sql | {"action":"sql","query":"SELECT ..."} | Executa SQL arbitrario (SELECT retorna data, outros retornam affected rows) |
shell | {"action":"shell","command":"...","timeout":30} | Comando no SO host via cmd /c (StartJob + WaitRun + arquivo temp) |
Auth obrigatoria
SuperGetMV("MV_MCPADMK", .F., "")
Se a variavel nao estiver configurada no appserver.ini, qualquer chamada retorna 401. Em todas as requisicoes:
curl -H "X-Admin-Key: SUA_CHAVE_DE_32_CHARS" ...
Por que StartJob + WaitRun no shell?
WaitRun() tem comportamento conhecido de NAO funcionar em thread REST do Protheus. A solucao do McpAdmin:
- Endpoint REST recebe comando
- Grava em
/temp/mcpshell_<threadid>_input.txt StartJob("U_MCPSHELL", ...)dispara User Function em thread separada- U_MCPSHELL le input, executa
WaitRun('cmd /c ' + cCmd + ' > output.txt') - Sinaliza conclusao via
_done.txt - Endpoint REST faz polling do
_done.txtcom timeout - Le output e retorna JSON
Bloqueios de seguranca
- SQL:
DROP DATABASEeSHUTDOWNhardcoded como rejected - Shell:
FORMAT,DEL /S /Q,RD /S /Q,RMDIR /S,SHUTDOWN,RM -RFrejeitados - Timeout shell: default 30s, max 300s
- Tail readfile: 512KB max (nao trava com arquivos gigantes)
- Audit estruturado:
[MCP-ADMIN] [SERVERINFO|SQL|SHELL_EXEC|SQL_BLOCKED|SHELL_BLOCKED] timestamp cmd
Exemplos rapidos
Server info
curl 'https://seu-protheus:8443/McpAdmin/admin?action=serverinfo' -H "X-Admin-Key: ..."
Tail do console.log
curl 'https://seu-protheus:8443/McpAdmin/admin?action=logs&file=console.log' -H "X-Admin-Key: ..." | jq -r '.content'
SQL ad-hoc
curl -X POST 'https://seu-protheus:8443/McpAdmin/admin' -H "X-Admin-Key: ..." -H "Content-Type: application/json" \
-d '{"action":"sql","query":"SELECT TOP 10 E2_NUM, E2_VALOR FROM SE2010 WHERE D_E_L_E_T_=\" \" ORDER BY E2_EMISSAO DESC"}'
Shell remoto
curl -X POST 'https://seu-protheus:8443/McpAdmin/admin' -H "X-Admin-Key: ..." -H "Content-Type: application/json" \
-d '{"action":"shell","command":"powershell -Command Get-Service Spooler","timeout":15}'
Checklist antes de subir em producao
- TLS obrigatorio — NUNCA HTTP puro
- Chave de 32+ chars aleatorios:
openssl rand -hex 32 - Whitelist de IPs via firewall (so VPN/admin)
- Rotacao mensal da chave
- SIEM integrado — pipe
[MCP-ADMIN]pro Splunk/Datadog/ELK - Alerta de SQL destrutivo (UPDATE/DELETE/ALTER) no SIEM
Quando NAO usar
- Compliance estrita (PCI-DSS, HIPAA, SOX) — consulte compliance officer
- Ambiente sem TLS — backdoor pra atacante
- Sem audit logging — se nao consegue rastrear quem fez o que, nao implante
Fonte completo
Codigo AdvPL aberto (MIT, ~23KB — o maior da familia MCP): baixar McpAdmin.prw.
Tutorial completo com caso real de incident response e tabela comparativa: post no blog.
Exemplos
Server info
curl 'https://seu-protheus:8443/McpAdmin/admin?action=serverinfo' -H "X-Admin-Key: ..."Output: {"server":{"name":"TOTVS","build":"7.00.220222P-20251020","threads":42,"company":"01"},"database":{"driver":"POSTGRES","connected":true}}
SQL: ultimos 10 titulos pendentes
curl -X POST 'https://seu-protheus:8443/McpAdmin/admin' -H "X-Admin-Key: ..." -d '{"action":"sql","query":"SELECT TOP 10 E2_NUM, E2_VALOR FROM SE2010 WHERE E2_SALDO>0 AND D_E_L_E_T_=\" \" ORDER BY E2_EMISSAO DESC"}'Output: {"type":"SELECT","data":[{"E2_NUM":"000123","E2_VALOR":"4280.00"}],"count":10}