Debian Codegraph @

gopls/internal/mcp/outline.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 "strconv"
11
12 "github.com/modelcontextprotocol/go-sdk/mcp"
13 "golang.org/x/tools/gopls/internal/cache/metadata"
14 )
15
16 type outlineParams struct {
17 PackagePaths []string `json:"packagePaths" jsonschema:"the go package paths to describe"`
18 }
19
20 func (h *handler) outlineHandler(ctx context.Context, req *mcp.CallToolRequest, params outlineParams) (*mcp.CallToolResult, any, error) {
21 countGoPackageAPIMCP.Inc()
22 snapshot, release, err := h.snapshot()
23 if err != nil {
24 return nil, nil, err
25 }
26 defer release()
27
28 // Await initialization to ensure we've at least got an initial package graph
29 md, err := snapshot.LoadMetadataGraph(ctx)
30 if err != nil {
31 return nil, nil, err
32 }
33 var toSummarize []*metadata.Package
34 for _, imp := range params.PackagePaths {
35 pkgPath := metadata.PackagePath(imp)
36 if len(imp) > 0 && imp[0] == '"' {
37 unquoted, err := strconv.Unquote(imp)
38 if err != nil {
39 return nil, nil, fmt.Errorf("failed to unquote %s: %v", imp, err)
40 }
41 pkgPath = metadata.PackagePath(unquoted)
42 }
43 if mps := md.ForPackagePath[pkgPath]; len(mps) > 0 {
44 toSummarize = append(toSummarize, mps[0]) // first is best
45 }
46 }
47
48 var content []mcp.Content
49 for _, mp := range toSummarize {
50 if md == nil {
51 continue // ignore error
52 }
53 if summary := summarizePackage(ctx, snapshot, mp); summary != "" {
54 content = append(content, &mcp.TextContent{Text: summary})
55 }
56 }
57 return &mcp.CallToolResult{
58 Content: content,
59 }, nil, nil
60 }