Skip to content

feat(core): implement lsp_query tool for semantic code analysis#22580

Closed
GitmanIII wants to merge 3 commits intogoogle-gemini:mainfrom
GitmanIII:fix/lsp-tool-parameters-and-docs
Closed

feat(core): implement lsp_query tool for semantic code analysis#22580
GitmanIII wants to merge 3 commits intogoogle-gemini:mainfrom
GitmanIII:fix/lsp-tool-parameters-and-docs

Conversation

@GitmanIII
Copy link
Copy Markdown

@GitmanIII GitmanIII commented Mar 15, 2026

Summary

Implement the lsp_query tool, enabling semantic code analysis (definitions, references, hover, symbols) via the Language Server Protocol (LSP).

Details

Note: This feature was originally developed using gemini-cli-0.34.0-nightly.20260304.28af4e127 as a baseline and has since been rebased and updated to integrate cleanly into the current main branch.

This PR introduces the lsp_query tool to the Gemini CLI, allowing the agent to perform deep semantic analysis of codebases.

  • Added LspTool to packages/core.
  • Implemented LspServerManager and LspClient for managing lifecycle and communication with local language servers.
  • Fixed a critical bug in LspTool where parameter schemas were incorrectly initialized, preventing the model from providing required arguments.
  • Updated the core system prompt to prioritize lsp_query for precise navigation tasks.
  • Added comprehensive unit tests for LspTool and LspServerManager.
  • Added documentation following Google style guidelines.

Related Issues

Fixes the issue where semantic navigation was limited to text-based search.

How to Validate

  1. Rebuild the project: npm run build
  2. Ensure you have a language server installed (e.g., typescript-language-server for TS).
  3. Start the CLI in a workspace.
  4. Ask the agent to find the definition or references of a symbol: "find definition of LspTool in packages/core/src/tools/lsp-tool.ts".
  5. Verify the tool executes correctly and returns the expected result.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • Linux
      • npm run

@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@GitmanIII GitmanIII force-pushed the fix/lsp-tool-parameters-and-docs branch 3 times, most recently from fe7d6f3 to 3012f3e Compare March 16, 2026 00:36
@GitmanIII GitmanIII force-pushed the fix/lsp-tool-parameters-and-docs branch 2 times, most recently from 06c3710 to a856f8e Compare March 16, 2026 19:04
@GitmanIII GitmanIII marked this pull request as ready for review March 16, 2026 19:05
@GitmanIII GitmanIII requested review from a team as code owners March 16, 2026 19:05
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Gemini CLI's code understanding capabilities by integrating a new Language Server Protocol (LSP) query tool. This allows the agent to move beyond basic text searches and perform deep semantic analysis, such as finding definitions or references, which is crucial for complex software engineering tasks like bug fixing, feature development, and refactoring. The change provides a robust framework for managing language servers and ensures the agent can intelligently interact with various programming languages.

Highlights

  • New LSP Query Tool: Implemented the lsp_query tool, enabling the Gemini CLI agent to perform semantic code analysis (definitions, references, hover, symbols) using the Language Server Protocol (LSP).
  • LSP Infrastructure: Introduced LspClient for JSON-RPC 2.0 communication with language servers and LspServerManager to manage the lifecycle and prevent duplicate server spawns across the workspace.
  • Prompt System Integration: Updated the core system prompt to prioritize lsp_query for precise code navigation and research tasks, enhancing the agent's understanding capabilities.
  • Bug Fix and Testing: Fixed a critical bug in LspTool related to incorrect parameter schema initialization and added comprehensive unit tests for both LspTool and LspServerManager.
  • Documentation and Policies: Added new documentation for the lsp_query tool and updated security policies (plan.toml, read-only.toml) to allow its usage.
