Skip to main content
Glama
c0sc0s
by c0sc0s

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
NameRequiredDescriptionDefault
fileYes
workspaceRootNo
projectTsconfigPathNo
lineYes
columnYes

Implementation Reference

  • 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,
        },
      };
    },
  • 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,
          },
        };
      },
    );

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/c0sc0s/agent-workspace-mcp'

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