The main MCP Server class. This class orchestrates all the major components of the application, including: - Database connection management (`DatabaseManager` or `FalkorDBManager`) - Background job tracking (`JobManager`) - File system watching for live updates (`CodeWatche
| 115 | |
| 116 | |
| 117 | class MCPServer: |
| 118 | """ |
| 119 | The main MCP Server class. |
| 120 | |
| 121 | This class orchestrates all the major components of the application, including: |
| 122 | - Database connection management (`DatabaseManager` or `FalkorDBManager`) |
| 123 | - Background job tracking (`JobManager`) |
| 124 | - File system watching for live updates (`CodeWatcher`) |
| 125 | - Tool handlers for graph building, code searching, etc. |
| 126 | - The main JSON-RPC communication loop for interacting with an AI assistant. |
| 127 | """ |
| 128 | |
| 129 | def __init__(self, loop=None, cwd: Path | None = None): |
| 130 | """ |
| 131 | Initializes the MCP server and its components. |
| 132 | |
| 133 | Args: |
| 134 | loop: The asyncio event loop to use. If not provided, it gets the current |
| 135 | running loop or creates a new one. |
| 136 | cwd: Working directory used for context resolution. Defaults to Path.cwd(). |
| 137 | """ |
| 138 | self.cwd = (cwd or Path.cwd()).resolve() |
| 139 | self.discovered_child_contexts: List[dict] = [] |
| 140 | self._context_note_pending = False |
| 141 | self.disabled_tools: Set[str] = set() |
| 142 | |
| 143 | try: |
| 144 | ctx = resolve_context(cwd=self.cwd) |
| 145 | self.resolved_context = ctx |
| 146 | |
| 147 | if ctx.database: |
| 148 | os.environ['CGC_RUNTIME_DB_TYPE'] = ctx.database |
| 149 | |
| 150 | self.db_manager = get_database_manager(db_path=ctx.db_path) |
| 151 | self.db_manager.get_driver() |
| 152 | |
| 153 | if not ctx.is_local: |
| 154 | try: |
| 155 | children = discover_child_contexts(self.cwd, max_depth=1) |
| 156 | if children: |
| 157 | self.discovered_child_contexts = [asdict(c) for c in children] |
| 158 | self._context_note_pending = True |
| 159 | except Exception: |
| 160 | pass |
| 161 | except ValueError as e: |
| 162 | raise ValueError(f"Database configuration error: {e}") |
| 163 | |
| 164 | # Initialize managers for jobs and file watching. |
| 165 | self.job_manager = JobManager() |
| 166 | |
| 167 | # Get the current event loop to pass to thread-sensitive components like the graph builder. |
| 168 | if loop is None: |
| 169 | try: |
| 170 | loop = asyncio.get_running_loop() |
| 171 | except RuntimeError: |
| 172 | loop = asyncio.new_event_loop() |
| 173 | asyncio.set_event_loop(loop) |
| 174 | self.loop = loop |
no outgoing calls