Changelog
  • docs/sidebar.json
    • Added 'LSP Query' to the documentation sidebar.
  • docs/tools/lsp-query.md
    • Added new documentation detailing the lsp_query tool's purpose, technical reference, supported languages, configuration, and implementation notes.
  • packages/core/src/config/config.ts
    • Imported and registered the new LspTool within the Config class.
  • packages/core/src/core/snapshots/prompts.test.ts.snap
    • Updated various prompt snapshots to include lsp_query in the list of tools for codebase investigation and research.
    • Corrected 'grep_search and glob search tools' to 'grep_search and glob tools' in several prompt descriptions.
  • packages/core/src/core/prompts.test.ts
    • Modified test cases for CodebaseInvestigator to include lsp_query in the tool names.
    • Updated assertions related to search tool mentions in generated prompts.
  • packages/core/src/lsp/LspClient.ts
    • Added a new file implementing LspClient, a JSON-RPC 2.0 client for communicating with Language Servers over standard I/O, handling message framing, requests, and notifications.
  • packages/core/src/lsp/LspServerManager.test.ts
    • Added unit tests for LspServerManager, covering scenarios like handling missing language server binaries and successfully obtaining a client for TypeScript.
  • packages/core/src/lsp/LspServerManager.ts
    • Added a new file implementing LspServerManager, which orchestrates the lifecycle of LSP clients, maps file extensions to language server configurations, verifies binary existence, and prevents duplicate server spawns.
  • packages/core/src/policy/policies/plan.toml
    • Added lsp_query to the list of allowed tools in the plan.toml policy.
  • packages/core/src/policy/policies/read-only.toml
    • Added lsp_query to the list of allowed tools in the read-only.toml policy.
  • packages/core/src/prompts/promptProvider.ts
    • Imported LSP_TOOL_NAME and added enableLspTool to the PrimaryWorkflowsOptions for prompt generation.
  • packages/core/src/prompts/snippets.legacy.ts
    • Imported LSP_TOOL_NAME and added enableLspTool to PrimaryWorkflowsOptions.
    • Updated the workflowStepUnderstand function to conditionally include lsp_query in search tool mentions.
  • packages/core/src/prompts/snippets.ts
    • Imported LSP_TOOL_NAME and added enableLspTool to PrimaryWorkflowsOptions.
    • Updated the workflowStepResearch function to conditionally include lsp_query in search tool mentions.
  • packages/core/src/tools/definitions/base-declarations.ts
    • Added constants for LSP_TOOL_NAME and its associated parameters (LSP_PARAM_OPERATION, LSP_PARAM_FILE_PATH, LSP_PARAM_LINE, LSP_PARAM_CHARACTER).
  • packages/core/src/tools/definitions/coreTools.ts
    • Exported LSP_TOOL_NAME and its parameters.
    • Added LSP_DEFINITION to the core tool definitions.
  • packages/core/src/tools/definitions/model-family-sets/default-legacy.ts
    • Imported LSP_TOOL_NAME and its parameters.
    • Added the lsp_query tool definition with its JSON schema to the DEFAULT_LEGACY_SET.
  • packages/core/src/tools/definitions/model-family-sets/gemini-3.ts
    • Imported LSP_TOOL_NAME and its parameters.
    • Added the lsp_query tool definition with its JSON schema to the GEMINI_3_SET.
  • packages/core/src/tools/definitions/types.ts
    • Added lsp_query: FunctionDeclaration; to the CoreToolSet interface.
  • packages/core/src/tools/lsp-tool.test.ts
    • Added unit tests for LspTool, verifying its parameter schema, successful execution for documentSymbols on a TypeScript file, and proactive error hints for missing language servers.
  • packages/core/src/tools/lsp-tool.ts
    • Added a new file implementing LspTool, which serves as the bridge between the LLM interface and the LSP core logic, handling tool invocation, path validation, and routing LSP operations.
  • packages/core/src/tools/tool-names.ts
    • Exported LSP_TOOL_NAME and its parameters.
    • Added LSP_TOOL_NAME to ALL_BUILTIN_TOOL_NAMES and PLAN_MODE_TOOLS.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant and valuable feature: the lsp_query tool for semantic code analysis. The implementation is well-structured, separating the low-level LSP client, the server manager, and the tool logic into distinct, well-defined modules. The addition of documentation and comprehensive tests is commendable. My review includes one high-severity comment regarding error handling in the LspServerManager to improve robustness and provide clearer feedback to the user in case of environment configuration issues. This comment aligns with general good practices and does not contradict any established rules. Overall, this is a solid contribution that greatly enhances the agent's code understanding capabilities.

Note: Security Review did not run due to the size of the PR.

Comment on lines +507 to +509
if (checkResult.status !== 0) {
throw new Error(`[LSP Configuration Error]: ${config.installHint}`);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current check for the language server binary is not fully robust. If spawnSync fails to execute checkCmd (e.g., which is not in the user's PATH), checkResult.error will be set and checkResult.status will be null. While null !== 0 is true, the error thrown gives the user a misleading hint. It suggests installing the language server, when the actual problem is that the verification command (which or where) is missing.

A more robust check would differentiate between these two failure modes to provide a more accurate error message to the user.

    if (checkResult.error || checkResult.status !== 0) {
      const errorMessage = checkResult.error
        ? `Failed to check for language server binary. Please ensure '${checkCmd}' is in your system's PATH.`
        : `[LSP Configuration Error]: ${config.installHint}`;
      throw new Error(errorMessage);
    }

@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Mar 16, 2026
@GitmanIII GitmanIII force-pushed the fix/lsp-tool-parameters-and-docs branch from f1e67a8 to e7c63b2 Compare March 16, 2026 20:39
@GitmanIII GitmanIII marked this pull request as draft March 16, 2026 21:32
Address PR feedback from Gemini-code-assist by differentiating between a missing language server and a missing check command (which/where). Added a unit test to verify this failure mode.
…ess cleanup

This update addresses several production-critical issues:
1. Implements RFC-compliant file URIs using pathToFileURL for Windows compatibility.
2. Adds a mapping from file extensions to standard LSP language identifiers.
3. Exports and integrates shutdownLspServers() into the Config disposal sequence to prevent zombie processes.
4. Enhances unit tests to verify the cleanup logic.
@GitmanIII GitmanIII force-pushed the fix/lsp-tool-parameters-and-docs branch from e7c63b2 to d17f90d Compare March 16, 2026 21:36
@gemini-cli gemini-cli bot removed the status/need-issue Pull requests that need to have an associated issue. label Mar 16, 2026
@GitmanIII GitmanIII closed this Mar 16, 2026
@GitmanIII GitmanIII reopened this Mar 16, 2026
@GitmanIII GitmanIII closed this Mar 16, 2026
@GitmanIII GitmanIII deleted the fix/lsp-tool-parameters-and-docs branch March 16, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant