refactor: use environment variables for Tryton credentials

This commit is contained in:
2026-03-07 23:13:41 -05:00
parent 2c12a8ec10
commit d1b0c16d03
4 changed files with 76 additions and 37 deletions

5
.env.example Normal file
View File

@@ -0,0 +1,5 @@
TRYTON_HOSTNAME=dev.naliia.co
TRYTON_DATABASE=capacitacion
TRYTON_USERNAME=admin
TRYTON_PASSWORD=admin
TRYTON_PORT=8000

View File

@@ -66,17 +66,24 @@ fastmcp run tryton_mcp.server
## Configuration ## Configuration
Edit `TRYTON_CREDENTIALS` in `server.py:12-17` to change connection settings: Credentials are loaded from environment variables (with sensible defaults):
```python ```bash
TRYTON_CREDENTIALS = { export TRYTON_HOSTNAME=dev.naliia.co
"hostname": 'dev.naliia.co', export TRYTON_DATABASE=capacitacion
"database": 'capacitacion', export TRYTON_USERNAME=admin
"username": 'admin', export TRYTON_PASSWORD=admin
"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 ## Testing RPC Calls
Use `test_rpc.py` to test Tryton RPC calls independently: Use `test_rpc.py` to test Tryton RPC calls independently:

View File

@@ -1,6 +1,8 @@
""" """
MCP Server for Naliia Module MCP Server for Naliia Module
""" """
import os
from sabatron_tryton_rpc_client.client import Client from sabatron_tryton_rpc_client.client import Client
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from dataclasses import dataclass from dataclasses import dataclass
@@ -9,12 +11,17 @@ from typing import List, AsyncIterator
import datetime import datetime
TRYTON_CREDENTIALS = { def get_tryton_credentials() -> dict:
"hostname": 'dev.naliia.co', return {
"database": 'capacitacion', "hostname": os.environ.get("TRYTON_HOSTNAME", "localhost"),
"username": 'admin', "database": os.environ.get("TRYTON_DATABASE", "tryton"),
"password": 'admin' "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 @dataclass
@@ -24,27 +31,25 @@ class AppContext:
client: Client client: Client
mcp = FastMCP( mcp = FastMCP("Tryton MCP Server")
"Tryton MCP Server"
)
@mcp.tool() @mcp.tool()
def create_schedule( def create_schedule(
professional: int, description: str, customer: int, date: str, service_center: int professional: int, description: str, customer: int, date: str, service_center: int
) -> List[int]: ) -> List[int]:
example_schedule = [{ example_schedule = [
'professional': 6, {
'description': description, "professional": 6,
'customer': 4, "description": description,
'date': datetime.datetime.fromisoformat(date), "customer": 4,
'service_center': 11 "date": datetime.datetime.fromisoformat(date),
}] "service_center": 11,
}
]
client = Client( client = Client(**TRYTON_CREDENTIALS)
**TRYTON_CREDENTIALS
)
client.connect() client.connect()
name = "model.naliia.schedule.create" name = "model.naliia.schedule.create"
@@ -56,28 +61,32 @@ def create_schedule(
@mcp.tool() @mcp.tool()
def find_service_centers(): def find_service_centers():
client = Client( client = Client(**TRYTON_CREDENTIALS)
**TRYTON_CREDENTIALS
)
client.connect() client.connect()
name = "model.naliia.service_center.search_read" 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) response = client.call(name, args)
return response return response
@mcp.tool() @mcp.tool()
def find_products_and_services(): def find_products_and_services():
client = Client( client = Client(**TRYTON_CREDENTIALS)
**TRYTON_CREDENTIALS
)
client.connect() client.connect()
name = "model.product.product.search_read" 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) response = client.call(name, args)
return response return response

18
test_rpc.py Normal file
View File

@@ -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)