62
62
from mcpgateway .services .prompt_service import PromptNotFoundError , PromptService
63
63
from mcpgateway .services .resource_service import ResourceNotFoundError , ResourceService
64
64
from mcpgateway .services .root_service import RootService
65
- from mcpgateway .services .server_service import ServerError , ServerNotFoundError , ServerService
65
+ from mcpgateway .services .server_service import ServerError , ServerNotFoundError , ServerService , ServerNameConflictError
66
66
from mcpgateway .services .tool_service import ToolError , ToolNotFoundError , ToolService
67
67
from mcpgateway .utils .create_jwt_token import get_jwt_token
68
68
from mcpgateway .utils .error_formatter import ErrorFormatter
@@ -294,7 +294,7 @@ async def admin_get_server(server_id: str, db: Session = Depends(get_db), user:
294
294
295
295
296
296
@admin_router .post ("/servers" , response_model = ServerRead )
297
- async def admin_add_server (request : Request , db : Session = Depends (get_db ), user : str = Depends (require_auth )) -> RedirectResponse :
297
+ async def admin_add_server (request : Request , db : Session = Depends (get_db ), user : str = Depends (require_auth )) -> JSONResponse :
298
298
"""
299
299
Add a new server via the admin UI.
300
300
@@ -316,7 +316,7 @@ async def admin_add_server(request: Request, db: Session = Depends(get_db), user
316
316
user (str): Authenticated user dependency
317
317
318
318
Returns:
319
- RedirectResponse : A redirect to the admin dashboard catalog section
319
+ JSONResponse : A JSON response indicating success or failure of the server creation operation.
320
320
321
321
Examples:
322
322
>>> import asyncio
@@ -436,7 +436,6 @@ async def admin_add_server(request: Request, db: Session = Depends(get_db), user
436
436
return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 422 )
437
437
438
438
except Exception as ex :
439
- logger .info (f"error,{ ex } " )
440
439
if isinstance (ex , ServerError ):
441
440
# Custom server logic error — 500 Internal Server Error makes sense
442
441
return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
@@ -467,7 +466,7 @@ async def admin_edit_server(
467
466
request : Request ,
468
467
db : Session = Depends (get_db ),
469
468
user : str = Depends (require_auth ),
470
- ) -> RedirectResponse :
469
+ ) -> JSONResponse :
471
470
"""
472
471
Edit an existing server via the admin UI.
473
472
@@ -490,7 +489,7 @@ async def admin_edit_server(
490
489
user (str): Authenticated user dependency
491
490
492
491
Returns:
493
- RedirectResponse : A redirect to the admin dashboard catalog section with a status code of 303
492
+ JSONResponse : A JSON response indicating success or failure of the server update operation.
494
493
495
494
Examples:
496
495
>>> import asyncio
@@ -546,7 +545,7 @@ async def admin_edit_server(
546
545
>>> server_service.update_server = original_update_server
547
546
"""
548
547
form = await request .form ()
549
- is_inactive_checked = form .get ("is_inactive_checked" , "false" )
548
+ # is_inactive_checked = form.get("is_inactive_checked", "false")
550
549
try :
551
550
logger .debug (f"User { user } is editing server ID { server_id } with name: { form .get ('name' )} " )
552
551
server = ServerUpdate (
@@ -559,18 +558,42 @@ async def admin_edit_server(
559
558
)
560
559
await server_service .update_server (db , server_id , server )
561
560
562
- root_path = request .scope .get ("root_path" , "" )
561
+ # root_path = request.scope.get("root_path", "")
563
562
564
- if is_inactive_checked .lower () == "true" :
565
- return RedirectResponse (f"{ root_path } /admin/?include_inactive=true#catalog" , status_code = 303 )
566
- return RedirectResponse (f"{ root_path } /admin#catalog" , status_code = 303 )
567
- except Exception as e :
568
- logger .error (f"Error editing server: { e } " )
563
+ # if is_inactive_checked.lower() == "true":
564
+ # return RedirectResponse(f"{root_path}/admin/?include_inactive=true#catalog", status_code=303)
565
+ # return RedirectResponse(f"{root_path}/admin#catalog", status_code=303)
566
+ return JSONResponse (
567
+ content = {"message" : "Server update successfully!" , "success" : True },
568
+ status_code = 200 ,
569
+ )
570
+ except Exception as ex :
571
+ if isinstance (ex , ServerNameConflictError ):
572
+ # Custom server name conflict error — 409 Conflict is appropriate
573
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 409 )
569
574
570
- root_path = request .scope .get ("root_path" , "" )
571
- if is_inactive_checked .lower () == "true" :
572
- return RedirectResponse (f"{ root_path } /admin/?include_inactive=true#catalog" , status_code = 303 )
573
- return RedirectResponse (f"{ root_path } /admin#catalog" , status_code = 303 )
575
+ if isinstance (ex , ServerError ):
576
+ # Custom server logic error — 500 Internal Server Error makes sense
577
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
578
+
579
+ if isinstance (ex , ValueError ):
580
+ # Invalid input — 400 Bad Request is appropriate
581
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 400 )
582
+
583
+ if isinstance (ex , RuntimeError ):
584
+ # Unexpected error during runtime — 500 is suitable
585
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
586
+
587
+ if isinstance (ex , ValidationError ):
588
+ # Pydantic or input validation failure — 422 Unprocessable Entity is correct
589
+ return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = 422 )
590
+
591
+ if isinstance (ex , IntegrityError ):
592
+ # DB constraint violation — 409 Conflict is appropriate
593
+ return JSONResponse (content = ErrorFormatter .format_database_error (ex ), status_code = 409 )
594
+
595
+ # For any other unhandled error, default to 500
596
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
574
597
575
598
576
599
@admin_router .post ("/servers/{server_id}/toggle" )
@@ -1935,7 +1958,7 @@ async def admin_edit_tool(
1935
1958
return JSONResponse (content = {"message" : "Edit tool successfully" , "success" : True }, status_code = 200 )
1936
1959
except IntegrityError as ex :
1937
1960
error_message = ErrorFormatter .format_database_error (ex )
1938
- logger .error (f"IntegrityError in admin_edit_resource : { error_message } " )
1961
+ logger .error (f"IntegrityError in admin_tool_resource : { error_message } " )
1939
1962
return JSONResponse (status_code = 409 , content = error_message )
1940
1963
except ToolError as ex :
1941
1964
logger .error (f"ToolError in admin_edit_tool: { str (ex )} " )
@@ -2686,7 +2709,7 @@ async def admin_get_resource(uri: str, db: Session = Depends(get_db), user: str
2686
2709
2687
2710
2688
2711
@admin_router .post ("/resources" )
2689
- async def admin_add_resource (request : Request , db : Session = Depends (get_db ), user : str = Depends (require_auth )) -> RedirectResponse :
2712
+ async def admin_add_resource (request : Request , db : Session = Depends (get_db ), user : str = Depends (require_auth )) -> JSONResponse :
2690
2713
"""
2691
2714
Add a resource via the admin UI.
2692
2715
@@ -2771,7 +2794,7 @@ async def admin_edit_resource(
2771
2794
request : Request ,
2772
2795
db : Session = Depends (get_db ),
2773
2796
user : str = Depends (require_auth ),
2774
- ) -> RedirectResponse :
2797
+ ) -> JSONResponse :
2775
2798
"""
2776
2799
Edit a resource via the admin UI.
2777
2800
@@ -2788,7 +2811,7 @@ async def admin_edit_resource(
2788
2811
user: Authenticated user.
2789
2812
2790
2813
Returns:
2791
- RedirectResponse : A redirect response to the admin dashboard .
2814
+ JSONResponse : A JSON response indicating success or failure of the resource update operation .
2792
2815
2793
2816
Examples:
2794
2817
>>> import asyncio
@@ -2839,18 +2862,28 @@ async def admin_edit_resource(
2839
2862
"""
2840
2863
logger .debug (f"User { user } is editing resource URI { uri } " )
2841
2864
form = await request .form ()
2842
- resource = ResourceUpdate (
2843
- name = form ["name" ],
2844
- description = form .get ("description" ),
2845
- mime_type = form .get ("mimeType" ),
2846
- content = form ["content" ],
2847
- )
2848
- await resource_service .update_resource (db , uri , resource )
2849
- root_path = request .scope .get ("root_path" , "" )
2850
- is_inactive_checked = form .get ("is_inactive_checked" , "false" )
2851
- if is_inactive_checked .lower () == "true" :
2852
- return RedirectResponse (f"{ root_path } /admin/?include_inactive=true#resources" , status_code = 303 )
2853
- return RedirectResponse (f"{ root_path } /admin#resources" , status_code = 303 )
2865
+ try :
2866
+ resource = ResourceUpdate (
2867
+ name = form ["name" ],
2868
+ description = form .get ("description" ),
2869
+ mime_type = form .get ("mimeType" ),
2870
+ content = form ["content" ],
2871
+ )
2872
+ await resource_service .update_resource (db , uri , resource )
2873
+ return JSONResponse (
2874
+ content = {"message" : "Resource updated successfully!" , "success" : True },
2875
+ status_code = 200 ,
2876
+ )
2877
+ except Exception as ex :
2878
+ if isinstance (ex , ValidationError ):
2879
+ logger .error (f"ValidationError in admin_edit_resource: { ErrorFormatter .format_validation_error (ex )} " )
2880
+ return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = 422 )
2881
+ if isinstance (ex , IntegrityError ):
2882
+ error_message = ErrorFormatter .format_database_error (ex )
2883
+ logger .error (f"IntegrityError in admin_edit_resource: { error_message } " )
2884
+ return JSONResponse (status_code = 409 , content = error_message )
2885
+ logger .error (f"Error in admin_edit_resource: { ex } " )
2886
+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
2854
2887
2855
2888
2856
2889
@admin_router .post ("/resources/{uri:path}/delete" )
@@ -3211,7 +3244,7 @@ async def admin_edit_prompt(
3211
3244
request : Request ,
3212
3245
db : Session = Depends (get_db ),
3213
3246
user : str = Depends (require_auth ),
3214
- ) -> RedirectResponse :
3247
+ ) -> JSONResponse :
3215
3248
"""Edit a prompt via the admin UI.
3216
3249
3217
3250
Expects form fields:
@@ -3302,13 +3335,13 @@ async def admin_edit_prompt(
3302
3335
)
3303
3336
except Exception as ex :
3304
3337
if isinstance (ex , ValidationError ):
3305
- logger .error (f"ValidationError in admin_add_prompt : { ErrorFormatter .format_validation_error (ex )} " )
3338
+ logger .error (f"ValidationError in admin_edit_prompt : { ErrorFormatter .format_validation_error (ex )} " )
3306
3339
return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = 422 )
3307
3340
if isinstance (ex , IntegrityError ):
3308
3341
error_message = ErrorFormatter .format_database_error (ex )
3309
- logger .error (f"IntegrityError in admin_add_prompt : { error_message } " )
3342
+ logger .error (f"IntegrityError in admin_edit_prompt : { error_message } " )
3310
3343
return JSONResponse (status_code = 409 , content = error_message )
3311
- logger .error (f"Error in admin_add_prompt : { ex } " )
3344
+ logger .error (f"Error in admin_edit_prompt : { ex } " )
3312
3345
return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
3313
3346
3314
3347
0 commit comments