mcp: JSON-RPC client + ffi/curl status_code; PHASE0 §4 amended

First commit of Phase 2 per docs/PHASE2.md §12. Three changes bundled:

mcp.lua (new, 153 lines):
  - M.connect(url, opts) returns a Session.
  - Session:initialize() round-trips initialize + notifications/initialized
    + tools/list. Caches tools for session lifetime (lmcp announces
    capabilities.tools.listChanged = false; no refetch).
  - Session:list_tools() returns the cached tool list.
  - Session:call_tool(name, args) returns (result_table, kind) where
    kind ∈ {"ok", "handler_error", "rpc_error", "transport_error"} per
    the §4 error split. Folded HTTP-level failure into transport_error.
  - Per-server Bearer auth via opts.auth_token or opts.auth_env env-var
    indirection.
  - Captures protocolVersion mismatch as a warning string rather than
    aborting (lmcp doesn't negotiate — N3 in review).

ffi/curl.lua extension:
  - Add curl_easy_getinfo to ffi.cdef.
  - Pre-cast as getinfo_long; helper get_response_code() fetches
    CURLINFO_RESPONSE_CODE (decimal 2097154 = CURLINFOTYPE_LONG | 2).
  - M.post now returns (body, status_code) on transport success;
    (nil, errmsg) on libcurl failure stays unchanged. Phase 1 callers
    reading only the first slot are unaffected.

docs/PHASE0.md §4:
  - Insert `mcp.lua` between broker.lua and router.lua per PHASE2.md §9.
  - Module-stability invariant clarified: rename prohibition is what
    matters; adding new files is additive.

Smoke-test passes for all four kinds against boltzmann lmcp v0.5.4:
  - initialize: ok (7 tools cached)
  - list_dir /tmp: ok (1.2KB content)
  - read_file /nonexistent: ok (boltzmann's baseline §3 quirk —
    isError:false even on failure; content is authoritative)
  - nope_tool: rpc_error (code=-32601)
  - wrong auth: transport_error (HTTP 401)
  - unreachable host: transport_error (DNS failure)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-12 13:06:39 +00:00
parent f5daa6afc0
commit 6c194deea0
3 changed files with 184 additions and 5 deletions
+29 -4
View File
@@ -25,6 +25,7 @@ struct curl_slist *curl_slist_append(struct curl_slist *list, const char *string
void curl_slist_free_all(struct curl_slist *list);
int curl_easy_setopt(CURL *handle, int option, ...);
int curl_easy_getinfo(CURL *handle, int info, ...);
]]
-- libcurl-dev's unversioned `libcurl.so` symlink isn't assumed; fall back to
@@ -64,13 +65,36 @@ local setopt_str = ffi.cast("int(*)(void*, int, const char*)", C.curl_easy_seto
local setopt_long = ffi.cast("int(*)(void*, int, long)", C.curl_easy_setopt)
local setopt_ptr = ffi.cast("int(*)(void*, int, void*)", C.curl_easy_setopt)
-- curl_easy_getinfo is variadic too. The Phase 2 caller only needs the
-- CURLINFO_LONG family (HTTP response code); pre-cast to that signature.
-- CURLINFO_RESPONSE_CODE = CURLINFO_LONG (0x200000) + 2 = 2097154.
local getinfo_long = ffi.cast("int(*)(void*, int, long*)", C.curl_easy_getinfo)
local INFO_RESPONSE_CODE = 2097154
local function get_response_code(handle)
local out = ffi.new("long[1]")
if getinfo_long(handle, INFO_RESPONSE_CODE, out) == 0 then
return tonumber(out[0])
end
return 0 -- 0 = no response (e.g. couldn't connect)
end
local M = {}
-- POST `body` to `url` with `headers` (list of "Name: value" strings) and an
-- optional `timeout_ms`.
-- Returns:
-- string response body on success
-- nil, errmsg on libcurl failure (non-zero CURLcode)
-- body, status_code on transport success — body is the raw response
-- string (may be empty); status_code is the HTTP
-- response code (2xx success, 4xx/5xx surface as
-- transport-level failure for callers that care,
-- e.g. mcp.lua treating 401 as auth failure).
-- FAILONERROR is intentionally NOT set so the body
-- is observable on non-2xx (lmcp's 401 returns a
-- non-JSON-RPC body that callers need to recognise).
-- nil, errmsg on libcurl-level failure (non-zero CURLcode)
-- Phase 1 callers reading only the first slot stay correct: success
-- returns truthy body, failure returns nil — same disjunction as before.
function M.post(url, body, headers, timeout_ms)
local handle = C.curl_easy_init()
if handle == nil then return nil, "curl_easy_init returned NULL" end
@@ -101,9 +125,10 @@ function M.post(url, body, headers, timeout_ms)
end
local rc = C.curl_easy_perform(handle)
local result, err
local result, status, err
if rc == 0 then
result = table.concat(chunks)
status = get_response_code(handle)
else
err = ffi.string(C.curl_easy_strerror(rc))
end
@@ -112,7 +137,7 @@ function M.post(url, body, headers, timeout_ms)
if slist ~= nil then C.curl_slist_free_all(slist) end
write_cb:free()
if rc == 0 then return result end
if rc == 0 then return result, status end
return nil, err
end