@@ -63,11 +63,6 @@ SELECT mcp_connect(
6363);
6464```
6565
66- ** Response:**
67- ``` json
68- {"status" : " connected" , "transport" : " streamable_http" }
69- ```
70-
7166See [ USAGE.md] ( USAGE.md ) for more examples of using custom headers.
7267
7368---
@@ -423,10 +418,31 @@ SELECT mcp_connect('http://localhost:8931/sse', NULL, 1);
423418
424419## Error Handling
425420
426- All functions return JSON with error information on failure:
421+ The sqlite-mcp extension has two types of error handling:
422+
423+ 1 . ** JSON Functions** (ending with ` _json ` ): Return JSON with error information
424+ 2 . ** Non-JSON Functions** : Return error strings directly (extracted from JSON)
425+ 3 . ** Virtual Tables** : Return no rows on error, use scalar functions to check status
426+
427+ ### mcp_connect()
428+
429+ Returns ` NULL ` on success, or an error string on failure:
430+
431+ ``` sql
432+ -- Check if connection succeeded
433+ SELECT mcp_connect(' http://localhost:8000/mcp' ) IS NULL ;
434+ -- Returns 1 (true) on success, 0 (false) on failure
435+
436+ -- Get error message if connection failed
437+ SELECT mcp_connect(' http://invalid:8000/mcp' );
438+ -- Returns: "Failed to connect to MCP server: ..."
439+ ```
440+
441+ ### JSON Functions: mcp_list_tools_json() and mcp_call_tool_json()
442+
443+ Always return JSON - either with results or error information:
427444
428445``` json
429- {"error" : " Connection failed: timeout" }
430446{"error" : " Not connected. Call mcp_connect() first" }
431447{"error" : " Tool not found: invalid_tool" }
432448{"error" : " Invalid JSON arguments" }
@@ -442,4 +458,74 @@ SELECT
442458 ELSE ' Success'
443459 END
444460FROM (SELECT mcp_call_tool_json(' test' , ' {}' ) as result);
461+ ```
462+
463+ ### Virtual Tables: mcp_list_tools, mcp_call_tool, mcp_list_tools_respond, mcp_call_tool_respond
464+
465+ Virtual tables return no rows on error. To check for errors, use the corresponding JSON functions:
466+
467+ ``` sql
468+ -- Check if server is connected before querying virtual table
469+ SELECT
470+ CASE
471+ WHEN json_extract(mcp_list_tools_json(), ' $.error' ) IS NOT NULL
472+ THEN ' Error: Cannot query virtual table - ' || json_extract(mcp_list_tools_json(), ' $.error' )
473+ ELSE ' OK to query virtual table'
474+ END;
475+
476+ -- Query virtual table only if no error
477+ SELECT name, description FROM mcp_list_tools WHERE name LIKE ' browser_%' ;
478+ ```
479+
480+ ### Common Error Messages
481+
482+ All functions may return these error types:
483+
484+ - ** Connection errors** : ` "Not connected. Call mcp_connect() first" `
485+ - ** Server errors** : ` "Failed to connect to MCP server: ..." `
486+ - ** Tool errors** : ` "Tool not found: tool_name" `
487+ - ** Argument errors** : ` "Invalid JSON arguments" `
488+ - ** Transport errors** : ` "Transport error: ..." `
489+ - ** Timeout errors** : ` "Request timeout" `
490+
491+ ### Error Handling Best Practices
492+
493+ 1 . ** Always check mcp_connect() result** :
494+ ``` sql
495+ -- Good practice: Check connection first
496+ SELECT
497+ CASE
498+ WHEN mcp_connect(' http://localhost:8931/mcp' ) IS NULL
499+ THEN ' Connected successfully'
500+ ELSE mcp_connect(' http://localhost:8931/mcp' )
501+ END;
502+ ```
503+
504+ 2 . ** Use JSON functions for error checking before virtual tables** :
505+ ``` sql
506+ -- Check for errors before using virtual table
507+ WITH error_check AS (
508+ SELECT json_extract(mcp_list_tools_json(), ' $.error' ) as error
509+ )
510+ SELECT
511+ CASE
512+ WHEN error_check .error IS NOT NULL
513+ THEN ' Error: ' || error_check .error
514+ ELSE ' Tools: ' || (SELECT GROUP_CONCAT(name) FROM mcp_list_tools)
515+ END
516+ FROM error_check;
517+ ```
518+
519+ 3 . ** Handle tool call errors** :
520+ ``` sql
521+ -- Safe tool calling with error handling
522+ SELECT
523+ CASE
524+ WHEN json_extract(result, ' $.error' ) IS NOT NULL
525+ THEN ' Tool Error: ' || json_extract(result, ' $.error' )
526+ ELSE json_extract(result, ' $.content[0].text' )
527+ END as output
528+ FROM (
529+ SELECT mcp_call_tool_json(' browser_navigate' , ' {"url": "https://example.com"}' ) as result
530+ );
445531```
0 commit comments