Update mcp_server_sqlite to use robust text encoding by default

The Claude Desktop client will hang in Windows when utilizing extended/multibyte characters in sqlite as a result of decoding errors. The decoding errors are resolved by using UTF-8 encoding.

Configuring Windows to use UTF-8 in place of windows-1252 makes the default server behavior consistent with macOS.  

os.environ PYTHONIOENCODING is checked as to not interfere with an environment override, such as:

{
  "mcpServers": {
    "sqlite": {
      "command": "uvx",
      "args": [
        "mcp-server-sqlite", "--db-path", "./example.db"
      ],
      "env": {
        "PYTHONIOENCODING": "utf-8" 
      }
    }
  }
}
This commit is contained in:
interrobot
2024-12-18 20:56:08 -05:00
committed by GitHub
parent 926d86c763
commit 2ce3666337

View File

@@ -1,3 +1,5 @@
import os
import sys
import sqlite3
import logging
from contextlib import closing
@@ -9,6 +11,12 @@ import mcp.server.stdio
from pydantic import AnyUrl
from typing import Any
# reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
if sys.platform == "win32" and os.environ.get('PYTHONIOENCODING') is None:
sys.stdin.reconfigure(encoding="utf-8")
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
logger = logging.getLogger('mcp_sqlite_server')
logger.info("Starting MCP SQLite Server")