fastmcp/examples/providers/sqlite
Jeremiah Lowin 7d76c9d055
Add examples/ to the ty static-analysis gate (#4466)
* Add examples/ to ty static-analysis gate

* Fix example type errors and stale SDK idioms for ty

* Use typing_extensions.TypedDict for the quiz tool-param type

Question is a take_quiz parameter, so FastMCP builds a Pydantic schema
for it; typing.TypedDict raises PydanticUserError on Python 3.10/3.11
(only 3.12+ accepts it). ty and 3.12 runs miss this, so it slipped in.

* Guard get_access_token() None case in huggingface_oauth example

Caught by the ty gate this PR adds: the example, merged separately,
had never been type-checked against examples/. Matches the existing
aws_oauth/keycloak_oauth pattern.

* Print actual YAML text in custom serializer example
2026-07-18 19:44:13 -04:00
..
README.md Convert mounted servers to MountedProvider (#2635) 2025-12-17 22:21:51 -05:00
server.py Add examples/ to the ty static-analysis gate (#4466) 2026-07-18 19:44:13 -04:00
setup_db.py Convert mounted servers to MountedProvider (#2635) 2025-12-17 22:21:51 -05:00

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 (committed for convenience)
  • setup_db.py - Script to create/reset the database
  • server.py - MCP server that loads tools from the database

Usage

# Reset the database (optional - tools.db is pre-seeded)
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(BaseToolProvider):
    async def list_tools(self) -> list[Tool]:
        # Query database for enabled tools
        ...

    async def get_tool(self, name: str) -> 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.