Debian Codegraph @

gopls/internal/mcp/workspace.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 "bytes"
9 "context"
10 "fmt"
11 "io"
12 "strings"
13
14 "slices"
15
16 "github.com/modelcontextprotocol/go-sdk/mcp"
17 "golang.org/x/tools/gopls/internal/cache"
18 "golang.org/x/tools/gopls/internal/protocol"
19 "golang.org/x/tools/gopls/internal/util/immutable"
20 )
21
22 func (h *handler) workspaceHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
23 countGoWorkspaceMCP.Inc()
24 var summary bytes.Buffer
25 views := h.session.Views()
26 for _, v := range views {
27 snapshot, release, err := v.Snapshot()
28 if err != nil {
29 continue // view is shut down
30 }
31 defer release()
32
33 pkgs := snapshot.WorkspacePackages()
34
35 // Special case: check if it's likely that this isn't actually a Go workspace.
36 if len(views) == 1 && // only view
37 (v.Type() == cache.AdHocView || v.Type() == cache.GoPackagesDriverView) && // not necessarily Go code
38 pkgs.Len() == 0 { // no packages
39
40 return &mcp.CallToolResult{
41 Content: []mcp.Content{&mcp.TextContent{Text: "This is not a Go workspace. To work on Go code, open a directory inside a Go module."}},
42 }, nil, nil
43 }
44
45 dir := v.Root().Path()
46 switch v.Type() {
47 case cache.GoPackagesDriverView:
48 fmt.Fprintf(&summary, "The `%s` directory is loaded using a custom golang.org/x/tools/go/packages driver.\n", dir)
49 fmt.Fprintf(&summary, "This indicates a non-standard build system.\n")
50
51 case cache.GOPATHView:
52 fmt.Fprintf(&summary, "The `%s` directory is loaded using a the legacy GOPATH build system.\n", dir)
53
54 case cache.GoModView:
55 fmt.Fprintf(&summary, "The `%s` directory uses Go modules, with the following main modules:\n", dir)
56 summarizeModFiles(ctx, &summary, snapshot)
57
58 case cache.GoWorkView:
59 fmt.Fprintf(&summary, "The `%s` directory is in the go workspace defined by `%s`, with the following main modules:\n", dir, v.GoWork().Path())
60 summarizeModFiles(ctx, &summary, snapshot)
61
62 case cache.AdHocView:
63 fmt.Fprintf(&summary, "The `%s` directory is an ad-hoc Go package, not in a Go module.\n", dir)
64 }
65 fmt.Fprintln(&summary)
66 const summarizePackages = false
67 if summarizePackages {
68 summaries := packageSummaries(snapshot, pkgs)
69 fmt.Fprintf(&summary, "It contains the following Go packages:\n")
70 fmt.Fprintf(&summary, "\t%s\n", strings.Join(summaries, "\n\t"))
71 fmt.Fprintln(&summary)
72 }
73 }
74 return textResult(summary.String()), nil, nil
75 }
76
77 func summarizeModFiles(ctx context.Context, w io.Writer, snapshot *cache.Snapshot) {
78 v := snapshot.View()
79 for _, m := range v.ModFiles() {
80 if modPath, err := modulePath(ctx, snapshot, m); err != nil {
81 // Fall back on just the go.mod file.
82 fmt.Fprintf(w, "\t%s\n", m.Path())
83 } else {
84 fmt.Fprintf(w, "\t%s (module %s)\n", m.Path(), modPath)
85 }
86 }
87 }
88
89 func modulePath(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (string, error) {
90 fh, err := snapshot.ReadFile(ctx, uri)
91 if err != nil {
92 return "", fmt.Errorf("Reading %s: %v", uri, err)
93 }
94 pmf, err := snapshot.ParseMod(ctx, fh)
95 if err != nil {
96 return "", fmt.Errorf("parsing modfile: %v", err)
97 }
98 if pmf.File == nil || pmf.File.Module == nil {
99 return "", fmt.Errorf("malformed modfile")
100 }
101 return pmf.File.Module.Mod.Path, nil
102 }
103
104 func packageSummaries(snapshot *cache.Snapshot, pkgs immutable.Map[cache.PackageID, cache.PackagePath]) []string {
105 var summaries []string
106 for id := range pkgs.All() {
107 mp := snapshot.Metadata(id)
108 if len(mp.CompiledGoFiles) == 0 {
109 continue // For convenience, just skip uncompiled packages; we could do more if it matters.
110 }
111 dir := mp.CompiledGoFiles[0].DirPath()
112 summaries = append(summaries, fmt.Sprintf("The `%s` directory contains the %q package with path %q", dir, mp.Name, mp.PkgPath))
113 }
114 slices.Sort(summaries) // for stability
115 return summaries
116 }