File size: 3,776 Bytes
56c063a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""

Example usage of Clerk authentication integration.

Demonstrates how to set up FastAPI with Clerk authentication.

"""

from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

from shared.middleware import AuthenticationMiddleware, require_auth, require_admin_permission
from shared.auth_endpoints import auth_router, oauth_router
from shared.auth import UserContext
from shared.config import settings


def create_app() -> FastAPI:
    """Create FastAPI application with Clerk authentication."""
    
    app = FastAPI(
        title="MCP AI Data Scientist API",
        description="AI-powered data science platform with Clerk authentication",
        version="1.0.0"
    )
    
    # Add CORS middleware
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],  # Configure appropriately for production
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    
    # Add authentication middleware
    app.add_middleware(
        AuthenticationMiddleware,
        exclude_paths=[
            "/health",
            "/docs",
            "/redoc", 
            "/openapi.json",
            "/oauth/.well-known/oauth-authorization-server"
        ]
    )
    
    # Include authentication and OAuth routers
    app.include_router(auth_router)
    app.include_router(oauth_router)
    
    # Example protected endpoints
    @app.get("/")
    async def root():
        """Public root endpoint."""
        return {"message": "MCP AI Data Scientist API", "status": "running"}
    
    @app.get("/health")
    async def health_check():
        """Health check endpoint (no auth required)."""
        return {"status": "healthy", "service": "mcp-ai-data-scientist"}
    
    @app.get("/protected")
    async def protected_endpoint(user: UserContext = Depends(require_auth)):
        """Example protected endpoint requiring authentication."""
        return {
            "message": "This is a protected endpoint",
            "user_id": user.clerk_user_id,
            "email": user.email,
            "permissions": user.permissions
        }
    
    @app.get("/admin-only")
    async def admin_only_endpoint(user: UserContext = Depends(require_admin_permission)):
        """Example admin-only endpoint."""
        return {
            "message": "This endpoint requires admin permissions",
            "user_id": user.clerk_user_id,
            "admin": True
        }
    
    @app.post("/analysis/start")
    async def start_analysis(

        business_context: str,

        user: UserContext = Depends(require_auth)

    ):
        """Example endpoint to start data analysis (requires authentication)."""
        # This would integrate with the MCP servers
        return {
            "message": "Analysis started",
            "user_id": user.clerk_user_id,
            "business_context": business_context,
            "session_id": "mock-session-123"
        }
    
    return app


# Example of how to run the application
if __name__ == "__main__":
    app = create_app()
    
    uvicorn.run(
        app,
        host=settings.fastapi_host,
        port=settings.fastapi_port,
        reload=settings.fastapi_debug
    )


# Example usage in other modules:
"""

from shared.auth_example import create_app

from shared.middleware import require_auth, require_write_permission

from shared.auth import UserContext



app = create_app()



@app.post("/my-endpoint")

async def my_endpoint(user: UserContext = Depends(require_write_permission)):

    # This endpoint requires write permission

    return {"user": user.clerk_user_id}

"""