Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

1.9 KB
Raw Blame

Dynamic Tools from SQLite

This example demonstrates serving MCP tools from a database. Tools can be added, modified, or disabled by updating the database - no server restart required.

Structure

  • tools.db - SQLite database with tool configurations (created by setup_db.py; gitignored)
  • setup_db.py - Script to create/reset the database
  • server.py - MCP server that loads tools from the database

Usage

# Create or reset the database (required before the first run)
uv run examples/providers/sqlite/setup_db.py

# Run the server
uv run fastmcp run examples/providers/sqlite/server.py

How It Works

The SQLiteToolProvider queries the database on every list_tools and call_tool request:

class SQLiteToolProvider(Provider):
    async def _list_tools(self) -> Sequence[Tool]:
        # Query database for enabled tools
        ...

    async def _get_tool(
        self, name: str, version: VersionSpec | None = None
    ) -> Tool | None:
        # Efficient single-tool lookup
        ...

Tools are defined as ConfigurableTool subclasses that combine schema and execution:

class ConfigurableTool(Tool):
    operation: str  # "add", "multiply", etc.

    async def run(self, arguments: dict[str, Any]) -> ToolResult:
        # Execute based on configured operation
        ...

Modifying Tools at Runtime

While the server is running, you can modify tools in the database:

# Add a new tool
sqlite3 examples/providers/sqlite/tools.db "INSERT INTO tools VALUES ('subtract_numbers', 'Subtract two numbers', '{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"number\"},\"b\":{\"type\":\"number\"}},\"required\":[\"a\",\"b\"]}', 'subtract', 0, 1)"

# Disable a tool
sqlite3 examples/providers/sqlite/tools.db "UPDATE tools SET enabled = 0 WHERE name = 'divide_numbers'"

The next list_tools or call_tool request will reflect these changes.