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)

ActionO que faz
serverinfoBuild, OS, threads, environment, FWCodEmp, paths (RootPath, RpoDb)
readfileLe qualquer arquivo do servidor (tail 512KB). Param: ?file=<path>
listdirLista diretorio. Param: ?path=<dir>&filter=*.log
sourcesLista fontes do RPO. Param: ?filter=MATA
sourceLe codigo-fonte do RPO. Param: ?file=MATA030.prw
envConfiguracoes do appserver.ini. Params: ?section=GENERAL&key=MaxStringSize
threadsTotal de threads ativas + info
logsLista logs em /log/ ou le um (param file)
processesInfo do ambiente (server, build, threads, FWCodEmp, dbDriver, OS)

Actions disponiveis (POST)

ActionBodyO 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:

  1. Endpoint REST recebe comando
  2. Grava em /temp/mcpshell_<threadid>_input.txt
  3. StartJob("U_MCPSHELL", ...) dispara User Function em thread separada
  4. U_MCPSHELL le input, executa WaitRun('cmd /c ' + cCmd + ' > output.txt')
  5. Sinaliza conclusao via _done.txt
  6. Endpoint REST faz polling do _done.txt com timeout
  7. Le output e retorna JSON

Bloqueios de seguranca

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

Quando NAO usar

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}

Veja também