refactor: add Settings singleton pattern for config management

This commit is contained in:
2026-03-07 23:19:34 -05:00
parent d1b0c16d03
commit 2efaf24a04
6 changed files with 356 additions and 51 deletions

View File

42
src/tryton_mcp/config.py Normal file
View File

@@ -0,0 +1,42 @@
import os
from dataclasses import dataclass, field
from sabatron_tryton_rpc_client.client import Client
@dataclass
class TrytonSettings:
hostname: str = field(
default_factory=lambda: os.environ.get("TRYTON_HOSTNAME", "localhost")
)
database: str = field(
default_factory=lambda: os.environ.get("TRYTON_DATABASE", "tryton")
)
username: str = field(
default_factory=lambda: os.environ.get("TRYTON_USERNAME", "admin")
)
password: str = field(default_factory=lambda: os.environ.get("TRYTON_PASSWORD", ""))
port: int = field(
default_factory=lambda: int(os.environ.get("TRYTON_PORT", "8000"))
)
def to_dict(self) -> dict:
return {
"hostname": self.hostname,
"database": self.database,
"username": self.username,
"password": self.password,
"port": self.port,
}
def get_client(self) -> Client:
return Client(**self.to_dict())
def __post_init__(self):
if not self.hostname:
raise ValueError("TRYTON_HOSTNAME is required")
if not self.database:
raise ValueError("TRYTON_DATABASE is required")
settings = TrytonSettings()

71
src/tryton_mcp/server.py Normal file
View File

@@ -0,0 +1,71 @@
"""
MCP Server for Naliia Module
"""
from sabatron_tryton_rpc_client.client import Client
from fastmcp import FastMCP
from typing import List
import datetime
from tryton_mcp.config import settings
mcp = FastMCP("Tryton MCP Server")
@mcp.tool()
def create_schedule(
professional: int, description: str, customer: int, date: str, service_center: int
) -> List[int]:
example_schedule = [
{
"professional": 6,
"description": description,
"customer": 4,
"date": datetime.datetime.fromisoformat(date),
"service_center": 11,
}
]
client = Client(**settings.to_dict())
client.connect()
name = "model.naliia.schedule.create"
args = [example_schedule, {}]
response = client.call(name, args)
return response
@mcp.tool()
def find_service_centers():
client = Client(**settings.to_dict())
client.connect()
name = "model.naliia.service_center.search_read"
args = [[[]], 0, None, None, ["name", "address.street"], {}]
response = client.call(name, args)
return response
@mcp.tool()
def find_products_and_services():
client = Client(**settings.to_dict())
client.connect()
name = "model.product.product.search_read"
args = [
[["active", "=", True], ["salable", "=", True]],
0,
None,
None,
["name", "list_price", "description"],
{"company": 1},
]
response = client.call(name, args)
return response