From b66e3ce32d133113804cfd20d3b4c8832e86e161 Mon Sep 17 00:00:00 2001 From: Hermes Date: Mon, 23 Nov 2026 08:12:29 +0000 Subject: [PATCH] libmcp: arguments that arrive as a string are parsed, not dropped Refresh the vendored header to libmcp 6d2fcb4. A client in the OpenAI function-call shape sends "arguments":"{\"key\":1}" - a string, not an object. tools_call handed that straight to the handler, so mcp_arg() returned NULL for every key and a handler that filters on an argument saw "no filter" and acted on everything. A value that is neither absent, an object, nor a string holding an object is now refused with an isError answer instead of silently becoming "no arguments". Built and tested here: make test green. --- vendor/libmcp/mcp.h | 62 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/vendor/libmcp/mcp.h b/vendor/libmcp/mcp.h index 183ba20..8d9ea80 100644 --- a/vendor/libmcp/mcp.h +++ b/vendor/libmcp/mcp.h @@ -731,22 +731,80 @@ static void tools_list(mcp_conn *c, const mcp_server *srv) mcp_buf_free(&b); } +/* + * The arguments of a tools/call, always as an object or NULL. + * + * Some clients send arguments as a JSON *string* instead of an object: the + * OpenAI/Anthropic function-call shape has "arguments":"{\"key\":1}", and a + * proxy that forwards that shape unchanged produces exactly such a request. + * Passing the string straight to the handler made mcp_arg() return NULL for + * every key, so a handler that filters on an argument silently saw "no + * filter" and acted on everything - task_next(scope="splinter") handed out a + * makronix task because scope was invisible to it. So: a string is parsed, + * and anything that does not yield an object is an error, never "no + * arguments". + * + * Returns 0 and sets *outp, or -1 when the value is neither absent, an object, + * nor a string holding an object. *owned says who owns the object: when it is + * 0 the object is part of the request and must NOT be freed; when it is 1 the + * caller frees it with mcp_json_free. + */ +static int call_arguments(const mcp_json *params, mcp_json **outp, int *owned) +{ + const mcp_json *args = params != NULL ? mcp_arg(params, "arguments") : NULL; + + *outp = NULL; + *owned = 0; + if (args == NULL || args->t == MCP_NULL) + return 0; + if (args->t == MCP_OBJ) { + *outp = (mcp_json *)args; + return 0; + } + if (args->t == MCP_STR) { + mcp_json *v = mcp_parse(args->s); + + if (v == NULL || v->t != MCP_OBJ) { + mcp_json_free(v); + return -1; + } + *outp = v; + *owned = 1; + return 0; + } + return -1; +} + static void tools_call(mcp_conn *c, const mcp_server *srv, const mcp_json *params) { const char *name = mcp_arg_string(params, "name"); - const mcp_json *args = mcp_arg(params, "arguments"); + mcp_json *parsed = NULL; + const mcp_json *args; + int owned = 0; size_t i; if (name == NULL) { mcp_reply_rpc_error(c, -32602, "missing tool name"); return; } + if (call_arguments(params, &parsed, &owned) != 0) { + mcp_buf b; + + mcp_buf_init(&b); + mcp_buf_printf(&b, "the arguments of '%s' are not a JSON object", name); + emit_content(c, b.p, 1); + mcp_buf_free(&b); + return; + } + args = parsed; for (i = 0; i < srv->ntools; i++) { if (strcmp(srv->tools[i].name, name) != 0) continue; c->replied = 0; if (srv->tools[i].handler != NULL) srv->tools[i].handler(c, args, srv->user); + if (owned) + mcp_json_free(parsed); if (!c->replied) { mcp_buf b; @@ -760,6 +818,8 @@ static void tools_call(mcp_conn *c, const mcp_server *srv, const mcp_json *param { mcp_buf b; + if (owned) + mcp_json_free(parsed); mcp_buf_init(&b); mcp_buf_printf(&b, "unknown tool: %s", name); mcp_reply_rpc_error(c, -32601, b.p);