Skip to main content
Glama

find_references

Locate all references to a symbol in your codebase using Language Server Protocol integration. Specify file, line, and character position to identify the symbol and find where it's used throughout the project.

Instructions

Find all references to a symbol using LSP

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesFile containing the symbol
lineYesLine number of the symbol
characterNoCharacter position of the symbol
languageNoProgramming languagetypescript

Implementation Reference

  • The main handler function that executes the 'find_references' tool. It uses LSP to find references to a symbol at a given position by opening workspace files and sending a textDocument/references request.
    export async function findReferences(args: FindReferencesArgs, clientManager: LSPClientManager) { const { file, line, character = 0, language = 'typescript' } = args; const workspaceRoot = findWorkspaceRoot(file); try { const client = await clientManager.getOrCreateLSPClient(language, workspaceRoot); // Open all files in the workspace to enable cross-file reference finding const { glob } = await import('glob'); // Determine file extension based on language const extensions = { typescript: '**/*.{ts,tsx}', javascript: '**/*.{js,jsx}', python: '**/*.py', }; const pattern = extensions[language as keyof typeof extensions] || '**/*.ts'; const workspaceFiles = await glob(pattern, { cwd: workspaceRoot, absolute: true, ignore: ['**/node_modules/**', '**/dist/**', '**/.git/**'], }); // Open all files in the workspace for (const filePath of workspaceFiles) { try { const fileContent = await fs.readFile(filePath, 'utf-8'); await clientManager.sendLSPNotification(client, 'textDocument/didOpen', { textDocument: { uri: `file://${filePath}`, languageId: language, version: 1, text: fileContent, }, }); } catch (_error) { // Skip files that can't be read continue; } } const referencesResponse = await clientManager.sendLSPRequest(client, 'textDocument/references', { textDocument: { uri: `file://${file}` }, position: { line: line - 1, character }, context: { includeDeclaration: true }, }); const references = Array.isArray(referencesResponse) ? (referencesResponse as LSPLocation[]) : []; const formattedReferences = references.map((ref: LSPLocation) => ({ file: ref.uri.replace('file://', ''), line: ref.range.start.line + 1, character: ref.range.start.character, })); return { references: formattedReferences, count: formattedReferences.length, content: [ { type: 'text', text: `Found ${formattedReferences.length} references:\n${formattedReferences .map((ref: { file: string; line: number; character: number }) => `${ref.file}:${ref.line}:${ref.character}`) .join('\n')}`, }, ], }; } catch (error) { throw new Error(`Failed to find references: ${error instanceof Error ? error.message : String(error)}`); } }
  • src/server.ts:143-170 (registration)
    Registers the 'find_references' tool in the MCP server's tool list, including name, description, and input schema.
    { name: 'find_references', description: 'Find all references to a symbol using LSP', inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'File containing the symbol', }, line: { type: 'number', description: 'Line number of the symbol', }, character: { type: 'number', description: 'Character position of the symbol', default: 0, }, language: { type: 'string', description: 'Programming language', default: 'typescript', }, }, required: ['file', 'line'], }, },
  • src/server.ts:218-219 (registration)
    Handles incoming calls to the 'find_references' tool by dispatching to the findReferences handler function.
    case 'find_references': return await findReferences(args as unknown as FindReferencesArgs, this.clientManager);
  • TypeScript interface defining the input arguments for the 'find_references' tool, used for type safety in the handler and server.
    export interface FindReferencesArgs { file: string; line: number; character?: number; language?: string; }

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/sminnee/lsp-mcp'

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