| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/ast" |
| 11 |
"go/types" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 15 |
"golang.org/x/tools/gopls/internal/golang" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
) |
| 18 |
|
| 19 |
type fileContextParams struct { |
| 20 |
File string `json:"file" jsonschema:"the absolute path to the file"` |
| 21 |
} |
| 22 |
|
| 23 |
func (h *handler) fileContextHandler(ctx context.Context, req *mcp.CallToolRequest, params fileContextParams) (*mcp.CallToolResult, any, error) { |
| 24 |
countGoFileContextMCP.Inc() |
| 25 |
fh, snapshot, release, err := h.fileOf(ctx, params.File) |
| 26 |
if err != nil { |
| 27 |
return nil, nil, err |
| 28 |
} |
| 29 |
defer release() |
| 30 |
|
| 31 |
pkg, pgf, err := golang.NarrowestPackageForFile(ctx, snapshot, fh.URI()) |
| 32 |
if err != nil { |
| 33 |
return nil, nil, err |
| 34 |
} |
| 35 |
|
| 36 |
info := pkg.TypesInfo() |
| 37 |
if info == nil { |
| 38 |
return nil, nil, fmt.Errorf("no types info for package %q", pkg.Metadata().PkgPath) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
otherFiles := make(map[protocol.DocumentURI]map[string]bool) |
| 43 |
addObj := func(obj types.Object) { |
| 44 |
if obj == nil { |
| 45 |
return |
| 46 |
} |
| 47 |
pos := obj.Pos() |
| 48 |
if !pos.IsValid() { |
| 49 |
return |
| 50 |
} |
| 51 |
objFile := pkg.FileSet().File(pos) |
| 52 |
if objFile == nil { |
| 53 |
return |
| 54 |
} |
| 55 |
uri := protocol.URIFromPath(objFile.Name()) |
| 56 |
if uri == fh.URI() { |
| 57 |
return |
| 58 |
} |
| 59 |
if _, ok := otherFiles[uri]; !ok { |
| 60 |
otherFiles[uri] = make(map[string]bool) |
| 61 |
} |
| 62 |
otherFiles[uri][obj.Name()] = true |
| 63 |
} |
| 64 |
|
| 65 |
for cur := range pgf.Cursor().Preorder((*ast.Ident)(nil)) { |
| 66 |
id := cur.Node().(*ast.Ident) |
| 67 |
addObj(info.Uses[id]) |
| 68 |
addObj(info.Defs[id]) |
| 69 |
} |
| 70 |
|
| 71 |
var result strings.Builder |
| 72 |
fmt.Fprintf(&result, "File `%s` is in package %q.\n", params.File, pkg.Metadata().PkgPath) |
| 73 |
fmt.Fprintf(&result, "Below is a summary of the APIs it uses from other files.\n") |
| 74 |
fmt.Fprintf(&result, "To read the full API of any package, use go_package_api.\n") |
| 75 |
for uri, decls := range otherFiles { |
| 76 |
pkgPath := "UNKNOWN" |
| 77 |
md, err := snapshot.NarrowestMetadataForFile(ctx, uri) |
| 78 |
if err != nil { |
| 79 |
if ctx.Err() != nil { |
| 80 |
return nil, nil, ctx.Err() |
| 81 |
} |
| 82 |
} else { |
| 83 |
pkgPath = string(md.PkgPath) |
| 84 |
} |
| 85 |
fmt.Fprintf(&result, "Referenced declarations from %s (package %q):\n", uri.Path(), pkgPath) |
| 86 |
result.WriteString("```go\n") |
| 87 |
if err := writeFileSummary(ctx, snapshot, uri, &result, false, decls); err != nil { |
| 88 |
return nil, nil, err |
| 89 |
} |
| 90 |
result.WriteString("```\n\n") |
| 91 |
} |
| 92 |
|
| 93 |
return textResult(result.String()), nil, nil |
| 94 |
} |