Skip to main content
Glama

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
NameRequiredDescriptionDefault
characterYesZero-based character offset for completion position
contentYesThe current content of the file
filePathYesAbsolute or relative path to the source file
languageIdYesThe language identifier (e.g., "typescript", "javascript")
lineYesZero-based line number for completion position
projectRootYesImportant: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located

Implementation Reference

  • 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;
  • 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'],
    },
Install Server

Other Tools

Related Tools

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/alexwohletz/language-server-mcp'

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