get_completions
Generate code completion suggestions for a specified position in a document by providing language, file path, content, and project context.
Instructions
Get completion suggestions for a position in a document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| character | Yes | Zero-based character offset for completion position | |
| content | Yes | The current content of the file | |
| filePath | Yes | Absolute or relative path to the source file | |
| languageId | Yes | The language identifier (e.g., "typescript", "javascript") | |
| line | Yes | Zero-based line number for completion position | |
| projectRoot | Yes | Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located |
Implementation Reference
- src/index.ts:481-543 (handler)The core handler function that implements the 'get_completions' tool logic. It extracts parameters, ensures the language server is running, opens the document, sends a completion request to the LSP server, and formats the response.private async handleGetCompletions(args: any): Promise<any> { const { languageId, filePath, content, line, character, projectRoot } = args; console.log(`[handleGetCompletions] Processing request for ${languageId}`); const server = await this.getOrCreateServer(languageId, projectRoot); const actualRoot = server.workspaceRoot; const absolutePath = isAbsolute(filePath) ? filePath : join(actualRoot, filePath); const uri = `file://${absolutePath}`; // Ensure directory exists const dir = dirname(absolutePath); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } const textDocument: TextDocumentItem = { uri, languageId, version: 1, text: content, }; console.log(`[handleGetCompletions] Sending document to server:`, textDocument); await server.connection.sendNotification('textDocument/didOpen', { textDocument, } as DidOpenTextDocumentParams); try { console.log(`[handleGetCompletions] Requesting completions`); const completionParams: CompletionParams = { textDocument: { uri }, position: { line, character }, }; const completions: CompletionItem[] | null = await server.connection.sendRequest( 'textDocument/completion', completionParams ); console.log(`[handleGetCompletions] Received completions:`, completions); return { content: [ { type: 'text', text: completions ? JSON.stringify(completions, null, 2) : 'No completions available', }, ], }; } catch (error) { console.error('[handleGetCompletions] Request failed:', error); return { content: [ { type: 'text', text: 'Failed to get completions', }, ], isError: true, }; }
- src/index.ts:325-358 (registration)The tool registration object returned in the listTools response, defining the name, description, and input schema for 'get_completions'.{ name: 'get_completions', description: 'Get completion suggestions for a position in a document', inputSchema: { type: 'object', properties: { languageId: { type: 'string', description: 'The language identifier (e.g., "typescript", "javascript")' }, filePath: { type: 'string', description: 'Absolute or relative path to the source file' }, content: { type: 'string', description: 'The current content of the file' }, line: { type: 'number', description: 'Zero-based line number for completion position' }, character: { type: 'number', description: 'Zero-based character offset for completion position' }, projectRoot: { type: 'string', description: 'Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located' }, }, required: ['languageId', 'filePath', 'content', 'line', 'character', 'projectRoot'], }, },
- src/index.ts:398-400 (registration)The dispatch case in the callTool request handler that routes 'get_completions' calls to the handleGetCompletions method.case 'get_completions': result = await this.handleGetCompletions(args); break;
- src/index.ts:328-357 (schema)The input schema defining the parameters expected by the 'get_completions' tool.inputSchema: { type: 'object', properties: { languageId: { type: 'string', description: 'The language identifier (e.g., "typescript", "javascript")' }, filePath: { type: 'string', description: 'Absolute or relative path to the source file' }, content: { type: 'string', description: 'The current content of the file' }, line: { type: 'number', description: 'Zero-based line number for completion position' }, character: { type: 'number', description: 'Zero-based character offset for completion position' }, projectRoot: { type: 'string', description: 'Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located' }, }, required: ['languageId', 'filePath', 'content', 'line', 'character', 'projectRoot'], },