| 771 | warning_logger(f"Error during server shutdown: {exc}") |
| 772 | |
| 773 | async def _run_loop(self, loop): |
| 774 | request_count = 0 |
| 775 | while True: |
| 776 | try: |
| 777 | if request_count and request_count % 50 == 0: |
| 778 | self.job_manager.cleanup_old_jobs(max_age_hours=24) |
| 779 | # Read a request from the standard input. |
| 780 | line = await loop.run_in_executor(None, sys.stdin.readline) |
| 781 | if not line: |
| 782 | debug_logger("Client disconnected (EOF received). Shutting down.") |
| 783 | break |
| 784 | |
| 785 | request = json.loads(line.strip()) |
| 786 | method = request.get('method') |
| 787 | params = request.get('params', {}) |
| 788 | request_id = request.get('id') |
| 789 | request_count += 1 |
| 790 | |
| 791 | response = {} |
| 792 | # Route the request based on the JSON-RPC method. |
| 793 | if method == 'initialize': |
| 794 | # Build system prompt with custom prompts if any |
| 795 | system_prompt = build_system_prompt() |
| 796 | |
| 797 | response = { |
| 798 | "jsonrpc": "2.0", "id": request_id, |
| 799 | "result": { |
| 800 | "protocolVersion": "2025-03-26", |
| 801 | "serverInfo": { |
| 802 | "name": "CodeGraphContext", |
| 803 | "version": self._get_version(), |
| 804 | "systemPrompt": system_prompt, |
| 805 | "instructionsAvailable": True, |
| 806 | }, |
| 807 | "capabilities": {"tools": {"listTools": True}}, |
| 808 | "instructions": LLM_SYSTEM_PROMPT, |
| 809 | } |
| 810 | } |
| 811 | elif method == 'tools/list': |
| 812 | # Return the list of tools defined in _init_tools. |
| 813 | response = { |
| 814 | "jsonrpc": "2.0", "id": request_id, |
| 815 | "result": {"tools": list(self.tools.values())} |
| 816 | } |
| 817 | elif method == 'tools/call': |
| 818 | # Execute a tool call and return the result. |
| 819 | tool_name = params.get('name') |
| 820 | args = params.get('arguments', {}) |
| 821 | result = await self.handle_tool_call(tool_name, args) |
| 822 | result = _strip_workspace_prefix(result) |
| 823 | |
| 824 | if "error" in result: |
| 825 | response = { |
| 826 | "jsonrpc": "2.0", "id": request_id, |
| 827 | "error": {"code": -32000, "message": "Tool execution error", "data": result} |
| 828 | } |
| 829 | else: |
| 830 | response_text = encode_response(result) |