Skip to content

Commit 3982ada

Browse files
committed
search support imported By
1 parent 06dcc33 commit 3982ada

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,46 @@ just use your client to request. it servers on stdio
1818

1919
## Todo
2020

21+
- localCache
22+
- [ ] use a local cache to avoid search every time(From reddit user)
2123
- searchPackage
22-
- [ ] imported by how many packages
24+
- [x] imported by how many packages
2325
- getPackageInfo
2426
- [ ] get examples
2527
- release
2628
- [x] use github actions to release for multiple platforms
2729

30+
## Develop Experience
31+
The description is very important, when describing the param should tell the client how
32+
to use one tool's output and change the format to match another tool's input.
33+
34+
For example, the output of `searchPackage` contains subpackage's name, but do not contain
35+
the package's name. So if want the llm to use `getPackageInfo` to get the subpackage's info,
36+
I wrote the description for param `packageName` about how to use it to work with `searchPackage`:
37+
38+
> package name for search. if use searchPackages before, and user want to get the
39+
subpackage info. you should plus them for example, when user query mcp, and it return
40+
packageName: github.com/mark3labs/mcp-go/mcp and subpackage client, then if user want
41+
to get the client package info, you should set the packageName to
42+
github.com/mark3labs/mcp-go/mcp/client rather than client
43+
44+
45+
When using this detail description, the llm will tell you some subpackage's name after
46+
search. You can just tell it which package you want to get info, or which package and the
47+
subpackage's name, it will combine them and call `getPackageInfo` to get the info.
48+
49+
50+
So maybe we should make the description configurable by user, to make the tool more useful
51+
and efficiency.
52+
2853
## Library Usage
2954

30-
The exported Go API of this module should currently be considered unstable, and subject to breaking changes. In the future, we may offer stability; please file an issue if there is a use case where this would be valuable.
55+
The exported Go API of this module should currently be considered unstable, and subject to
56+
breaking changes. In the future, we may offer stability; please file an issue if there is
57+
a use case where this would be valuable.
3158

3259

3360
## License
3461

35-
This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE) for the full terms.
62+
This project is licensed under the terms of the MIT open source license. Please refer
63+
to [MIT](./LICENSE) for the full terms.

cmd/godoc-mcp-server/search.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212

1313
func getSearchTool() (tool mcp.Tool, handler server.ToolHandlerFunc) {
1414
return mcp.NewTool("searchPackages",
15-
mcp.WithDescription("provide a query, search related golang packages from pkg.go.dev"),
15+
mcp.WithDescription("provide a query, search related golang packages from pkg.go.dev include "+
16+
"name, path, synopsis, go doc url, imported by how many packages, subpackages in this package"),
1617
mcp.WithString("q",
1718
mcp.Required(),
1819
mcp.Description("query")),

pkg/godoc/search.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godoc
22

33
import (
4+
"strconv"
45
"strings"
56

67
"github.com/PuerkitoBio/goquery"
@@ -14,11 +15,12 @@ type SearchResult struct {
1415
}
1516

1617
type SearchPackageInfo struct {
17-
Name string
18-
Path string
19-
Synopsis string
20-
GoDocUrl string
21-
OtherPackages []string `json:"other_packages_in_this_module,omitempty"`
18+
Name string
19+
Path string
20+
Synopsis string
21+
GoDocUrl string
22+
ImportedBy int
23+
SubPackages []string `json:"sub_packages,omitempty"`
2224
}
2325

2426
func Search(query string) (*SearchResult, error) {
@@ -75,17 +77,23 @@ func extractPackageInfo(selection *goquery.Selection) (*SearchPackageInfo, error
7577
if err != nil {
7678
return nil, err
7779
}
80+
imptBy, err := extractImportedBy(selection)
81+
if err != nil {
82+
return nil, err
83+
}
84+
7885
otherPackages, err := extractOtherPackages(selection)
7986
if err != nil {
8087
return nil, err
8188
}
8289

8390
return &SearchPackageInfo{
84-
Name: name,
85-
Path: path,
86-
Synopsis: synopsis,
87-
GoDocUrl: baseURL() + url,
88-
OtherPackages: otherPackages,
91+
Name: name,
92+
Path: path,
93+
Synopsis: synopsis,
94+
GoDocUrl: baseURL() + url,
95+
SubPackages: otherPackages,
96+
ImportedBy: imptBy,
8997
}, nil
9098
}
9199

@@ -119,6 +127,17 @@ func extractPackageSynopsis(selection *goquery.Selection) (string, error) {
119127
return synopsis, nil
120128
}
121129

130+
func extractImportedBy(selection *goquery.Selection) (int, error) {
131+
132+
im := selection.
133+
Find("div.SearchSnippet-infoLabel").
134+
Find("a[aria-label='Go to Imported By']").
135+
Find("strong").Text()
136+
137+
atoi, _ := strconv.Atoi(im)
138+
return atoi, nil
139+
}
140+
122141
func extractPackageGoDocUrl(selection *goquery.Selection) (string, error) {
123142
var goDocUrl string
124143

0 commit comments

Comments
 (0)