|
25 | 25 | from typing import List, Union
|
26 | 26 | from uuid import uuid4
|
27 | 27 |
|
28 |
| -import mcp.types as types |
| 28 | +from mcp import types |
29 | 29 | from fastapi.security.utils import get_authorization_scheme_param
|
30 | 30 | from mcp.server.lowlevel import Server
|
31 | 31 | from mcp.server.streamable_http import (
|
|
38 | 38 | from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
|
39 | 39 | from mcp.types import JSONRPCMessage
|
40 | 40 | from starlette.datastructures import Headers
|
41 |
| -from starlette.middleware.base import BaseHTTPMiddleware |
42 | 41 | from starlette.responses import JSONResponse
|
43 | 42 | from starlette.status import HTTP_401_UNAUTHORIZED
|
44 | 43 | from starlette.types import Receive, Scope, Send
|
@@ -201,15 +200,15 @@ async def list_tools() -> List[types.Tool]:
|
201 | 200 | tools = await tool_service.list_server_tools(db, server_id)
|
202 | 201 | return [types.Tool(name=tool.name, description=tool.description, inputSchema=tool.input_schema) for tool in tools]
|
203 | 202 | except Exception as e:
|
204 |
| - logger.exception("Error listing tools") |
| 203 | + logger.exception(f"Error listing tools:{e}") |
205 | 204 | return []
|
206 | 205 | else:
|
207 | 206 | try:
|
208 | 207 | async with get_db() as db:
|
209 | 208 | tools = await tool_service.list_tools(db)
|
210 | 209 | return [types.Tool(name=tool.name, description=tool.description, inputSchema=tool.input_schema) for tool in tools]
|
211 | 210 | except Exception as e:
|
212 |
| - logger.exception("Error listing tools") |
| 211 | + logger.exception(f"Error listing tools:{e}") |
213 | 212 | return []
|
214 | 213 |
|
215 | 214 |
|
@@ -274,68 +273,12 @@ async def handle_streamable_http(self, scope: Scope, receive: Receive, send: Sen
|
274 | 273 | try:
|
275 | 274 | await self.session_manager.handle_request(scope, receive, send)
|
276 | 275 | except Exception as e:
|
277 |
| - logger.exception("Error handling streamable HTTP request") |
| 276 | + logger.exception(f"Error handling streamable HTTP request: {e}") |
278 | 277 | raise
|
279 | 278 |
|
280 | 279 |
|
281 | 280 | ## ------------------------- Authentication for /mcp routes ------------------------------
|
282 | 281 |
|
283 |
| -# async def streamable_http_auth(scope, receive, send): |
284 |
| -# """ |
285 |
| -# Perform authentication check in middleware context (ASGI scope). |
286 |
| - |
287 |
| -# If path does not end with "/mcp", just continue (return True). |
288 |
| - |
289 |
| -# If auth fails, sends 401 JSONResponse and returns False. |
290 |
| - |
291 |
| -# If auth succeeds or not required, returns True. |
292 |
| -# """ |
293 |
| - |
294 |
| -# path = scope.get("path", "") |
295 |
| -# if not path.endswith("/mcp"): |
296 |
| -# # No auth needed for other paths in this middleware usage |
297 |
| -# return True |
298 |
| - |
299 |
| -# headers = Headers(scope=scope) |
300 |
| -# authorization = headers.get("authorization") |
301 |
| -# cookie_header = headers.get("cookie", "") |
302 |
| - |
303 |
| -# token = None |
304 |
| -# if authorization: |
305 |
| -# scheme, credentials = get_authorization_scheme_param(authorization) |
306 |
| -# if scheme.lower() == "bearer" and credentials: |
307 |
| -# token = credentials |
308 |
| - |
309 |
| -# if not token: |
310 |
| -# # parse cookie header manually |
311 |
| -# for cookie in cookie_header.split(";"): |
312 |
| -# if cookie.strip().startswith("jwt_token="): |
313 |
| -# token = cookie.strip().split("=", 1)[1] |
314 |
| -# break |
315 |
| - |
316 |
| -# if settings.auth_required and not token: |
317 |
| -# response = JSONResponse( |
318 |
| -# {"detail": "Not authenticated"}, |
319 |
| -# status_code=HTTP_401_UNAUTHORIZED, |
320 |
| -# headers={"WWW-Authenticate": "Bearer"}, |
321 |
| -# ) |
322 |
| -# await response(scope, receive, send) |
323 |
| -# return False |
324 |
| - |
325 |
| -# if token: |
326 |
| -# try: |
327 |
| -# await verify_credentials(token) |
328 |
| -# except Exception: |
329 |
| -# response = JSONResponse( |
330 |
| -# {"detail": "Authentication failed"}, |
331 |
| -# status_code=HTTP_401_UNAUTHORIZED, |
332 |
| -# headers={"WWW-Authenticate": "Bearer"}, |
333 |
| -# ) |
334 |
| -# await response(scope, receive, send) |
335 |
| -# return False |
336 |
| - |
337 |
| -# return True |
338 |
| - |
339 | 282 |
|
340 | 283 | async def streamable_http_auth(scope, receive, send):
|
341 | 284 | """
|
|
0 commit comments