Skip to content

Commit de84e4d

Browse files
committed
feat(mcp): add get_godoc tool to retrieve Go documentation using 'go doc -all'
1 parent 5ea45c1 commit de84e4d

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Lux is a tool for managing a knowledge repository (.md files) and providing it t
2525
- **Knowledge Search (Tools)**:
2626
- `search_knowledge`: Searches for a list of knowledge items based on tag matching.
2727
- `get_knowledge_content`: Retrieves the detailed content of a specific knowledge item by its ID.
28+
- `get_godoc`: Retrieves Go documentation for a specific package, type, or function using `go doc`.
2829
- **Embedded Index**: The generated search index is embedded into the binary for easy distribution and deployment.
2930

3031
## Getting Started

pkg/mcp/godoc.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package mcp
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os/exec"
7+
"strings"
8+
9+
"github.com/modelcontextprotocol/go-sdk/mcp"
10+
)
11+
12+
// GoDocInput is the input schema for go doc lookup.
13+
type GoDocInput struct {
14+
Target string `json:"target" jsonschema:"the package, function, or type to look up. e.g. 'net/http', 'net/http.Client', 'fmt.Printf'"`
15+
}
16+
17+
// GoDocOutput is the output schema for go doc lookup.
18+
type GoDocOutput struct {
19+
Content string `json:"content" jsonschema:"the documentation result from go doc"`
20+
}
21+
22+
// GetGoDocTool provides documentation results by running 'go doc'.
23+
func GetGoDocTool(ctx context.Context, req *mcp.CallToolRequest, input GoDocInput) (
24+
*mcp.CallToolResult,
25+
GoDocOutput,
26+
error,
27+
) {
28+
// target이 비어있으면 에러 반환
29+
if strings.TrimSpace(input.Target) == "" {
30+
return &mcp.CallToolResult{
31+
IsError: true,
32+
Content: []mcp.Content{
33+
&mcp.TextContent{
34+
Text: "Target is required.",
35+
},
36+
},
37+
}, GoDocOutput{}, nil
38+
}
39+
40+
// go doc -all <target> 실행
41+
cmd := exec.CommandContext(ctx, "go", "doc", "-all", input.Target)
42+
out, err := cmd.CombinedOutput()
43+
if err != nil {
44+
return &mcp.CallToolResult{
45+
IsError: true,
46+
Content: []mcp.Content{
47+
&mcp.TextContent{
48+
Text: fmt.Sprintf("go doc failed: %s\nOutput: %s", err, string(out)),
49+
},
50+
},
51+
}, GoDocOutput{}, nil
52+
}
53+
54+
return nil, GoDocOutput{
55+
Content: string(out),
56+
}, nil
57+
}

pkg/mcp/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,10 @@ func NewServer() *mcp.Server {
104104
Description: "Get the full content and tags of a knowledge item by its ID.",
105105
}, GetKnowledgeContentTool)
106106

107+
mcp.AddTool(s, &mcp.Tool{
108+
Name: "get_godoc",
109+
Description: "Retrieve Go documentation for a specific package, type, or function using 'go doc -all'.",
110+
}, GetGoDocTool)
111+
107112
return s
108113
}

0 commit comments

Comments
 (0)