import json
from fastapi import FastAPI, Request, Response
import httpx
import os
import logging

app = FastAPI()
logging.basicConfig(level=logging.INFO)

DUMMY_MCP_URL = os.getenv("DUMMY_MCP_URL", "http://dummy-mcp:4200")

@app.post("/")
async def handle_request(request: Request):
    data = await request.json()
    logging.info(f"[Mini-Bridge] Received POST: {json.dumps(data)}")

    # leite Request an Dummy-MCP weiter:
    async with httpx.AsyncClient() as client:
        response = await client.post("http://dummy-mcp:4200", json=data)
        response_data = response.json()

    logging.info(f"[Mini-Bridge] Forwarding response: {json.dumps(response_data)}")

    # Keine "message"-Keys hinzufügen oder verändern!
    return Response(content=json.dumps(response_data), media_type="application/json")


@app.get("/manifest.json")
async def manifest():
    return {
        "name": "Mini-Bridge",
        "version": "0.2",
        "description": "Pass-through proxy for AnythingLLM MCP test"
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=4100)
