Skip to main content
Glama

search_identifiers

Search code files for specific identifiers to find their locations with file paths, line numbers, and surrounding context for better code navigation and understanding.

Instructions

Search for identifiers in code files. Get back a list of matching identifiers with their file, line number, and context. When searching, just use the identifier name without any special characters, prefixes or suffixes. The search is case-insensitive.

Args: project_root: Root directory of the project to search. (must be an absolute path!) query: Search query (identifier name) max_results: Maximum number of results to return context_lines: Number of lines of context to show include_definitions: Whether to include definition occurrences include_references: Whether to include reference occurrences

Returns: Dictionary containing search results or error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
context_linesNo
include_definitionsNo
include_referencesNo
max_resultsNo
project_rootYes
queryYes

Implementation Reference

  • The handler function for the search_identifiers tool, including its registration via @mcp.tool(). It initializes RepoMap, scans source files, extracts tags (defs/refs), filters by query, sorts, limits results, and provides context snippets for matches.
    @mcp.tool() async def search_identifiers( project_root: str, query: str, max_results: int = 50, context_lines: int = 2, include_definitions: bool = True, include_references: bool = True ) -> Dict[str, Any]: """Search for identifiers in code files. Get back a list of matching identifiers with their file, line number, and context. When searching, just use the identifier name without any special characters, prefixes or suffixes. The search is case-insensitive. Args: project_root: Root directory of the project to search. (must be an absolute path!) query: Search query (identifier name) max_results: Maximum number of results to return context_lines: Number of lines of context to show include_definitions: Whether to include definition occurrences include_references: Whether to include reference occurrences Returns: Dictionary containing search results or error message """ if not os.path.isdir(project_root): return {"error": f"Project root directory not found: {project_root}"} try: # Initialize RepoMap with search-specific settings repo_map = RepoMap( root=project_root, token_counter_func=lambda text: count_tokens(text, "gpt-4"), file_reader_func=read_text, output_handler_funcs={'info': log.info, 'warning': log.warning, 'error': log.error}, verbose=False, exclude_unranked=True ) # Find all source files in the project all_files = find_src_files(project_root) # Get all tags (definitions and references) for all files all_tags = [] for file_path in all_files: rel_path = str(Path(file_path).relative_to(project_root)) tags = repo_map.get_tags(file_path, rel_path) all_tags.extend(tags) # Filter tags based on search query and options matching_tags = [] query_lower = query.lower() for tag in all_tags: if query_lower in tag.name.lower(): if (tag.kind == "def" and include_definitions) or \ (tag.kind == "ref" and include_references): matching_tags.append(tag) # Sort by relevance (definitions first, then references) matching_tags.sort(key=lambda x: (x.kind != "def", x.name.lower().find(query_lower))) # Limit results matching_tags = matching_tags[:max_results] # Format results with context results = [] for tag in matching_tags: file_path = str(Path(project_root) / tag.rel_fname) # Calculate context range based on context_lines parameter start_line = max(1, tag.line - context_lines) end_line = tag.line + context_lines context_range = list(range(start_line, end_line + 1)) context = repo_map.render_tree( file_path, tag.rel_fname, context_range ) if context: results.append({ "file": tag.rel_fname, "line": tag.line, "name": tag.name, "kind": tag.kind, "context": context }) return {"results": results} except Exception as e: log.exception(f"Error searching identifiers in project '{project_root}': {e}") return {"error": f"Error searching identifiers: {str(e)}"}

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pdavis68/RepoMapper'

If you have feedback or need assistance with the MCP directory API, please join our Discord server