add clerk oauth

This commit is contained in:
saidsurucu
2025-07-01 17:31:48 +03:00
parent bbf99870eb
commit e0bba7625f
5 changed files with 571 additions and 80 deletions
+24 -25
View File
@@ -2,35 +2,34 @@ import os
from functools import lru_cache
from fastmcp import FastMCP
from fastmcp.server.auth import BearerAuthProvider
from oauth_middleware import ClerkOAuthMiddleware
@lru_cache
def create_app() -> FastMCP:
"""Return a FastMCP instance; Clerk JWT validation when ENABLE_AUTH=true."""
if os.getenv("ENABLE_AUTH", "false").lower() != "true":
return FastMCP(
name="Yargı MCP DEV",
instructions="MCP server for TR legal databases (Yargitay, Danistay, Emsal, Uyusmazlik, Anayasa-Norm, Anayasa-Bireysel, KIK, Sayistay, Rekabet).",
dependencies=["httpx", "beautifulsoup4", "markitdown", "pydantic", "aiohttp", "playwright"]
)
# Public key'i environment variable'dan al, yoksa None
public_key_pem = os.environ.get("CLERK_PUBLIC_KEY")
"""Return a FastMCP instance; OAuth authentication when ENABLE_AUTH=true."""
# Base app configuration
app_config = {
"instructions": "MCP server for TR legal databases (Yargitay, Danistay, Emsal, Uyusmazlik, Anayasa-Norm, Anayasa-Bireysel, KIK, Sayistay, Rekabet).",
"dependencies": ["httpx", "beautifulsoup4", "markitdown", "pydantic", "aiohttp", "playwright"]
}
if public_key_pem:
# Eğer public key varsa, onu kullan (production)
auth = BearerAuthProvider(
public_key=public_key_pem,
# issuer, audience ve required_scopes kontrollerini yapmıyoruz
if os.getenv("ENABLE_AUTH", "false").lower() != "true":
# Development mode - no authentication
app = FastMCP(
name="Yargı MCP DEV",
**app_config
)
else:
# Public key yoksa JWKS endpoint kullan (development/fallback)
clerk_issuer = os.environ.get("CLERK_ISSUER", "https://clerk.accounts.dev")
auth = BearerAuthProvider(
jwks_uri=f"{clerk_issuer}/.well-known/jwks.json",
# Production mode - OAuth authentication via middleware
app = FastMCP(
name="Yargı MCP PROD",
**app_config
)
return FastMCP(
name="Yargı MCP PROD",
auth=auth,
instructions="MCP server for TR legal databases (Yargitay, Danistay, Emsal, Uyusmazlik, Anayasa-Norm, Anayasa-Bireysel, KIK, Sayistay, Rekabet) with JWT authentication.",
dependencies=["httpx", "beautifulsoup4", "markitdown", "pydantic", "aiohttp", "playwright"]
)
# Add OAuth middleware instead of BearerAuthProvider
app.add_middleware(ClerkOAuthMiddleware())
# Update instructions to reflect OAuth
app.instructions += " with OAuth authentication via Clerk."
return app