Substitui barras de caixa (─ ═ │ ┌ ┐ └ ┘ ├ ┤ ┬ ┴), travessões (—), setas (→ ←), reticências (…), bullets (•), multiplicação (×) e guillemets (› ‹) por equivalentes ASCII. Acentos portugueses são preservados.
110 lines
3.7 KiB
C
110 lines
3.7 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)
|