File size: 762 Bytes
644bdfe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import AsyncIterator
import mariadb
from mcp.server.fastmcp import FastMCP
from mcp_server_mariadb_vector.settings import DatabaseSettings
@dataclass
class AppContext:
conn: mariadb.Connection
@asynccontextmanager
async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]:
"""Open a MariaDB connection for the duration of the FastMCP session."""
cfg = DatabaseSettings()
conn = mariadb.connect(
host=cfg.host,
port=cfg.port,
user=cfg.user,
password=cfg.password,
database=cfg.database,
)
conn.autocommit = True
try:
yield AppContext(conn=conn)
finally:
conn.close()
|