MotherDuck Connector Protocol Server
This is a sample implementation of the MotherDuck Connector Protocol (MCP) server that allows DuckDB clients to connect to MotherDuck or local DuckDB instances via a lightweight HTTP server. It's built on FastAPI. The current version is 1.0.4, and its release cadence is tied to updates in the MCP specification or the underlying DuckDB/MotherDuck APIs, serving primarily as a reference and bridge.
Common errors
-
ModuleNotFoundError: No module named 'uvicorn'
cause Uvicorn, the ASGI server, is not installed.fixInstall uvicorn with its standard dependencies: `pip install 'uvicorn[standard]'` -
duckdb.duckdb.ConnectionException: Unable to connect to MotherDuck. Please set the 'MD_TOKEN' environment variable.
cause The `MD_TOKEN` environment variable, required for MotherDuck connections, is not set or is invalid.fixSet the `MD_TOKEN` environment variable with your MotherDuck token before starting the server. Example: `export MD_TOKEN='YOUR_TOKEN_HERE'`. -
ERROR: [Errno 98] Address already in use
cause Another application is already listening on the default port (8000).fixRun the server on a different port: `uvicorn mcp_server_motherduck.main:app --host 0.0.0.0 --port 8001` (or any other free port). -
Error response from server: {'detail': 'Invalid DuckDB path or MotherDuck token'}cause The `DUCKDB_PATH` environment variable for local DuckDB is invalid, or the `MD_TOKEN` for MotherDuck is incorrect or expired.fixVerify that the `DUCKDB_PATH` points to a valid .duckdb file or that your `MD_TOKEN` is correct and active. Restart the server after updating.
Warnings
- gotcha Configuration requires environment variables `MD_TOKEN` for MotherDuck or `DUCKDB_PATH` for a local DuckDB file. Without these, the server may fail to connect to a backend.
- gotcha This library provides a MotherDuck Connector Protocol *server*, not a client library for direct programmatic interaction with MotherDuck from Python. Its primary use case is to be run as a standalone service.
- gotcha The default server port (8000) might already be in use by another application. This will prevent the server from starting.
- gotcha The server is labeled as an 'example implementation'. While functional, it might require additional hardening or features for production use cases.
Install
-
pip install mcp-server-motherduck
Imports
- app
from mcp_server_motherduck.main import app
Quickstart
import os
# Set environment variables for MotherDuck (or DUCKDB_PATH for local DuckDB)
# For local DuckDB: os.environ['DUCKDB_PATH'] = '/path/to/my_local.duckdb'
# For MotherDuck: os.environ['MD_TOKEN'] = os.environ.get('MD_TOKEN', 'YOUR_MOTHERDUCK_TOKEN')
print("To run the MCP server:")
print("1. Ensure mcp-server-motherduck and uvicorn are installed: pip install mcp-server-motherduck uvicorn[standard]")
print("2. Run the server from your terminal:")
print(" uvicorn mcp_server_motherduck.main:app --host 0.0.0.0 --port 8000")
print("\nOnce running, you can connect using the DuckDB CLI:")
print(" duckdb 'md:http://localhost:8000/'")
print(" -- or if using a local DuckDB file --")
print(" duckdb 'md:http://localhost:8000/?duckdb_path=/path/to/my_local.duckdb'")