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

Method handle_tools_call

AskAgent/python/webserver/mcp_wrapper.py:291–516  ·  view source on GitHub ↗

Handle tools/call request

(self, params, query_params)

Source from the content-addressed store, hash-verified

289 await send_chunk(json.dumps(error_response).encode('utf-8'), end_response=True)
290
291 async def handle_tools_call(self, params, query_params):
292 """Handle tools/call request"""
293 tool_name = params.get("name")
294 arguments = params.get("arguments", {})
295
296 logger.info(f"MCP tool call: {tool_name} with args: {arguments}")
297 # Commented out verbose logging
298 # print(f"=== TOOL CALL: {tool_name} ===")
299 # print(f"Arguments: {json.dumps(arguments, indent=2)}")
300
301 if tool_name == "ask":
302 # Handle the main query tool
303 query = arguments.get("query", "")
304 # print(f"=== PROCESSING ASK TOOL ===")
305 # print(f"Query: {query}")
306 sites = arguments.get("site", [])
307 generate_mode = arguments.get("generate_mode", "list")
308
309 # Update query params with MCP arguments
310 # Make sure to format values as lists (like URL parameters)
311 query_params["query"] = [query] if query else []
312 if sites:
313 query_params["site"] = sites if isinstance(sites, list) else [sites]
314 query_params["generate_mode"] = [generate_mode] if generate_mode else ["list"]
315 # print(f"=== QUERY PARAMS BEING PASSED ===")
316 # print(f"query_params: {query_params}")
317
318 # Create a response accumulator
319 response_content = []
320
321 # Create a wrapper class that provides write_stream method
322 class ChunkCapture:
323 async def write_stream(self, data, end_response=False):
324 # Convert data to string
325 if isinstance(data, dict):
326 chunk = json.dumps(data)
327 elif isinstance(data, bytes):
328 chunk = data.decode('utf-8')
329 else:
330 chunk = str(data)
331 response_content.append(chunk)
332
333 capture_chunk = ChunkCapture()
334
335 # Process the query using NLWebHandler with a timeout
336 # print(f"=== CREATING NLWebHandler ===")
337 # print(f"Query params: {query_params}")
338 handler = NLWebHandler(query_params, capture_chunk)
339 try:
340 # print(f"=== CALLING handler.runQuery() ===")
341 # Add a 30-second timeout for MCP requests
342 result = await asyncio.wait_for(handler.runQuery(), timeout=30.0)
343 # print(f"=== HANDLER RETURNED: {result} ===")
344 except asyncio.TimeoutError:
345 logger.warning("MCP tool call timed out after 30 seconds")
346 return {
347 "content": [
348 {

Callers 1

handle_requestMethod · 0.95

Calls 11

runQueryMethod · 0.95
NLWebHandlerClass · 0.90
get_vector_db_clientFunction · 0.90
WhoHandlerClass · 0.90
ChunkCaptureClass · 0.85
getMethod · 0.80
infoMethod · 0.45
warningMethod · 0.45
get_sitesMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected