ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
protocol.c File Reference

Go to the source code of this file.

Functions

yyjson_mut_doc * mcp_error (yyjson_val *id, int code, const char *message)
void mcp_jobs_reap (mcp_server_t *s, bool all)
yyjson_mut_doc * mcp_dispatch (mcp_server_t *s, yyjson_doc *message, void *connection, const char *session_id, bool *pending)

Function Documentation

◆ mcp_dispatch()

yyjson_mut_doc * mcp_dispatch ( mcp_server_t * s,
yyjson_doc * message,
void * connection,
const char * session_id,
bool * pending )

Definition at line 259 of file mcp-server/protocol.c.

261 {
262 ASCII_OTEL_SCOPE(MCP_REQUEST);
263 ASCII_OTEL_ATTR_STRING("rpc.system", "jsonrpc");
264 ASCII_OTEL_ATTR_STRING("mcp.transport", s->policy.hosted ? "http" : "stdio");
266 *pending = false;
267 yyjson_val *root = yyjson_doc_get_root(message), *id = mcp_get(root, "id"),
268 *method = mcp_get(root, "method"),
269 *version = mcp_get(root, "jsonrpc");
270 if (!yyjson_is_obj(root) || !yyjson_is_str(version) ||
271 strcmp(yyjson_get_str(version), "2.0") || !yyjson_is_str(method) ||
272 strlen(yyjson_get_str(method)) != yyjson_get_len(method) ||
273 (id && !yyjson_is_str(id) && !yyjson_is_int(id)))
274 return mcp_error(id, -32600, "Invalid JSON-RPC request");
275 const char *name = yyjson_get_str(method);
276 static const char *const methods[] = {"initialize",
277 "ping",
278 "tools/list",
279 "tools/call",
280 "notifications/initialized",
281 "notifications/cancelled"};
282 const char *method_name = "unknown";
283 for (size_t i = 0; i < sizeof(methods) / sizeof(methods[0]); i++)
284 if (!strcmp(name, methods[i]))
285 method_name = methods[i];
286 ASCII_OTEL_ATTR_STRING("rpc.method", method_name);
287 yyjson_val *params = mcp_get(root, "params");
288 if (params && !yyjson_is_obj(params))
289 return id ? mcp_error(id, -32602, "params must be an object") : NULL;
290 if (!id) {
291 if (!strcmp(name, "notifications/initialized"))
292 s->initialized = true;
293 if (!strcmp(name, "notifications/cancelled")) {
294 yyjson_val *cancel = mcp_get(params, "requestId");
295 for (unsigned i = 0; i < s->policy.max_jobs; ++i)
296 if (s->jobs[i].renderer_job.started &&
297 (!s->policy.hosted ||
298 (session_id && *session_id &&
299 !strcmp(s->jobs[i].session_id, session_id)) ||
300 ((!session_id || !*session_id) &&
301 s->jobs[i].connection == connection)) &&
302 same_id(cancel,
303 mcp_get(yyjson_doc_get_root(s->jobs[i].message), "id")))
305 }
306 return NULL;
307 }
308 yyjson_mut_doc *d = envelope(id);
309 yyjson_mut_val *result = yyjson_mut_obj(d);
310 yyjson_mut_obj_add_val(d, yyjson_mut_doc_get_root(d), "result", result);
311 if (!strcmp(name, "initialize")) {
312 yyjson_val *requested = mcp_get(params, "protocolVersion");
313 const char *v = yyjson_get_str(requested);
314 if (!v || (strcmp(v, "2025-11-25") && strcmp(v, "2025-06-18") &&
315 strcmp(v, "2025-03-26")))
316 v = "2025-11-25";
317 yyjson_mut_obj_add_strcpy(d, result, "protocolVersion", v);
318 yyjson_mut_val *caps = yyjson_mut_obj(d), *info = yyjson_mut_obj(d);
319 yyjson_mut_obj_add_val(d, caps, "tools", yyjson_mut_obj(d));
320 yyjson_mut_obj_add_val(d, result, "capabilities", caps);
321 yyjson_mut_obj_add_str(d, info, "name", "ascii-chat");
322 yyjson_mut_obj_add_str(d, info, "version", ASCII_CHAT_VERSION_FULL);
323 yyjson_mut_obj_add_val(d, result, "serverInfo", info);
324 const char *instructions =
326 ? "Render uploaded bytes or HTTP(S) media URLs as ASCII art. The "
327 "hosted server cannot access your webcam "
328 "or "
329 "local filesystem. Rendered files are returned as temporary "
330 "download links with UTC expiration "
331 "timestamps; "
332 "links expire after 30 minutes. Use output_format to select the "
333 "file format, snapshot-delay to set clip "
334 "duration, and options.audio=true to include source audio."
335 : "Render media as ASCII art. Uploaded bytes must be base64 "
336 "encoded by the client. snapshot-delay measures "
337 "capture duration after the first rendered frame; use seek for "
338 "media timestamps. output_path saves ASCII "
339 "art "
340 "as an image/video and never replaces an existing file.";
341 yyjson_mut_obj_add_str(d, result, "instructions", instructions);
342 return d;
343 }
344 if (!strcmp(name, "ping"))
345 return d;
346 if (!strcmp(name, "tools/list")) {
347 yyjson_mut_obj_add_val(d, result, "tools", mcp_tools_schema(d, &s->policy));
348 return d;
349 }
350 yyjson_mut_doc_free(d);
351 if (strcmp(name, "tools/call"))
352 return mcp_error(id, -32601, "Method not found");
353 const char *tool = yyjson_get_str(mcp_get(params, "name"));
354 if (tool &&
355 yyjson_get_len(mcp_get(params, "name")) == sizeof("device_list") - 1 &&
356 !strcmp(tool, "device_list")) {
357 if (s->policy.restricted)
358 return mcp_error(id, -32602, "Unknown tool");
359 ASCII_OTEL_ATTR_STRING("mcp.tool", "device_list");
360 return device_list_result(id, mcp_get(params, "arguments"));
361 }
362 const char *names[] = {"render_file", "render_url", "render_webcam",
363 "render_visualization"};
364 int kind = -1;
365 for (int i = 0; i < 4; ++i)
366 if (tool && strlen(tool) == yyjson_get_len(mcp_get(params, "name")) &&
367 !strcmp(tool, names[i]))
368 kind = i;
369 if (kind < 0 || (s->policy.restricted && kind == MCP_WEBCAM))
370 return mcp_error(id, -32602, "Unknown tool");
371 mcp_jobs_reap(s, false);
372 mcp_job_t *j = NULL;
373 for (unsigned i = 0; i < s->policy.max_jobs; ++i)
374 if (!s->jobs[i].renderer_job.started) {
375 j = &s->jobs[i];
376 break;
377 }
378 if (!j) {
379 char busy_message[128];
380 safe_snprintf(busy_message, sizeof(busy_message),
381 "Server busy; at most %u render%s may run concurrently",
382 s->policy.max_jobs, s->policy.max_jobs == 1 ? "" : "s");
383 return mcp_error(id, -32000, busy_message);
384 }
385 yyjson_val *arguments = mcp_get(params, "arguments");
386 yyjson_doc *empty = NULL;
387 if (!arguments) {
388 empty = yyjson_read("{}", 2, 0);
389 arguments = yyjson_doc_get_root(empty);
390 }
392 mcp_request_parse(&s->policy, (mcp_tool_t)kind, arguments, &j->request);
393 if (empty)
394 yyjson_doc_free(empty);
395 if (err != ASCIICHAT_OK) {
397 ASCII_OTEL_ATTR_STRING("error.type", "invalid_arguments");
400 SAFE_STRNCPY(fail.error,
401 HAS_ERRNO(&ctx) ? ctx.context_message
402 : "Invalid render request",
403 sizeof(fail.error));
405 return tool_result(id, &fail);
406 }
407 // Copy before returning to the transport, which owns and frees the input
408 // document.
409 size_t len;
410 char *copy = yyjson_write(message, 0, &len);
411 j->message = yyjson_read(copy, len, 0);
412 free(copy);
413 j->tool = (mcp_tool_t)kind;
415 j->server = s;
416 j->connection = connection;
417 if (session_id)
419 if (http_renderer_job_start(&j->renderer_job, "mcp-render", run_job, j) !=
420 ASCIICHAT_OK) {
422 yyjson_doc_free(j->message);
423 memset(j, 0, sizeof(*j));
424 return mcp_error(id, -32603, "Cannot create render worker");
425 }
426 *pending = true;
427 return NULL;
428}
#define HAS_ERRNO(context)
#define SAFE_STRNCPY(dst, src, size)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
asciichat_error_t http_renderer_job_start(http_renderer_job_t *job, const char *name, http_renderer_job_fn execute, void *context)
void http_renderer_job_cancel(http_renderer_job_t *job)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
yyjson_mut_doc * mcp_error(yyjson_val *id, int code, const char *message)
void mcp_jobs_reap(mcp_server_t *s, bool all)
void mcp_request_destroy(http_renderer_process_request_t *request)
Definition request.c:801
yyjson_mut_val * mcp_tools_schema(yyjson_mut_doc *doc, const mcp_policy_t *policy)
Definition request.c:173
mcp_tool_t
Definition mcp.h:35
@ MCP_WEBCAM
Definition mcp.h:35
struct mcp_job mcp_job_t
When code needs to work with mcp policy, it keeps hosted, restricted, address, address6 here instead ...
asciichat_error_t mcp_request_parse(const mcp_policy_t *policy, mcp_tool_t tool, yyjson_val *arguments, http_renderer_process_request_t *out)
Definition request.c:483
gotel_context_t ascii_otel_context(void)
Definition otel.c:870
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:92
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:88
uint8_t session_id[SESSION_ID_SIZE]
yyjson_doc * message
Definition mcp.h:66
mcp_tool_t tool
Definition mcp.h:69
void * connection
Definition mcp.h:72
gotel_context_t telemetry_parent
Definition mcp.h:70
http_renderer_job_t renderer_job
Definition mcp.h:71
char session_id[32+1]
Definition mcp.h:73
struct mcp_server * server
Definition mcp.h:65
http_renderer_process_request_t request
Definition mcp.h:68
unsigned max_jobs
Definition mcp.h:42
bool restricted
Definition mcp.h:38
bool hosted
Definition mcp.h:37
bool initialized
Definition mcp.h:92
mcp_job_t * jobs
Definition mcp.h:88
mcp_policy_t policy
Definition mcp.h:87

References ASCII_OTEL_ATTR_STRING, ascii_otel_context(), ASCII_OTEL_SCOPE, ASCIICHAT_OK, mcp_job::connection, asciichat_error_context_t::context_message, http_renderer_process_result_t::error, HAS_ERRNO, mcp_policy_t::hosted, http_renderer_job_cancel(), http_renderer_job_start(), mcp_server::initialized, mcp_server::jobs, mcp_policy_t::max_jobs, mcp_error(), mcp_jobs_reap(), mcp_request_destroy(), mcp_request_parse(), mcp_tools_schema(), MCP_WEBCAM, mcp_job::message, mcp_server::policy, mcp_job::renderer_job, mcp_job::request, mcp_policy_t::restricted, SAFE_STRNCPY, mcp_job::server, mcp_job::session_id, session_id, http_renderer_job::started, stats_counter_add(), STATS_COUNTER_MCP_REJECTED, STATS_COUNTER_MCP_REQUESTS, stats_runtime_scope(), mcp_job::telemetry_parent, and mcp_job::tool.

Referenced by mcp_stdio_run().

◆ mcp_error()

yyjson_mut_doc * mcp_error ( yyjson_val * id,
int code,
const char * message )

Definition at line 18 of file mcp-server/protocol.c.

18 {
19 ASCII_OTEL_SCOPE(MCP_PROTOCOL_ERROR);
20 ASCII_OTEL_ATTR_INT("rpc.jsonrpc.error_code", code);
22 ascii_otel_error("MCP protocol request rejected");
23 yyjson_mut_doc *d = envelope(id);
24 yyjson_mut_val *e = yyjson_mut_obj(d);
25 yyjson_mut_obj_add_int(d, e, "code", code);
26 yyjson_mut_obj_add_strcpy(d, e, "message", message);
27 yyjson_mut_obj_add_val(d, yyjson_mut_doc_get_root(d), "error", e);
28 return d;
29}
void ascii_otel_error(const char *message)
Definition otel.c:965
#define ASCII_OTEL_ATTR_INT(key, value)
Definition otel.h:94

