Skip to content

[MEDIUM] Duplicate view code paths increase maintenance risk #43

@7tg

Description

@7tg

Description

Two separate code paths implement identical MCP endpoint logic, risking divergence.

Location

  • File: django_admin_mcp/views.py
  • MCPHTTPView: lines 58-126 (class-based view)
  • mcp_endpoint(): lines 136-213 (function-based async view)

Issue

Both implementations:

  • Call authenticate_token()
  • Parse JSON requests
  • Route to handlers
  • Format responses

If a bug is fixed in one, the other may be forgotten.

Current Duplication

# MCPHTTPView.post()
token = await authenticate_token(request)
if not token:
    return JsonResponse({"error": "..."}, status=401)
# ... parsing and routing ...

# mcp_endpoint()
token = await authenticate_token(request)
if not token:
    return JsonResponse({"error": "..."}, status=401)
# ... same parsing and routing ...

Recommended Fix

Consolidate into single implementation:

async def _handle_mcp_request(request: HttpRequest) -> JsonResponse:
    """Core MCP request handling logic."""
    token = await authenticate_token(request)
    if not token:
        return JsonResponse({"error": "Invalid or missing token"}, status=401)
    
    # ... shared logic ...

# Class-based wrapper
class MCPHTTPView(View):
    async def post(self, request):
        return await _handle_mcp_request(request)

# Function-based wrapper
async def mcp_endpoint(request):
    if request.method != "POST":
        return JsonResponse({"error": "Method not allowed"}, status=405)
    return await _handle_mcp_request(request)

Metadata

Metadata

Assignees

No one assigned

    Labels

    mediumMedium priority

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions