Repository navigation
Expand file tree
/
Copy pathtools.go
More file actions
179 lines (152 loc) · 5.09 KB
/
Copy pathtools.go
File metadata and controls
179 lines (152 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package mcp
import (
"context"
"strings"
"github.com/go-playground/validator/v10"
"github.com/ka2n/miru/api"
"github.com/ka2n/miru/api/source"
"github.com/ka2n/miru/api/sourceimpl"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/mitchellh/mapstructure"
)
var validate = validator.New()
func InitTools() []server.ServerTool {
tools := []server.ServerTool{}
tools = append(tools, newServerTool(SearchDocumentation()))
return tools
}
func SearchDocumentation() (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool(
"fetch_library_docs",
mcp.WithDescription("Fetch library documentation content and other links from repository or registry."),
mcp.WithString("package", mcp.Required(), mcp.Description(`Package name or repository path.
For example:
- For GitHub repository: "github.com/user/repo"
- For JavaScript: "express", along with "lang" parameter set to "npm"
`)),
mcp.WithString("lang", mcp.Description(`Language hint.
Supported languages include: go, js/typescript, rust, ruby, python, php, and more.
`)),
mcp.WithString("type_of_document", mcp.Description(`Documentation type. Defaults to "readme" when omitted.
Available document types:
- readme: Package README file
- documentation: Official documentation
- homepage: Package homepage
- registry: Package registry page
- repository: Source code repository
`)),
), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
type ToolArguments struct {
Package string `json:"package" validate:"required"`
Lang string `json:"lang" validate:"omitempty"`
DocType string `json:"type_of_document" validate:"omitempty"`
}
var args ToolArguments
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: nil,
Result: &args,
TagName: "json",
})
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if err := decoder.Decode(req.Params.Arguments); err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if err := validate.StructCtx(ctx, args); err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
initialQuery, err := api.NewInitialQuery(api.UserInput{
PackagePath: args.Package,
Language: args.Lang,
ForceUpdate: false,
})
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if initialQuery.SourceRef.Type == source.TypeUnknown {
return mcp.NewToolResultError("Unknown source type"), nil
}
investigation := api.NewInvestigation(initialQuery)
if err := investigation.Do(); err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
result := api.CreateResult(investigation)
docType := strings.ToLower(args.DocType)
// Handle different document types
switch docType {
case "", "readme":
return mcp.NewToolResultResource("README", mcp.TextResourceContents{
MIMEType: "text/markdown",
Text: result.README,
}), nil
case "documentation":
// Get documentation URL
docURL := result.GetDocumentation()
if docURL == nil {
return mcp.NewToolResultError("Documentation URL not found"), nil
}
// Fetch HTML content
html, err := sourceimpl.FetchHTML(docURL, false)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultResource("documentation", mcp.TextResourceContents{
URI: docURL.String(),
MIMEType: "text/markdown",
Text: html,
}), nil
case "homepage":
// Get homepage URL
homepageURL := result.GetHomepage()
if homepageURL == nil {
return mcp.NewToolResultError("Homepage URL not found"), nil
}
// Fetch HTML content
html, err := sourceimpl.FetchHTML(homepageURL, false)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultResource("homepage", mcp.TextResourceContents{
URI: homepageURL.String(),
MIMEType: "text/markdown",
Text: html,
}), nil
case "registry":
// Get registry URL
registryURL := result.GetRegistry()
if registryURL == nil {
return mcp.NewToolResultError("Registry URL not found"), nil
}
// Fetch HTML content
html, err := sourceimpl.FetchHTML(registryURL, false)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultResource("registry", mcp.TextResourceContents{
URI: registryURL.String(),
MIMEType: "text/markdown",
Text: html,
}), nil
case "repository":
// Get repository URL
repoURL := result.GetRepository()
if repoURL == nil {
return mcp.NewToolResultError("Repository URL not found"), nil
}
// Fetch HTML content
html, err := sourceimpl.FetchHTML(repoURL, false)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultResource("repository", mcp.TextResourceContents{
URI: repoURL.String(),
MIMEType: "text/markdown",
Text: html,
}), nil
default:
return mcp.NewToolResultError("Invalid document type: " + docType), nil
}
}
}