get_references
Find all definition and usage references for symbols in TypeScript projects by specifying file location, enabling code navigation and analysis without IDE integration.
Instructions
Resolve project-aware definition and usage references for the symbol at a 1-based line and column in a file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| workspaceRoot | No | ||
| projectTsconfigPath | No | ||
| line | Yes | ||
| column | Yes |
Implementation Reference
- src/server.ts:112-129 (handler)The handler for the 'get_references' tool, which calls `cache.getReferences` and formats the output.
async (args: PositionArgs) => { const { references, project } = cache.getReferences(args.file, args.line, args.column, args); return { content: [ { type: "text", text: JSON.stringify({ file: args.file, line: args.line, column: args.column, references, project }, null, 2), }, ], structuredContent: { file: args.file, line: args.line, column: args.column, references, project, }, }; }, - src/project-service.ts:107-129 (handler)The core logic for fetching references using the TypeScript Language Service.
public getReferences(filePath: string, line: number, column: number): ReferenceItem[] { const normalizedFile = this.prepareFile(filePath); const sourceFile = this.languageService.getProgram()?.getSourceFile(normalizedFile); if (!sourceFile) { throw new Error(`TypeScript program did not include ${normalizedFile}`); } const offset = toOffset(sourceFile, line, column); const references = this.languageService.getReferencesAtPosition(normalizedFile, offset) ?? []; const definitionKeys = new Set( (this.languageService.getDefinitionAtPosition(normalizedFile, offset) ?? []) .map((definition) => this.toDefinitionItem(definition.fileName, definition.textSpan.start, definition.textSpan.length)) .map((definition) => `${canonicalPath(definition.file)}:${definition.line}:${definition.column}`), ); return references.map((reference) => { const item = this.toDefinitionItem(reference.fileName, reference.textSpan.start, reference.textSpan.length); return { ...item, isDefinition: definitionKeys.has(`${canonicalPath(item.file)}:${item.line}:${item.column}`), }; }); } - src/server.ts:105-130 (registration)Registration of the 'get_references' tool in the McpServer instance.
server.registerTool( "get_references", { title: "Get References", description: "Resolve project-aware definition and usage references for the symbol at a 1-based line and column in a file.", inputSchema: definitionSchema, }, async (args: PositionArgs) => { const { references, project } = cache.getReferences(args.file, args.line, args.column, args); return { content: [ { type: "text", text: JSON.stringify({ file: args.file, line: args.line, column: args.column, references, project }, null, 2), }, ], structuredContent: { file: args.file, line: args.line, column: args.column, references, project, }, }; }, );