/* * gh.h - shared declarations for plugin_github (SCORPIOX BOT open-source * GitHub MCP connector). * * One binary, five modes, exactly as the plugin_ spec requires: * * 1. --meta / SX_PLUGIN_META=1 one line of discovery JSON (unauthenticated) * {"name":"github","desc":"GitHub tools","kind":"mcp","mcp":true, * "daemon":false,"menus":false,"verified":"mock"} * 2. --mcp MCP JSON-RPC 2.0 over stdio with five tools: * github_get_repo, github_list_issues, * github_create_issue, github_get_pull_request, * github_search_code * 3. --schema the tools/list array (compat) * 4. ?action=... | ?format=json the node-auth-gated JSON API the setup page * talks to (status/save/test/tools/clear) * 5. GET (default) the PAT + API-base setup page * * Files: * plugin_github.c identity + tool table + main() + MCP/CGI dispatch * gh_api.c the one outbound HTTP call (verified TLS via the * vendored BearSSL transport in src/sx_http_tls.c) and * the five REST mappings + result shaping * gh_config.c connector dir, config.json at mode 0600, env resolution, * growable buffer + helpers * sx_connector.* the SCORPIOX BOT Connector SDK runtime (vendored) * sx_http_tls.* HTTP/1.1 + TLS client (vendored BearSSL, flat tree in * vendor/bearssl) * * Clean-room rule: zero LAN literals, zero credentials, zero external * dependencies. The API base comes from the environment or the config file, * the token from the environment or config.json - never from source. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_GH_H #define SX_GH_H #include #include #include "yyjson.h" #include "sx_connector.h" #define GH_SLUG "github" #define GH_ID "plugin_github" #define GH_VER "1.0.2" #define GH_NAME "github" #define GH_DESC "GitHub tools" #define GH_DEFAULT_API_BASE "https://api.github.com" #define GH_API_VERSION "2022-11-28" #define GH_UA "scorpiox-bot-plugin-github/" GH_VER /* Config file inside the connector directory, written at mode 0600. */ #define GH_CONFIG_FILE "config.json" #define GH_HTTP_TIMEOUT_MS 20000 /* A 429/5xx is retried once, sleeping min(Retry-After, GH_RETRY_MAX_MS). */ #define GH_RETRY_MAX_MS 5000 /* Shaped tool output is capped under the SDK's 64 KiB reply buffer. */ #define GH_OUT_CAP 48000 /* An issue/PR page is fetched with this many entries per request. */ #define GH_PER_PAGE 30 /* github_search_code reports at most this many items. */ #define GH_SEARCH_MAX_ITEMS 10 /* ------------------------------------------------------------------ */ /* Growable byte buffer + small helpers (gh_config.c) */ /* ------------------------------------------------------------------ */ typedef struct { char *p; size_t n; size_t cap; } gh_buf; void gh_buf_init(gh_buf *b); int gh_buf_need(gh_buf *b, size_t extra); int gh_buf_add(gh_buf *b, const char *s, size_t len); int gh_buf_puts(gh_buf *b, const char *s); int gh_buf_putc(gh_buf *b, int c); int gh_buf_printf(gh_buf *b, const char *fmt, ...); const char *gh_buf_str(const gh_buf *b); void gh_buf_free(gh_buf *b); /* JSON string escape into a buffer (quotes, backslash, control chars). * Non-UTF-8 bytes are replaced with U+FFFD so the reply is always valid. */ int gh_json_str(gh_buf *b, const char *s); /* Bounded copy that always NUL-terminates. */ void gh_copy(char *dst, size_t capsz, const char *src); /* Percent-encode everything but [A-Za-z0-9._~-] into a buffer. * (gh_buf version; sx_url_encode covers the fixed-buffer cases.) */ void gh_url_encode(const char *in, gh_buf *out); /* ------------------------------------------------------------------ */ /* Config (gh_config.c) */ /* ------------------------------------------------------------------ */ typedef struct { char api_base[256]; /* always resolved, never empty */ char token[256]; /* "" == unauthenticated (public reads only) */ int has_token; } gh_config; /* Resolve configuration. Precedence for each value: * GITHUB_API_BASE / GITHUB_TOKEN environment > config.json > default. * Returns 0 on success (config is always usable, possibly token-less). */ int gh_config_load(gh_config *cfg); /* Persist api_base and/or token. Empty/NULL arguments leave the stored value * unchanged EXCEPT force_clear_token=1 which wipes the token. Writes * config.json atomically at mode 0600. 0 on success, -1 on failure. */ int gh_config_save(const char *api_base, const char *token, int force_clear_token); /* Validate + normalise an API base URL: http(s) only, no userinfo, no query, * a trailing slash is dropped. Returns 0 ok, -1 with a message in err. */ int gh_api_base_valid(const char *base, char *out, size_t outsz, char *err, size_t errsz); /* ------------------------------------------------------------------ */ /* HTTP + REST (gh_api.c) */ /* ------------------------------------------------------------------ */ /* Perform one GitHub API request. `path` starts with "/" and may carry a * query string; it is appended to cfg->api_base. `body` (JSON or NULL). * On 429/5xx the request is retried once after honouring Retry-After * (capped at GH_RETRY_MAX_MS). *status receives the HTTP code. The body is * malloc'd into *resp (caller frees, may be "" but never NULL on success of * the transport). Returns 0 when the transport worked (whatever the HTTP * status), -1 with a transport error message in err. */ int gh_api_call(const gh_config *cfg, const char *method, const char *path, const char *body, long *status, char **resp, char *err, size_t errsz); /* Extract {"message": "..."} (and documentation_url when present) from an * error body and format the upstream refusal: the relay rule - the message * GitHub itself answered is shown, never a generic invented one. */ void gh_upstream_error(const char *resp, long status, char *err, size_t errsz); /* ---- the five tools: shape human-readable text into out ---- */ int gh_tool_get_repo(const char *args_json, char *out, size_t outsz, char *err, size_t errsz); int gh_tool_list_issues(const char *args_json, char *out, size_t outsz, char *err, size_t errsz); int gh_tool_create_issue(const char *args_json, char *out, size_t outsz, char *err, size_t errsz); int gh_tool_get_pull_request(const char *args_json, char *out, size_t outsz, char *err, size_t errsz); int gh_tool_search_code(const char *args_json, char *out, size_t outsz, char *err, size_t errsz); /* Small arg helper: read a string argument into a fixed buffer. * Returns 1 found, 0 absent (required missing -> error is emitted). */ int gh_arg_str(const char *args_json, const char *key, char *out, size_t capsz, int required, char *err, size_t errsz); /* Read an integer-ish argument (int or numeric string). */ int gh_arg_int(const char *args_json, const char *key, long *out_val, int required, char *err, size_t errsz); #endif /* SX_GH_H */