mcp-vector-search 0.12.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. mcp_vector_search/__init__.py +10 -0
  2. mcp_vector_search/cli/__init__.py +1 -0
  3. mcp_vector_search/cli/commands/__init__.py +1 -0
  4. mcp_vector_search/cli/commands/auto_index.py +397 -0
  5. mcp_vector_search/cli/commands/config.py +393 -0
  6. mcp_vector_search/cli/commands/demo.py +358 -0
  7. mcp_vector_search/cli/commands/index.py +744 -0
  8. mcp_vector_search/cli/commands/init.py +645 -0
  9. mcp_vector_search/cli/commands/install.py +675 -0
  10. mcp_vector_search/cli/commands/install_old.py +696 -0
  11. mcp_vector_search/cli/commands/mcp.py +1182 -0
  12. mcp_vector_search/cli/commands/reset.py +393 -0
  13. mcp_vector_search/cli/commands/search.py +773 -0
  14. mcp_vector_search/cli/commands/status.py +549 -0
  15. mcp_vector_search/cli/commands/uninstall.py +485 -0
  16. mcp_vector_search/cli/commands/visualize.py +1467 -0
  17. mcp_vector_search/cli/commands/watch.py +287 -0
  18. mcp_vector_search/cli/didyoumean.py +500 -0
  19. mcp_vector_search/cli/export.py +320 -0
  20. mcp_vector_search/cli/history.py +295 -0
  21. mcp_vector_search/cli/interactive.py +342 -0
  22. mcp_vector_search/cli/main.py +461 -0
  23. mcp_vector_search/cli/output.py +412 -0
  24. mcp_vector_search/cli/suggestions.py +375 -0
  25. mcp_vector_search/config/__init__.py +1 -0
  26. mcp_vector_search/config/constants.py +24 -0
  27. mcp_vector_search/config/defaults.py +200 -0
  28. mcp_vector_search/config/settings.py +134 -0
  29. mcp_vector_search/core/__init__.py +1 -0
  30. mcp_vector_search/core/auto_indexer.py +298 -0
  31. mcp_vector_search/core/connection_pool.py +360 -0
  32. mcp_vector_search/core/database.py +1214 -0
  33. mcp_vector_search/core/directory_index.py +318 -0
  34. mcp_vector_search/core/embeddings.py +294 -0
  35. mcp_vector_search/core/exceptions.py +89 -0
  36. mcp_vector_search/core/factory.py +318 -0
  37. mcp_vector_search/core/git_hooks.py +345 -0
  38. mcp_vector_search/core/indexer.py +1002 -0
  39. mcp_vector_search/core/models.py +294 -0
  40. mcp_vector_search/core/project.py +333 -0
  41. mcp_vector_search/core/scheduler.py +330 -0
  42. mcp_vector_search/core/search.py +952 -0
  43. mcp_vector_search/core/watcher.py +322 -0
  44. mcp_vector_search/mcp/__init__.py +5 -0
  45. mcp_vector_search/mcp/__main__.py +25 -0
  46. mcp_vector_search/mcp/server.py +733 -0
  47. mcp_vector_search/parsers/__init__.py +8 -0
  48. mcp_vector_search/parsers/base.py +296 -0
  49. mcp_vector_search/parsers/dart.py +605 -0
  50. mcp_vector_search/parsers/html.py +413 -0
  51. mcp_vector_search/parsers/javascript.py +643 -0
  52. mcp_vector_search/parsers/php.py +694 -0
  53. mcp_vector_search/parsers/python.py +502 -0
  54. mcp_vector_search/parsers/registry.py +223 -0
  55. mcp_vector_search/parsers/ruby.py +678 -0
  56. mcp_vector_search/parsers/text.py +186 -0
  57. mcp_vector_search/parsers/utils.py +265 -0
  58. mcp_vector_search/py.typed +1 -0
  59. mcp_vector_search/utils/__init__.py +40 -0
  60. mcp_vector_search/utils/gitignore.py +250 -0
  61. mcp_vector_search/utils/monorepo.py +277 -0
  62. mcp_vector_search/utils/timing.py +334 -0
  63. mcp_vector_search/utils/version.py +47 -0
  64. mcp_vector_search-0.12.6.dist-info/METADATA +754 -0
  65. mcp_vector_search-0.12.6.dist-info/RECORD +68 -0
  66. mcp_vector_search-0.12.6.dist-info/WHEEL +4 -0
  67. mcp_vector_search-0.12.6.dist-info/entry_points.txt +2 -0
  68. mcp_vector_search-0.12.6.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,733 @@
