MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / handle_request

Method handle_request

AskAgent/python/webserver/mcp_wrapper.py:39–131  ·  view source on GitHub ↗

Handle a JSON-RPC 2.0 MCP request Args: request_data: Parsed JSON request data query_params: URL query parameters send_response: Function to send response headers send_chunk: Function to send response body

(self, request_data, query_params, send_response, send_chunk)

Source from the content-addressed store, hash-verified

37 self._tools_cache = None
38
39 async def handle_request(self, request_data, query_params, send_response, send_chunk):
40 """
41 Handle a JSON-RPC 2.0 MCP request
42
43 Args:
44 request_data: Parsed JSON request data
45 query_params: URL query parameters
46 send_response: Function to send response headers
47 send_chunk: Function to send response body
48 """
49 # Extract JSON-RPC fields
50 jsonrpc = request_data.get("jsonrpc", "2.0")
51 method = request_data.get("method")
52 params = request_data.get("params", {})
53 request_id = request_data.get("id")
54
55 # Check if this is a notification (no id field)
56 is_notification = request_id is None
57
58 logger.info(f"MCP request: method={method}, id={request_id}, is_notification={is_notification}")
59 # Commented out verbose logging
60 # print(f"=== MCP REQUEST: method={method}, id={request_id}, initialized={self.initialized}, handler_id={id(self)} ===")
61
62 try:
63 # Route based on method
64 if method == "initialize":
65 result = await self.handle_initialize(params)
66 # print(f"=== INITIALIZE COMPLETE, sending response ===")
67 elif method == "initialized" or method == "notifications/initialized":
68 # This is a notification, no response needed
69 self.initialized = True
70 logger.info("MCP server initialized")
71 # print(f"=== SERVER MARKED AS INITIALIZED ===")
72 if not is_notification:
73 result = {"status": "ok"}
74 else:
75 return # No response for notifications
76 elif method == "tools/list":
77 # Temporarily disable initialization check for debugging
78 # if not self.initialized:
79 # raise Exception("Server not initialized")
80 logger.info(f"tools/list called, initialized={self.initialized}")
81 result = await self.handle_tools_list(params)
82 elif method == "tools/call":
83 # print(f"=== TOOLS/CALL: initialized={self.initialized} ===")
84 # Remove the initialization check - MCP clients might not send initialize first
85 # if not self.initialized:
86 # raise Exception("Server not initialized")
87
88 # Check if this is a streaming request
89 is_streaming = (
90 query_params.get('streaming') == ['true'] and
91 params.get("arguments", {}).get("streaming", False)
92 )
93
94 if is_streaming:
95 # Handle streaming request with SSE
96 await self.handle_streaming_tools_call(params, query_params, send_response, send_chunk)

Callers 1

handle_mcp_requestFunction · 0.80

Calls 9

handle_initializeMethod · 0.95
handle_tools_listMethod · 0.95
handle_tools_callMethod · 0.95
getMethod · 0.80
send_responseFunction · 0.50
send_chunkFunction · 0.50
infoMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected