(request)
| 8 | |
| 9 | @csrf_exempt |
| 10 | def mcp_view(request): |
| 11 | request_id = None |
| 12 | try: |
| 13 | data = json.loads(request.body) |
| 14 | method = data.get("method") |
| 15 | params = data.get("params", {}) |
| 16 | request_id = data.get("id") |
| 17 | |
| 18 | if request_id is None: |
| 19 | return HttpResponse(status=204) |
| 20 | |
| 21 | auth_header = request.headers.get("Authorization", "").replace("Bearer ", "") |
| 22 | handler = MCPToolHandler(auth_header) |
| 23 | |
| 24 | # 路由方法 |
| 25 | if method == "initialize": |
| 26 | result = handler.initialize() |
| 27 | |
| 28 | elif method == "tools/list": |
| 29 | result = handler.list_tools() |
| 30 | |
| 31 | elif method == "tools/call": |
| 32 | result = handler.call_tool(params) |
| 33 | |
| 34 | else: |
| 35 | return JsonResponse({ |
| 36 | "jsonrpc": "2.0", |
| 37 | "id": request_id, |
| 38 | "error": { |
| 39 | "code": -32601, |
| 40 | "message": f"Method not found: {method}" |
| 41 | } |
| 42 | }) |
| 43 | |
| 44 | # 成功响应 |
| 45 | return JsonResponse({ |
| 46 | "jsonrpc": "2.0", |
| 47 | "id": request_id, |
| 48 | "result": result |
| 49 | }) |
| 50 | |
| 51 | except Exception as e: |
| 52 | return JsonResponse({ |
| 53 | "jsonrpc": "2.0", |
| 54 | "id": request_id, |
| 55 | "error": { |
| 56 | "code": -32603, |
| 57 | "message": f"Internal error: {str(e)}" |
| 58 | } |
| 59 | }) |
nothing calls this directly
no test coverage detected