feat: add api and mcp

This commit is contained in:
hh
2026-05-30 01:32:35 +02:00
parent 6a5cde6ae4
commit c40e720163
30 changed files with 2354 additions and 31 deletions
+36
View File
@@ -0,0 +1,36 @@
from starlette.types import ASGIApp, Receive, Scope, Send
PROTECTED_PREFIXES = ("/api", "/mcp")
_UNAUTHORIZED = b'{"detail":"unauthorized"}'
class BearerAuthMiddleware:
def __init__(self, app: ASGIApp, token: str) -> None:
self.app = app
self.token = token
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
if scope["method"] == "OPTIONS" or not scope["path"].startswith(
PROTECTED_PREFIXES
):
await self.app(scope, receive, send)
return
headers = dict(scope["headers"])
authorization = headers.get(b"authorization", b"").decode()
if authorization == f"Bearer {self.token}":
await self.app(scope, receive, send)
return
await send(
{
"type": "http.response.start",
"status": 401,
"headers": [
(b"content-type", b"application/json"),
(b"www-authenticate", b"Bearer"),
],
}
)
await send({"type": "http.response.body", "body": _UNAUTHORIZED})