GitHub Copilot + LSP: Smarter Code Context and Better Token Efficiency

This guide walks through configuring Language Server Protocol (LSP) support for Go, Python, Ruby, TypeScript, and JavaScript in GitHub Copilot CLI. It also includes practices for keeping AI coding sessions efficient and token-conscious.

Part of a Series

This post is part of a broader series exploring how modern AI coding assistants understand and work with code.

The series starts with the foundations of how source code moves through the development toolchain—from ASTs and parsing to compilers and language servers (LSP)—and then builds on those concepts to explain how AI coding tools can use this information to navigate and reason about a codebase.

Series

  1. ASTs: How Programming Languages Understand Your Code ← ⏳ Coming soon
  2. From AST to Machine Code: Understanding the Compiler Workflow ← 🚧 In progress
  3. Language Server Protocol (LSP): How Your Editor Understands Your Code
  4. LSP + ASTs + Compilers: The Semantic Foundation of Modern Code Intelligence
  5. GitHub Copilot + LSP: Smarter Code Context and Better Token Efficiency ← 📍You are here

Coming from another post? If you’re new to LSP or want to understand what’s happening under the hood, start with the AST and compiler posts before continuing with this setup guide.


1. Install Your Language Servers

Before Copilot can understand your code semantically, make sure the appropriate language servers are installed and available.

Go

Install gopls, the official Go language server maintained by the Go team:

go install golang.org/x/tools/gopls@latest

Make sure Go’s binary directory is on your PATH:

export PATH="$PATH:$(go env GOPATH)/bin"

To make this persistent across terminal sessions, add the export to your shell configuration, such as ~/.zshrc or ~/.bashrc.

Python

Install Pyright, Microsoft’s Python language server, globally via npm:

npm install -g pyright

Ruby

Add ruby-lsp to the development group in your project’s Gemfile:

group :development do
  gem "ruby-lsp", require: false
end

Then install the dependency:

bundle install

TypeScript & JavaScript

Install the standard TypeScript language server:

npm install -g typescript-language-server typescript

2. Configure Global Copilot LSP Support

Project-level LSP configurations can sometimes be unreliable or ignored by the Copilot CLI. A global configuration ensures Copilot can load your language servers regardless of which repository you’re working in.

Create the Global Configuration Directory

mkdir -p ~/.copilot

Create the following file:

~/.copilot/lsp-config.json

Add this configuration:

{
  "lspServers": {
    "go": {
      "command": "gopls",
      "args": [],
      "fileExtensions": {
        ".go": "go"
      }
    },
    "python": {
      "command": "pyright-langserver",
      "args": ["--stdio"],
      "fileExtensions": {
        ".py": "python"
      }
    },
    "ruby": {
      "command": "bundle",
      "args": ["exec", "ruby-lsp"],
      "fileExtensions": {
        ".rb": "ruby",
        "Gemfile": "ruby",
        "Rakefile": "ruby"
      }
    },
    "typescript": {
      "command": "typescript-language-server",
      "args": ["--stdio"],
      "fileExtensions": {
        ".js": "javascript",
        ".jsx": "javascriptreact",
        ".ts": "typescript",
        ".tsx": "typescriptreact"
      }
    }
  }
}

Supported Languages

LanguageLanguage ServerInstallationFile Extensions
Gogoplsgo install golang.org/x/tools/gopls@latest.go
Pythonpyrightnpm install -g pyright.py
Rubyruby-lspbundle install.rb, Gemfile, Rakefile
TypeScripttypescript-language-servernpm install -g typescript-language-server typescript.ts, .tsx
JavaScripttypescript-language-servernpm install -g typescript-language-server typescript.js, .jsx

3. Verify and Test the Connections

Open an interactive Copilot CLI session in your terminal and reload the LSP configuration:

/lsp reload

Then inspect the registered language servers:

/lsp

You should see Go, Python, Ruby, and TypeScript listed under your active user configurations.

Test Each Language Server

Run the following commands:

/lsp test go
/lsp test python
/lsp test ruby
/lsp test typescript

If each test returns a green checkmark or a message such as:

Server started successfully

your multi-language semantic bridge is working correctly.


4. Maximize Your Token Efficiency

Once your environment is connected, a few habits can help keep Copilot’s context usage low while improving the quality of its answers.

Isolate Code Questions

Ask contextual questions rather than requesting broad repository searches.

For example:

Where is our user verification logic defined?

With LSP support enabled, Copilot can use semantic tools such as workspaceSymbol to navigate the codebase instead of relying solely on raw text searches.

Keep Sessions Short

Use:

Ctrl + L

frequently to clear your current chat context.

Long-running conversations can accumulate stale context, causing Copilot to repeatedly process old logs and discussion history.

Filter Terminal Logs

Avoid dumping entire test runs or server logs into the prompt.

Instead, pipe the output down to the relevant errors:

npm test | grep -E "Error|Failure"

This gives Copilot the information it needs without unnecessarily consuming context.


5. Troubleshooting

gopls: command not found

If Copilot cannot find gopls, verify where Go installs binaries:

go env GOPATH

Then check that the binary exists:

ls "$(go env GOPATH)/bin/gopls"

If necessary, add Go’s binary directory to your PATH:

export PATH="$PATH:$(go env GOPATH)/bin"

You can verify that the command is available:

gopls version

Language Server Fails to Start

For each language server, verify that the executable is available from your terminal:

which gopls
which pyright-langserver
which ruby-lsp
which typescript-language-server

For Ruby, remember that the configured command is:

bundle exec ruby-lsp

so the ruby-lsp gem needs to be available in the project’s bundle.


Summary

With gopls, Pyright, Ruby LSP, and TypeScript Language Server configured globally, Copilot CLI can interact with your codebase through semantic code intelligence rather than relying exclusively on text-based searches.

The result is:

Enjoy faster, smarter, and more cost-efficient AI-assisted development across your entire stack!

Comments

comments powered by Disqus