/* The MCP protocol (mcp_wire.c): one connection to one server. It speaks the current revision * (2026-07-28, stateless: every request carries its own _meta) first and falls back to the 2025 * handshake (initialize, notifications/initialized, a session id over HTTP) by the spec's rule: * a refusal that is not one of the current revision's errors means an older server. Transports: * streamable HTTP in every build (http.c), answered with JSON or a request-scoped SSE stream; * stdio (one JSON-RPC message per line) through mcp_stdio.c where processes exist. JSON goes * through SQLite's JSON functions. Nothing here touches a database file or a credential: the * caller hands over the headers with their secrets already opened. */ #ifndef PIC_MCP_WIRE_H #define PIC_MCP_WIRE_H #include #define PIC_MCP_CURRENT "2026-07-28" typedef struct { const char *name; /* the server's name, for messages */ /* streamable HTTP: the endpoint, https:// or http:// to loopback; NULL for stdio */ const char *url; const char *const *headers; /* "Name: value", secrets opened */ size_t nheaders; /* stdio: the program and its arguments (argv[0] is the program), "NAME=VALUE" strings added * to pic's environment, and the directory it starts in (NULL: the workspace's) */ const char *const *argv; const char *const *env; const char *cwd; const char *era; /* the revision the server spoke last time, or NULL to find out */ int timeout; /* seconds a request may go without an answer */ } pic_mcp_target; typedef struct pic_mcp_conn pic_mcp_conn; /* Connects to T (kept by pointer until pic_mcp_close) and settles the revision: with T->era the * current one, nothing is sent yet; otherwise server/discover is asked, then the 2025 handshake * when the server does not speak the current revision. NULL with err (never a header's value). */ pic_mcp_conn *pic_mcp_open(const pic_mcp_target *t, char *err, size_t errcap); /* Sends METHOD with PARAMS (a JSON object, or NULL for none); NAME is the tool, resource or * prompt it is about, for the current revision's Mcp-Name header (NULL: none). The result * object (malloc'd), or NULL with err: a transport failure, or the server's JSON-RPC error as * "CODE MESSAGE". *ANSWERED (may be NULL) is 1 when the server answered at all. A request the * server refused unread under a cached current revision is asked again after the handshake. */ char *pic_mcp_ask(pic_mcp_conn *c, const char *method, const char *params, const char *name, int *answered, char *err, size_t errcap); /* The revision spoken ("2026-07-28", "2025-11-25", ...), and the server's instructions or NULL. */ const char *pic_mcp_era(const pic_mcp_conn *c); const char *pic_mcp_instructions(const pic_mcp_conn *c); /* The server's capabilities object as server/discover or initialize gave it, or NULL when this * connection did not ask (a cached revision: open with T->era NULL to ask). */ const char *pic_mcp_capabilities(const pic_mcp_conn *c); /* Ends the connection: a 2025 session is ended with DELETE (best effort), a stdio server is * stopped. NULL is ignored. */ void pic_mcp_close(pic_mcp_conn *c); /* Every tool of the server, following nextCursor: a JSON array of the tools/list entries * (malloc'd), or NULL with err. */ char *pic_mcp_list_tools(pic_mcp_conn *c, char *err, size_t errcap); /* Every prompt of the server, the same way: a JSON array of prompts/list's entries. */ char *pic_mcp_list_prompts(pic_mcp_conn *c, char *err, size_t errcap); /* The stdio transport's seam (mcp_stdio.c on hosts; a build without processes has none and * refuses stdio servers). start runs T's program with pipes on its stdin and stdout; send * writes one line; recv reads one line within T->timeout seconds (malloc'd, NULL with err at the * end, a timeout or an interrupt); stop closes its stdin, then SIGTERM and SIGKILL to its * process group. run runs COMMAND through /bin/sh with ENV ("NAME=VALUE", NULL-ended; may be * NULL) added, and gives what it wrote on stdout (malloc'd, at most 64 KB) when it exits 0 within * TIMEOUT seconds, else NULL with err; its stdin is empty and its stderr is dropped. */ typedef struct pic_mcp_pipe pic_mcp_pipe; typedef struct { pic_mcp_pipe *(*start)(const pic_mcp_target *t, char *err, size_t errcap); int (*send)(pic_mcp_pipe *p, const char *line, size_t n, char *err, size_t errcap); char *(*recv)(pic_mcp_pipe *p, int timeout, char *err, size_t errcap); void (*stop)(pic_mcp_pipe *p); char *(*run)(const char *command, const char *const *env, int timeout, char *err, size_t errcap); } pic_mcp_pipe_ops; void pic_mcp_pipe_register(const pic_mcp_pipe_ops *ops); /* The registered seam's run, or NULL with err in a build that cannot run programs. */ char *pic_mcp_run(const char *command, const char *const *env, int timeout, char *err, size_t errcap); #endif