Debian Codegraph @

gopls/internal/mcp/search.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package mcp
6
7 import (
8 "context"
9 "fmt"
10 "strings"
11
12 "github.com/modelcontextprotocol/go-sdk/mcp"
13 "golang.org/x/tools/gopls/internal/protocol"
14 )
15
16 type searchParams struct {
17 Query string `json:"query" jsonschema:"the fuzzy search query to use for matching symbols"`
18 }
19
20 func (h *handler) searchHandler(ctx context.Context, req *mcp.CallToolRequest, params searchParams) (*mcp.CallToolResult, any, error) {
21 countGoSearchMCP.Inc()
22 query := params.Query
23 if len(query) == 0 {
24 return nil, nil, fmt.Errorf("empty query")
25 }
26 syms, err := h.lspServer.Symbol(ctx, &protocol.WorkspaceSymbolParams{
27 Query: params.Query,
28 })
29 if err != nil {
30 return nil, nil, fmt.Errorf("failed to execute symbol query: %v", err)
31 }
32 if len(syms) == 0 {
33 return textResult("No symbols found."), nil, nil
34 }
35 var b strings.Builder
36 fmt.Fprintf(&b, "Top symbol matches:\n")
37 for _, sym := range syms {
38 fmt.Fprintf(&b, "\t%s (%s in `%s`)\n", sym.Name, kindName(sym.Kind), sym.Location.URI.Path())
39 }
40 return textResult(b.String()), nil, nil
41 }
42
43 // kindName returns the adjusted name for the given symbol kind,
44 // fixing LSP conventions that don't work for go, like 'Class'.
45 func kindName(k protocol.SymbolKind) string {
46 if k == protocol.Class {
47 return "Type"
48 }
49 return fmt.Sprint(k)
50 }