fastmcp/examples/providers/sqlite/README.md
Jeremiah Lowin ede8ff6703
Convert mounted servers to MountedProvider (#2635)
* Simplify Provider interface and consolidate docket registration

- Remove get_http_routes from Provider (unused)
- Remove ProviderLifespanConfig, _base_lifespan, _register_tasks
- Remove supports_tasks flag from Provider.__init__
- Consolidate all docket registration in server._docket_lifespan()
- Simplify lifespan() to take no parameters
- Move MountedProvider to separate module

* Fix control flow in ComponentService resource methods

* Move providers to server/providers

* Ensure MountedProvider get_* methods go through middleware

* Fix get_resource to only return concrete resources

Reverts template-checking in get_resource that broke task execution.
Tasks need access to the original template, not instantiated resources.

* Move prefix utilities into mounted.py, deprecate import_server

- Add resource prefix functions (add/remove/has_resource_prefix) to mounted.py
- Deprecate import_server with warning to use mount() instead
- Add tool_names uniqueness validation in MountedProvider

* Fix provider iteration order and remove dead _is_mounted flag

- Remove unused _is_mounted flag (MountedProvider.lifespan() calls _lifespan
  not _lifespan_manager, so the flag was never checked)
- Fix provider iteration: change reversed() to forward order in execution
  methods (_call_tool, _read_resource_middleware, _get_prompt_content_middleware)
  to match documented "first non-None wins" semantics
- Fix ComponentService to handle prefix-less mounted servers using
  _strip_tool_prefix()/_strip_resource_prefix() methods
- Update conflict resolution tests to expect first-registered provider wins
- Add regression tests for Docket behavior and prefix-less ComponentService

* Add TaskComponents type and exception handling for provider task registration

- Create TaskComponents dataclass with FunctionTool/FunctionResource/etc. types
  for proper typing of get_tasks() return value
- Add try/except wrapper around provider.get_tasks() in _docket_lifespan for
  consistent error handling (warn + continue or raise based on settings)
- Remove type: ignore comments from server.py task registration loop
2025-12-17 22:21:51 -05:00

1.8 KiB

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.