Editor como servidor MCP: agentes leem e editam as pilhas por código
tinyblocks-mcp-server (C, sem Python): servidor MCP nas duas eras do protocolo. Moderna (2026-07-28): sem handshake, versão e capacidades em _meta a cada pedido, server/discover, resultType, cabeçalhos MCP-Protocol-Version/Mcp-Method/Mcp-Name conferidos com o corpo (HeaderMismatch, UnsupportedProtocolVersion). Antiga: initialize. HTTP só em 127.0.0.1 (Origin conferido, GET/DELETE 405, sem sessão), sem bloquear (o editor atende a cada quadro), e stdio. test-mcp-server. Ferramentas do editor (tb_editor_mcp.c; http://127.0.0.1:7811/mcp, TINYBLOCKS_MCP_PORT muda a porta, 0 = sem rede): get_editor, list_block_types, list_stacks (scratchblocks + ids dos blocos), get_scratchblocks, add_scratchblocks, replace_stack, move_stack, delete_stack, add_block / move_block (solto, embaixo, num campo ou numa boca; alone = só o bloco) / delete_block / set_inputs, run / stop / get_output, new_tab / select_tab, save_project / open_project e ui_tree / ui_action / ui_type (a interface pela árvore acessível). O editor exporta e importa scratchblocks por pilha (numa aba existente, numa posição), e o modelo de blocos fica na API privada para o servidor. O scratchblocks usa também os rótulos declarados pelas libraries. Duplicar um bloco C de três bocas copiava só duas. O cliente (tinyblocks-mcp) fala as duas eras: sonda com server/discover no stdio, pedido moderno no HTTP, e cai para o initialize com servidores antigos. test-mcp testa contra o servidor antigo de teste e contra o moderno (stdio e HTTP); harness cenário 29 usa as ferramentas do editor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
10a8099c46
commit
2a2d22a64a
19 files changed
+2669
-116
No files matched your search
@@ -24,6 +24,7 @@
|
||||
#include <tb_input_lib.h>
|
||||
#include <tb_environ.h>
|
||||
#include <tb_a11y.h>
|
||||
#include <tb_mcp_server.h>
|
||||
|
||||
static TinyBlocks* rt;
|
||||
static TinyBlocks_Block *ev, *scene;
|
||||
@@ -138,6 +139,51 @@ static bool ax_do(int n, TinyBlocks_A11yAction a) {
|
||||
frame(x, y, 0, 0, 0, 0, NULL);
|
||||
return ok;
|
||||
}
|
||||
/* == Servidor MCP do editor: chama uma ferramenta (era moderna, sem rede) ===
|
||||
* Devolve o texto do resultado (malloc); *err = isError; *sc = structuredContent
|
||||
* (referencia nova, ou NULL) */
|
||||
static char* mcp(const char* tool, const char* args, bool* err, TinyBlocks_JsonValue** sc) {
|
||||
char* req = malloc(strlen(args) + 512);
|
||||
sprintf(req, "{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"tools/call\",\"params\":{\"name\":\"%s\","
|
||||
"\"arguments\":%s,\"_meta\":{\"io.modelcontextprotocol/protocolVersion\":\"2026-07-28\","
|
||||
"\"io.modelcontextprotocol/clientCapabilities\":{}}}}", tool, args);
|
||||
int st = 0;
|
||||
char* resp = tb_mcp_server_handle(tb_editor_mcp_server(scene), req, NULL, &st);
|
||||
free(req);
|
||||
TinyBlocks_JsonValue* r = resp ? tb_json_parse(resp, NULL, 0) : NULL;
|
||||
free(resp);
|
||||
TinyBlocks_JsonValue* res = r ? tb_json_object_get(r, "result") : NULL;
|
||||
TinyBlocks_JsonValue* ie = res ? tb_json_object_get(res, "isError") : NULL;
|
||||
TinyBlocks_JsonValue* content = res ? tb_json_object_get(res, "content") : NULL;
|
||||
TinyBlocks_JsonValue* first = content ? tb_json_array_get(content, 0) : NULL;
|
||||
TinyBlocks_JsonValue* text = first ? tb_json_object_get(first, "text") : NULL;
|
||||
if (err) *err = !res || tb_json_as_bool(ie, false);
|
||||
if (sc) *sc = res ? tb_json_object_get(res, "structuredContent") : NULL;
|
||||
char* out = strdup(text && tb_json_as_string(text) ? tb_json_as_string(text) : "");
|
||||
tb_json_release(text); tb_json_release(first); tb_json_release(content);
|
||||
tb_json_release(ie); tb_json_release(res); tb_json_release(r);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL); /* a interface mostra o que mudou */
|
||||
return out;
|
||||
}
|
||||
/* Numero num caminho do structuredContent: "stacks.0.id" */
|
||||
static double sc_num(const TinyBlocks_JsonValue* v, const char* path) {
|
||||
char key[64];
|
||||
TinyBlocks_JsonValue* cur = (TinyBlocks_JsonValue*)v;
|
||||
if (cur) tb_json_retain(cur);
|
||||
while (cur && *path) {
|
||||
size_t n = strcspn(path, ".");
|
||||
snprintf(key, sizeof(key), "%.*s", (int)n, path);
|
||||
TinyBlocks_JsonValue* nx = tb_json_kind(cur) == TB_JSON_ARRAY ? tb_json_array_get(cur, (uint32_t)atoi(key))
|
||||
: tb_json_object_get(cur, key);
|
||||
tb_json_release(cur);
|
||||
cur = nx;
|
||||
path += n + (path[n] == '.');
|
||||
}
|
||||
double d = cur ? tb_json_as_number(cur, -1) : -1;
|
||||
tb_json_release(cur);
|
||||
return d;
|
||||
}
|
||||
|
||||
/* Arrastar e soltar pela arvore (o editor decide o encaixe pelo papel do destino) */
|
||||
static bool ax_drop(int src, int dst) {
|
||||
if (src < 0 || dst < 0) { printf("FAIL soltar: origem %d, destino %d\n", src, dst); fails++; return false; }
|
||||
@@ -232,6 +278,7 @@ static bool drawn_text(int node, const char* sub) {
|
||||
int main(void) {
|
||||
/* configurações isoladas: o harness não lê nem grava o ~/.config real */
|
||||
setenv("XDG_CONFIG_HOME", "/tmp/tb_harness_cfg", 1);
|
||||
setenv("TINYBLOCKS_MCP_PORT", "0", 1); /* o servidor MCP atende sem rede (cenario 29) */
|
||||
unsetenv("TINYBLOCKS_SETTINGS");
|
||||
remove("/tmp/tb_harness_cfg/tinyblocks/settings.csv");
|
||||
tinyblocks_init(&rt, NULL);
|
||||
@@ -1138,6 +1185,140 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 29. Servidor MCP do editor: um agente edita as pilhas por codigo */
|
||||
{
|
||||
bool err;
|
||||
TinyBlocks_JsonValue* sc = NULL;
|
||||
char* t;
|
||||
TinyBlocks_JsonValue* tl = NULL;
|
||||
{
|
||||
int st = 0;
|
||||
char* r = tb_mcp_server_handle(tb_editor_mcp_server(scene),
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{\"_meta\":{"
|
||||
"\"io.modelcontextprotocol/protocolVersion\":\"2026-07-28\",\"io.modelcontextprotocol/clientCapabilities\":{}}}}",
|
||||
NULL, &st);
|
||||
CHECK(r && strstr(r, "\"add_scratchblocks\"") && strstr(r, "\"replace_stack\"") && strstr(r, "\"move_block\"") &&
|
||||
strstr(r, "\"ui_action\""), "mcp: o editor lista as ferramentas");
|
||||
free(r);
|
||||
}
|
||||
(void)tl;
|
||||
t = mcp("new_tab", "{\"name\":\"Agente\"}", &err, NULL);
|
||||
CHECK(!err && (N(ax_in(ax_in(0, TB_A11Y_TAB_LIST, "Abas"), TB_A11Y_TAB, "Agente"))->states & TB_A11Y_SELECTED),
|
||||
"mcp: new_tab cria e seleciona a aba");
|
||||
free(t);
|
||||
|
||||
t = mcp("add_scratchblocks", "{\"code\":\"when flag clicked\\nset [x v] to (0)\\nrepeat (3)\\n"
|
||||
"change [x v] by (1)\\nend\\nsay (x)\\n\",\"x\":40,\"y\":40}", &err, &sc);
|
||||
int stack = (int)sc_num(sc, "stacks.0.id");
|
||||
tb_json_release(sc);
|
||||
CHECK(!err && stack >= 0 && ax_in(ws(), TB_A11Y_LIST, "Pilha: quando a bandeira for clicado") >= 0 &&
|
||||
ws_block("repita (3) vezes", 0) >= 0, "mcp: add_scratchblocks cria a pilha (aparece na aba)");
|
||||
free(t);
|
||||
|
||||
t = mcp("list_stacks", "{}", &err, &sc);
|
||||
CHECK(!err && strstr(t, "repeat (3)") && strstr(t, "repita (3) vezes") && sc_num(sc, "stacks.0.id") == stack,
|
||||
"mcp: list_stacks mostra o código e os blocos com ids");
|
||||
/* acha o id do "repita" na lista de blocos */
|
||||
int rep_id = -1;
|
||||
for (int i = 0; i < 20; i++) {
|
||||
char path[32];
|
||||
snprintf(path, sizeof(path), "stacks.0.blocks.%d.id", i);
|
||||
int id = (int)sc_num(sc, path);
|
||||
if (id < 0) break;
|
||||
TinyBlocks_JsonValue* stks = tb_json_object_get(sc, "stacks");
|
||||
TinyBlocks_JsonValue* st0 = tb_json_array_get(stks, 0);
|
||||
TinyBlocks_JsonValue* bl = tb_json_object_get(st0, "blocks");
|
||||
TinyBlocks_JsonValue* b = tb_json_array_get(bl, (uint32_t)i);
|
||||
TinyBlocks_JsonValue* ty = tb_json_object_get(b, "type");
|
||||
if (ty && !strcmp(tb_json_as_string(ty), "Control.Repeat")) rep_id = id;
|
||||
tb_json_release(ty); tb_json_release(b); tb_json_release(bl); tb_json_release(st0); tb_json_release(stks);
|
||||
}
|
||||
tb_json_release(sc);
|
||||
free(t);
|
||||
CHECK(rep_id >= 0, "mcp: o id do repita está na lista");
|
||||
|
||||
t = mcp("run", "{}", &err, NULL);
|
||||
free(t);
|
||||
for (int i = 0; i < 5; i++) frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
t = mcp("get_output", "{}", &err, NULL);
|
||||
CHECK(!err && strstr(t, "Saida:\n3"), "mcp: run + get_output (a pilha escreve 3)");
|
||||
free(t);
|
||||
|
||||
char args[256];
|
||||
snprintf(args, sizeof(args), "{\"block\":%d,\"inputs\":{\"vezes\":5}}", rep_id);
|
||||
t = mcp("set_inputs", args, &err, NULL);
|
||||
CHECK(!err && strstr(t, "repeat (5)") && ws_block("repita (5) vezes", 0) >= 0, "mcp: set_inputs muda o campo");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"type\":\"IO.Print\",\"inputs\":{\"0\":\"fim\"},\"below\":%d}", rep_id);
|
||||
t = mcp("add_block", args, &err, &sc);
|
||||
int fim = (int)sc_num(sc, "block");
|
||||
tb_json_release(sc);
|
||||
CHECK(!err && fim >= 0 && strstr(t, "say [fim]"), "mcp: add_block encaixa embaixo do repita");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"block\":%d,\"into\":%d,\"mouth\":0,\"alone\":true}", fim, rep_id);
|
||||
t = mcp("move_block", args, &err, NULL);
|
||||
CHECK(!err && strstr(t, "repeat (5)\n say [fim]\n change [x v] by (1)\nend\nsay (x)"),
|
||||
"mcp: move_block (alone) põe só o bloco no começo da boca");
|
||||
free(t);
|
||||
snprintf(args, sizeof(args), "{\"block\":%d,\"below\":%d,\"alone\":true}", fim, rep_id);
|
||||
t = mcp("move_block", args, &err, NULL);
|
||||
CHECK(!err && strstr(t, "repeat (5)\n change [x v] by (1)\nend\nsay [fim]\nsay (x)"),
|
||||
"mcp: move_block encaixa embaixo (o que estava ali vai depois)");
|
||||
free(t);
|
||||
|
||||
t = mcp("move_block", "{\"block\":9999,\"x\":0,\"y\":0}", &err, NULL);
|
||||
CHECK(err && strstr(t, "nao existe"), "mcp: bloco inexistente vira erro da ferramenta");
|
||||
free(t);
|
||||
snprintf(args, sizeof(args), "{\"block\":%d,\"below\":%d}", rep_id, fim);
|
||||
t = mcp("move_block", args, &err, NULL);
|
||||
CHECK(err && strstr(t, "nele mesmo"), "mcp: não encaixa um bloco dentro dele mesmo");
|
||||
free(t);
|
||||
t = mcp("add_block", "{\"type\":\"Nao.Existe\"}", &err, NULL);
|
||||
CHECK(err && strstr(t, "list_block_types"), "mcp: tipo desconhecido sugere list_block_types");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"stack\":%d}", stack);
|
||||
t = mcp("get_scratchblocks", args, &err, NULL);
|
||||
CHECK(!err && strstr(t, "when flag clicked") && strstr(t, "repeat (5)"), "mcp: get_scratchblocks de uma pilha");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"stack\":%d,\"x\":300,\"y\":200}", stack);
|
||||
float y_before = N(ax_in(ws(), TB_A11Y_LIST, "Pilha: quando a bandeira for clicado"))->y;
|
||||
t = mcp("move_stack", args, &err, NULL);
|
||||
CHECK(!err && N(ax_in(ws(), TB_A11Y_LIST, "Pilha: quando a bandeira for clicado"))->y > y_before,
|
||||
"mcp: move_stack leva a pilha (a interface acompanha)");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"stack\":%d,\"code\":\"when flag clicked\\nsay [trocado]\\n\"}", stack);
|
||||
t = mcp("replace_stack", args, &err, &sc);
|
||||
int stack2 = (int)sc_num(sc, "stacks.0.id");
|
||||
tb_json_release(sc);
|
||||
CHECK(!err && ws_block("repita", 0) < 0 && ws_block("escreva (trocado)", 0) >= 0, "mcp: replace_stack troca o código");
|
||||
free(t);
|
||||
snprintf(args, sizeof(args), "{\"stack\":%d,\"code\":\"isso nao e scratchblocks\"}", stack2);
|
||||
t = mcp("replace_stack", args, &err, NULL);
|
||||
CHECK(err && ws_block("escreva (trocado)", 0) >= 0, "mcp: código inválido não apaga a pilha");
|
||||
free(t);
|
||||
|
||||
snprintf(args, sizeof(args), "{\"stack\":%d}", stack2);
|
||||
t = mcp("delete_stack", args, &err, NULL);
|
||||
CHECK(!err && ax_in(ws(), TB_A11Y_LIST, NULL) < 0, "mcp: delete_stack apaga a pilha");
|
||||
free(t);
|
||||
|
||||
/* a interface pela arvore acessivel */
|
||||
t = mcp("ui_action", "{\"role\":\"botão\",\"name\":\"Config\"}", &err, NULL);
|
||||
CHECK(!err && ax_in(0, TB_A11Y_DIALOG, "Configurações") >= 0, "mcp: ui_action clica no botão Config");
|
||||
free(t);
|
||||
t = mcp("ui_tree", "{\"within\":\"Configurações\"}", &err, NULL);
|
||||
CHECK(!err && strstr(t, "Configurações, diálogo") && strstr(t, "Voltar, botão"), "mcp: ui_tree mostra o diálogo");
|
||||
free(t);
|
||||
t = mcp("ui_type", "{\"key\":\"escape\"}", &err, NULL);
|
||||
CHECK(!err && ax_in(0, TB_A11Y_DIALOG, "Configurações") < 0, "mcp: ui_type aperta Esc e fecha");
|
||||
free(t);
|
||||
}
|
||||
|
||||
tinyblocks_deinit(&rt);
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
return fails != 0;
|
||||
|
||||
Reference in new issue
Block a user