fix: validate date format in create_schedule with clear error message
This commit is contained in:
@@ -4,10 +4,17 @@ MCP Server for Naliia Module
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
from fastmcp import FastMCP
|
||||
from typing import List, AsyncIterator, Any, Dict
|
||||
import datetime
|
||||
from typing import List, Dict, AsyncIterator, Any
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from tryton_mcp.config import settings
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -29,18 +36,63 @@ def tryton_call(name: str, args: list) -> Dict[str, Any]:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def find_customer_by_identifier(identifier: str) -> List[Dict]:
|
||||
result = tryton_call(
|
||||
"model.party.identifier.search_read",
|
||||
[
|
||||
[["code", "=", identifier]],
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
["party", "party.name"],
|
||||
{"company": 1},
|
||||
],
|
||||
)
|
||||
|
||||
if not result["success"]:
|
||||
raise Exception(f"Customer with identifier {identifier} not found.")
|
||||
|
||||
return result["data"]
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def create_customer(
|
||||
name: str, identifiers: List[tuple[str, List[Dict[str, Any]]]]
|
||||
) -> List[int]:
|
||||
|
||||
result = tryton_call(
|
||||
"model.party.party.create",
|
||||
[{"name": name, "identifiers": identifiers}, {}],
|
||||
)
|
||||
|
||||
if not result["success"]:
|
||||
raise Exception(f"Failed to create {name} as customer")
|
||||
|
||||
return result["data"]
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def create_schedule(
|
||||
professional: int, description: str, customer: int, date: str, service_center: int
|
||||
) -> List[int]:
|
||||
|
||||
try:
|
||||
dt = datetime.fromisoformat(date)
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Invalid date format: '{date}'. Expected ISO format (e.g., '2026-03-15T10:00:00' or '2026-03-15T10:00:00+00:00')"
|
||||
)
|
||||
|
||||
utc_dt = dt.astimezone(timezone.utc)
|
||||
|
||||
example_schedule = [
|
||||
{
|
||||
"professional": 6,
|
||||
"professional": professional,
|
||||
"description": description,
|
||||
"customer": 4,
|
||||
"date": datetime.datetime.fromisoformat(date),
|
||||
"service_center": 11,
|
||||
"customer": customer,
|
||||
"date": utc_dt,
|
||||
"service_center": service_center,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user