Files
wehub-resource-sync 60e0ffc959
Upgrade checks / Notify on failure (push) Has been cancelled
Upgrade checks / Close issue on success (push) Has been cancelled
Schema Crash Test / Real-world schema crash test (232K schemas) (push) Has been cancelled
Run static analysis / static_analysis (push) Has been cancelled
Tests / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Tests / Tests with lowest-direct dependencies (push) Has been cancelled
Tests / MCP conformance tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Tests / Package install smoke (push) Has been cancelled
Upgrade checks / Static analysis (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Upgrade checks / Integration tests (push) Has been cancelled
Update MCPServerConfig Schema / update-config-schema (push) Has been cancelled
Update SDK Documentation / update-sdk-docs (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:59 +08:00

60 lines
1.8 KiB
Markdown

# 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
```bash
# 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:
```python
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:
```python
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:
```bash
# 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.