@@ -418,11 +418,11 @@ SELECT mcp_connect('http://localhost:8931/sse', NULL, 1);
418418
419419## Error Handling
420420
421- The sqlite-mcp extension has two types of error handling:
421+ The sqlite-mcp extension has consistent error handling across all interfaces :
422422
4234231 . ** JSON Functions** (ending with ` _json ` ): Return JSON with error information
4244242 . ** 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
425+ 3 . ** Virtual Tables** : Return SQL errors with extracted error messages (like non-JSON functions)
426426
427427### mcp_connect()
428428
@@ -462,21 +462,26 @@ FROM (SELECT mcp_call_tool_json('test', '{}') as result);
462462
463463### Virtual Tables: mcp_list_tools, mcp_call_tool, mcp_list_tools_respond, mcp_call_tool_respond
464464
465- Virtual tables return no rows on error. To check for errors, use the corresponding JSON functions :
465+ Virtual tables return SQL errors with extracted error messages (similar to ` mcp_connect() ` ) :
466466
467467``` 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;
468+ -- Query virtual table - errors are automatically returned as SQL errors
469+ SELECT name, description FROM mcp_list_tools;
470+ -- If not connected, returns SQL error: "Not connected. Call mcp_connect() first"
471+
472+ -- Query call tool virtual table
473+ SELECT text FROM mcp_call_tool( ' nonexistent_tool ' , ' {} ' );
474+ -- Returns SQL error: "Tool not found: nonexistent_tool"
475475
476- -- Query virtual table only if no error
477- SELECT name, description FROM mcp_list_tools WHERE name LIKE ' browser_% ' ;
476+ -- Errors can be caught in application code using sqlite3_errmsg()
477+ -- Or handled in SQL with error handlers
478478```
479479
480+ ** Error Behavior:**
481+ - When an MCP error occurs (not connected, tool not found, invalid JSON, etc.), the virtual table returns ` SQLITE_ERROR `
482+ - The error message is extracted from the JSON error response and set as the SQL error message
483+ - This provides immediate, clear feedback without needing to check JSON functions separately
484+
480485### Common Error Messages
481486
482487All functions may return these error types:
@@ -493,37 +498,34 @@ All functions may return these error types:
4934981 . ** Always check mcp_connect() result** :
494499``` sql
495500-- Good practice: Check connection first
496- SELECT
497- CASE
498- WHEN mcp_connect(' http://localhost:8931/mcp' ) IS NULL
501+ SELECT
502+ CASE
503+ WHEN mcp_connect(' http://localhost:8931/mcp' ) IS NULL
499504 THEN ' Connected successfully'
500505 ELSE mcp_connect(' http://localhost:8931/mcp' )
501506 END;
502507```
503508
504- 2 . ** Use JSON functions for error checking before virtual tables ** :
509+ 2 . ** Virtual tables automatically return errors ** :
505510``` 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;
511+ -- Virtual tables automatically return SQL errors - no need to pre-check
512+ SELECT name, description FROM mcp_list_tools;
513+ -- If not connected, query fails with error: "Not connected. Call mcp_connect() first"
514+
515+ -- Errors can be caught in application code:
516+ -- C: sqlite3_errmsg(db)
517+ -- Python: except sqlite3.Error as e
518+ -- Node.js: try/catch with better-sqlite3
517519```
518520
519- 3 . ** Handle tool call errors** :
521+ 3 . ** Handle JSON function errors manually ** :
520522``` sql
521- -- Safe tool calling with error handling
522- SELECT
523- CASE
523+ -- JSON functions still return JSON with error field
524+ SELECT
525+ CASE
524526 WHEN json_extract(result, ' $.error' ) IS NOT NULL
525527 THEN ' Tool Error: ' || json_extract(result, ' $.error' )
526- ELSE json_extract(result, ' $.content[0].text' )
528+ ELSE json_extract(result, ' $.content[0].text' )
527529 END as output
528530FROM (
529531 SELECT mcp_call_tool_json(' browser_navigate' , ' {"url": "https://example.com"}' ) as result
0 commit comments