mirror of
https://github.com/latinogino/dolibarr-mcp.git
synced 2026-04-24 02:25:35 +02:00
Align docs and entry points with prestashop-mcp
This commit is contained in:
@@ -6,8 +6,8 @@ from typing import Optional
|
||||
|
||||
import click
|
||||
|
||||
from .config import Config
|
||||
from .dolibarr_mcp_server import main as server_main
|
||||
from .testing import test_connection as run_test_connection
|
||||
|
||||
|
||||
@click.group()
|
||||
@@ -22,57 +22,9 @@ def cli():
|
||||
@click.option("--api-key", help="Dolibarr API key")
|
||||
def test(url: Optional[str], api_key: Optional[str]):
|
||||
"""Test the connection to Dolibarr API."""
|
||||
|
||||
async def run_test():
|
||||
try:
|
||||
# Import here to avoid circular imports
|
||||
from .dolibarr_client import DolibarrClient
|
||||
|
||||
# Create config with optional overrides
|
||||
try:
|
||||
config = Config()
|
||||
if url:
|
||||
config.dolibarr_url = url
|
||||
if api_key:
|
||||
config.api_key = api_key
|
||||
except Exception as e:
|
||||
click.echo(f"❌ Configuration error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
async with DolibarrClient(config) as client:
|
||||
click.echo("🧪 Testing Dolibarr API connection...")
|
||||
|
||||
# Test basic status endpoint
|
||||
result = await client.get_status()
|
||||
|
||||
if "success" in result or "dolibarr_version" in str(result):
|
||||
click.echo("✅ Connection successful!")
|
||||
if isinstance(result, dict) and "success" in result:
|
||||
version = result.get("success", {}).get("dolibarr_version", "Unknown")
|
||||
click.echo(f"Dolibarr Version: {version}")
|
||||
|
||||
# Test a few more endpoints
|
||||
try:
|
||||
users = await client.get_users(limit=1)
|
||||
click.echo(f"Users accessible: {len(users) if isinstance(users, list) else 'Error'}")
|
||||
except Exception:
|
||||
click.echo("Users: ⚠️ Access limited or unavailable")
|
||||
|
||||
try:
|
||||
customers = await client.get_customers(limit=1)
|
||||
click.echo(f"Customers accessible: {len(customers) if isinstance(customers, list) else 'Error'}")
|
||||
except Exception:
|
||||
click.echo("Customers: ⚠️ Access limited or unavailable")
|
||||
|
||||
else:
|
||||
click.echo(f"❌ Connection failed: {result}")
|
||||
sys.exit(1)
|
||||
|
||||
except Exception as e:
|
||||
click.echo(f"❌ Test failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
asyncio.run(run_test())
|
||||
exit_code = run_test_connection(url=url, api_key=api_key)
|
||||
if exit_code != 0:
|
||||
sys.exit(exit_code)
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
||||
@@ -43,7 +43,12 @@ class Config(BaseSettings):
|
||||
def validate_dolibarr_url(cls, v: str) -> str:
|
||||
"""Validate Dolibarr URL."""
|
||||
if not v:
|
||||
v = os.getenv("DOLIBARR_URL") or os.getenv("DOLIBARR_BASE_URL", "")
|
||||
v = (
|
||||
os.getenv("DOLIBARR_URL")
|
||||
or os.getenv("DOLIBARR_BASE_URL")
|
||||
or os.getenv("DOLIBARR_SHOP_URL")
|
||||
or ""
|
||||
)
|
||||
if not v:
|
||||
# Print warning but don't fail
|
||||
print(
|
||||
@@ -114,7 +119,12 @@ class Config(BaseSettings):
|
||||
"""Create configuration from environment variables with validation."""
|
||||
try:
|
||||
config = cls(
|
||||
dolibarr_url=os.getenv("DOLIBARR_URL") or os.getenv("DOLIBARR_BASE_URL", ""),
|
||||
dolibarr_url=(
|
||||
os.getenv("DOLIBARR_URL")
|
||||
or os.getenv("DOLIBARR_BASE_URL")
|
||||
or os.getenv("DOLIBARR_SHOP_URL")
|
||||
or ""
|
||||
),
|
||||
dolibarr_api_key=os.getenv("DOLIBARR_API_KEY", ""),
|
||||
log_level=os.getenv("LOG_LEVEL", "INFO"),
|
||||
)
|
||||
|
||||
25
src/dolibarr_mcp/test_connection.py
Normal file
25
src/dolibarr_mcp/test_connection.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""Standalone entry point to verify Dolibarr connectivity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
from .testing import test_connection
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description="Test the Dolibarr MCP configuration")
|
||||
parser.add_argument("--url", help="Override the Dolibarr API URL", default=None)
|
||||
parser.add_argument("--api-key", help="Override the Dolibarr API key", default=None)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
exit_code = test_connection(url=args.url, api_key=args.api_key)
|
||||
raise SystemExit(exit_code)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover - manual execution entry
|
||||
main()
|
||||
|
||||
55
src/dolibarr_mcp/testing.py
Normal file
55
src/dolibarr_mcp/testing.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""Utility helpers shared by command line entry points."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
from .config import Config
|
||||
from .dolibarr_client import DolibarrClient
|
||||
|
||||
|
||||
async def _run_test(url: Optional[str], api_key: Optional[str]) -> int:
|
||||
"""Execute the Dolibarr connectivity test returning an exit code."""
|
||||
|
||||
try:
|
||||
config = Config()
|
||||
if url:
|
||||
config.dolibarr_url = url
|
||||
if api_key:
|
||||
config.api_key = api_key
|
||||
except Exception as exc: # pragma: no cover - defensive printing
|
||||
print(f"❌ Configuration error: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
try:
|
||||
async with DolibarrClient(config) as client:
|
||||
print("🧪 Testing Dolibarr API connection...")
|
||||
result = await client.get_status()
|
||||
|
||||
if "success" in result or "dolibarr_version" in str(result):
|
||||
print("✅ Connection successful!")
|
||||
if isinstance(result, dict) and "success" in result:
|
||||
version = result.get("success", {}).get("dolibarr_version", "Unknown")
|
||||
print(f"Dolibarr Version: {version}")
|
||||
return 0
|
||||
|
||||
print(f"⚠️ API responded unexpectedly: {result}")
|
||||
print("⚠️ Server will run but API calls may fail")
|
||||
return 2
|
||||
|
||||
except Exception as exc: # pragma: no cover - network failure path
|
||||
print(f"❌ Test failed: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
def test_connection(url: Optional[str] = None, api_key: Optional[str] = None) -> int:
|
||||
"""Synchronously test the Dolibarr API connection.
|
||||
|
||||
Parameters mirror the CLI flags and environment variables.
|
||||
Returns an exit code compatible with `sys.exit`.
|
||||
"""
|
||||
|
||||
return asyncio.run(_run_test(url=url, api_key=api_key))
|
||||
|
||||
Reference in New Issue
Block a user