-
Notifications
You must be signed in to change notification settings - Fork 99
LCORE-1797: Add OTEL spans for MCP auth and MCP servers endpoints #2527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| from typing import Annotated, Any | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Request, status | ||
| from opentelemetry import trace | ||
|
|
||
| from authentication import get_auth_dependency | ||
| from authentication.interface import AuthTuple | ||
|
|
@@ -25,8 +26,10 @@ | |
| from models.common import MCPServerInfo | ||
| from models.config import Action, ModelContextProtocolServer | ||
| from utils.endpoints import check_configuration_loaded | ||
| from utils.otel_tracing import SpanAttributes, set_span_attributes | ||
|
|
||
| logger = get_logger(__name__) | ||
| tracer = trace.get_tracer(__name__) | ||
| router = APIRouter(tags=["mcp-servers"]) | ||
|
|
||
|
|
||
|
|
@@ -71,26 +74,37 @@ async def register_mcp_server_handler( | |
| _ = auth | ||
| _ = request | ||
|
|
||
| check_configuration_loaded(configuration) | ||
| with tracer.start_as_current_span("mcp_server.register") as span: | ||
| set_span_attributes( | ||
| span, | ||
| { | ||
| SpanAttributes.MCP_OPERATION: "register", | ||
| SpanAttributes.MCP_SERVER_NAME: body.name, | ||
| SpanAttributes.MCP_SERVER_PROVIDER_ID: body.provider_id | ||
| or "model-context-protocol", | ||
| }, | ||
| ) | ||
|
|
||
| mcp_server = ModelContextProtocolServer.model_validate( | ||
| body.model_dump(exclude_none=True) | ||
| ) | ||
| check_configuration_loaded(configuration) | ||
|
|
||
| try: | ||
| configuration.add_mcp_server(mcp_server) | ||
| except ValueError as e: | ||
| response = ConflictResponse(resource="MCP server", resource_id=body.name) | ||
| raise HTTPException(**response.model_dump()) from e | ||
| mcp_server = ModelContextProtocolServer.model_validate( | ||
| body.model_dump(exclude_none=True) | ||
| ) | ||
|
|
||
| logger.info("Dynamically registered MCP server: %s at %s", body.name, body.url) | ||
| try: | ||
| configuration.add_mcp_server(mcp_server) | ||
| except ValueError as e: | ||
| response = ConflictResponse(resource="MCP server", resource_id=body.name) | ||
| raise HTTPException(**response.model_dump()) from e | ||
|
|
||
| return MCPServerRegistrationResponse( | ||
| name=mcp_server.name, | ||
| url=mcp_server.url, | ||
| provider_id=mcp_server.provider_id, | ||
| message=f"MCP server '{mcp_server.name}' registered successfully", | ||
| ) | ||
| logger.info("Dynamically registered MCP server: %s at %s", body.name, body.url) | ||
|
|
||
| return MCPServerRegistrationResponse( | ||
| name=mcp_server.name, | ||
| url=mcp_server.url, | ||
| provider_id=mcp_server.provider_id, | ||
| message=f"MCP server '{mcp_server.name}' registered successfully", | ||
| ) | ||
|
|
||
|
|
||
| list_responses: dict[int | str, dict[str, Any]] = { | ||
|
|
@@ -125,19 +139,26 @@ async def list_mcp_servers_handler( | |
| _ = auth | ||
| _ = request | ||
|
|
||
| check_configuration_loaded(configuration) | ||
| with tracer.start_as_current_span("mcp_server.list") as span: | ||
| set_span_attributes(span, {SpanAttributes.MCP_OPERATION: "list"}) | ||
|
|
||
| servers = [ | ||
| MCPServerInfo( | ||
| name=mcp.name, | ||
| url=mcp.url, | ||
| provider_id=mcp.provider_id, | ||
| source="api" if configuration.is_dynamic_mcp_server(mcp.name) else "config", | ||
| ) | ||
| for mcp in configuration.mcp_servers | ||
| ] | ||
| check_configuration_loaded(configuration) | ||
|
|
||
| return MCPServerListResponse(servers=servers) | ||
| servers = [ | ||
| MCPServerInfo( | ||
| name=mcp.name, | ||
| url=mcp.url, | ||
| provider_id=mcp.provider_id, | ||
| source=( | ||
| "api" if configuration.is_dynamic_mcp_server(mcp.name) else "config" | ||
| ), | ||
| ) | ||
| for mcp in configuration.mcp_servers | ||
| ] | ||
|
|
||
| set_span_attributes(span, {SpanAttributes.MCP_SERVERS_COUNT: len(servers)}) | ||
|
|
||
| return MCPServerListResponse(servers=servers) | ||
|
Comment on lines
+142
to
+161
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 Performance & Scalability | 🟠 Major | 🏗️ Heavy lift Add pagination or a strict result limit to both MCP server list endpoints. Dynamic registration can grow
As per coding guidelines, flag “missing pagination or limits on list operations and API endpoints.” 📍 Affects 2 files
🤖 Prompt for AI AgentsSource: Coding guidelines |
||
|
|
||
|
|
||
| delete_responses: dict[int | str, dict[str, Any]] = { | ||
|
|
@@ -175,19 +196,30 @@ async def delete_mcp_server_handler( | |
| _ = auth | ||
| _ = request | ||
|
|
||
| check_configuration_loaded(configuration) | ||
| with tracer.start_as_current_span("mcp_server.delete") as span: | ||
| set_span_attributes( | ||
| span, | ||
| { | ||
| SpanAttributes.MCP_OPERATION: "delete", | ||
| SpanAttributes.MCP_SERVER_NAME: name, | ||
| }, | ||
| ) | ||
|
|
||
| check_configuration_loaded(configuration) | ||
|
|
||
| if not configuration.is_dynamic_mcp_server(name): | ||
| static_mcp_names = {s.name for s in configuration.mcp_servers} | ||
| if name in static_mcp_names: | ||
| response = ForbiddenResponse.mcp_server_static_config(name) | ||
| raise HTTPException(**response.model_dump()) | ||
|
|
||
| if not configuration.is_dynamic_mcp_server(name): | ||
| static_mcp_names = {s.name for s in configuration.mcp_servers} | ||
| if name in static_mcp_names: | ||
| response = ForbiddenResponse.mcp_server_static_config(name) | ||
| raise HTTPException(**response.model_dump()) | ||
| try: | ||
| configuration.remove_mcp_server(name) | ||
| local_deleted = True | ||
| except ValueError as e: | ||
| logger.error("Failed to remove MCP server from configuration: %s", e) | ||
| local_deleted = False | ||
|
|
||
| try: | ||
| configuration.remove_mcp_server(name) | ||
| local_deleted = True | ||
| except ValueError as e: | ||
| logger.error("Failed to remove MCP server from configuration: %s", e) | ||
| local_deleted = False | ||
| set_span_attributes(span, {SpanAttributes.MCP_SERVER_DELETED: local_deleted}) | ||
|
|
||
| return MCPServerDeleteResponse(deleted=local_deleted, name=name) | ||
| return MCPServerDeleteResponse(deleted=local_deleted, name=name) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Record the actual provider ID.
provider_idaccepts"". Registration preserves that value, but Lines 83-84 record"model-context-protocol"in the span. This makes the trace disagree with the response and registered server.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents