@@ -208,6 +208,59 @@ pub export fn turso_mcp_reset() void {
208208}
209209
210210// ---------------------------------------------------------------------------
211+ // ═══════════════════════════════════════════════════════════════════════
212+ // Standard ABI (ADR-0005 four symbols + ADR-0006 invoke)
213+ // ═══════════════════════════════════════════════════════════════════════
214+
215+ const shim = @import ("cartridge_shim" );
216+
217+ const CARTRIDGE_NAME_PTR : [* :0 ]const u8 = "turso-mcp" ;
218+ const CARTRIDGE_VERSION_PTR : [* :0 ]const u8 = "0.1.0" ;
219+
220+ export fn boj_cartridge_init () callconv (.c ) c_int {
221+ return 0 ;
222+ }
223+
224+ export fn boj_cartridge_deinit () callconv (.c ) void {}
225+
226+ export fn boj_cartridge_name () callconv (.c ) [* :0 ]const u8 {
227+ return CARTRIDGE_NAME_PTR ;
228+ }
229+
230+ export fn boj_cartridge_version () callconv (.c ) [* :0 ]const u8 {
231+ return CARTRIDGE_VERSION_PTR ;
232+ }
233+
234+ /// Dispatch the cartridge.json MCP tools. Grade D Alpha stubs.
235+ export fn boj_cartridge_invoke (
236+ tool_name : [* c ]const u8 ,
237+ json_args : [* c ]const u8 ,
238+ out_buf : [* c ]u8 ,
239+ in_out_len : [* c ]usize ,
240+ ) callconv (.c ) i32 {
241+ _ = json_args ;
242+ if (shim .invokeArgsNull (tool_name , out_buf , in_out_len )) return shim .RC_BAD_ARGS ;
243+
244+ const body : []const u8 = if (shim .toolIs (tool_name , "turso_connect" ))
245+ "{\" result\" :{\" status\" :\" stub\" }}"
246+ else if (shim .toolIs (tool_name , "turso_query" ))
247+ "{\" result\" :{\" matches\" :[],\" status\" :\" stub\" }}"
248+ else if (shim .toolIs (tool_name , "turso_execute" ))
249+ "{\" result\" :{\" status\" :\" stub\" }}"
250+ else if (shim .toolIs (tool_name , "turso_batch" ))
251+ "{\" result\" :{\" status\" :\" stub\" }}"
252+ else if (shim .toolIs (tool_name , "turso_list_tables" ))
253+ "{\" result\" :{\" items\" :[],\" count\" :0,\" status\" :\" stub\" }}"
254+ else if (shim .toolIs (tool_name , "turso_sync" ))
255+ "{\" result\" :{\" status\" :\" stub\" }}"
256+ else if (shim .toolIs (tool_name , "turso_disconnect" ))
257+ "{\" result\" :{\" status\" :\" stub\" }}"
258+ else
259+ return shim .RC_UNKNOWN_TOOL ;
260+
261+ return shim .writeResult (out_buf , in_out_len , body );
262+ }
263+
211264// Tests
212265// ---------------------------------------------------------------------------
213266
@@ -290,3 +343,50 @@ test "action requires connection" {
290343 try std .testing .expectEqual (@as (c_int , 0 ), turso_mcp_action_requires_connection (0 )); // list_databases
291344 try std .testing .expectEqual (@as (c_int , 0 ), turso_mcp_action_requires_connection (99 )); // out of range
292345}
346+
347+ // ═══════════════════════════════════════════════════════════════════════
348+ // ADR-0006 invoke dispatch tests
349+ // ═══════════════════════════════════════════════════════════════════════
350+
351+ test "boj_cartridge_name returns turso-mcp" {
352+ const n = std .mem .span (boj_cartridge_name ());
353+ try std .testing .expectEqualStrings ("turso-mcp" , n );
354+ }
355+
356+ test "boj_cartridge_init returns 0" {
357+ try std .testing .expectEqual (@as (c_int , 0 ), boj_cartridge_init ());
358+ }
359+
360+ test "invoke: each declared tool succeeds" {
361+ var buf : [256 ]u8 = undefined ;
362+ const tools = [_ ][]const u8 {
363+ "turso_connect" ,
364+ "turso_query" ,
365+ "turso_execute" ,
366+ "turso_batch" ,
367+ "turso_list_tables" ,
368+ "turso_sync" ,
369+ "turso_disconnect" ,
370+ };
371+ for (tools ) | t | {
372+ var len : usize = buf .len ;
373+ const rc = boj_cartridge_invoke (t .ptr , "{}" , & buf , & len );
374+ try std .testing .expectEqual (@as (i32 , 0 ), rc );
375+ try std .testing .expect (std .mem .indexOf (u8 , buf [0.. len ], "result" ) != null );
376+ }
377+ }
378+
379+ test "invoke: unknown tool returns -1" {
380+ var buf : [64 ]u8 = undefined ;
381+ var len : usize = buf .len ;
382+ const rc = boj_cartridge_invoke ("nope" , "{}" , & buf , & len );
383+ try std .testing .expectEqual (@as (i32 , -1 ), rc );
384+ }
385+
386+ test "invoke: buffer too small returns -3" {
387+ var buf : [4 ]u8 = undefined ;
388+ var len : usize = buf .len ;
389+ const rc = boj_cartridge_invoke ("turso_connect" , "{}" , & buf , & len );
390+ try std .testing .expectEqual (@as (i32 , -3 ), rc );
391+ try std .testing .expect (len > 4 );
392+ }
0 commit comments