Runs the main server loop, listening for JSON-RPC requests from stdin.
(self)
| 520 | return {"error": f"Unknown tool: {tool_name}"} |
| 521 | |
| 522 | async def run(self): |
| 523 | """ |
| 524 | Runs the main server loop, listening for JSON-RPC requests from stdin. |
| 525 | """ |
| 526 | # info_logger("MCP Server is running. Waiting for requests...") |
| 527 | print("MCP Server is running. Waiting for requests...", file=sys.stderr, flush=True) |
| 528 | self.code_watcher.start() |
| 529 | |
| 530 | loop = asyncio.get_event_loop() |
| 531 | while True: |
| 532 | try: |
| 533 | # Read a request from the standard input. |
| 534 | line = await loop.run_in_executor(None, sys.stdin.readline) |
| 535 | if not line: |
| 536 | debug_logger("Client disconnected (EOF received). Shutting down.") |
| 537 | break |
| 538 | |
| 539 | request = json.loads(line.strip()) |
| 540 | method = request.get('method') |
| 541 | params = request.get('params', {}) |
| 542 | request_id = request.get('id') |
| 543 | |
| 544 | response = {} |
| 545 | # Route the request based on the JSON-RPC method. |
| 546 | if method == 'initialize': |
| 547 | response = { |
| 548 | "jsonrpc": "2.0", "id": request_id, |
| 549 | "result": { |
| 550 | "protocolVersion": "2025-03-26", |
| 551 | "serverInfo": { |
| 552 | "name": "CodeGraphContext", "version": self._get_version(), |
| 553 | "systemPrompt": LLM_SYSTEM_PROMPT |
| 554 | }, |
| 555 | "capabilities": {"tools": {"listTools": True}}, |
| 556 | } |
| 557 | } |
| 558 | elif method == 'tools/list': |
| 559 | # Return the list of tools defined in _init_tools. |
| 560 | response = { |
| 561 | "jsonrpc": "2.0", "id": request_id, |
| 562 | "result": {"tools": list(self.tools.values())} |
| 563 | } |
| 564 | elif method == 'tools/call': |
| 565 | # Execute a tool call and return the result. |
| 566 | tool_name = params.get('name') |
| 567 | args = params.get('arguments', {}) |
| 568 | result = await self.handle_tool_call(tool_name, args) |
| 569 | result = _strip_workspace_prefix(result) |
| 570 | |
| 571 | if "error" in result: |
| 572 | response = { |
| 573 | "jsonrpc": "2.0", "id": request_id, |
| 574 | "error": {"code": -32000, "message": "Tool execution error", "data": result} |
| 575 | } |
| 576 | else: |
| 577 | response_text = json.dumps(result, indent=2) |
| 578 | response_text = _apply_response_token_limit(tool_name, response_text) |
| 579 | response = { |
no test coverage detected