|
12 | 12 | from chromadb.api.models.AsyncCollection import AsyncCollection |
13 | 13 | from chromadb.errors import InvalidCollectionException |
14 | 14 |
|
| 15 | +from vectorcode.subcommands.vectorise import ( |
| 16 | + VectoriseStats, |
| 17 | + chunked_add, |
| 18 | + exclude_paths_by_spec, |
| 19 | + find_exclude_specs, |
| 20 | +) |
| 21 | + |
15 | 22 | try: # pragma: nocover |
16 | 23 | from mcp import ErrorData, McpError |
17 | 24 | from mcp.server.fastmcp import FastMCP |
|
26 | 33 | Config, |
27 | 34 | cleanup_path, |
28 | 35 | config_logging, |
| 36 | + expand_globs, |
29 | 37 | find_project_config_dir, |
30 | 38 | get_project_config, |
31 | 39 | load_config_file, |
@@ -89,6 +97,69 @@ async def list_collections() -> list[str]: |
89 | 97 | return names |
90 | 98 |
|
91 | 99 |
|
| 100 | +async def vectorise_files(paths: list[str], project_root: str) -> dict[str, int]: |
| 101 | + logger.info( |
| 102 | + f"vectorise tool called with the following args: {paths=}, {project_root=}" |
| 103 | + ) |
| 104 | + project_root = os.path.expanduser(project_root) |
| 105 | + if not os.path.isdir(project_root): |
| 106 | + logger.error(f"Invalid project root: {project_root}") |
| 107 | + raise McpError( |
| 108 | + ErrorData(code=1, message=f"{project_root} is not a valid path.") |
| 109 | + ) |
| 110 | + config = await get_project_config(project_root) |
| 111 | + try: |
| 112 | + client = await get_client(config) |
| 113 | + collection = await get_collection(client, config, True) |
| 114 | + except Exception as e: |
| 115 | + logger.error("Failed to access collection at %s", project_root) |
| 116 | + raise McpError( |
| 117 | + ErrorData( |
| 118 | + code=1, |
| 119 | + message=f"{e.__class__.__name__}: Failed to create the collection at {project_root}.", |
| 120 | + ) |
| 121 | + ) |
| 122 | + if collection is None: |
| 123 | + raise McpError( |
| 124 | + ErrorData( |
| 125 | + code=1, |
| 126 | + message=f"Failed to access the collection at {project_root}. Use `list_collections` tool to get a list of valid paths for this field.", |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + paths = [os.path.expanduser(i) for i in await expand_globs(paths)] |
| 131 | + final_config = await config.merge_from( |
| 132 | + Config(files=[i for i in paths if os.path.isfile(i)], project_root=project_root) |
| 133 | + ) |
| 134 | + for ignore_spec in find_exclude_specs(final_config): |
| 135 | + if os.path.isfile(ignore_spec): |
| 136 | + logger.info(f"Loading ignore specs from {ignore_spec}.") |
| 137 | + paths = exclude_paths_by_spec((str(i) for i in paths), ignore_spec) |
| 138 | + stats = VectoriseStats() |
| 139 | + collection_lock = asyncio.Lock() |
| 140 | + stats_lock = asyncio.Lock() |
| 141 | + max_batch_size = await client.get_max_batch_size() |
| 142 | + semaphore = asyncio.Semaphore(os.cpu_count() or 1) |
| 143 | + tasks = [ |
| 144 | + asyncio.create_task( |
| 145 | + chunked_add( |
| 146 | + str(file), |
| 147 | + collection, |
| 148 | + collection_lock, |
| 149 | + stats, |
| 150 | + stats_lock, |
| 151 | + final_config, |
| 152 | + max_batch_size, |
| 153 | + semaphore, |
| 154 | + ) |
| 155 | + ) |
| 156 | + for file in paths |
| 157 | + ] |
| 158 | + for i, task in enumerate(asyncio.as_completed(tasks), start=1): |
| 159 | + await task |
| 160 | + return stats.to_dict() |
| 161 | + |
| 162 | + |
92 | 163 | async def query_tool( |
93 | 164 | n_query: int, query_messages: list[str], project_root: str |
94 | 165 | ) -> list[str]: |
@@ -199,6 +270,14 @@ async def mcp_server(): |
199 | 270 | ), |
200 | 271 | ) |
201 | 272 |
|
| 273 | + mcp.add_tool( |
| 274 | + fn=vectorise_files, |
| 275 | + name="vectorise", |
| 276 | + description="\n".join( |
| 277 | + prompt_by_categories["vectorise"] + prompt_by_categories["general"] |
| 278 | + ), |
| 279 | + ) |
| 280 | + |
202 | 281 | return mcp |
203 | 282 |
|
204 | 283 |
|
|
0 commit comments