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

@@ -8,11 +8,14 @@ TrytonMCP is an MCP (Model Context Protocol) server that enables LLMs to interac
```
TrytonMCP/
├── src/tryton-mcp/
── __init__.py
└── server.py # Main MCP server with tool definitions
├── test_rpc.py # Standalone RPC test script
└── pyproject.toml # Project dependencies
├── src/
── tryton_mcp/
├── __init__.py
│ ├── config.py # Settings singleton with TrytonSettings
│ └── server.py # Main MCP server with tool definitions
├── test_rpc.py # Standalone RPC test script
├── pyproject.toml # Project dependencies
└── .env.example # Environment variables template
```
## Architecture
@@ -23,16 +26,15 @@ TrytonMCP/
### Core Components
1. **Client Connection** (`server.py:45-47, 59-61, 73-75`)
- Creates a new `Client` instance for each tool call
- Credentials are hardcoded in `TRYTON_CREDENTIALS` dict
1. **Settings** (`config.py`) - Singleton pattern for configuration management
- Uses environment variables with defaults
- Provides `to_dict()` and `get_client()` methods
- Validates required fields in `__post_init__`
2. **Tools** - Functions decorated with `@mcp.tool()`:
- `create_schedule` (line 32-54) - Creates appointments in Naliia module
- `find_service_centers` (line 57-69) - Searches service centers
- `find_products_and_services` (line 71-83) - Searches salable products
3. **Context** (`server.py:20-24`) - Dataclass for managing client state (currently unused)
2. **Tools** (`server.py`) - Functions decorated with `@mcp.tool()`:
- `create_schedule` - Creates appointments in Naliia module
- `find_service_centers` - Searches service centers
- `find_products_and_services` - Searches salable products
## Adding New Tools
@@ -41,7 +43,7 @@ To add a new Tryton model as an MCP tool:
```python
@mcp.tool()
def your_tool_name(param1: int, param2: str) -> List[dict]:
client = Client(**TRYTON_CREDENTIALS)
client = settings.get_client()
client.connect()
name = "model.your.model.method"