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
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| workspaceRoot | No | ||
| projectTsconfigPath | No | ||
| line | Yes | ||
| column | Yes |
Implementation Reference
- src/server.ts:68-103 (handler)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, }, }; }, ); - src/project-service.ts:92-105 (handler)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); }); } - src/project-service.ts:294-305 (helper)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(), }; }