References ASCII_OTEL_ATTR_INT, ascii_otel_error(), ASCII_OTEL_SCOPE, stats_counter_add(), STATS_COUNTER_MCP_REJECTED, and stats_runtime_scope().

Referenced by mcp_dispatch(), and mcp_stdio_run().

◆ mcp_jobs_reap()

void mcp_jobs_reap ( mcp_server_t * s,
bool all )

Definition at line 237 of file mcp-server/protocol.c.

237 {
238 for (unsigned i = 0; i < s->policy.max_jobs; ++i) {
239 mcp_job_t *j = &s->jobs[i];
240 if (!j->renderer_job.started ||
241 (!all && (!http_renderer_job_is_done(&j->renderer_job) ||
242 (s->policy.hosted && j->connection))))
243 continue;
245 yyjson_doc_free(j->message);
246 if (j->response)
247 yyjson_mut_doc_free(j->response);
248 memset(j, 0, sizeof(*j));
249 }
250}
void http_renderer_job_join(http_renderer_job_t *job)
bool http_renderer_job_is_done(const http_renderer_job_t *job)
yyjson_mut_doc * response
Definition mcp.h:67

References mcp_job::connection, mcp_policy_t::hosted, http_renderer_job_is_done(), http_renderer_job_join(), mcp_server::jobs, mcp_policy_t::max_jobs, mcp_job::message, mcp_server::policy, mcp_job::renderer_job, mcp_job::response, and http_renderer_job::started.

Referenced by mcp_dispatch(), mcp_http_run(), mcp_server_main(), and mcp_stdio_run().