Skip to content

Commit 70f8331

Browse files
committed
replace github.com/mark3labs/mcp-go with github.com/modelcontextprotocol/go-sdk
1 parent 03535fa commit 70f8331

File tree

8 files changed

+116
-162
lines changed

8 files changed

+116
-162
lines changed

cmd/godoc-mcp-server/main.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
package main
22

33
import (
4-
"fmt"
5-
"os"
4+
"context"
5+
"log"
66

7-
"github.com/mark3labs/mcp-go/server"
8-
)
9-
10-
var (
11-
version = "1.0.0"
7+
"github.com/modelcontextprotocol/go-sdk/mcp"
128
)
139

1410
func main() {
15-
mcpServer := server.NewMCPServer(
16-
"godoc-mcp-server",
17-
version,
18-
)
19-
mcpServer.AddTool(getSearchTool())
20-
mcpServer.AddTool(getPackageInfoTool())
11+
s := initServer()
2112

22-
_, _ = fmt.Fprintf(os.Stderr, "godoc-mcp-server running on stdio...\n")
23-
err := server.ServeStdio(mcpServer)
13+
err := s.Run(context.Background(), mcp.NewStdioTransport())
2414
if err != nil {
25-
panic(err)
15+
log.Fatal("unknown err, will exit. err:", err)
2616
}
2717
}

cmd/godoc-mcp-server/packageInfo.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

cmd/godoc-mcp-server/search.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

cmd/godoc-mcp-server/server.go

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
package main
22

33
import (
4-
"fmt"
5-
6-
"github.com/mark3labs/mcp-go/mcp"
4+
"github.com/modelcontextprotocol/go-sdk/mcp"
5+
"github.com/yikakia/godoc-mcp-server/pkg/tool"
76
)
87

