|
| 1 | +"""Handler for REST API calls to dynamically manage MCP servers.""" |
| 2 | + |
| 3 | +from typing import Annotated, Any |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, Request, status |
| 6 | +from llama_stack_client import APIConnectionError |
| 7 | + |
| 8 | +from authentication import get_auth_dependency |
| 9 | +from authentication.interface import AuthTuple |
| 10 | +from authorization.middleware import authorize |
| 11 | +from client import AsyncLlamaStackClientHolder |
| 12 | +from configuration import configuration |
| 13 | +from models.config import Action, ModelContextProtocolServer |
| 14 | +from models.requests import MCPServerRegistrationRequest |
| 15 | +from models.responses import ( |
| 16 | + ConflictResponse, |
| 17 | + ForbiddenResponse, |
| 18 | + InternalServerErrorResponse, |
| 19 | + MCPServerDeleteResponse, |
| 20 | + MCPServerInfo, |
| 21 | + MCPServerListResponse, |
| 22 | + MCPServerRegistrationResponse, |
| 23 | + NotFoundResponse, |
| 24 | + ServiceUnavailableResponse, |
| 25 | + UnauthorizedResponse, |
| 26 | +) |
| 27 | +from utils.endpoints import check_configuration_loaded |
| 28 | +from log import get_logger |
| 29 | + |
| 30 | +logger = get_logger(__name__) |
| 31 | +router = APIRouter(tags=["mcp-servers"]) |
| 32 | + |
| 33 | + |
| 34 | +register_responses: dict[int | str, dict[str, Any]] = { |
| 35 | + 201: MCPServerRegistrationResponse.openapi_response(), |
| 36 | + 401: UnauthorizedResponse.openapi_response( |
| 37 | + examples=["missing header", "missing token"] |
| 38 | + ), |
| 39 | + 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), |
| 40 | + 409: ConflictResponse.openapi_response(examples=["mcp server"]), |
| 41 | + 500: InternalServerErrorResponse.openapi_response(examples=["configuration"]), |
| 42 | + 503: ServiceUnavailableResponse.openapi_response(), |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +@router.post( |
| 47 | + "/mcp-servers", |
| 48 | + responses=register_responses, |
| 49 | + status_code=status.HTTP_201_CREATED, |
| 50 | +) |
| 51 | +@authorize(Action.REGISTER_MCP_SERVER) |
| 52 | +async def register_mcp_server_handler( |
| 53 | + request: Request, |
| 54 | + body: MCPServerRegistrationRequest, |
| 55 | + auth: Annotated[AuthTuple, Depends(get_auth_dependency())], |
| 56 | +) -> MCPServerRegistrationResponse: |
| 57 | + """Register an MCP server dynamically at runtime. |
| 58 | +
|
| 59 | + Adds the MCP server to the runtime configuration and registers it |
| 60 | + as a toolgroup with Llama Stack so it becomes available for queries. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + HTTPException: On duplicate name, Llama Stack connection error, |
| 64 | + or registration failure. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + MCPServerRegistrationResponse: Details of the newly registered server. |
| 68 | + """ |
| 69 | + _ = auth |
| 70 | + _ = request |
| 71 | + |
| 72 | + check_configuration_loaded(configuration) |
| 73 | + |
| 74 | + mcp_server = ModelContextProtocolServer( |
| 75 | + name=body.name, |
| 76 | + url=body.url, |
| 77 | + provider_id=body.provider_id, |
| 78 | + authorization_headers=body.authorization_headers or {}, |
| 79 | + headers=body.headers or [], |
| 80 | + timeout=body.timeout, |
| 81 | + ) |
| 82 | + |
| 83 | + try: |
| 84 | + configuration.add_mcp_server(mcp_server) |
| 85 | + except ValueError as e: |
| 86 | + response = ConflictResponse(resource="MCP server", resource_id=body.name) |
| 87 | + raise HTTPException(**response.model_dump()) from e |
| 88 | + |
| 89 | + try: |
| 90 | + client = AsyncLlamaStackClientHolder().get_client() |
| 91 | + await client.toolgroups.register( # pyright: ignore[reportDeprecated] |
| 92 | + toolgroup_id=mcp_server.name, |
| 93 | + provider_id=mcp_server.provider_id, |
| 94 | + mcp_endpoint={"uri": mcp_server.url}, |
| 95 | + ) |
| 96 | + except APIConnectionError as e: |
| 97 | + configuration.remove_mcp_server(body.name) |
| 98 | + logger.error("Failed to register MCP server with Llama Stack: %s", e) |
| 99 | + response = ServiceUnavailableResponse(backend_name="Llama Stack", cause=str(e)) |
| 100 | + raise HTTPException(**response.model_dump()) from e |
| 101 | + except Exception as e: # pylint: disable=broad-exception-caught |
| 102 | + configuration.remove_mcp_server(body.name) |
| 103 | + logger.error("Failed to register MCP toolgroup: %s", e) |
| 104 | + error_response = InternalServerErrorResponse( |
| 105 | + response="Failed to register MCP server", |
| 106 | + cause=str(e), |
| 107 | + ) |
| 108 | + raise HTTPException(**error_response.model_dump()) from e |
| 109 | + |
| 110 | + logger.info("Dynamically registered MCP server: %s at %s", body.name, body.url) |
| 111 | + |
| 112 | + return MCPServerRegistrationResponse( |
| 113 | + name=mcp_server.name, |
| 114 | + url=mcp_server.url, |
| 115 | + provider_id=mcp_server.provider_id, |
| 116 | + message=f"MCP server '{mcp_server.name}' registered successfully", |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +list_responses: dict[int | str, dict[str, Any]] = { |
| 121 | + 200: MCPServerListResponse.openapi_response(), |
| 122 | + 401: UnauthorizedResponse.openapi_response( |
| 123 | + examples=["missing header", "missing token"] |
| 124 | + ), |
| 125 | + 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), |
| 126 | + 500: InternalServerErrorResponse.openapi_response(examples=["configuration"]), |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +@router.get("/mcp-servers", responses=list_responses) |
| 131 | +@authorize(Action.LIST_MCP_SERVERS) |
| 132 | +async def list_mcp_servers_handler( |
| 133 | + request: Request, |
| 134 | + auth: Annotated[AuthTuple, Depends(get_auth_dependency())], |
| 135 | +) -> MCPServerListResponse: |
| 136 | + """List all registered MCP servers. |
| 137 | +
|
| 138 | + Returns both statically configured (from YAML) and dynamically |
| 139 | + registered (via API) MCP servers. |
| 140 | +
|
| 141 | + Raises: |
| 142 | + HTTPException: If configuration is not loaded. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + MCPServerListResponse: List of all registered MCP servers with source info. |
| 146 | + """ |
| 147 | + _ = auth |
| 148 | + _ = request |
| 149 | + |
| 150 | + check_configuration_loaded(configuration) |
| 151 | + |
| 152 | + servers = [] |
| 153 | + for mcp in configuration.mcp_servers: |
| 154 | + source = "api" if configuration.is_dynamic_mcp_server(mcp.name) else "config" |
| 155 | + servers.append( |
| 156 | + MCPServerInfo( |
| 157 | + name=mcp.name, |
| 158 | + url=mcp.url, |
| 159 | + provider_id=mcp.provider_id, |
| 160 | + source=source, |
| 161 | + ) |
| 162 | + ) |
| 163 | + |
| 164 | + return MCPServerListResponse(servers=servers) |
| 165 | + |
| 166 | + |
| 167 | +delete_responses: dict[int | str, dict[str, Any]] = { |
| 168 | + 200: MCPServerDeleteResponse.openapi_response(), |
| 169 | + 401: UnauthorizedResponse.openapi_response( |
| 170 | + examples=["missing header", "missing token"] |
| 171 | + ), |
| 172 | + 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), |
| 173 | + 404: NotFoundResponse.openapi_response(examples=["mcp server"]), |
| 174 | + 500: InternalServerErrorResponse.openapi_response(examples=["configuration"]), |
| 175 | + 503: ServiceUnavailableResponse.openapi_response(), |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +@router.delete("/mcp-servers/{name}", responses=delete_responses) |
| 180 | +@authorize(Action.DELETE_MCP_SERVER) |
| 181 | +async def delete_mcp_server_handler( |
| 182 | + request: Request, |
| 183 | + name: str, |
| 184 | + auth: Annotated[AuthTuple, Depends(get_auth_dependency())], |
| 185 | +) -> MCPServerDeleteResponse: |
| 186 | + """Unregister a dynamically registered MCP server. |
| 187 | +
|
| 188 | + Removes the MCP server from the runtime configuration and unregisters |
| 189 | + its toolgroup from Llama Stack. Only servers registered via the API |
| 190 | + can be deleted; statically configured servers cannot be removed. |
| 191 | +
|
| 192 | + Raises: |
| 193 | + HTTPException: If the server is not found, is statically configured, |
| 194 | + or Llama Stack unregistration fails. |
| 195 | +
|
| 196 | + Returns: |
| 197 | + MCPServerDeleteResponse: Confirmation of the deletion. |
| 198 | + """ |
| 199 | + _ = auth |
| 200 | + _ = request |
| 201 | + |
| 202 | + check_configuration_loaded(configuration) |
| 203 | + |
| 204 | + if not configuration.is_dynamic_mcp_server(name): |
| 205 | + found = any(s.name == name for s in configuration.mcp_servers) |
| 206 | + if found: |
| 207 | + response = ForbiddenResponse( |
| 208 | + response="Cannot delete statically configured MCP server", |
| 209 | + cause=f"MCP server '{name}' was configured in lightspeed-stack.yaml " |
| 210 | + "and cannot be removed via the API.", |
| 211 | + ) |
| 212 | + else: |
| 213 | + response = NotFoundResponse(resource="MCP server", resource_id=name) |
| 214 | + raise HTTPException(**response.model_dump()) |
| 215 | + |
| 216 | + try: |
| 217 | + client = AsyncLlamaStackClientHolder().get_client() |
| 218 | + await client.toolgroups.unregister( # pyright: ignore[reportDeprecated] |
| 219 | + toolgroup_id=name |
| 220 | + ) |
| 221 | + except APIConnectionError as e: |
| 222 | + logger.error("Failed to unregister MCP toolgroup from Llama Stack: %s", e) |
| 223 | + svc_response = ServiceUnavailableResponse( |
| 224 | + backend_name="Llama Stack", cause=str(e) |
| 225 | + ) |
| 226 | + raise HTTPException(**svc_response.model_dump()) from e |
| 227 | + except Exception as e: # pylint: disable=broad-exception-caught |
| 228 | + logger.warning( |
| 229 | + "Llama Stack toolgroup unregister failed for '%s', " |
| 230 | + "proceeding with local removal: %s", |
| 231 | + name, |
| 232 | + e, |
| 233 | + ) |
| 234 | + |
| 235 | + try: |
| 236 | + configuration.remove_mcp_server(name) |
| 237 | + except ValueError as e: |
| 238 | + logger.error("Failed to remove MCP server from configuration: %s", e) |
| 239 | + response = NotFoundResponse(resource="MCP server", resource_id=name) |
| 240 | + raise HTTPException(**response.model_dump()) from e |
| 241 | + |
| 242 | + logger.info("Dynamically unregistered MCP server: %s", name) |
| 243 | + |
| 244 | + return MCPServerDeleteResponse( |
| 245 | + name=name, |
| 246 | + message=f"MCP server '{name}' unregistered successfully", |
| 247 | + ) |
0 commit comments