pgurazada1 commited on
Commit
f87d282
·
verified ·
1 Parent(s): 7737dc2

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +4 -3
server.py CHANGED
@@ -125,8 +125,8 @@ llm = ChatOpenAI(
125
  # Hugging Face Token Auth Middleware
126
  class HuggingFaceTokenAuthMiddleware(BaseHTTPMiddleware):
127
  async def dispatch(self, request: Request, call_next):
128
- # Only protect /mcp/ and /tools endpoints, allow "/" open
129
- if request.url.path not in ["/", "/mcp/", "/tools"]:
130
  return await call_next(request)
131
  # Check Authorization header
132
  auth = request.headers.get("authorization")
@@ -142,6 +142,7 @@ class HuggingFaceTokenAuthMiddleware(BaseHTTPMiddleware):
142
  if resp.status_code != 200:
143
  return PlainTextResponse("Invalid or expired Hugging Face token", status_code=401)
144
  hf_user_info = resp.json()
 
145
  request.state.hf_user = hf_user_info
146
  return await call_next(request)
147
 
@@ -224,7 +225,7 @@ async def list_tools(request: Request) -> JSONResponse:
224
  """Return all registered tool metadata as JSON."""
225
  return JSONResponse(tool_registry)
226
 
227
- # Add the Hugging Face token auth middleware at app construction time
228
  app = mcp.streamable_http_app()
229
  app.add_middleware(HuggingFaceTokenAuthMiddleware)
230
 
 
125
  # Hugging Face Token Auth Middleware
126
  class HuggingFaceTokenAuthMiddleware(BaseHTTPMiddleware):
127
  async def dispatch(self, request: Request, call_next):
128
+ # Allow "/" to be public, protect everything else
129
+ if request.url.path == "/":
130
  return await call_next(request)
131
  # Check Authorization header
132
  auth = request.headers.get("authorization")
 
142
  if resp.status_code != 200:
143
  return PlainTextResponse("Invalid or expired Hugging Face token", status_code=401)
144
  hf_user_info = resp.json()
145
+ # Attach the HF user info to request.state for downstream use if needed
146
  request.state.hf_user = hf_user_info
147
  return await call_next(request)
148
 
 
225
  """Return all registered tool metadata as JSON."""
226
  return JSONResponse(tool_registry)
227
 
228
+ # --- Build the app and add middleware ---
229
  app = mcp.streamable_http_app()
230
  app.add_middleware(HuggingFaceTokenAuthMiddleware)
231