1
+ """MCP server implementation for MCP Vector Search."""
2
+
3
+ import asyncio
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from loguru import logger
10
+ from mcp.server import Server
11
+ from mcp.server.models import InitializationOptions
12
+ from mcp.server.stdio import stdio_server
13
+ from mcp.types import (
14
+ CallToolRequest,
15
+ CallToolResult,
16
+ ServerCapabilities,
17
+ TextContent,
18
+ Tool,
19
+ )
20
+
21
+ from ..core.database import ChromaVectorDatabase
22
+ from ..core.embeddings import create_embedding_function
23
+ from ..core.exceptions import ProjectNotFoundError
24
+ from ..core.indexer import SemanticIndexer
25
+ from ..core.project import ProjectManager
26
+ from ..core.search import SemanticSearchEngine
27
+ from ..core.watcher import FileWatcher
28
+
29
+
30
+ class MCPVectorSearchServer:
31
+ """MCP server for vector search functionality."""
32
+
33
+ def __init__(
34
+ self,
35
+ project_root: Path | None = None,
36
+ enable_file_watching: bool | None = None,
37
+ ):
38
+ """Initialize the MCP server.
39
+
40
+ Args:
41
+ project_root: Project root directory. If None, will auto-detect.
42
+ enable_file_watching: Enable file watching for automatic reindexing.
43
+ If None, checks MCP_ENABLE_FILE_WATCHING env var (default: True).
44
+ """
45
+ self.project_root = project_root or Path.cwd()
46
+ self.project_manager = ProjectManager(self.project_root)
47
+ self.search_engine: SemanticSearchEngine | None = None
48
+ self.file_watcher: FileWatcher | None = None
49
+ self.indexer: SemanticIndexer | None = None
50
+ self.database: ChromaVectorDatabase | None = None
51
+ self._initialized = False
52
+
53
+ # Determine if file watching should be enabled
54
+ if enable_file_watching is None:
55
+ # Check environment variable, default to True
56
+ env_value = os.getenv("MCP_ENABLE_FILE_WATCHING", "true").lower()
57
+ self.enable_file_watching = env_value in ("true", "1", "yes", "on")
58
+ else:
59
+ self.enable_file_watching = enable_file_watching
60
+
61
+ async def initialize(self) -> None:
62
+ """Initialize the search engine and database."""
63
+ if self._initialized:
64
+ return
65
+
66
+ try:
67
+ # Load project configuration
68
+ config = self.project_manager.load_config()
69
+
70
+ # Setup embedding function
71
+ embedding_function, _ = create_embedding_function(
72
+ model_name=config.embedding_model
73
+ )
74
+
75
+ # Setup database
76
+ self.database = ChromaVectorDatabase(
77
+ persist_directory=config.index_path,
78
+ embedding_function=embedding_function,
79
+ )
80
+
81
+ # Initialize database
82
+ await self.database.__aenter__()
83
+
84
+ # Setup search engine
85
+ self.search_engine = SemanticSearchEngine(
86
+ database=self.database, project_root=self.project_root
87
+ )
88
+
89
+ # Setup indexer for file watching
90
+ if self.enable_file_watching:
91
+ self.indexer = SemanticIndexer(
92
+ database=self.database,
93
+ project_root=self.project_root,
94
+ config=config,
95
+ )
96
+
97
+ # Setup file watcher
98
+ self.file_watcher = FileWatcher(
99
+ project_root=self.project_root,
100
+ config=config,
101
+ indexer=self.indexer,
102
+ database=self.database,
103
+ )
104
+
105
+ # Start file watching
106
+ await self.file_watcher.start()
107
+ logger.info("File watching enabled for automatic reindexing")
108
+ else:
109
+ logger.info("File watching disabled")
110
+
111
+ self._initialized = True
112
+ logger.info(f"MCP server initialized for project: {self.project_root}")
113
+
114
+ except ProjectNotFoundError:
115
+ logger.error(f"Project not initialized at {self.project_root}")
116
+ raise
117
+ except Exception as e:
118
+ logger.error(f"Failed to initialize MCP server: {e}")
119
+ raise
120
+
121
+ async def cleanup(self) -> None:
122
+ """Cleanup resources."""
123
+ # Stop file watcher if running
124
+ if self.file_watcher and self.file_watcher.is_running:
125
+ logger.info("Stopping file watcher...")
126
+ await self.file_watcher.stop()
127
+ self.file_watcher = None
128
+
129
+ # Cleanup database connection
130
+ if self.database and hasattr(self.database, "__aexit__"):
131
+ await self.database.__aexit__(None, None, None)
132
+ self.database = None
133
+
134
+ # Clear references
135
+ self.search_engine = None
136
+ self.indexer = None
137
+ self._initialized = False
138
+ logger.info("MCP server cleanup completed")
139
+
140
+ def get_tools(self) -> list[Tool]:
141
+ """Get available MCP tools."""
142
+ tools = [
143
+ Tool(
144
+ name="search_code",
145
+ description="Search for code using semantic similarity",
146
+ inputSchema={
147
+ "type": "object",
148
+ "properties": {
149
+ "query": {
150
+ "type": "string",
151
+ "description": "The search query to find relevant code",
152
+ },
153
+ "limit": {
154
+ "type": "integer",
155
+ "description": "Maximum number of results to return",
156
+ "default": 10,
157
+ "minimum": 1,
158
+ "maximum": 50,
159
+ },
160
+ "similarity_threshold": {
161
+ "type": "number",
162
+ "description": "Minimum similarity threshold (0.0-1.0)",
163
+ "default": 0.3,
164
+ "minimum": 0.0,
165
+ "maximum": 1.0,
166
+ },
167
+ "file_extensions": {
168
+ "type": "array",
169
+ "items": {"type": "string"},
170
+ "description": "Filter by file extensions (e.g., ['.py', '.js'])",
171
+ },
172
+ "language": {
173
+ "type": "string",
174
+ "description": "Filter by programming language",
175
+ },
176
+ "function_name": {
177
+ "type": "string",
178
+ "description": "Filter by function name",
179
+ },
180
+ "class_name": {
181
+ "type": "string",
182
+ "description": "Filter by class name",
183
+ },
184
+ "files": {
185
+ "type": "string",
186
+ "description": "Filter by file patterns (e.g., '*.py' or 'src/*.js')",
187
+ },
188
+ },
189
+ "required": ["query"],
190
+ },
191
+ ),
192
+ Tool(
193
+ name="search_similar",
194
+ description="Find code similar to a specific file or function",
195
+ inputSchema={
196
+ "type": "object",
197
+ "properties": {
198
+ "file_path": {
199
+ "type": "string",
200
+ "description": "Path to the file to find similar code for",
201
+ },
202
+ "function_name": {
203
+ "type": "string",
204
+ "description": "Optional function name within the file",
205
+ },
206
+ "limit": {
207
+ "type": "integer",
208
+ "description": "Maximum number of results to return",
209
+ "default": 10,
210
+ "minimum": 1,
211
+ "maximum": 50,
212
+ },
213
+ "similarity_threshold": {
214
+ "type": "number",
215
+ "description": "Minimum similarity threshold (0.0-1.0)",
216
+ "default": 0.3,
217
+ "minimum": 0.0,
218
+ "maximum": 1.0,
219
+ },
220
+ },
221
+ "required": ["file_path"],
222
+ },
223
+ ),
224
+ Tool(
225
+ name="search_context",
226
+ description="Search for code based on contextual description",
227
+ inputSchema={
228
+ "type": "object",
229
+ "properties": {
230
+ "description": {
231
+ "type": "string",
232
+ "description": "Contextual description of what you're looking for",
233
+ },
234
+ "focus_areas": {
235
+ "type": "array",
236
+ "items": {"type": "string"},
237
+ "description": "Areas to focus on (e.g., ['security', 'authentication'])",
238
+ },
239
+ "limit": {
240
+ "type": "integer",
241
+ "description": "Maximum number of results to return",
242
+ "default": 10,
243
+ "minimum": 1,
244
+ "maximum": 50,
245
+ },
246
+ },
247
+ "required": ["description"],
248
+ },
249
+ ),
250
+ Tool(
251
+ name="get_project_status",
252
+ description="Get project indexing status and statistics",
253
+ inputSchema={"type": "object", "properties": {}, "required": []},
254
+ ),
255
+ Tool(
256
+ name="index_project",
257
+ description="Index or reindex the project codebase",
258
+ inputSchema={
259
+ "type": "object",
260
+ "properties": {
261
+ "force": {
262
+ "type": "boolean",
263
+ "description": "Force reindexing even if index exists",
264
+ "default": False,
265
+ },
266
+ "file_extensions": {
267
+ "type": "array",
268
+ "items": {"type": "string"},
269
+ "description": "File extensions to index (e.g., ['.py', '.js'])",
270
+ },
271
+ },
272
+ "required": [],
273
+ },
274
+ ),
275
+ ]
276
+
277
+ return tools
278
+
279
+ def get_capabilities(self) -> ServerCapabilities:
280
+ """Get server capabilities."""
281
+ return ServerCapabilities(tools={"listChanged": True}, logging={})
282
+
283
+ async def call_tool(self, request: CallToolRequest) -> CallToolResult:
284
+ """Handle tool calls."""
285
+ if not self._initialized:
286
+ await self.initialize()
287
+
288
+ try:
289
+ if request.params.name == "search_code":
290
+ return await self._search_code(request.params.arguments)
291
+ elif request.params.name == "search_similar":
292
+ return await self._search_similar(request.params.arguments)
293
+ elif request.params.name == "search_context":
294
+ return await self._search_context(request.params.arguments)
295
+ elif request.params.name == "get_project_status":
296
+ return await self._get_project_status(request.params.arguments)
297
+ elif request.params.name == "index_project":
298
+ return await self._index_project(request.params.arguments)
299
+ else:
300
+ return CallToolResult(
301
+ content=[
302
+ TextContent(
303
+ type="text", text=f"Unknown tool: {request.params.name}"
304
+ )
305
+ ],
306
+ isError=True,
307
+ )
308
+ except Exception as e:
309
+ logger.error(f"Tool call failed: {e}")
310
+ return CallToolResult(
311
+ content=[
312
+ TextContent(type="text", text=f"Tool execution failed: {str(e)}")
313
+ ],
314
+ isError=True,
315
+ )
316
+
317
+ async def _search_code(self, args: dict[str, Any]) -> CallToolResult:
318
+ """Handle search_code tool call."""
319
+ query = args.get("query", "")
320
+ limit = args.get("limit", 10)
321
+ similarity_threshold = args.get("similarity_threshold", 0.3)
322
+ file_extensions = args.get("file_extensions")
323
+ language = args.get("language")
324
+ function_name = args.get("function_name")
325
+ class_name = args.get("class_name")
326
+ files = args.get("files")
327
+
328
+ if not query:
329
+ return CallToolResult(
330
+ content=[TextContent(type="text", text="Query parameter is required")],
331
+ isError=True,
332
+ )
333
+
334
+ # Build filters
335
+ filters = {}
336
+ if file_extensions:
337
+ filters["file_extension"] = {"$in": file_extensions}
338
+ if language:
339
+ filters["language"] = language
340
+ if function_name:
341
+ filters["function_name"] = function_name
342
+ if class_name:
343
+ filters["class_name"] = class_name
344
+ if files:
345
+ # Convert file pattern to filter (simplified)
346
+ filters["file_pattern"] = files
347
+
348
+ # Perform search
349
+ results = await self.search_engine.search(
350
+ query=query,
351
+ limit=limit,
352
+ similarity_threshold=similarity_threshold,
353
+ filters=filters,
354
+ )
355
+
356
+ # Format results
357
+ if not results:
358
+ response_text = f"No results found for query: '{query}'"
359
+ else:
360
+ response_lines = [f"Found {len(results)} results for query: '{query}'\n"]
361
+
362
+ for i, result in enumerate(results, 1):
363
+ response_lines.append(
364
+ f"## Result {i} (Score: {result.similarity_score:.3f})"
365
+ )
366
+ response_lines.append(f"**File:** {result.file_path}")
367
+ if result.function_name:
368
+ response_lines.append(f"**Function:** {result.function_name}")
369
+ if result.class_name:
370
+ response_lines.append(f"**Class:** {result.class_name}")
371
+ response_lines.append(
372
+ f"**Lines:** {result.start_line}-{result.end_line}"
373
+ )
374
+ response_lines.append("**Code:**")
375
+ response_lines.append("```" + (result.language or ""))
376
+ response_lines.append(result.content)
377
+ response_lines.append("```\n")
378
+
379
+ response_text = "\n".join(response_lines)
380
+
381
+ return CallToolResult(content=[TextContent(type="text", text=response_text)])
382
+
383
+ async def _get_project_status(self, args: dict[str, Any]) -> CallToolResult:
384
+ """Handle get_project_status tool call."""
385
+ try:
386
+ config = self.project_manager.load_config()
387
+
388
+ # Get database stats
389
+ if self.search_engine:
390
+ stats = await self.search_engine.database.get_stats()
391
+
392
+ status_info = {
393
+ "project_root": str(config.project_root),
394
+ "index_path": str(config.index_path),
395
+ "file_extensions": config.file_extensions,
396
+ "embedding_model": config.embedding_model,
397
+ "languages": config.languages,
398
+ "total_chunks": stats.total_chunks,
399
+ "total_files": stats.total_files,
400
+ "index_size": f"{stats.index_size_mb:.2f} MB"
401
+ if hasattr(stats, "index_size_mb")
402
+ else "Unknown",
403
+ }
404
+ else:
405
+ status_info = {
406
+ "project_root": str(config.project_root),
407
+ "index_path": str(config.index_path),
408
+ "file_extensions": config.file_extensions,
409
+ "embedding_model": config.embedding_model,
410
+ "languages": config.languages,
411
+ "status": "Not indexed",
412
+ }
413
+
414
+ response_text = "# Project Status\n\n"
415
+ response_text += f"**Project Root:** {status_info['project_root']}\n"
416
+ response_text += f"**Index Path:** {status_info['index_path']}\n"
417
+ response_text += (
418
+ f"**File Extensions:** {', '.join(status_info['file_extensions'])}\n"
419
+ )
420
+ response_text += f"**Embedding Model:** {status_info['embedding_model']}\n"
421
+ response_text += f"**Languages:** {', '.join(status_info['languages'])}\n"
422
+
423
+ if "total_chunks" in status_info:
424
+ response_text += f"**Total Chunks:** {status_info['total_chunks']}\n"
425
+ response_text += f"**Total Files:** {status_info['total_files']}\n"
426
+ response_text += f"**Index Size:** {status_info['index_size']}\n"
427
+ else:
428
+ response_text += f"**Status:** {status_info['status']}\n"
429
+
430
+ return CallToolResult(
431
+ content=[TextContent(type="text", text=response_text)]
432
+ )
433
+
434
+ except ProjectNotFoundError:
435
+ return CallToolResult(
436
+ content=[
437
+ TextContent(
438
+ type="text",
439
+ text=f"Project not initialized at {self.project_root}. Run 'mcp-vector-search init' first.",
440
+ )
441
+ ],
442
+ isError=True,
443
+ )
444
+
445
+ async def _index_project(self, args: dict[str, Any]) -> CallToolResult:
446
+ """Handle index_project tool call."""
447
+ force = args.get("force", False)
448
+ file_extensions = args.get("file_extensions")
449
+
450
+ try:
451
+ # Import indexing functionality
452
+ from ..cli.commands.index import run_indexing
453
+
454
+ # Run indexing
455
+ await run_indexing(
456
+ project_root=self.project_root,
457
+ force_reindex=force,
458
+ extensions=file_extensions,
459
+ show_progress=False, # Disable progress for MCP
460
+ )
461
+
462
+ # Reinitialize search engine after indexing
463
+ await self.cleanup()
464
+ await self.initialize()
465
+
466
+ return CallToolResult(
467
+ content=[
468
+ TextContent(
469
+ type="text", text="Project indexing completed successfully!"
470
+ )
471
+ ]
472
+ )
473
+
474
+ except Exception as e:
475
+ return CallToolResult(
476
+ content=[TextContent(type="text", text=f"Indexing failed: {str(e)}")],
477
+ isError=True,
478
+ )
479
+
480
+ async def _search_similar(self, args: dict[str, Any]) -> CallToolResult:
481
+ """Handle search_similar tool call."""
482
+ file_path = args.get("file_path", "")
483
+ function_name = args.get("function_name")
484
+ limit = args.get("limit", 10)
485
+ similarity_threshold = args.get("similarity_threshold", 0.3)
486
+
487
+ if not file_path:
488
+ return CallToolResult(
489
+ content=[
490
+ TextContent(type="text", text="file_path parameter is required")
491
+ ],
492
+ isError=True,
493
+ )
494
+
495
+ try:
496
+ from pathlib import Path
497
+
498
+ # Convert to Path object
499
+ file_path_obj = Path(file_path)
500
+ if not file_path_obj.is_absolute():
501
+ file_path_obj = self.project_root / file_path_obj
502
+
503
+ if not file_path_obj.exists():
504
+ return CallToolResult(
505
+ content=[
506
+ TextContent(type="text", text=f"File not found: {file_path}")
507
+ ],
508
+ isError=True,
509
+ )
510
+
511
+ # Run similar search
512
+ results = await self.search_engine.search_similar(
513
+ file_path=file_path_obj,
514
+ function_name=function_name,
515
+ limit=limit,
516
+ similarity_threshold=similarity_threshold,
517
+ )
518
+
519
+ # Format results
520
+ if not results:
521
+ return CallToolResult(
522
+ content=[
523
+ TextContent(
524
+ type="text", text=f"No similar code found for {file_path}"
525
+ )
526
+ ]
527
+ )
528
+
529
+ response_lines = [
530
+ f"Found {len(results)} similar code snippets for {file_path}\n"
531
+ ]
532
+
533
+ for i, result in enumerate(results, 1):
534
+ response_lines.append(
535
+ f"## Result {i} (Score: {result.similarity_score:.3f})"
536
+ )
537
+ response_lines.append(f"**File:** {result.file_path}")
538
+ if result.function_name:
539
+ response_lines.append(f"**Function:** {result.function_name}")
540
+ if result.class_name:
541
+ response_lines.append(f"**Class:** {result.class_name}")
542
+ response_lines.append(
543
+ f"**Lines:** {result.start_line}-{result.end_line}"
544
+ )
545
+ response_lines.append("**Code:**")
546
+ response_lines.append("```" + (result.language or ""))
547
+ # Show more of the content for similar search
548
+ content_preview = (
549
+ result.content[:500]
550
+ if len(result.content) > 500
551
+ else result.content
552
+ )
553
+ response_lines.append(
554
+ content_preview + ("..." if len(result.content) > 500 else "")
555
+ )
556
+ response_lines.append("```\n")
557
+
558
+ result_text = "\n".join(response_lines)
559
+
560
+ return CallToolResult(content=[TextContent(type="text", text=result_text)])
561
+
562
+ except Exception as e:
563
+ return CallToolResult(
564
+ content=[
565
+ TextContent(type="text", text=f"Similar search failed: {str(e)}")
566
+ ],
567
+ isError=True,
568
+ )
569
+
570
+ async def _search_context(self, args: dict[str, Any]) -> CallToolResult:
571
+ """Handle search_context tool call."""
572
+ description = args.get("description", "")
573
+ focus_areas = args.get("focus_areas")
574
+ limit = args.get("limit", 10)
575
+
576
+ if not description:
577
+ return CallToolResult(
578
+ content=[
579
+ TextContent(type="text", text="description parameter is required")
580
+ ],
581
+ isError=True,
582
+ )
583
+
584
+ try:
585
+ # Perform context search
586
+ results = await self.search_engine.search_by_context(
587
+ context_description=description, focus_areas=focus_areas, limit=limit
588
+ )
589
+
590
+ # Format results
591
+ if not results:
592
+ return CallToolResult(
593
+ content=[
594
+ TextContent(
595
+ type="text",
596
+ text=f"No contextually relevant code found for: {description}",
597
+ )
598
+ ]
599
+ )
600
+
601
+ response_lines = [
602
+ f"Found {len(results)} contextually relevant code snippets"
603
+ ]
604
+ if focus_areas:
605
+ response_lines[0] += f" (focus: {', '.join(focus_areas)})"
606
+ response_lines[0] += f" for: {description}\n"
607
+
608
+ for i, result in enumerate(results, 1):
609
+ response_lines.append(
610
+ f"## Result {i} (Score: {result.similarity_score:.3f})"
611
+ )
612
+ response_lines.append(f"**File:** {result.file_path}")
613
+ if result.function_name:
614
+ response_lines.append(f"**Function:** {result.function_name}")
615
+ if result.class_name:
616
+ response_lines.append(f"**Class:** {result.class_name}")
617
+ response_lines.append(
618
+ f"**Lines:** {result.start_line}-{result.end_line}"
619
+ )
620
+ response_lines.append("**Code:**")
621
+ response_lines.append("```" + (result.language or ""))
622
+ # Show more of the content for context search
623
+ content_preview = (
624
+ result.content[:500]
625
+ if len(result.content) > 500
626
+ else result.content
627
+ )
628
+ response_lines.append(
629
+ content_preview + ("..." if len(result.content) > 500 else "")
630
+ )
631
+ response_lines.append("```\n")
632
+
633
+ result_text = "\n".join(response_lines)
634
+
635
+ return CallToolResult(content=[TextContent(type="text", text=result_text)])
636
+
637
+ except Exception as e:
638
+ return CallToolResult(
639
+ content=[
640
+ TextContent(type="text", text=f"Context search failed: {str(e)}")
641
+ ],
642
+ isError=True,
643
+ )
644
+
645
+
646
+ def create_mcp_server(
647
+ project_root: Path | None = None, enable_file_watching: bool | None = None
648
+ ) -> Server:
649
+ """Create and configure the MCP server.
650
+
651
+ Args:
652
+ project_root: Project root directory. If None, will auto-detect.
653
+ enable_file_watching: Enable file watching for automatic reindexing.
654
+ If None, checks MCP_ENABLE_FILE_WATCHING env var (default: True).
655
+ """
656
+ server = Server("mcp-vector-search")
657
+ mcp_server = MCPVectorSearchServer(project_root, enable_file_watching)
658
+
659
+ @server.list_tools()
660
+ async def handle_list_tools() -> list[Tool]:
661
+ """List available tools."""
662
+ return mcp_server.get_tools()
663
+
664
+ @server.call_tool()
665
+ async def handle_call_tool(name: str, arguments: dict | None):
666
+ """Handle tool calls."""
667
+ # Create a mock request object for compatibility
668
+ from types import SimpleNamespace
669
+
670
+ mock_request = SimpleNamespace()
671
+ mock_request.params = SimpleNamespace()
672
+ mock_request.params.name = name
673
+ mock_request.params.arguments = arguments or {}
674
+
675
+ result = await mcp_server.call_tool(mock_request)
676
+
677
+ # Return the content from the result
678
+ return result.content
679
+
680
+ # Store reference for cleanup
681
+ server._mcp_server = mcp_server
682
+
683
+ return server
684
+
685
+
686
+ async def run_mcp_server(
687
+ project_root: Path | None = None, enable_file_watching: bool | None = None
688
+ ) -> None:
689
+ """Run the MCP server using stdio transport.
690
+
691
+ Args:
692
+ project_root: Project root directory. If None, will auto-detect.
693
+ enable_file_watching: Enable file watching for automatic reindexing.
694
+ If None, checks MCP_ENABLE_FILE_WATCHING env var (default: True).
695
+ """
696
+ server = create_mcp_server(project_root, enable_file_watching)
697
+
698
+ # Create initialization options with proper capabilities
699
+ init_options = InitializationOptions(
700
+ server_name="mcp-vector-search",
701
+ server_version="0.4.0",
702
+ capabilities=ServerCapabilities(tools={"listChanged": True}, logging={}),
703
+ )
704
+
705
+ try:
706
+ async with stdio_server() as (read_stream, write_stream):
707
+ await server.run(read_stream, write_stream, init_options)
708
+ except KeyboardInterrupt:
709
+ logger.info("Received interrupt signal, shutting down...")
710
+ except Exception as e:
711
+ logger.error(f"MCP server error: {e}")
712
+ raise
713
+ finally:
714
+ # Cleanup
715
+ if hasattr(server, "_mcp_server"):
716
+ logger.info("Performing server cleanup...")
717
+ await server._mcp_server.cleanup()
718
+
719
+
720
+ if __name__ == "__main__":
721
+ # Allow specifying project root as command line argument
722
+ project_root = Path(sys.argv[1]) if len(sys.argv) > 1 else None
723
+
724
+ # Check for file watching flag in command line args
725
+ enable_file_watching = None
726
+ if "--no-watch" in sys.argv:
727
+ enable_file_watching = False
728
+ sys.argv.remove("--no-watch")
729
+ elif "--watch" in sys.argv:
730
+ enable_file_watching = True
731
+ sys.argv.remove("--watch")
732
+
733
+ asyncio.run(run_mcp_server(project_root, enable_file_watching))