/* * ws.h - shared declarations for plugin_websearch (SCORPIOX BOT open-source * multi-provider web search MCP connector). * * One binary, five modes, exactly as the plugin_ spec requires: * * 1. --meta / SX_PLUGIN_META=1 one line of discovery JSON (unauthenticated) * 2. --mcp MCP JSON-RPC 2.0 over stdio (web_search, * web_search_providers) * 3. --schema the tools/list array (compat) * 4. ?action=... | ?format=json the JSON API the setup page talks to * 5. GET (default) the provider/key/live-search page * * Files: * plugin_websearch.c main, --meta, MCP loop, CGI dispatch, auth, HTML * ws_buf.c growable buffer, JSON escaping, HTML entity decode, * tag strip, URL/param helpers * ws_config.c connector dir, config.json at 0600, env resolution * ws_http.c the one outbound HTTP call (verified TLS via the * vendored BearSSL transport in src/sx_http_tls.c) * ws_providers.c the six providers and their result normalisation * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_WS_H #define SX_WS_H #include #include #include "yyjson.h" #define WS_SLUG "websearch" #define WS_ID "plugin_websearch" #define WS_VER "1.0.4" #define WS_NAME "websearch" #define WS_DESC "Web search MCP" #define WS_MCP_PROTOCOL "2025-06-18" #define WS_SERVER_NAME "websearch-server" #define WS_STDIN_LINE_MAX (1024 * 1024) #define WS_BODY_MAX (1024 * 1024) #define WS_DEFAULT_COUNT 5 #define WS_MAX_COUNT 20 #define WS_MAX_RESULTS 25 /* ------------------------------------------------------------------ */ /* Growable byte buffer (ws_buf.c) */ /* ------------------------------------------------------------------ */ typedef struct { char *p; size_t n; size_t cap; } ws_buf; void ws_buf_init(ws_buf *b); int ws_buf_need(ws_buf *b, size_t extra); int ws_buf_add(ws_buf *b, const char *s, size_t n); int ws_buf_puts(ws_buf *b, const char *s); int ws_buf_putc(ws_buf *b, int c); int ws_buf_printf(ws_buf *b, const char *fmt, ...); const char *ws_buf_str(const ws_buf *b); void ws_buf_free(ws_buf *b); /* JSON string escape into a buffer (control characters, quotes, backslash). */ int ws_json_str(ws_buf *b, const char *s); /* HTML -> text: decodes the entities a search page emits and drops tags. * `out` is NUL-terminated and whitespace-collapsed. */ void ws_html_text(char *out, size_t outsz, const char *html); /* Decode entities only (no tag stripping), into out. */ void ws_html_unescape(char *out, size_t outsz, const char *s); /* Percent-decode a query-string value (also '+' -> space). */ void ws_url_decode(char *out, size_t outsz, const char *s); /* Percent-encode everything but [A-Za-z0-9._~-]. */ void ws_url_encode(const char *in, ws_buf *out); /* Copy with a size bound and always NUL-terminate. */ void ws_copy(char *dst, size_t capsz, const char *src); /* Case-insensitive compare/prefix. */ int ws_ieq(const char *a, const char *b); int ws_iprefix(const char *s, const char *prefix); /* Collapse runs of whitespace to single spaces, trim, in place. */ void ws_collapse(char *s); /* Bounded strstr that never reads past len (the needle length comes from the * needle itself, so a call site cannot pass a length that disagrees with it). */ const char *ws_memmem(const char *hay, size_t hlen, const char *needle); /* ------------------------------------------------------------------ */ /* One outbound HTTP exchange (ws_http.c) */ /* ------------------------------------------------------------------ */ typedef struct { int ok; /* 1 = an HTTP response was received */ char error[512]; /* set when ok == 0 */ long status; /* HTTP status */ char *body; /* heap, NUL-terminated (empty body -> "") */ size_t body_len; } ws_resp; /* method/url/headers("Name: V\r\n"...)/body. timeout_ms bounds connect and * each read. HTTP >= 400 is a normal response (status + body), not an error. */ int ws_http_do(const char *method, const char *url, const char *headers, const char *body, size_t body_len, long timeout_ms, ws_resp *out); void ws_resp_free(ws_resp *r); /* ------------------------------------------------------------------ */ /* Configuration (ws_config.c) */ /* ------------------------------------------------------------------ */ #define WS_KEY_MAX 256 #define WS_URL_MAX 512 #define WS_ID_MAX 32 typedef struct { char active[WS_ID_MAX]; /* provider id; "" = auto */ int count; /* default result count */ char region[24]; /* DuckDuckGo kl (e.g. nz-en) */ char safe[16]; /* strict | moderate | off */ char language[16]; /* ISO 639-1, for the providers that take it */ char searxng_categories[128]; int tavily_advanced; /* search_depth advanced */ int tavily_answer; /* include_answer */ char brave_key[WS_KEY_MAX]; char tavily_key[WS_KEY_MAX]; char exa_key[WS_KEY_MAX]; char pplx_key[WS_KEY_MAX]; char ddg_base[WS_URL_MAX]; char searxng_base[WS_URL_MAX]; char brave_base[WS_URL_MAX]; char tavily_base[WS_URL_MAX]; char exa_base[WS_URL_MAX]; char pplx_base[WS_URL_MAX]; } ws_config; /* Fills defaults, then config.json, then the environment (env wins). */ void ws_config_load(ws_config *c); /* Writes config.json atomically at mode 0600. 0 on success. */ int ws_config_save(const ws_config *c); /* The connector state directory, created on demand. 0 on success. */ int ws_config_dir(char *out, size_t capsz); /* Full path of config.json (dir is created). 0 on success. */ int ws_config_path(char *out, size_t capsz); /* ------------------------------------------------------------------ */ /* Providers (ws_providers.c) */ /* ------------------------------------------------------------------ */ #define WS_PROVIDER_COUNT 6 typedef struct { char title[512]; char url[1024]; char snippet[2048]; char published[64]; char site[128]; } ws_result; typedef struct { char query[512]; char provider[WS_ID_MAX]; int count; long long took_ms; ws_result *items; int n; int cap; int truncated; ws_buf answer; /* Tavily include_answer / Perplexity content */ ws_buf citations; /* Perplexity citations, newline separated */ } ws_results; void ws_results_init(ws_results *r); void ws_results_free(ws_results *r); ws_result *ws_results_push(ws_results *r); typedef struct { const char *id; const char *label; const char *hint; const char *signup; /* where a key is obtained ("" if none) */ int needs_key; int needs_base; /* self-hosted only (SearXNG) */ } ws_provider_info; extern const ws_provider_info WS_PROVIDERS[WS_PROVIDER_COUNT]; /* 1 when `id` names a provider. */ int ws_provider_known(const char *id); /* The provider that will actually be used for `cfg` (explicit or auto). */ const char *ws_provider_active(const ws_config *cfg); /* 1 when this provider has everything it needs (key / base url). */ int ws_provider_ready(const ws_config *cfg, const char *id); /* 1 when a key is configured for `id` (no secret is exposed). */ int ws_provider_has_key(const ws_config *cfg, const char *id); /* One search. Returns 0 on success (r filled), non-zero with a message in * err (and `status_out` set to the upstream HTTP status when there was one, * 0 otherwise). Never returns a key in any output. */ int ws_search(const ws_config *cfg, const char *provider, const char *query, int count, const char *country, const char *language, const char *freshness, const char *safe_override, ws_results *r, long *status_out, char *err, size_t errsz); /* The tools/list array as JSON text (caller frees). */ char *ws_tools_json(void); #endif /* SX_WS_H */