9-
// requiredParam is a helper function that can be used to fetch a requested parameter from the request.
10-
// It does the following checks:
11-
// 1. Checks if the parameter is present in the request.
12-
// 2. Checks if the parameter is of the expected type.
13-
// 3. Checks if the parameter is not empty, i.e: non-zero value
14-
func requiredParam[T comparable](r mcp.CallToolRequest, p string) (T, error) {
15-
var zero T
16-
17-
// Check if the parameter is present in the request
18-
if _, ok := r.Params.Arguments[p]; !ok {
19-
return zero, fmt.Errorf("missing required parameter: %s", p)
20-
}
21-
22-
// Check if the parameter is of the expected type
23-
if _, ok := r.Params.Arguments[p].(T); !ok {
24-
return zero, fmt.Errorf("parameter %s is not of type %T", p, zero)
25-
}
26-
27-
if r.Params.Arguments[p].(T) == zero {
28-
return zero, fmt.Errorf("missing required parameter: %s", p)
29-
30-
}
31-
32-
return r.Params.Arguments[p].(T), nil
33-
}
34-
35-
// OptionalParamOK is a helper function that can be used to fetch a requested parameter from the request.
36-
// It returns the value, a boolean indicating if the parameter was present, and an error if the type is wrong.
37-
func OptionalParamOK[T any](r mcp.CallToolRequest, p string) (value T, ok bool, err error) {
38-
// Check if the parameter is present in the request
39-
val, exists := r.Params.Arguments[p]
40-
if !exists {
41-
// Not present, return zero value, false, no error
42-
return
43-
}
44-
45-
// Check if the parameter is of the expected type
46-
value, ok = val.(T)
47-
if !ok {
48-
// Present but wrong type
49-
err = fmt.Errorf("parameter %s is not of type %T, is %T", p, value, val)
50-
ok = true // Set ok to true because the parameter *was* present, even if wrong type
51-
return
52-
}
8+
var (
9+
name = "godoc-mcp-server"
10+
version = "v1.0.0"
11+
)
5312

54-
// Present and correct type
55-
ok = true
56-
return
13+
func initServer() *mcp.Server {
14+
server := mcp.NewServer(&mcp.Implementation{
15+
Name: name,
16+
Version: version,
17+
}, nil)
18+
19+
mcp.AddTool(server, &mcp.Tool{
20+
Description: "provide a golang package name,get package consts,types,functions,variables," +
21+
"subpackages and how to use it",
22+
Name: "getPackageInfo",
23+
}, tool.GetPkgInfoTool())
24+
25+
mcp.AddTool(server, &mcp.Tool{
26+
Description: "provide a query, search related golang packages from pkg.go.dev include " +
27+
"name, path, synopsis, go doc url, imported by how many packages, subpackages in this package " +
28+
"the path is the package full name. if want to use getPackageInfo. llm should pass the path as " +
29+
"packageName to getPackageInfo",
30+
Name: "searchPackages",
31+
}, tool.GetSearchToolV2())
32+
33+
return server
5734
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/PuerkitoBio/goquery v1.10.2
77
github.com/coocood/freecache v1.2.4
88
github.com/go-resty/resty/v2 v2.16.5
9-
github.com/mark3labs/mcp-go v0.18.0
9+
github.com/modelcontextprotocol/go-sdk v0.2.0
1010
github.com/pkg/errors v0.9.1
1111
go.uber.org/multierr v1.11.0
1212
golang.org/x/net v0.39.0
@@ -15,6 +15,6 @@ require (
1515
require (
1616
github.com/andybalholm/cascadia v1.3.3 // indirect
1717
github.com/cespare/xxhash/v2 v2.1.2 // indirect
18-
github.com/google/uuid v1.6.0 // indirect
18+
github.com/stretchr/testify v1.9.0 // indirect
1919
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
2020
)

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1111
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
1212
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
1313
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
14-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
15-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
16-
github.com/mark3labs/mcp-go v0.18.0 h1:YuhgIVjNlTG2ZOwmrkORWyPTp0dz1opPEqvsPtySXao=
17-
github.com/mark3labs/mcp-go v0.18.0/go.mod h1:KmJndYv7GIgcPVwEKJjNcbhVQ+hJGJhrCCB/9xITzpE=
14+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
15+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
16+
github.com/modelcontextprotocol/go-sdk v0.2.0 h1:PESNYOmyM1c369tRkzXLY5hHrazj8x9CY1Xu0fLCryM=
17+
github.com/modelcontextprotocol/go-sdk v0.2.0/go.mod h1:0sL9zUKKs2FTTkeCCVnKqbLJTw5TScefPAzojjU459E=
1818
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1919
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2020
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -92,6 +92,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
9292
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
9393
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
9494
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
95+
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
96+
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
9597
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9698
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9799
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/tool/pkgInfoTool.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tool
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/modelcontextprotocol/go-sdk/mcp"
8+
"github.com/pkg/errors"
9+
"github.com/yikakia/godoc-mcp-server/pkg/godoc"
10+
)
11+
12+
type GetPkgInfoParams struct {
13+
// TODO add the description to the filed pkgName and needURL
14+
15+
// pkgName
16+
// package name for search. if use searchPackages before, and user want to get the subpackage info. you should plus
17+
// them for example, when user query mcp, and it return packageName: github.com/mark3labs/mcp-go/mcp and subpackage
18+
// client, then if user want to get the client package info, you should set the packageName to
19+
// github.com/mark3labs/mcp-go/mcp/client rather than client
20+
PkgName string `json:"pkgName" jsonschema:"the package name user search"`
21+
// default is false. if it`s true, will return the url of the definition of the package`s consts,types,functions,
22+
// variables,subpackages. only when user need it, set it
23+
NeedURL bool `json:"needURL" jsonschema:"if user need the link to the definition"`
24+
}
25+
26+
func GetPkgInfoTool() mcp.ToolHandlerFor[GetPkgInfoParams, any] {
27+
return func(ctx context.Context, session *mcp.ServerSession, c *mcp.CallToolParamsFor[GetPkgInfoParams]) (*mcp.CallToolResultFor[any], error) {
28+
pkgDoc, err := godoc.GetPackageDocument(godoc.GetPackageRequest{
29+
PackageName: c.Arguments.PkgName,
30+
NeedURL: c.Arguments.NeedURL,
31+
})
32+
if err != nil {
33+
return nil, errors.WithMessage(err, "get pkg info failed")
34+
}
35+
marshal, err := json.Marshal(pkgDoc)
36+
if err != nil {
37+
return nil, errors.Wrap(err, "marshal pkgDoc failed")
38+
}
39+
40+
return &mcp.CallToolResultFor[any]{
41+
Content: []mcp.Content{&mcp.TextContent{Text: string(marshal)}},
42+
}, nil
43+
}
44+
}

pkg/tool/searchTool.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tool
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/modelcontextprotocol/go-sdk/mcp"
8+
"github.com/pkg/errors"
9+
"github.com/yikakia/godoc-mcp-server/pkg/godoc"
10+
)
11+
12+
type searchParams struct {
13+
Q string `json:"q" jsonschema:"query string"`
14+
}
15+
16+
func GetSearchToolV2() mcp.ToolHandlerFor[searchParams, any] {
17+
return func(ctx context.Context, session *mcp.ServerSession, c *mcp.CallToolParamsFor[searchParams]) (*mcp.CallToolResultFor[any], error) {
18+
19+
search, err := godoc.Search(c.Arguments.Q)
20+
if err != nil {
21+
return nil, errors.WithMessage(err, "search failed.")
22+
}
23+
marshal, err := json.Marshal(search.Packages)
24+
if err != nil {
25+
return nil, errors.Wrap(err, "marshal pkgs failed.")
26+
}
27+
return &mcp.CallToolResultFor[any]{
28+
Content: []mcp.Content{&mcp.TextContent{Text: string(marshal)}},
29+
}, nil
30+
}
31+
}

0 commit comments

Comments
 (0)