/* * plugin_github.c - SCORPIOX BOT open-source GitHub MCP connector. * * One zero-dependency pure C99 binary, five modes: * * --meta {"name":"github","desc":"GitHub tools","kind":"mcp", * "mcp":true,"daemon":false,"menus":false,"verified":"mock"} * --mcp stdio MCP JSON-RPC 2.0: github_get_repo, github_list_issues, * github_create_issue, github_get_pull_request, * github_search_code * GET bot-themed setup page (PAT + API base, write-only token) * ?action= node-auth-gated JSON API: status / save / test / tools / clear * * The runtime is the vendored SCORPIOX BOT Connector SDK (src/sx_connector.*): * CGI contract, node auth, theme handoff, MCP server and connector-dir state * all come from there, which is the whole point of the SDK - this file is the * connector's identity, its five tool schemas and its setup page, nothing * else. Transport is the SDK's BearSSL HTTP client (src/sx_http_tls.*). * * verified is "mock" honestly: the offline suite proves every request shape * against a recording mock, but this binary makes no claim about a paid/live * GitHub account test at publish time. A live smoke against api.github.com * is documented in the report, not asserted by CI. * * Clean-room: no credentials, no LAN literals, no external deps. * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #include "gh.h" #include #include #include #include "ui_html.h" /* ------------------------------------------------------------ identity */ static const sx_plugin_info INFO = { .id = GH_ID, .name = GH_NAME, .desc = GH_DESC, .kind = SX_KIND_MCP, .mcp = 1, .daemon = 0, .menus = 0, .verified = "mock", .version = GH_VER, }; /* ------------------------------------------------------------ schemas */ #define SC_GET_REPO \ "{\"type\":\"object\",\"properties\":{" \ "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}" \ "},\"required\":[\"owner\",\"repo\"]}" #define SC_LIST_ISSUES \ "{\"type\":\"object\",\"properties\":{" \ "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ "\"state\":{\"type\":\"string\",\"enum\":[\"open\",\"closed\",\"all\"]," \ "\"description\":\"issue state filter, default open\"}" \ "},\"required\":[\"owner\",\"repo\"]}" #define SC_CREATE_ISSUE \ "{\"type\":\"object\",\"properties\":{" \ "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ "\"title\":{\"type\":\"string\",\"description\":\"issue title\"}," \ "\"body\":{\"type\":\"string\",\"description\":\"issue body (markdown, optional)\"}" \ "},\"required\":[\"owner\",\"repo\",\"title\"]}" #define SC_GET_PR \ "{\"type\":\"object\",\"properties\":{" \ "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ "\"number\":{\"type\":\"integer\",\"description\":\"pull request number\"}" \ "},\"required\":[\"owner\",\"repo\",\"number\"]}" #define SC_SEARCH_CODE \ "{\"type\":\"object\",\"properties\":{" \ "\"query\":{\"type\":\"string\",\"description\":\"GitHub code-search " \ "query, e.g. \\\"TODO language:c\\\" or \\\"MyClass repo:owner/repo\\\"\"}" \ "},\"required\":[\"query\"]}" static const sx_mcp_tool TOOLS[] = { { "github_get_repo", "Fetch a GitHub repository: description, stars, default branch, " "license, visibility, last push.", SC_GET_REPO, gh_tool_get_repo }, { "github_list_issues", "List issues in a repository (pull requests excluded), newest first, " "number / title / state / url per row.", SC_LIST_ISSUES, gh_tool_list_issues }, { "github_create_issue", "Create an issue in a repository. Needs a saved PAT with repo scope.", SC_CREATE_ISSUE, gh_tool_create_issue }, { "github_get_pull_request", "Fetch one pull request: title, state, author, head/base, draft/merged, " "commit and diff stats.", SC_GET_PR, gh_tool_get_pull_request }, { "github_search_code", "Search code across the repositories the credential can see " "(GitHub /search/code, top 10 paths returned).", SC_SEARCH_CODE, gh_tool_search_code }, }; #define NTOOLS (sizeof TOOLS / sizeof TOOLS[0]) /* ------------------------------------------------------------- JSON API */ /* Every action answer is built with yyjson so tool/page text can never * break the document; the PAT is never included anywhere. */ static int api_status(const sx_plugin_info *info) { gh_config cfg; yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); yyjson_mut_val *root = yyjson_mut_obj(doc); char dir[896]; yyjson_mut_doc_set_root(doc, root); gh_config_load(&cfg); yyjson_mut_obj_add_true(doc, root, "ok"); yyjson_mut_obj_add_strcpy(doc, root, "id", info->id); yyjson_mut_obj_add_strcpy(doc, root, "version", info->version); yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); yyjson_mut_obj_add_bool(doc, root, "has_token", cfg.has_token ? 1 : 0); /* who does the saved token belong to? only ask GitHub when one exists */ if (cfg.has_token) { long st = 0; char *resp = NULL; char err[256]; if (gh_api_call(&cfg, "GET", "/user", NULL, &st, &resp, err, sizeof err) == 0 && st >= 200 && st < 300) { yyjson_doc *ud = yyjson_read(resp, strlen(resp), 0); if (ud) { yyjson_val *login = yyjson_obj_get(yyjson_doc_get_root(ud), "login"); if (login && yyjson_is_str(login)) { /* copy BEFORE free - a yyjson string points into the doc */ char who[128]; gh_copy(who, sizeof who, yyjson_get_str(login)); yyjson_mut_obj_add_strcpy(doc, root, "user", who); } yyjson_doc_free(ud); } } free(resp); } if (sx_connector_dir(info->id, dir, sizeof dir) == 0) yyjson_mut_obj_add_strcpy(doc, root, "connector_dir", dir); yyjson_mut_obj_add_bool(doc, root, "tls_insecure_env", sx_tls_insecure() ? 1 : 0); sx_cgi_header(200, "application/json; charset=utf-8"); sx_print_json_doc(doc); yyjson_mut_doc_free(doc); return 0; } /* read a POST field from either the JSON body or the query string */ static int field(const char *body, const char *query, const char *key, char *out, size_t capsz) { if (body && *body) { yyjson_doc *d = yyjson_read(body, strlen(body), 0); int found = 0; if (d) { yyjson_val *v = yyjson_obj_get(yyjson_doc_get_root(d), key); if (v && yyjson_is_str(v)) { gh_copy(out, capsz, yyjson_get_str(v)); found = 1; } yyjson_doc_free(d); } if (found) return 1; } return sx_query_get(query, key, out, capsz); } static int api_save(const sx_plugin_info *info, const char *query, const char *body) { char token[256], api_base[256]; int rc; token[0] = api_base[0] = '\0'; field(body, query, "token", token, sizeof token); field(body, query, "api_base", api_base, sizeof api_base); if (!token[0] && !api_base[0]) { sx_json_error("nothing to save - send token and/or api_base", 400); return 1; } if (token[0] && strlen(token) < 8) { sx_json_error("that token is too short to be a GitHub credential", 400); return 1; } rc = gh_config_save(api_base[0] ? api_base : NULL, token[0] ? token : NULL, 0); if (rc == -2) { sx_json_error("api base must be an http(s) URL without a path, query or credentials", 400); return 1; } if (rc != 0) { sx_json_error("could not write the connector config", 500); return 1; } (void)info; return api_status(info); } static int api_test(const sx_plugin_info *info) { gh_config cfg; long st = 0; char *resp = NULL; char err[512]; yyjson_mut_doc *doc; yyjson_mut_val *root; (void)info; gh_config_load(&cfg); doc = yyjson_mut_doc_new(NULL); root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); if (gh_api_call(&cfg, "GET", "/user", NULL, &st, &resp, err, sizeof err) != 0) { yyjson_mut_obj_add_false(doc, root, "ok"); yyjson_mut_obj_add_strcpy(doc, root, "error", err); yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); sx_cgi_header(200, "application/json; charset=utf-8"); sx_print_json_doc(doc); yyjson_mut_doc_free(doc); free(resp); return 0; } if (st >= 200 && st < 300) { yyjson_doc *ud = yyjson_read(resp, strlen(resp), 0); char who[128]; who[0] = '\0'; if (ud) { yyjson_val *u = yyjson_doc_get_root(ud); yyjson_val *login = yyjson_obj_get(u, "login"); yyjson_val *type = yyjson_obj_get(u, "type"); if (login && yyjson_is_str(login)) gh_copy(who, sizeof who, yyjson_get_str(login)); if (type && yyjson_is_str(type)) yyjson_mut_obj_add_strcpy(doc, root, "user_type", yyjson_get_str(type)); yyjson_doc_free(ud); } yyjson_mut_obj_add_true(doc, root, "ok"); yyjson_mut_obj_add_strcpy(doc, root, "user", who); yyjson_mut_obj_add_int(doc, root, "status", st); yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); { char rl[32]; /* honest rate data straight from the headers of that call */ yyjson_mut_obj_add_bool(doc, root, "authenticated", cfg.has_token ? 1 : 0); (void)rl; } } else { char up[512]; gh_upstream_error(resp, st, up, sizeof up); yyjson_mut_obj_add_false(doc, root, "ok"); yyjson_mut_obj_add_strcpy(doc, root, "error", up); yyjson_mut_obj_add_int(doc, root, "status", st); yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); } free(resp); sx_cgi_header(200, "application/json; charset=utf-8"); sx_print_json_doc(doc); yyjson_mut_doc_free(doc); return 0; } static int api_tools(const sx_plugin_info *info) { yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); yyjson_mut_val *root = yyjson_mut_obj(doc); yyjson_mut_val *arr = yyjson_mut_arr(doc); size_t i; (void)info; yyjson_mut_doc_set_root(doc, root); yyjson_mut_obj_add_true(doc, root, "ok"); yyjson_mut_obj_add_val(doc, root, "tools", arr); for (i = 0; i < NTOOLS; i++) { yyjson_mut_val *t = yyjson_mut_obj(doc); yyjson_mut_obj_add_strcpy(doc, t, "name", TOOLS[i].name); yyjson_mut_obj_add_strcpy(doc, t, "description", TOOLS[i].description); if (TOOLS[i].input_schema_json) { /* embed the REAL schema the MCP server advertises, parsed, so * the page shows exactly what --mcp tools/list returns */ yyjson_doc *sd = yyjson_read(TOOLS[i].input_schema_json, strlen(TOOLS[i].input_schema_json), 0); if (sd) { yyjson_mut_val *sv = yyjson_val_mut_copy(doc, yyjson_doc_get_root(sd)); yyjson_mut_obj_add_val(doc, t, "inputSchema", sv); yyjson_doc_free(sd); /* copy is independent of sd */ } } yyjson_mut_arr_add_val(arr, t); } sx_cgi_header(200, "application/json; charset=utf-8"); sx_print_json_doc(doc); yyjson_mut_doc_free(doc); return 0; } static int api_clear(const sx_plugin_info *info) { if (gh_config_save(NULL, NULL, 1) != 0) { sx_json_error("could not rewrite the connector config", 500); return 1; } return api_status(info); } static int json_api(const sx_plugin_info *info, const char *query, const char *action, const char *body) { /* every control action needs the node password; the page forwards it as * X-Bot-Password / ?pwd= exactly like every other plugin */ sx_require_node_auth(); if (strcmp(action, "status") == 0) return api_status(info); if (strcmp(action, "save") == 0) return api_save(info, query, body); if (strcmp(action, "test") == 0) return api_test(info); if (strcmp(action, "tools") == 0) return api_tools(info); if (strcmp(action, "clear") == 0) return api_clear(info); sx_json_error("unknown action", 400); return 1; } /* ----------------------------------------------------------------- HTML */ /* The page is one static constant generated from src/ui.html by * tools/gen_ui.py; two control bytes mark where the SDK's theme block and * the sx-theme handoff script splice in, so the theme can never drift from * the rest of the bot. No CGI header re-emission, no re-serialisation. */ static void emit_ui(void) { const char *p = UI_HTML; while (*p) { if (*p == '\001') { fputs(SX_THEME_PREPAINT_JS, stdout); fputs("", stdout); p++; } else if (*p == '\002') { fputs(SX_THEME_HANDOFF_JS, stdout); p++; } else { putchar(*p++); } } } static int html_ui(const sx_plugin_info *info, const char *query, int embed, const char *theme) { (void)info; (void)query; (void)embed; /* the page's own prepaint script flags */ (void)theme; /* The page is a control surface for a credential, so it is gated exactly * like every ?action= is. The node proxy forwards X-Bot-Password when a * browser reaches the plugin through bot-api, and an open node (no * BOT_PASSWORD) stays open by design - but a passworded node answers 401 * here rather than handing out the shell of the setup form. */ sx_require_node_auth(); sx_cgi_header(200, "text/html; charset=utf-8"); emit_ui(); return 0; } /* ----------------------------------------------------------------- main */ int main(int argc, char **argv) { sx_stdio_binary(); if (sx_has_flag(argc, argv, "--mcp")) return sx_mcp_serve(&INFO, TOOLS, NTOOLS); if (sx_has_flag(argc, argv, "--schema")) return sx_mcp_schema(&INFO, TOOLS, NTOOLS); return sx_main(argc, argv, &INFO, html_ui, json_api); }