|
| 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 | +} |
0 commit comments