diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..527b5e4 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +TRYTON_HOSTNAME=dev.naliia.co +TRYTON_DATABASE=capacitacion +TRYTON_USERNAME=admin +TRYTON_PASSWORD=admin +TRYTON_PORT=8000 diff --git a/AGENTS.md b/AGENTS.md index 6eb71bd..b5372f8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -66,17 +66,24 @@ fastmcp run tryton_mcp.server ## Configuration -Edit `TRYTON_CREDENTIALS` in `server.py:12-17` to change connection settings: +Credentials are loaded from environment variables (with sensible defaults): -```python -TRYTON_CREDENTIALS = { - "hostname": 'dev.naliia.co', - "database": 'capacitacion', - "username": 'admin', - "password": 'admin' -} +```bash +export TRYTON_HOSTNAME=dev.naliia.co +export TRYTON_DATABASE=capacitacion +export TRYTON_USERNAME=admin +export TRYTON_PASSWORD=admin +export TRYTON_PORT=8000 ``` +Or copy `.env.example` to `.env` and adjust values: + +```bash +cp .env.example .env +``` + +Note: `.env` is gitignored to protect credentials. + ## Testing RPC Calls Use `test_rpc.py` to test Tryton RPC calls independently: diff --git a/src/tryton-mcp/server.py b/src/tryton-mcp/server.py index 486155b..8c0a90d 100644 --- a/src/tryton-mcp/server.py +++ b/src/tryton-mcp/server.py @@ -1,6 +1,8 @@ """ MCP Server for Naliia Module """ + +import os from sabatron_tryton_rpc_client.client import Client from contextlib import asynccontextmanager from dataclasses import dataclass @@ -9,12 +11,17 @@ from typing import List, AsyncIterator import datetime -TRYTON_CREDENTIALS = { - "hostname": 'dev.naliia.co', - "database": 'capacitacion', - "username": 'admin', - "password": 'admin' -} +def get_tryton_credentials() -> dict: + return { + "hostname": os.environ.get("TRYTON_HOSTNAME", "localhost"), + "database": os.environ.get("TRYTON_DATABASE", "tryton"), + "username": os.environ.get("TRYTON_USERNAME", "admin"), + "password": os.environ.get("TRYTON_PASSWORD", ""), + "port": int(os.environ.get("TRYTON_PORT", 8000)), + } + + +TRYTON_CREDENTIALS = get_tryton_credentials() @dataclass @@ -24,27 +31,25 @@ class AppContext: client: Client -mcp = FastMCP( - "Tryton MCP Server" -) +mcp = FastMCP("Tryton MCP Server") @mcp.tool() def create_schedule( professional: int, description: str, customer: int, date: str, service_center: int - ) -> List[int]: +) -> List[int]: - example_schedule = [{ - 'professional': 6, - 'description': description, - 'customer': 4, - 'date': datetime.datetime.fromisoformat(date), - 'service_center': 11 - }] + example_schedule = [ + { + "professional": 6, + "description": description, + "customer": 4, + "date": datetime.datetime.fromisoformat(date), + "service_center": 11, + } + ] - client = Client( - **TRYTON_CREDENTIALS - ) + client = Client(**TRYTON_CREDENTIALS) client.connect() name = "model.naliia.schedule.create" @@ -56,28 +61,32 @@ def create_schedule( @mcp.tool() def find_service_centers(): - client = Client( - **TRYTON_CREDENTIALS - ) + client = Client(**TRYTON_CREDENTIALS) client.connect() name = "model.naliia.service_center.search_read" - args = [[[]], 0, None, None, ['name','address.street'], {}] + args = [[[]], 0, None, None, ["name", "address.street"], {}] response = client.call(name, args) return response + @mcp.tool() def find_products_and_services(): - client = Client( - **TRYTON_CREDENTIALS - ) + client = Client(**TRYTON_CREDENTIALS) client.connect() name = "model.product.product.search_read" - args = [[['active', '=', True], ['salable', '=', True]], 0, None, None, ['name','list_price', 'description'], {'company': 1}] + args = [ + [["active", "=", True], ["salable", "=", True]], + 0, + None, + None, + ["name", "list_price", "description"], + {"company": 1}, + ] response = client.call(name, args) - return response \ No newline at end of file + return response diff --git a/test_rpc.py b/test_rpc.py new file mode 100644 index 0000000..8ded476 --- /dev/null +++ b/test_rpc.py @@ -0,0 +1,18 @@ +import os +from sabatron_tryton_rpc_client.client import Client +import datetime + +client = Client( + hostname=os.environ.get("TRYTON_HOSTNAME", "localhost"), + database=os.environ.get("TRYTON_DATABASE", "tryton"), + username=os.environ.get("TRYTON_USERNAME", "admin"), + password=os.environ.get("TRYTON_PASSWORD", ""), + port=int(os.environ.get("TRYTON_PORT", 8000)), +) + +client.connect() + +name = "model.party.party.read" +args = ([1], ["id", "name", "code"], {}) + +client.call(name, args)