refactor: connect client once at server startup via lifespan
This commit is contained in:
@@ -2,15 +2,22 @@
|
||||
MCP Server for Naliia Module
|
||||
"""
|
||||
|
||||
from sabatron_tryton_rpc_client.client import Client
|
||||
from contextlib import asynccontextmanager
|
||||
from fastmcp import FastMCP
|
||||
from typing import List
|
||||
from typing import List, AsyncIterator
|
||||
import datetime
|
||||
|
||||
from tryton_mcp.config import settings
|
||||
|
||||
|
||||
mcp = FastMCP("Tryton MCP Server")
|
||||
@asynccontextmanager
|
||||
async def server_lifespan(server: FastMCP) -> AsyncIterator[None]:
|
||||
settings.connect()
|
||||
yield
|
||||
settings.disconnect()
|
||||
|
||||
|
||||
mcp = FastMCP("Tryton MCP Server", lifespan=server_lifespan)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
@@ -28,9 +35,7 @@ def create_schedule(
|
||||
}
|
||||
]
|
||||
|
||||
client = Client(**settings.to_dict())
|
||||
|
||||
client.connect()
|
||||
client = settings.get_client()
|
||||
name = "model.naliia.schedule.create"
|
||||
args = [example_schedule, {}]
|
||||
response = client.call(name, args)
|
||||
@@ -40,9 +45,7 @@ def create_schedule(
|
||||
|
||||
@mcp.tool()
|
||||
def find_service_centers():
|
||||
client = Client(**settings.to_dict())
|
||||
|
||||
client.connect()
|
||||
client = settings.get_client()
|
||||
|
||||
name = "model.naliia.service_center.search_read"
|
||||
args = [[[]], 0, None, None, ["name", "address.street"], {}]
|
||||
@@ -53,9 +56,7 @@ def find_service_centers():
|
||||
|
||||
@mcp.tool()
|
||||
def find_products_and_services():
|
||||
client = Client(**settings.to_dict())
|
||||
|
||||
client.connect()
|
||||
client = settings.get_client()
|
||||
|
||||
name = "model.product.product.search_read"
|
||||
args = [
|
||||
|
||||
Reference in New Issue
Block a user