| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 |
) |
| 14 |
|
| 15 |
type fileMetadataParams struct { |
| 16 |
File string `json:"file" jsonschema:"the absolute path to the file to describe"` |
| 17 |
} |
| 18 |
|
| 19 |
func (h *handler) fileMetadataHandler(ctx context.Context, req *mcp.CallToolRequest, params fileMetadataParams) (*mcp.CallToolResult, any, error) { |
| 20 |
countGoFileMetadataMCP.Inc() |
| 21 |
fh, snapshot, release, err := h.fileOf(ctx, params.File) |
| 22 |
if err != nil { |
| 23 |
return nil, nil, err |
| 24 |
} |
| 25 |
defer release() |
| 26 |
|
| 27 |
md, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI()) |
| 28 |
if err != nil { |
| 29 |
return nil, nil, err |
| 30 |
} |
| 31 |
|
| 32 |
var b strings.Builder |
| 33 |
addf := func(format string, args ...any) { |
| 34 |
fmt.Fprintf(&b, format, args...) |
| 35 |
} |
| 36 |
addf("File `%s` is in package %q, which has the following files:\n", params.File, md.PkgPath) |
| 37 |
for _, f := range md.CompiledGoFiles { |
| 38 |
addf("\t%s\n", f.Path()) |
| 39 |
} |
| 40 |
return textResult(b.String()), nil, nil |
| 41 |
} |