Skip to main content
Glama
lin2000wl

Serena MCP Server

by lin2000wl

find_referencing_symbols

Locate symbols that reference a specific symbol in your codebase, including subclasses and usage points, to analyze dependencies and inheritance.

Instructions

Finds symbols that reference the symbol at the given name_path. The result will contain metadata about the referencing symbols as well as a short code snippet around the reference (unless include_body is True, then the short snippet will be omitted). Note that among other kinds of references, this function can be used to find (direct) subclasses of a class, as subclasses are referencing symbols that have the kind class. Returns a list of JSON objects with the symbols referencing the requested symbol.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
name_pathYesFor finding the symbol to find references for, same logic as in the `find_symbol` tool.
relative_pathYesThe relative path to the file containing the symbol for which to find references. Note that here you can't pass a directory but must pass a file.
include_kindsNoSame as in the `find_symbol` tool.
exclude_kindsNoSame as in the `find_symbol` tool.
max_answer_charsNoSame as in the `find_symbol` tool.

Implementation Reference

  • Core handler logic for the find_referencing_symbols tool. Finds referencing symbols by first locating the symbol by name and file, then using LSP to find references at its location. Filters by kinds if specified.
    def find_referencing_symbols(
        self,
        name_path: str,
        relative_file_path: str,
        include_body: bool = False,
        include_kinds: Sequence[SymbolKind] | None = None,
        exclude_kinds: Sequence[SymbolKind] | None = None,
    ) -> list[ReferenceInSymbol]:
        """
        Find all symbols that reference the symbol with the given name.
        If multiple symbols fit the name (e.g. for variables that are overwritten), will use the first one.
    
        :param name_path: the name path of the symbol to find
        :param relative_file_path: the relative path of the file in which the referenced symbol is defined.
        :param include_body: whether to include the body of all symbols in the result.
            Not recommended, as the referencing symbols will often be files, and thus the bodies will be very long.
        :param include_kinds: which kinds of symbols to include in the result.
        :param exclude_kinds: which kinds of symbols to exclude from the result.
        """
        symbol_candidates = self.find_by_name(name_path, substring_matching=False, within_relative_path=relative_file_path)
        if len(symbol_candidates) == 0:
            log.warning(f"No symbol with name {name_path} found in file {relative_file_path}")
            return []
        if len(symbol_candidates) > 1:
            log.error(
                f"Found {len(symbol_candidates)} symbols with name {name_path} in file {relative_file_path}."
                f"May be an overwritten variable, in which case you can ignore this error. Proceeding with the first one. "
                f"Found symbols for {name_path=} in {relative_file_path=}: \n"
                f"{json.dumps([s.location.to_dict() for s in symbol_candidates], indent=2)}"
            )
        symbol = symbol_candidates[0]
        return self.find_referencing_symbols_by_location(
            symbol.location, include_body=include_body, include_kinds=include_kinds, exclude_kinds=exclude_kinds
        )
    
    def find_referencing_symbols_by_location(
        self,
        symbol_location: SymbolLocation,
        include_body: bool = False,
        include_kinds: Sequence[SymbolKind] | None = None,
        exclude_kinds: Sequence[SymbolKind] | None = None,
    ) -> list[ReferenceInSymbol]:
        """
        Find all symbols that reference the symbol at the given location.
    
        :param symbol_location: the location of the symbol for which to find references.
            Does not need to include an end_line, as it is unused in the search.
        :param include_body: whether to include the body of all symbols in the result.
            Not recommended, as the referencing symbols will often be files, and thus the bodies will be very long.
            Note: you can filter out the bodies of the children if you set include_children_body=False
            in the to_dict method.
        :param include_kinds: an optional sequence of ints representing the LSP symbol kind.
            If provided, only symbols of the given kinds will be included in the result.
        :param exclude_kinds: If provided, symbols of the given kinds will be excluded from the result.
            Takes precedence over include_kinds.
        :return: a list of symbols that reference the given symbol
        """
        if not symbol_location.has_position_in_file():
            raise ValueError("Symbol location does not contain a valid position in a file")
        assert symbol_location.relative_path is not None
        assert symbol_location.line is not None
        assert symbol_location.column is not None
        references = self._lang_server.request_referencing_symbols(
            relative_file_path=symbol_location.relative_path,
            line=symbol_location.line,
            column=symbol_location.column,
            include_imports=False,
            include_self=False,
            include_body=include_body,
            include_file_symbols=True,
        )
    
        if include_kinds is not None:
            references = [s for s in references if s.symbol["kind"] in include_kinds]
    
        if exclude_kinds is not None:
            references = [s for s in references if s.symbol["kind"] not in exclude_kinds]
    
        return [ReferenceInSymbol.from_lsp_reference(r) for r in references]
  • Dataclass representing a reference to a symbol, used in the return type of the handler.
    @dataclass
    class ReferenceInSymbol(ToStringMixin):
        """Same as the class of the same name in the language server, but using Serena's Symbol class.
        Be careful to not confuse it with counterpart!
        """
    
        symbol: Symbol
        line: int
        character: int
    
        def get_relative_path(self) -> str | None:
            return self.symbol.location.relative_path
    
        @classmethod
        def from_lsp_reference(cls, reference: LSPReferenceInSymbol) -> Self:
            return cls(symbol=Symbol(reference.symbol), line=reference.line, character=reference.character)
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden. It discloses key behaviors: the result includes metadata and code snippets (with conditions), and it can find subclasses. However, it lacks details on permissions, rate limits, error handling, or the exact structure of returned JSON objects, leaving gaps for a tool with 5 parameters.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized with three sentences. It front-loads the core purpose, adds behavioral details, and clarifies a use case. There's no wasted text, though the mention of 'include_body' (not in schema) could cause confusion, slightly reducing efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 5 parameters, no annotations, and no output schema, the description is moderately complete. It covers the tool's purpose and some behaviors but lacks details on output structure, error cases, or integration with siblings. For a complex reference-finding tool, this leaves significant gaps for an AI agent to infer correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters. The description adds minimal value: it mentions 'include_body' affecting snippet omission, but this parameter isn't in the schema (likely an error or omission). For other parameters, it only references 'same logic as in the `find_symbol` tool,' not providing additional semantics beyond the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Finds symbols that reference the symbol at the given `name_path`.' It specifies the verb ('finds'), resource ('symbols'), and target ('symbol at the given name_path'). However, it doesn't explicitly differentiate from sibling tools like 'find_symbol' beyond mentioning similar parameter logic.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by noting it 'can be used to find (direct) subclasses of a class,' providing some context. However, it doesn't explicitly state when to use this tool versus alternatives like 'find_symbol' or 'search_for_pattern,' nor does it mention prerequisites or exclusions beyond parameter references.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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/lin2000wl/Serena-cursor-mcp'

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