Skip to main content
Glama
c0sc0s
by c0sc0s

get_definition

Find where symbols are defined in code files by specifying file path, line, and column positions for navigation and analysis.

Instructions

Resolve the definition locations for a symbol at a 1-based line and column in a file.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYes
workspaceRootNo
projectTsconfigPathNo
lineYes
columnYes

Implementation Reference

  • The tool registration for "get_definition" in MCP server.
    server.registerTool(
      "get_definition",
      {
        title: "Get Definition",
        description: "Resolve the definition locations for a symbol at a 1-based line and column in a file.",
        inputSchema: definitionSchema,
      },
      async (args: DefinitionArgs) => {
        const { definitions, project } = cache.getDefinitionWithMetadata(args.file, args.line, args.column, args);
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  file: args.file,
                  line: args.line,
                  column: args.column,
                  definitions,
                  project,
                },
                null,
                2,
              ),
            },
          ],
          structuredContent: {
            file: args.file,
            line: args.line,
            column: args.column,
            definitions,
            project,
          },
        };
      },
    );
  • The actual implementation of getDefinition in ProjectService.
    public getDefinition(filePath: string, line: number, column: number): DefinitionItem[] {
      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 definitions = this.languageService.getDefinitionAtPosition(normalizedFile, offset) ?? [];
    
      return definitions.map((definition) => {
        return this.toDefinitionItem(definition.fileName, definition.textSpan.start, definition.textSpan.length);
      });
    }
  • Helper function in ProjectServiceCache that fetches definition and metadata.
    public getDefinitionWithMetadata(
      filePath: string,
      line: number,
      column: number,
      options: ProjectLookupOptions,
    ): { definitions: DefinitionItem[]; project: ProjectMetadata } {
      const service = this.getOrCreate({ ...options, file: filePath });
      return {
        definitions: service.getDefinition(filePath, line, column),
        project: service.getProjectMetadata(),
      };
    }

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