Skip to content

[FEATURE]: Expose LSP diagnostics as a tool and auto-surface them on read #16569

Closed as not planned

Description

@mmsaki

Description

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Feature Request: Expose LSP Diagnostics in the Tool Surface

Summary

Please add first-class LSP diagnostics support to the chat tool surface and auto-surface current-file diagnostics from read when an LSP client exists for that file type.

Problem

OpenCode already receives and stores diagnostics internally via textDocument/publishDiagnostics, but the chat-facing lsp tool does not expose them. As a result, when users ask simple questions like:

  • what warnings are in this file?
  • does this file have diagnostics?
  • read path/to/file

…the agent often falls back to running compiler commands, shell commands, or full build steps instead of using the language server directly.

The Problem: diagnostics are available internally, but not available as a first-class tool capability in the agent surface.

Impact: Without direct LSP diagnostics support, the agent cannot reliably:

  • answer file-warning questions cheaply
  • prefer language-server diagnostics over heavyweight compiler or build invocations
  • surface current file errors/warnings during normal file reads
  • avoid noisy project-wide output for simple file-level questions

This is especially noticeable in projects where users often just want the warnings for one file.

Example

Without LSP diagnostics surfaced (what the agent does today)

User: does this file have any warnings?
Agent: runs a compiler or build command
Agent: scans project-wide output for mentions of the file

With LSP diagnostics surfaced (what should happen)

User: does path/to/file have any warnings?
Agent: uses LSP diagnostics for path/to/file
Agent: returns:
- no-unused-vars: "x" is declared but its value is never read
- missing-return-type: Function is missing an explicit return type

Read flow example

User: read path/to/file
Tool output:
<path>/repo/path/to/file</path>
<type>file</type>
<content>
1: const x = 1
(End of file - total 1 lines)
</content>

<diagnostics file="/repo/path/to/file">
WARN [1:7] "x" is declared but its value is never read
WARN [1:1] Function is missing an explicit return type
</diagnostics>

Use Cases

  1. File-Level Warning Checks: Users ask if a specific file has warnings/errors and expect a quick answer without running a project-wide build.

  2. Better Tool Selection: The agent should prefer LSP diagnostics when available instead of reaching for compiler commands, shell commands, or full build steps just to get file errors.

  3. Diagnostics While Reading Code: read path/to/file should optionally surface current warnings/errors if an LSP server exists for that file type.

  4. Lower Noise, Faster Feedback: File-level diagnostics are more targeted than whole-project build output and are usually the right default for conversational queries.

Proposed Implementation

Files likely touched

  • packages/opencode/src/tool/lsp.ts
  • packages/opencode/src/tool/lsp.txt
  • packages/opencode/src/tool/read.ts
  • packages/opencode/src/tool/read.txt

1. Add a diagnostics LSP tool operation

Expose a first-class file-based diagnostics operation from the existing LSP tool.

Expected behavior:

  • operation: "diagnostics"
  • requires filePath
  • does not require line or character
  • touches the file / waits for diagnostics
  • returns diagnostics only for that file

Example request:

{
  "operation": "diagnostics",
  "filePath": "path/to/file"
}

Example implementation sketch in packages/opencode/src/tool/lsp.ts:

const operations = [
  "goToDefinition",
  "findReferences",
  "diagnostics",
  "hover",
  "documentSymbol",
  "workspaceSymbol",
  "goToImplementation",
  "prepareCallHierarchy",
  "incomingCalls",
  "outgoingCalls",
] as const

const positionOperations = [
  "goToDefinition",
  "findReferences",
  "hover",
  "goToImplementation",
  "prepareCallHierarchy",
  "incomingCalls",
  "outgoingCalls",
] as const

parameters: z.object({
  operation: z.enum(operations),
  filePath: z.string(),
  line: z.number().int().min(1).optional(),
  character: z.number().int().min(1).optional(),
}).superRefine((args, ctx) => {
  if (positionOperations.includes(args.operation)) {
    if (args.line === undefined) ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["line"], message: "line is required" })
    if (args.character === undefined) ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["character"], message: "character is required" })
  }
})

case "diagnostics": {
  await LSP.touchFile(file, true)
  const diagnostics = await LSP.diagnostics()
  return diagnostics[Filesystem.normalizePath(file)] ?? []
}

Example result:

[
  {
    "code": "no-unused-vars",
    "message": "\"x\" is declared but its value is never read.",
    "severity": 2,
    "source": "language-server"
  },
  {
    "code": "missing-return-type",
    "message": "Function is missing an explicit return type.",
    "severity": 2,
    "source": "language-server"
  }
]

2. Auto-surface diagnostics from read

When read is called on a file and LSP.hasClients(file) is true:

  • touch the file
  • wait for diagnostics
  • attach warning/error diagnostics for that file to the tool output and metadata

Example implementation sketch in packages/opencode/src/tool/read.ts:

async function getFileDiagnostics(filepath: string) {
  const hasClients = await LSP.hasClients(filepath).catch(() => false)
  if (!hasClients) return []

  await LSP.touchFile(filepath, true).catch(() => {})
  const diagnostics = await LSP.diagnostics().catch(() => ({}))
  const issues = diagnostics[Filesystem.normalizePath(filepath)] ?? []
  return issues.filter((item) => item.severity === 1 || item.severity === 2)
}

Then append to read output:

if (diagnostics.length > 0) {
  output += `\n\n<diagnostics file="${filepath}">\n${diagnostics.map(LSP.Diagnostic.pretty).join("\n")}\n</diagnostics>`
}

Related issues

I checked current open issues and did not find another open feature request for exposing diagnostics as a first-class chat/tool capability plus auto-surfacing them from read.

Related but distinct issues:

Those appear to focus on reliability, bugs, configuration, or other LSP feature exposure rather than this specific request to expose file diagnostics in the built-in chat tool surface and auto-surface them during reads.

Notes

OpenCode already has most of the plumbing:

  • diagnostics are collected internally from textDocument/publishDiagnostics
  • LSP.diagnostics() already aggregates them
  • opencode debug lsp diagnostics <file> can already print them

So the missing piece is mainly exposing diagnostics through the actual tool schema/API used by the chat agent and surfacing them automatically during normal file reads.

Example user flows

  • read path/to/file -> file contents plus current diagnostics
  • use lsp diagnostics on path/to/file -> explicit file diagnostics without fake positional arguments
  • does this file have any warnings? -> agent uses LSP diagnostics instead of compiler/build output

Priority

Medium-High

This would reduce unnecessary compiler invocations and make OpenCode's LSP support much more useful for normal coding conversations.

Additional Context

This feature is useful across languages and ecosystems because users often ask for warnings in one file, while compiler or build output is broader, noisier, and slower than the language server path.


Tags: lsp, diagnostics, enhancement, tooling

Activity

  1. added
    core
    on Mar 8, 2026
  2. changed the title [-]Expose LSP diagnostics as a tool and auto-surface them on read[/-] [+][FEATURE]: Expose LSP diagnostics as a tool and auto-surface them on read[/+] on Mar 8, 2026
  3. added
    needs:compliance
    and removed
    needs:compliance
    on Mar 8, 2026
  4. github-actions commented on Mar 8, 2026

    @github-actions
    Contributor

    Thanks for updating the issue! It now meets our contributing guidelines — the title has been corrected to start with [FEATURE]: and the verification checkbox is present. The issue looks good to go.

  5. assigned and unassigned on Apr 26, 2026
  6. removed
    core
    on May 3, 2026
  7. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions