| 1 | /* |
| 2 | * plugin_github.c - SCORPIOX BOT open-source GitHub MCP connector. |
| 3 | * |
| 4 | * One zero-dependency pure C99 binary, five modes: |
| 5 | * |
| 6 | * --meta {"name":"github","desc":"GitHub tools","kind":"mcp", |
| 7 | * "mcp":true,"daemon":false,"menus":false,"verified":"mock"} |
| 8 | * --mcp stdio MCP JSON-RPC 2.0: github_get_repo, github_list_issues, |
| 9 | * github_create_issue, github_get_pull_request, |
| 10 | * github_search_code |
| 11 | * GET bot-themed setup page (PAT + API base, write-only token) |
| 12 | * ?action= node-auth-gated JSON API: status / save / test / tools / clear |
| 13 | * |
| 14 | * The runtime is the vendored SCORPIOX BOT Connector SDK (src/sx_connector.*): |
| 15 | * CGI contract, node auth, theme handoff, MCP server and connector-dir state |
| 16 | * all come from there, which is the whole point of the SDK - this file is the |
| 17 | * connector's identity, its five tool schemas and its setup page, nothing |
| 18 | * else. Transport is the SDK's BearSSL HTTP client (src/sx_http_tls.*). |
| 19 | * |
| 20 | * verified is "mock" honestly: the offline suite proves every request shape |
| 21 | * against a recording mock, but this binary makes no claim about a paid/live |
| 22 | * GitHub account test at publish time. A live smoke against api.github.com |
| 23 | * is documented in the report, not asserted by CI. |
| 24 | * |
| 25 | * Clean-room: no credentials, no LAN literals, no external deps. |
| 26 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 27 | */ |
| 28 | |
| 29 | #include "gh.h" |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "ui_html.h" |
| 36 | |
| 37 | /* ------------------------------------------------------------ identity */ |
| 38 | |
| 39 | static const sx_plugin_info INFO = { |
| 40 | .id = GH_ID, |
| 41 | .name = GH_NAME, |
| 42 | .desc = GH_DESC, |
| 43 | .kind = SX_KIND_MCP, |
| 44 | .mcp = 1, |
| 45 | .daemon = 0, |
| 46 | .menus = 0, |
| 47 | .verified = "mock", |
| 48 | .version = GH_VER, |
| 49 | }; |
| 50 | |
| 51 | /* ------------------------------------------------------------ schemas */ |
| 52 | |
| 53 | #define SC_GET_REPO \ |
| 54 | "{\"type\":\"object\",\"properties\":{" \ |
| 55 | "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ |
| 56 | "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}" \ |
| 57 | "},\"required\":[\"owner\",\"repo\"]}" |
| 58 | |
| 59 | #define SC_LIST_ISSUES \ |
| 60 | "{\"type\":\"object\",\"properties\":{" \ |
| 61 | "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ |
| 62 | "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ |
| 63 | "\"state\":{\"type\":\"string\",\"enum\":[\"open\",\"closed\",\"all\"]," \ |
| 64 | "\"description\":\"issue state filter, default open\"}" \ |
| 65 | "},\"required\":[\"owner\",\"repo\"]}" |
| 66 | |
| 67 | #define SC_CREATE_ISSUE \ |
| 68 | "{\"type\":\"object\",\"properties\":{" \ |
| 69 | "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ |
| 70 | "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ |
| 71 | "\"title\":{\"type\":\"string\",\"description\":\"issue title\"}," \ |
| 72 | "\"body\":{\"type\":\"string\",\"description\":\"issue body (markdown, optional)\"}" \ |
| 73 | "},\"required\":[\"owner\",\"repo\",\"title\"]}" |
| 74 | |
| 75 | #define SC_GET_PR \ |
| 76 | "{\"type\":\"object\",\"properties\":{" \ |
| 77 | "\"owner\":{\"type\":\"string\",\"description\":\"repo owner (user or org)\"}," \ |
| 78 | "\"repo\":{\"type\":\"string\",\"description\":\"repository name\"}," \ |
| 79 | "\"number\":{\"type\":\"integer\",\"description\":\"pull request number\"}" \ |
| 80 | "},\"required\":[\"owner\",\"repo\",\"number\"]}" |
| 81 | |
| 82 | #define SC_SEARCH_CODE \ |
| 83 | "{\"type\":\"object\",\"properties\":{" \ |
| 84 | "\"query\":{\"type\":\"string\",\"description\":\"GitHub code-search " \ |
| 85 | "query, e.g. \\\"TODO language:c\\\" or \\\"MyClass repo:owner/repo\\\"\"}" \ |
| 86 | "},\"required\":[\"query\"]}" |
| 87 | |
| 88 | static const sx_mcp_tool TOOLS[] = { |
| 89 | { "github_get_repo", |
| 90 | "Fetch a GitHub repository: description, stars, default branch, " |
| 91 | "license, visibility, last push.", |
| 92 | SC_GET_REPO, gh_tool_get_repo }, |
| 93 | { "github_list_issues", |
| 94 | "List issues in a repository (pull requests excluded), newest first, " |
| 95 | "number / title / state / url per row.", |
| 96 | SC_LIST_ISSUES, gh_tool_list_issues }, |
| 97 | { "github_create_issue", |
| 98 | "Create an issue in a repository. Needs a saved PAT with repo scope.", |
| 99 | SC_CREATE_ISSUE, gh_tool_create_issue }, |
| 100 | { "github_get_pull_request", |
| 101 | "Fetch one pull request: title, state, author, head/base, draft/merged, " |
| 102 | "commit and diff stats.", |
| 103 | SC_GET_PR, gh_tool_get_pull_request }, |
| 104 | { "github_search_code", |
| 105 | "Search code across the repositories the credential can see " |
| 106 | "(GitHub /search/code, top 10 paths returned).", |
| 107 | SC_SEARCH_CODE, gh_tool_search_code }, |
| 108 | }; |
| 109 | #define NTOOLS (sizeof TOOLS / sizeof TOOLS[0]) |
| 110 | |
| 111 | /* ------------------------------------------------------------- JSON API */ |
| 112 | |
| 113 | /* Every action answer is built with yyjson so tool/page text can never |
| 114 | * break the document; the PAT is never included anywhere. */ |
| 115 | |
| 116 | static int api_status(const sx_plugin_info *info) |
| 117 | { |
| 118 | gh_config cfg; |
| 119 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 120 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 121 | char dir[896]; |
| 122 | |
| 123 | yyjson_mut_doc_set_root(doc, root); |
| 124 | gh_config_load(&cfg); |
| 125 | yyjson_mut_obj_add_true(doc, root, "ok"); |
| 126 | yyjson_mut_obj_add_strcpy(doc, root, "id", info->id); |
| 127 | yyjson_mut_obj_add_strcpy(doc, root, "version", info->version); |
| 128 | yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); |
| 129 | yyjson_mut_obj_add_bool(doc, root, "has_token", cfg.has_token ? 1 : 0); |
| 130 | /* who does the saved token belong to? only ask GitHub when one exists */ |
| 131 | if (cfg.has_token) { |
| 132 | long st = 0; |
| 133 | char *resp = NULL; |
| 134 | char err[256]; |
| 135 | if (gh_api_call(&cfg, "GET", "/user", NULL, &st, &resp, |
| 136 | err, sizeof err) == 0 && st >= 200 && st < 300) { |
| 137 | yyjson_doc *ud = yyjson_read(resp, strlen(resp), 0); |
| 138 | if (ud) { |
| 139 | yyjson_val *login = |
| 140 | yyjson_obj_get(yyjson_doc_get_root(ud), "login"); |
| 141 | if (login && yyjson_is_str(login)) { |
| 142 | /* copy BEFORE free - a yyjson string points into the doc */ |
| 143 | char who[128]; |
| 144 | gh_copy(who, sizeof who, yyjson_get_str(login)); |
| 145 | yyjson_mut_obj_add_strcpy(doc, root, "user", who); |
| 146 | } |
| 147 | yyjson_doc_free(ud); |
| 148 | } |
| 149 | } |
| 150 | free(resp); |
| 151 | } |
| 152 | if (sx_connector_dir(info->id, dir, sizeof dir) == 0) |
| 153 | yyjson_mut_obj_add_strcpy(doc, root, "connector_dir", dir); |
| 154 | yyjson_mut_obj_add_bool(doc, root, "tls_insecure_env", |
| 155 | sx_tls_insecure() ? 1 : 0); |
| 156 | |
| 157 | sx_cgi_header(200, "application/json; charset=utf-8"); |
| 158 | sx_print_json_doc(doc); |
| 159 | yyjson_mut_doc_free(doc); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* read a POST field from either the JSON body or the query string */ |
| 164 | static int field(const char *body, const char *query, const char *key, |
| 165 | char *out, size_t capsz) |
| 166 | { |
| 167 | if (body && *body) { |
| 168 | yyjson_doc *d = yyjson_read(body, strlen(body), 0); |
| 169 | int found = 0; |
| 170 | if (d) { |
| 171 | yyjson_val *v = yyjson_obj_get(yyjson_doc_get_root(d), key); |
| 172 | if (v && yyjson_is_str(v)) { |
| 173 | gh_copy(out, capsz, yyjson_get_str(v)); |
| 174 | found = 1; |
| 175 | } |
| 176 | yyjson_doc_free(d); |
| 177 | } |
| 178 | if (found) return 1; |
| 179 | } |
| 180 | return sx_query_get(query, key, out, capsz); |
| 181 | } |
| 182 | |
| 183 | static int api_save(const sx_plugin_info *info, const char *query, |
| 184 | const char *body) |
| 185 | { |
| 186 | char token[256], api_base[256]; |
| 187 | int rc; |
| 188 | |
| 189 | token[0] = api_base[0] = '\0'; |
| 190 | field(body, query, "token", token, sizeof token); |
| 191 | field(body, query, "api_base", api_base, sizeof api_base); |
| 192 | |
| 193 | if (!token[0] && !api_base[0]) { |
| 194 | sx_json_error("nothing to save - send token and/or api_base", 400); |
| 195 | return 1; |
| 196 | } |
| 197 | if (token[0] && strlen(token) < 8) { |
| 198 | sx_json_error("that token is too short to be a GitHub credential", 400); |
| 199 | return 1; |
| 200 | } |
| 201 | rc = gh_config_save(api_base[0] ? api_base : NULL, |
| 202 | token[0] ? token : NULL, 0); |
| 203 | if (rc == -2) { |
| 204 | sx_json_error("api base must be an http(s) URL without a path, query or credentials", 400); |
| 205 | return 1; |
| 206 | } |
| 207 | if (rc != 0) { |
| 208 | sx_json_error("could not write the connector config", 500); |
| 209 | return 1; |
| 210 | } |
| 211 | (void)info; |
| 212 | return api_status(info); |
| 213 | } |
| 214 | |
| 215 | static int api_test(const sx_plugin_info *info) |
| 216 | { |
| 217 | gh_config cfg; |
| 218 | long st = 0; |
| 219 | char *resp = NULL; |
| 220 | char err[512]; |
| 221 | yyjson_mut_doc *doc; |
| 222 | yyjson_mut_val *root; |
| 223 | |
| 224 | (void)info; |
| 225 | gh_config_load(&cfg); |
| 226 | doc = yyjson_mut_doc_new(NULL); |
| 227 | root = yyjson_mut_obj(doc); |
| 228 | yyjson_mut_doc_set_root(doc, root); |
| 229 | |
| 230 | if (gh_api_call(&cfg, "GET", "/user", NULL, &st, &resp, err, sizeof err) != 0) { |
| 231 | yyjson_mut_obj_add_false(doc, root, "ok"); |
| 232 | yyjson_mut_obj_add_strcpy(doc, root, "error", err); |
| 233 | yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); |
| 234 | sx_cgi_header(200, "application/json; charset=utf-8"); |
| 235 | sx_print_json_doc(doc); |
| 236 | yyjson_mut_doc_free(doc); |
| 237 | free(resp); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | if (st >= 200 && st < 300) { |
| 242 | yyjson_doc *ud = yyjson_read(resp, strlen(resp), 0); |
| 243 | char who[128]; |
| 244 | who[0] = '\0'; |
| 245 | if (ud) { |
| 246 | yyjson_val *u = yyjson_doc_get_root(ud); |
| 247 | yyjson_val *login = yyjson_obj_get(u, "login"); |
| 248 | yyjson_val *type = yyjson_obj_get(u, "type"); |
| 249 | if (login && yyjson_is_str(login)) |
| 250 | gh_copy(who, sizeof who, yyjson_get_str(login)); |
| 251 | if (type && yyjson_is_str(type)) |
| 252 | yyjson_mut_obj_add_strcpy(doc, root, "user_type", |
| 253 | yyjson_get_str(type)); |
| 254 | yyjson_doc_free(ud); |
| 255 | } |
| 256 | yyjson_mut_obj_add_true(doc, root, "ok"); |
| 257 | yyjson_mut_obj_add_strcpy(doc, root, "user", who); |
| 258 | yyjson_mut_obj_add_int(doc, root, "status", st); |
| 259 | yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); |
| 260 | { |
| 261 | char rl[32]; |
| 262 | /* honest rate data straight from the headers of that call */ |
| 263 | yyjson_mut_obj_add_bool(doc, root, "authenticated", |
| 264 | cfg.has_token ? 1 : 0); |
| 265 | (void)rl; |
| 266 | } |
| 267 | } else { |
| 268 | char up[512]; |
| 269 | gh_upstream_error(resp, st, up, sizeof up); |
| 270 | yyjson_mut_obj_add_false(doc, root, "ok"); |
| 271 | yyjson_mut_obj_add_strcpy(doc, root, "error", up); |
| 272 | yyjson_mut_obj_add_int(doc, root, "status", st); |
| 273 | yyjson_mut_obj_add_strcpy(doc, root, "api_base", cfg.api_base); |
| 274 | } |
| 275 | free(resp); |
| 276 | |
| 277 | sx_cgi_header(200, "application/json; charset=utf-8"); |
| 278 | sx_print_json_doc(doc); |
| 279 | yyjson_mut_doc_free(doc); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int api_tools(const sx_plugin_info *info) |
| 284 | { |
| 285 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 286 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 287 | yyjson_mut_val *arr = yyjson_mut_arr(doc); |
| 288 | size_t i; |
| 289 | |
| 290 | (void)info; |
| 291 | yyjson_mut_doc_set_root(doc, root); |
| 292 | yyjson_mut_obj_add_true(doc, root, "ok"); |
| 293 | yyjson_mut_obj_add_val(doc, root, "tools", arr); |
| 294 | for (i = 0; i < NTOOLS; i++) { |
| 295 | yyjson_mut_val *t = yyjson_mut_obj(doc); |
| 296 | yyjson_mut_obj_add_strcpy(doc, t, "name", TOOLS[i].name); |
| 297 | yyjson_mut_obj_add_strcpy(doc, t, "description", TOOLS[i].description); |
| 298 | if (TOOLS[i].input_schema_json) { |
| 299 | /* embed the REAL schema the MCP server advertises, parsed, so |
| 300 | * the page shows exactly what --mcp tools/list returns */ |
| 301 | yyjson_doc *sd = yyjson_read(TOOLS[i].input_schema_json, |
| 302 | strlen(TOOLS[i].input_schema_json), 0); |
| 303 | if (sd) { |
| 304 | yyjson_mut_val *sv = yyjson_val_mut_copy(doc, yyjson_doc_get_root(sd)); |
| 305 | yyjson_mut_obj_add_val(doc, t, "inputSchema", sv); |
| 306 | yyjson_doc_free(sd); /* copy is independent of sd */ |
| 307 | } |
| 308 | } |
| 309 | yyjson_mut_arr_add_val(arr, t); |
| 310 | } |
| 311 | sx_cgi_header(200, "application/json; charset=utf-8"); |
| 312 | sx_print_json_doc(doc); |
| 313 | yyjson_mut_doc_free(doc); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int api_clear(const sx_plugin_info *info) |
| 318 | { |
| 319 | if (gh_config_save(NULL, NULL, 1) != 0) { |
| 320 | sx_json_error("could not rewrite the connector config", 500); |
| 321 | return 1; |
| 322 | } |
| 323 | return api_status(info); |
| 324 | } |
| 325 | |
| 326 | static int json_api(const sx_plugin_info *info, const char *query, |
| 327 | const char *action, const char *body) |
| 328 | { |
| 329 | /* every control action needs the node password; the page forwards it as |
| 330 | * X-Bot-Password / ?pwd= exactly like every other plugin */ |
| 331 | sx_require_node_auth(); |
| 332 | |
| 333 | if (strcmp(action, "status") == 0) return api_status(info); |
| 334 | if (strcmp(action, "save") == 0) return api_save(info, query, body); |
| 335 | if (strcmp(action, "test") == 0) return api_test(info); |
| 336 | if (strcmp(action, "tools") == 0) return api_tools(info); |
| 337 | if (strcmp(action, "clear") == 0) return api_clear(info); |
| 338 | |
| 339 | sx_json_error("unknown action", 400); |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* ----------------------------------------------------------------- HTML */ |
| 344 | |
| 345 | /* The page is one static constant generated from src/ui.html by |
| 346 | * tools/gen_ui.py; two control bytes mark where the SDK's theme block and |
| 347 | * the sx-theme handoff script splice in, so the theme can never drift from |
| 348 | * the rest of the bot. No CGI header re-emission, no re-serialisation. */ |
| 349 | static void emit_ui(void) |
| 350 | { |
| 351 | const char *p = UI_HTML; |
| 352 | |
| 353 | while (*p) { |
| 354 | if (*p == '\001') { |
| 355 | fputs(SX_THEME_PREPAINT_JS, stdout); |
| 356 | fputs("<style>", stdout); |
| 357 | fputs(SX_THEME_CSS, stdout); |
| 358 | fputs("</style>", stdout); |
| 359 | p++; |
| 360 | } else if (*p == '\002') { |
| 361 | fputs(SX_THEME_HANDOFF_JS, stdout); |
| 362 | p++; |
| 363 | } else { |
| 364 | putchar(*p++); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static int html_ui(const sx_plugin_info *info, const char *query, |
| 370 | int embed, const char *theme) |
| 371 | { |
| 372 | (void)info; |
| 373 | (void)query; |
| 374 | (void)embed; /* the page's own prepaint script flags <html embedded> */ |
| 375 | (void)theme; |
| 376 | /* The page is a control surface for a credential, so it is gated exactly |
| 377 | * like every ?action= is. The node proxy forwards X-Bot-Password when a |
| 378 | * browser reaches the plugin through bot-api, and an open node (no |
| 379 | * BOT_PASSWORD) stays open by design - but a passworded node answers 401 |
| 380 | * here rather than handing out the shell of the setup form. */ |
| 381 | sx_require_node_auth(); |
| 382 | sx_cgi_header(200, "text/html; charset=utf-8"); |
| 383 | emit_ui(); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /* ----------------------------------------------------------------- main */ |
| 388 | |
| 389 | int main(int argc, char **argv) |
| 390 | { |
| 391 | sx_stdio_binary(); |
| 392 | if (sx_has_flag(argc, argv, "--mcp")) |
| 393 | return sx_mcp_serve(&INFO, TOOLS, NTOOLS); |
| 394 | if (sx_has_flag(argc, argv, "--schema")) |
| 395 | return sx_mcp_schema(&INFO, TOOLS, NTOOLS); |
| 396 | return sx_main(argc, argv, &INFO, html_ui, json_api); |
| 397 | } |