Files
tinyblocks/examples/mcp/mcp.c
T
mguinhosandClaude 4950ad6006 Estado atual do TinyBlocks: editor estilo Scratch + graphics como interface
Snapshot do projeto antes de implementar o diálogo "Criar um Bloco":
- runtime C block-based (src/, include/)
- libraries: graphics (interface), sdl2 (backend), editor (estilo Scratch),
  python, mcp
- exemplos (hello, sdl2, editor, vector, json, customappearance, python, mcp)
- Makefile com alvo help e build do editor

Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-20 19:10:43 -03:00

110 lines
4.8 KiB
C

/*
* mcp.c — Plugin TinyBlocks com blocos MCP (Model Context Protocol).
*
* Blocos:
* MCP.Server — literal: armazena a URL do servidor MCP
* HTTP: "http://localhost:3000/mcp"
* stdio: "stdio://npx @modelcontextprotocol/server-filesystem /tmp"
*
* MCP.Ping — input: url (text) → output: status (text)
* Verifica se o servidor responde.
*
* MCP.ListTools — input: url (text) → output: tools (text)
* Lista ferramentas disponíveis no servidor.
*
* MCP.Call — inputs: url, tool, args (text) → output: result (text)
* Chama uma ferramenta com argumentos JSON.
*
* Compilar:
* make mcp-lib
*
* Carregar no editor:
* ./build/editor build/mcp.so ou Ctrl+O → build/mcp.so
*
* Exemplo de uso no editor:
* [Literal.Text "stdio://npx @modelcontextprotocol/server-filesystem /tmp"]
* → [MCP.ListTools] → [IO.Print "tools"]
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks-mcp/tb_mcp.h>
/* ── Helper: lê texto de slot ────────────────────────────────────────────── */
static const char* gs(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
/* ── MCP.Ping ────────────────────────────────────────────────────────────── */
static void eval_mcp_ping(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
char* result = tb_mcp_ping(url);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* ── MCP.ListTools ───────────────────────────────────────────────────────── */
static void eval_mcp_list_tools(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
char* result = tb_mcp_list_tools(url);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* ── MCP.Call ────────────────────────────────────────────────────────────── */
static void eval_mcp_call(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
const char* tool = gs(self, rt, 1);
const char* args = gs(self, rt, 2);
char* result = tb_mcp_call_tool(url, tool, args);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* ── Slots ───────────────────────────────────────────────────────────────── */
static const TinyBlocks_SlotDesc url_in[] = {{"url", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc call_in[] = {{"url", TB_VALUE_TEXT},
{"tool", TB_VALUE_TEXT},
{"args", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc status_out[] = {{"status", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc tools_out[] = {{"tools", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc result_out[] = {{"result", TB_VALUE_TEXT}};
/* ── Registro ────────────────────────────────────────────────────────────── */
static bool mcp_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
if (!tb_mcp_init()) return false;
TinyBlocks_TypeBlock* t;
t = tb_type_block_create(rt, "MCP.Ping",
url_in, 1, status_out, 1, eval_mcp_ping);
if (t) tb_library_add(lib, &t->base);
t = tb_type_block_create(rt, "MCP.ListTools",
url_in, 1, tools_out, 1, eval_mcp_list_tools);
if (t) tb_library_add(lib, &t->base);
t = tb_type_block_create(rt, "MCP.Call",
call_in, 3, result_out, 1, eval_mcp_call);
if (t) tb_library_add(lib, &t->base);
return true;
}
static void mcp_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
tb_mcp_deinit();
}
TINYBLOCKS_DEFINE_LIBRARY(mcp,
"tinyblocks-mcp",
"1.0.0",
mcp_init,
mcp_deinit)