# Gopls Extension for Gemini CLI

English | [简体中文](./README_zh.md)

This extension integrates the [**official** MCP](https://tip.golang.org/gopls/features/mcp) of the Go language server (`gopls`) with Gemini CLI. It enables Gemini to understand, write, and modify Go code with greater precision by providing language-aware features directly within the CLI environment.

## Demo

To demonstrate its effectiveness, I performed the same code refactoring task in a Go project, both with and without this extension.

When using the extension, Gemini CLI calls the `gopls` tool to obtain precise symbol relationships and runs diagnostics immediately after each file modification. This avoids having to deal with a large number of syntax errors all at once during the final compilation.

Both the speed and accuracy of refactoring were significantly improved with the extension. For example, in the comparison case, Gemini missed updating some references to the refactored code when the extension was not used.

![compare](https://github.com/user-attachments/assets/c3913176-afae-4b9e-af23-a29dbd3dda81)

## Installation

Install this extension using the following Gemini CLI command:

```bash
gemini extensions install https://github.com/Menghuan1918/gemini-cli-gopls
```

The extension automatically checks the `gopls` version in the system `PATH` at runtime. Since the MCP feature requires `gopls` v0.20.0 or later, if a compatible version of `gopls` is not found, the extension will attempt to automatically download and install it to the `~/go/bin` directory.

If it still fails to start, please try manually installing `gopls` v0.20.0 or a later version into your `PATH`.

## Features

This extension provides Gemini CLI with the following tools based on the `gopls` MCP:

- `go_diagnostics`: Workspace-based syntax error checking
- `go_file_context`: Summarizes cross-file dependencies for a file
- `go_package_api`: Provides information on external/internal Go package APIs
- `go_search`: Searches for symbols in the workspace
- `go_symbol_references`: Provides reference locations for Go symbols
- `go_workspace`: Provides Go workspace information

Additionally, the plugin has built-in prompts from the official gopls MCP, with minor modifications to better suit Gemini CLI. You can view them in [GEMINI.md](./GEMINI.md).

> The built-in prompts from the official gopls MCP were exported using `gopls mcp -instructions > GEMINI.md`

## Usage

When launched in a Go project (a workspace containing a `go.mod` file), the relevant tools will be loaded automatically. Otherwise, only an empty MCP service will be loaded (to avoid errors).
