| 1 | /* |
| 2 | * ws.h - shared declarations for plugin_websearch (SCORPIOX BOT open-source |
| 3 | * multi-provider web search MCP connector). |
| 4 | * |
| 5 | * One binary, five modes, exactly as the plugin_<name> spec requires: |
| 6 | * |
| 7 | * 1. --meta / SX_PLUGIN_META=1 one line of discovery JSON (unauthenticated) |
| 8 | * 2. --mcp MCP JSON-RPC 2.0 over stdio (web_search, |
| 9 | * web_search_providers) |
| 10 | * 3. --schema the tools/list array (compat) |
| 11 | * 4. ?action=... | ?format=json the JSON API the setup page talks to |
| 12 | * 5. GET (default) the provider/key/live-search page |
| 13 | * |
| 14 | * Files: |
| 15 | * plugin_websearch.c main, --meta, MCP loop, CGI dispatch, auth, HTML |
| 16 | * ws_buf.c growable buffer, JSON escaping, HTML entity decode, |
| 17 | * tag strip, URL/param helpers |
| 18 | * ws_config.c connector dir, config.json at 0600, env resolution |
| 19 | * ws_http.c the one outbound HTTP call (verified TLS via the |
| 20 | * vendored BearSSL transport in src/sx_http_tls.c) |
| 21 | * ws_providers.c the six providers and their result normalisation |
| 22 | * |
| 23 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 24 | */ |
| 25 | #ifndef SX_WS_H |
| 26 | #define SX_WS_H |
| 27 | |
| 28 | #include <stddef.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include "yyjson.h" |
| 32 | |
| 33 | #define WS_SLUG "websearch" |
| 34 | #define WS_ID "plugin_websearch" |
| 35 | #define WS_VER "1.0.4" |
| 36 | #define WS_NAME "websearch" |
| 37 | #define WS_DESC "Web search MCP" |
| 38 | |
| 39 | #define WS_MCP_PROTOCOL "2025-06-18" |
| 40 | #define WS_SERVER_NAME "websearch-server" |
| 41 | |
| 42 | #define WS_STDIN_LINE_MAX (1024 * 1024) |
| 43 | #define WS_BODY_MAX (1024 * 1024) |
| 44 | #define WS_DEFAULT_COUNT 5 |
| 45 | #define WS_MAX_COUNT 20 |
| 46 | #define WS_MAX_RESULTS 25 |
| 47 | |
| 48 | /* ------------------------------------------------------------------ */ |
| 49 | /* Growable byte buffer (ws_buf.c) */ |
| 50 | /* ------------------------------------------------------------------ */ |
| 51 | |
| 52 | typedef struct { |
| 53 | char *p; |
| 54 | size_t n; |
| 55 | size_t cap; |
| 56 | } ws_buf; |
| 57 | |
| 58 | void ws_buf_init(ws_buf *b); |
| 59 | int ws_buf_need(ws_buf *b, size_t extra); |
| 60 | int ws_buf_add(ws_buf *b, const char *s, size_t n); |
| 61 | int ws_buf_puts(ws_buf *b, const char *s); |
| 62 | int ws_buf_putc(ws_buf *b, int c); |
| 63 | int ws_buf_printf(ws_buf *b, const char *fmt, ...); |
| 64 | const char *ws_buf_str(const ws_buf *b); |
| 65 | void ws_buf_free(ws_buf *b); |
| 66 | |
| 67 | /* JSON string escape into a buffer (control characters, quotes, backslash). */ |
| 68 | int ws_json_str(ws_buf *b, const char *s); |
| 69 | |
| 70 | /* HTML -> text: decodes the entities a search page emits and drops tags. |
| 71 | * `out` is NUL-terminated and whitespace-collapsed. */ |
| 72 | void ws_html_text(char *out, size_t outsz, const char *html); |
| 73 | /* Decode entities only (no tag stripping), into out. */ |
| 74 | void ws_html_unescape(char *out, size_t outsz, const char *s); |
| 75 | |
| 76 | /* Percent-decode a query-string value (also '+' -> space). */ |
| 77 | void ws_url_decode(char *out, size_t outsz, const char *s); |
| 78 | /* Percent-encode everything but [A-Za-z0-9._~-]. */ |
| 79 | void ws_url_encode(const char *in, ws_buf *out); |
| 80 | /* Copy with a size bound and always NUL-terminate. */ |
| 81 | void ws_copy(char *dst, size_t capsz, const char *src); |
| 82 | /* Case-insensitive compare/prefix. */ |
| 83 | int ws_ieq(const char *a, const char *b); |
| 84 | int ws_iprefix(const char *s, const char *prefix); |
| 85 | /* Collapse runs of whitespace to single spaces, trim, in place. */ |
| 86 | void ws_collapse(char *s); |
| 87 | /* Bounded strstr that never reads past len (the needle length comes from the |
| 88 | * needle itself, so a call site cannot pass a length that disagrees with it). */ |
| 89 | const char *ws_memmem(const char *hay, size_t hlen, const char *needle); |
| 90 | |
| 91 | /* ------------------------------------------------------------------ */ |
| 92 | /* One outbound HTTP exchange (ws_http.c) */ |
| 93 | /* ------------------------------------------------------------------ */ |
| 94 | |
| 95 | typedef struct { |
| 96 | int ok; /* 1 = an HTTP response was received */ |
| 97 | char error[512]; /* set when ok == 0 */ |
| 98 | long status; /* HTTP status */ |
| 99 | char *body; /* heap, NUL-terminated (empty body -> "") */ |
| 100 | size_t body_len; |
| 101 | } ws_resp; |
| 102 | |
| 103 | /* method/url/headers("Name: V\r\n"...)/body. timeout_ms bounds connect and |
| 104 | * each read. HTTP >= 400 is a normal response (status + body), not an error. */ |
| 105 | int ws_http_do(const char *method, const char *url, const char *headers, |
| 106 | const char *body, size_t body_len, long timeout_ms, |
| 107 | ws_resp *out); |
| 108 | void ws_resp_free(ws_resp *r); |
| 109 | |
| 110 | /* ------------------------------------------------------------------ */ |
| 111 | /* Configuration (ws_config.c) */ |
| 112 | /* ------------------------------------------------------------------ */ |
| 113 | |
| 114 | #define WS_KEY_MAX 256 |
| 115 | #define WS_URL_MAX 512 |
| 116 | #define WS_ID_MAX 32 |
| 117 | |
| 118 | typedef struct { |
| 119 | char active[WS_ID_MAX]; /* provider id; "" = auto */ |
| 120 | int count; /* default result count */ |
| 121 | char region[24]; /* DuckDuckGo kl (e.g. nz-en) */ |
| 122 | char safe[16]; /* strict | moderate | off */ |
| 123 | char language[16]; /* ISO 639-1, for the providers that take it */ |
| 124 | char searxng_categories[128]; |
| 125 | int tavily_advanced; /* search_depth advanced */ |
| 126 | int tavily_answer; /* include_answer */ |
| 127 | |
| 128 | char brave_key[WS_KEY_MAX]; |
| 129 | char tavily_key[WS_KEY_MAX]; |
| 130 | char exa_key[WS_KEY_MAX]; |
| 131 | char pplx_key[WS_KEY_MAX]; |
| 132 | |
| 133 | char ddg_base[WS_URL_MAX]; |
| 134 | char searxng_base[WS_URL_MAX]; |
| 135 | char brave_base[WS_URL_MAX]; |
| 136 | char tavily_base[WS_URL_MAX]; |
| 137 | char exa_base[WS_URL_MAX]; |
| 138 | char pplx_base[WS_URL_MAX]; |
| 139 | } ws_config; |
| 140 | |
| 141 | /* Fills defaults, then config.json, then the environment (env wins). */ |
| 142 | void ws_config_load(ws_config *c); |
| 143 | /* Writes config.json atomically at mode 0600. 0 on success. */ |
| 144 | int ws_config_save(const ws_config *c); |
| 145 | /* The connector state directory, created on demand. 0 on success. */ |
| 146 | int ws_config_dir(char *out, size_t capsz); |
| 147 | /* Full path of config.json (dir is created). 0 on success. */ |
| 148 | int ws_config_path(char *out, size_t capsz); |
| 149 | |
| 150 | /* ------------------------------------------------------------------ */ |
| 151 | /* Providers (ws_providers.c) */ |
| 152 | /* ------------------------------------------------------------------ */ |
| 153 | |
| 154 | #define WS_PROVIDER_COUNT 6 |
| 155 | |
| 156 | typedef struct { |
| 157 | char title[512]; |
| 158 | char url[1024]; |
| 159 | char snippet[2048]; |
| 160 | char published[64]; |
| 161 | char site[128]; |
| 162 | } ws_result; |
| 163 | |
| 164 | typedef struct { |
| 165 | char query[512]; |
| 166 | char provider[WS_ID_MAX]; |
| 167 | int count; |
| 168 | long long took_ms; |
| 169 | ws_result *items; |
| 170 | int n; |
| 171 | int cap; |
| 172 | int truncated; |
| 173 | ws_buf answer; /* Tavily include_answer / Perplexity content */ |
| 174 | ws_buf citations; /* Perplexity citations, newline separated */ |
| 175 | } ws_results; |
| 176 | |
| 177 | void ws_results_init(ws_results *r); |
| 178 | void ws_results_free(ws_results *r); |
| 179 | ws_result *ws_results_push(ws_results *r); |
| 180 | |
| 181 | typedef struct { |
| 182 | const char *id; |
| 183 | const char *label; |
| 184 | const char *hint; |
| 185 | const char *signup; /* where a key is obtained ("" if none) */ |
| 186 | int needs_key; |
| 187 | int needs_base; /* self-hosted only (SearXNG) */ |
| 188 | } ws_provider_info; |
| 189 | |
| 190 | extern const ws_provider_info WS_PROVIDERS[WS_PROVIDER_COUNT]; |
| 191 | |
| 192 | /* 1 when `id` names a provider. */ |
| 193 | int ws_provider_known(const char *id); |
| 194 | /* The provider that will actually be used for `cfg` (explicit or auto). */ |
| 195 | const char *ws_provider_active(const ws_config *cfg); |
| 196 | /* 1 when this provider has everything it needs (key / base url). */ |
| 197 | int ws_provider_ready(const ws_config *cfg, const char *id); |
| 198 | /* 1 when a key is configured for `id` (no secret is exposed). */ |
| 199 | int ws_provider_has_key(const ws_config *cfg, const char *id); |
| 200 | |
| 201 | /* One search. Returns 0 on success (r filled), non-zero with a message in |
| 202 | * err (and `status_out` set to the upstream HTTP status when there was one, |
| 203 | * 0 otherwise). Never returns a key in any output. */ |
| 204 | int ws_search(const ws_config *cfg, const char *provider, const char *query, |
| 205 | int count, const char *country, const char *language, |
| 206 | const char *freshness, const char *safe_override, |
| 207 | ws_results *r, long *status_out, char *err, size_t errsz); |
| 208 | |
| 209 | /* The tools/list array as JSON text (caller frees). */ |
| 210 | char *ws_tools_json(void); |
| 211 | |
| 212 | #endif /* SX_WS_H */ |