diff --git a/src/core/config_loader.py b/src/core/config_loader.py index 0404021c7..9ca2a56bb 100644 --- a/src/core/config_loader.py +++ b/src/core/config_loader.py @@ -153,6 +153,32 @@ def get_tenant_by_subdomain(subdomain: str) -> dict[str, Any] | None: raise +def get_tenant_by_id(tenant_id: str) -> dict[str, Any] | None: + """Get tenant by tenant_id. + + Args: + tenant_id: The tenant_id to look up (e.g., 'tenant_wonderstruck') + + Returns: + Tenant dict if found, None otherwise + """ + try: + with get_db_session() as db_session: + stmt = select(Tenant).filter_by(tenant_id=tenant_id, is_active=True) + tenant = db_session.scalars(stmt).first() + + if tenant: + from src.core.utils.tenant_utils import serialize_tenant_to_dict + + return serialize_tenant_to_dict(tenant) + return None + except Exception as e: + # If table doesn't exist or other DB errors, return None + if "no such table" in str(e) or "does not exist" in str(e): + return None + raise + + def get_tenant_by_virtual_host(virtual_host: str) -> dict[str, Any] | None: """Get tenant by virtual host.""" try: diff --git a/src/core/main.py b/src/core/main.py index fabaa82ef..2501f65e2 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -37,6 +37,7 @@ # Other imports from src.core.config_loader import ( get_current_tenant, + get_tenant_by_id, get_tenant_by_subdomain, get_tenant_by_virtual_host, load_config, @@ -405,7 +406,13 @@ def get_principal_from_context(context: Context | None) -> str | None: # Fallback: assume it's already a tenant_id requested_tenant_id = tenant_hint detection_method = "x-adcp-tenant header (direct)" - console.print(f"[yellow]Using x-adcp-tenant as tenant_id directly: {requested_tenant_id}[/yellow]") + # Need to look up and set tenant context + tenant_context = get_tenant_by_id(tenant_hint) + if tenant_context: + set_current_tenant(tenant_context) + console.print(f"[green]Tenant context set for tenant_id: {requested_tenant_id}[/green]") + else: + console.print(f"[yellow]Using x-adcp-tenant as tenant_id directly: {requested_tenant_id}[/yellow]") # 3. Check Apx-Incoming-Host header (for Approximated.app virtual hosts) if not requested_tenant_id: diff --git a/test_webhook_url.py b/test_webhook_url.py deleted file mode 100644 index 726ba5f7d..000000000 --- a/test_webhook_url.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -"""Test that sync_creatives accepts webhook_url parameter.""" - -import asyncio -import sys -from datetime import UTC, datetime - -from fastmcp.client import Client -from fastmcp.client.transports import StreamableHttpTransport - - -async def test_sync_creatives_with_webhook(): - """Test sync_creatives with webhook_url parameter.""" - - # Use local MCP server - headers = { - "x-adcp-auth": "f68ZhutgGiHEMwHo8jKlr0heEsptkmElRVNfzYiz1IY", # Default tenant token - } - - transport = StreamableHttpTransport(url="http://localhost:8085/mcp/", headers=headers) - - async with Client(transport=transport) as client: - print("āœ“ Connected to MCP server") - - # Create a test creative - test_creative = { - "creative_id": f"test_webhook_{datetime.now(UTC).timestamp()}", - "name": "Test Creative with Webhook", - "format_id": "display_300x250", - "url": "https://example.com/test-ad.jpg", - "click_url": "https://example.com/click", - "width": 300, - "height": 250, - } - - print("\nšŸ“¤ Calling sync_creatives with webhook_url parameter...") - print(f" Creative: {test_creative['name']}") - print(" Webhook: https://webhook.example.com/notify") - - try: - result = await client.call_tool( - "sync_creatives", {"creatives": [test_creative], "webhook_url": "https://webhook.example.com/notify"} - ) - - print("\nāœ… SUCCESS! Server accepted webhook_url parameter") - print("\nšŸ“Š Result:") - print(f" {result}") - - return True - - except Exception as e: - print(f"\nāŒ FAILED: {e}") - if "webhook_url" in str(e) and "Unexpected keyword argument" in str(e): - print("\nšŸ” Diagnosis: Server doesn't accept webhook_url parameter yet") - print(" - Check if server was restarted after code changes") - print(" - Verify _sync_creatives_impl() has webhook_url parameter") - return False - - -if __name__ == "__main__": - success = asyncio.run(test_sync_creatives_with_webhook()) - sys.exit(0 if success else 1)