-
-
Notifications
You must be signed in to change notification settings - Fork 273
feat: mcp server semantic search and update repository tools #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,10 @@ | |||||
| from codebase_rag.services.graph_service import MemgraphIngestor | ||||||
| from codebase_rag.services.llm import CypherGenerator | ||||||
| from codebase_rag.tools import tool_descriptions as td | ||||||
| from codebase_rag.tools.code_retrieval import CodeRetriever, create_code_retrieval_tool | ||||||
| from codebase_rag.tools.code_retrieval import ( | ||||||
| CodeRetriever, | ||||||
| create_code_retrieval_tool, | ||||||
| ) | ||||||
| from codebase_rag.tools.codebase_query import create_query_tool | ||||||
| from codebase_rag.tools.directory_lister import ( | ||||||
| DirectoryLister, | ||||||
|
|
@@ -29,6 +32,7 @@ | |||||
| MCPToolSchema, | ||||||
| QueryResultDict, | ||||||
| ) | ||||||
| from codebase_rag.utils.dependencies import has_semantic_dependencies | ||||||
|
|
||||||
|
|
||||||
| class MCPToolsRegistry: | ||||||
|
|
@@ -61,6 +65,19 @@ def __init__( | |||||
| directory_lister=self.directory_lister | ||||||
| ) | ||||||
|
|
||||||
| self._semantic_search_tool = None | ||||||
| self._semantic_search_available = False | ||||||
|
|
||||||
| if has_semantic_dependencies(): | ||||||
| from codebase_rag.tools.semantic_search import ( | ||||||
| create_semantic_search_tool, | ||||||
| ) | ||||||
|
|
||||||
| self._semantic_search_tool = create_semantic_search_tool() | ||||||
| self._semantic_search_available = True | ||||||
| else: | ||||||
| logger.info(lg.MCP_SEMANTIC_NOT_AVAILABLE) | ||||||
|
|
||||||
| self._tools: dict[str, ToolMetadata] = { | ||||||
| cs.MCPToolName.INDEX_REPOSITORY: ToolMetadata( | ||||||
| name=cs.MCPToolName.INDEX_REPOSITORY, | ||||||
|
|
@@ -73,6 +90,17 @@ def __init__( | |||||
| handler=self.index_repository, | ||||||
| returns_json=False, | ||||||
| ), | ||||||
| cs.MCPToolName.UPDATE_REPOSITORY: ToolMetadata( | ||||||
| name=cs.MCPToolName.UPDATE_REPOSITORY, | ||||||
| description=td.MCP_TOOLS[cs.MCPToolName.UPDATE_REPOSITORY], | ||||||
| input_schema=MCPInputSchema( | ||||||
| type=cs.MCPSchemaType.OBJECT, | ||||||
| properties={}, | ||||||
| required=[], | ||||||
| ), | ||||||
| handler=self.update_repository, | ||||||
| returns_json=False, | ||||||
| ), | ||||||
| cs.MCPToolName.QUERY_CODE_GRAPH: ToolMetadata( | ||||||
| name=cs.MCPToolName.QUERY_CODE_GRAPH, | ||||||
| description=td.MCP_TOOLS[cs.MCPToolName.QUERY_CODE_GRAPH], | ||||||
|
|
@@ -198,6 +226,28 @@ def __init__( | |||||
| returns_json=False, | ||||||
| ), | ||||||
| } | ||||||
| if self._semantic_search_available: | ||||||
| self._tools[cs.MCPToolName.SEMANTIC_SEARCH] = ToolMetadata( | ||||||
| name=cs.MCPToolName.SEMANTIC_SEARCH, | ||||||
| description=td.MCP_TOOLS[cs.MCPToolName.SEMANTIC_SEARCH], | ||||||
| input_schema=MCPInputSchema( | ||||||
| type=cs.MCPSchemaType.OBJECT, | ||||||
| properties={ | ||||||
| cs.MCPParamName.NATURAL_LANGUAGE_QUERY: MCPInputSchemaProperty( | ||||||
| type=cs.MCPSchemaType.STRING, | ||||||
| description=td.MCP_PARAM_NATURAL_LANGUAGE_QUERY, | ||||||
| ), | ||||||
| cs.MCPParamName.TOP_K: MCPInputSchemaProperty( | ||||||
| type=cs.MCPSchemaType.INTEGER, | ||||||
| description=td.MCP_PARAM_TOP_K, | ||||||
| default="5", | ||||||
| ), | ||||||
| }, | ||||||
| required=[cs.MCPParamName.NATURAL_LANGUAGE_QUERY], | ||||||
| ), | ||||||
| handler=self.semantic_search, | ||||||
| returns_json=False, | ||||||
| ) | ||||||
|
|
||||||
| async def index_repository(self) -> str: | ||||||
| logger.info(lg.MCP_INDEXING_REPO.format(path=self.project_root)) | ||||||
|
|
@@ -220,6 +270,23 @@ async def index_repository(self) -> str: | |||||
| logger.error(lg.MCP_ERROR_INDEXING.format(error=e)) | ||||||
| return cs.MCP_INDEX_ERROR.format(error=e) | ||||||
|
|
||||||
| async def update_repository(self) -> str: | ||||||
| logger.info(lg.MCP_UPDATING_REPO.format(path=self.project_root)) | ||||||
|
|
||||||
| try: | ||||||
| updater = GraphUpdater( | ||||||
| ingestor=self.ingestor, | ||||||
| repo_path=Path(self.project_root), | ||||||
| parsers=self.parsers, | ||||||
| queries=self.queries, | ||||||
| ) | ||||||
| updater.run() | ||||||
|
|
||||||
| return cs.MCP_UPDATE_SUCCESS.format(path=self.project_root) | ||||||
| except Exception as e: | ||||||
| logger.error(lg.MCP_ERROR_UPDATING.format(error=e)) | ||||||
| return cs.MCP_UPDATE_ERROR.format(error=e) | ||||||
|
Comment on lines
+286
to
+288
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catching a broad |
||||||
|
|
||||||
| async def query_code_graph(self, natural_language_query: str) -> QueryResultDict: | ||||||
| logger.info(lg.MCP_QUERY_CODE_GRAPH.format(query=natural_language_query)) | ||||||
| try: | ||||||
|
|
@@ -278,7 +345,10 @@ async def surgical_replace_code( | |||||
| return te.ERROR_WRAPPER.format(message=e) | ||||||
|
|
||||||
| async def read_file( | ||||||
| self, file_path: str, offset: int | None = None, limit: int | None = None | ||||||
| self, | ||||||
| file_path: str, | ||||||
| offset: int | None = None, | ||||||
| limit: int | None = None, | ||||||
| ) -> str: | ||||||
| logger.info(lg.MCP_READ_FILE.format(path=file_path, offset=offset, limit=limit)) | ||||||
| try: | ||||||
|
|
@@ -339,6 +409,17 @@ async def list_directory( | |||||
| logger.error(lg.MCP_ERROR_LIST_DIR.format(error=e)) | ||||||
| return te.ERROR_WRAPPER.format(message=e) | ||||||
|
|
||||||
| async def semantic_search(self, natural_language_query: str, top_k: int = 5) -> str: | ||||||
| if self._semantic_search_tool is None: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've introduced a
Suggested change
|
||||||
| return cs.MCP_SEMANTIC_NOT_AVAILABLE_RESPONSE | ||||||
|
|
||||||
| logger.info(lg.MCP_SEMANTIC_SEARCH.format(query=natural_language_query)) | ||||||
|
|
||||||
| result = await self._semantic_search_tool.function( | ||||||
| query=natural_language_query, top_k=top_k | ||||||
| ) | ||||||
| return str(result) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
|
||||||
| def get_tool_schemas(self) -> list[MCPToolSchema]: | ||||||
| return [ | ||||||
| MCPToolSchema( | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TOP_Kparameter is defined with typeINTEGER, but its default value is provided as a string"5". This type mismatch can lead to schema validation errors or unexpected behavior when the tool is used with default parameters. The default value should be an integer to match the specified type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class MCPInputSchemaProperty(TypedDict, total=False):
type: str
description: str
default: str