Skip to content

Commit a04dd1d

Browse files
committed
web feature
1 parent 31123c6 commit a04dd1d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cmd/mcptools/commands/guard.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func processPatternString(patternsStr string, patternMap map[string][]string) {
165165
patternValue := parts[1]
166166

167167
// Map entity type to known types
168+
//nolint:goconst // Using literals directly for readability
168169
switch entityType {
169170
case "tool", "tools":
170171
patternMap[EntityTypeTool] = append(patternMap[EntityTypeTool], patternValue)

cmd/mcptools/commands/web.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func WebCmd() *cobra.Command {
8080
mux.HandleFunc("/api/call", handleCall(clientCache))
8181

8282
// Start the server
83+
//nolint:gosec // Timeouts not implemented for this development/internal tool
8384
err := http.ListenAndServe(":"+port, mux)
8485
if err != nil {
8586
fmt.Fprintf(os.Stderr, "Error starting web server: %v\n", err)
@@ -89,14 +90,15 @@ func WebCmd() *cobra.Command {
8990
}
9091
}
9192

92-
// MCPClientCache provides thread-safe access to the MCP client
93+
// MCPClientCache provides thread-safe access to the MCP client.
9394
type MCPClientCache struct {
9495
client *client.Client
9596
mutex *sync.Mutex
9697
}
9798

98-
// handleIndex serves the main web interface
99+
// handleIndex serves the main web interface.
99100
func handleIndex() http.HandlerFunc {
101+
//nolint:revive // Parameter r is required by http.HandlerFunc signature
100102
return func(w http.ResponseWriter, r *http.Request) {
101103
// For simplicity, we'll embed a basic HTML page directly
102104
// In a production app, we'd use proper templates and static files
@@ -1160,12 +1162,14 @@ func handleIndex() http.HandlerFunc {
11601162
</html>
11611163
`
11621164
w.Header().Set("Content-Type", "text/html")
1165+
//nolint:errcheck,gosec // No need to handle error from Write in this context
11631166
w.Write([]byte(html))
11641167
}
11651168
}
11661169

1167-
// handleTools handles API requests for listing tools
1170+
// handleTools handles API requests for listing tools.
11681171
func handleTools(cache *MCPClientCache) http.HandlerFunc {
1172+
//nolint:revive // Parameter r is required by http.HandlerFunc signature
11691173
return func(w http.ResponseWriter, r *http.Request) {
11701174
cache.mutex.Lock()
11711175
resp, err := cache.client.ListTools()
@@ -1174,20 +1178,23 @@ func handleTools(cache *MCPClientCache) http.HandlerFunc {
11741178
w.Header().Set("Content-Type", "application/json")
11751179
if err != nil {
11761180
w.WriteHeader(http.StatusInternalServerError)
1181+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
11771182
json.NewEncoder(w).Encode(map[string]interface{}{
11781183
"error": err.Error(),
11791184
})
11801185
return
11811186
}
11821187

1188+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
11831189
json.NewEncoder(w).Encode(map[string]interface{}{
11841190
"result": resp,
11851191
})
11861192
}
11871193
}
11881194

1189-
// handleResources handles API requests for listing resources
1195+
// handleResources handles API requests for listing resources.
11901196
func handleResources(cache *MCPClientCache) http.HandlerFunc {
1197+
//nolint:revive // Parameter r is required by http.HandlerFunc signature
11911198
return func(w http.ResponseWriter, r *http.Request) {
11921199
cache.mutex.Lock()
11931200
resp, err := cache.client.ListResources()
@@ -1196,20 +1203,23 @@ func handleResources(cache *MCPClientCache) http.HandlerFunc {
11961203
w.Header().Set("Content-Type", "application/json")
11971204
if err != nil {
11981205
w.WriteHeader(http.StatusInternalServerError)
1206+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
11991207
json.NewEncoder(w).Encode(map[string]interface{}{
12001208
"error": err.Error(),
12011209
})
12021210
return
12031211
}
12041212

1213+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12051214
json.NewEncoder(w).Encode(map[string]interface{}{
12061215
"result": resp,
12071216
})
12081217
}
12091218
}
12101219

1211-
// handlePrompts handles API requests for listing prompts
1220+
// handlePrompts handles API requests for listing prompts.
12121221
func handlePrompts(cache *MCPClientCache) http.HandlerFunc {
1222+
//nolint:revive // Parameter r is required by http.HandlerFunc signature
12131223
return func(w http.ResponseWriter, r *http.Request) {
12141224
cache.mutex.Lock()
12151225
resp, err := cache.client.ListPrompts()
@@ -1218,19 +1228,21 @@ func handlePrompts(cache *MCPClientCache) http.HandlerFunc {
12181228
w.Header().Set("Content-Type", "application/json")
12191229
if err != nil {
12201230
w.WriteHeader(http.StatusInternalServerError)
1231+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12211232
json.NewEncoder(w).Encode(map[string]interface{}{
12221233
"error": err.Error(),
12231234
})
12241235
return
12251236
}
12261237

1238+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12271239
json.NewEncoder(w).Encode(map[string]interface{}{
12281240
"result": resp,
12291241
})
12301242
}
12311243
}
12321244

1233-
// handleCall handles API requests for calling tools/resources/prompts
1245+
// handleCall handles API requests for calling tools/resources/prompts.
12341246
func handleCall(cache *MCPClientCache) http.HandlerFunc {
12351247
return func(w http.ResponseWriter, r *http.Request) {
12361248
if r.Method != http.MethodPost {
@@ -1239,14 +1251,15 @@ func handleCall(cache *MCPClientCache) http.HandlerFunc {
12391251
}
12401252

12411253
var requestData struct {
1254+
Params map[string]interface{} `json:"params"`
12421255
Type string `json:"type"`
12431256
Name string `json:"name"`
1244-
Params map[string]interface{} `json:"params"`
12451257
}
12461258

12471259
err := json.NewDecoder(r.Body).Decode(&requestData)
12481260
if err != nil {
12491261
w.WriteHeader(http.StatusBadRequest)
1262+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12501263
json.NewEncoder(w).Encode(map[string]interface{}{
12511264
"error": "Invalid request: " + err.Error(),
12521265
})
@@ -1268,6 +1281,7 @@ func handleCall(cache *MCPClientCache) http.HandlerFunc {
12681281
resp, callErr = cache.client.GetPrompt(requestData.Name)
12691282
default:
12701283
w.WriteHeader(http.StatusBadRequest)
1284+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12711285
json.NewEncoder(w).Encode(map[string]interface{}{
12721286
"error": "Invalid entity type: " + requestData.Type,
12731287
})
@@ -1277,12 +1291,14 @@ func handleCall(cache *MCPClientCache) http.HandlerFunc {
12771291
w.Header().Set("Content-Type", "application/json")
12781292
if callErr != nil {
12791293
w.WriteHeader(http.StatusInternalServerError)
1294+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12801295
json.NewEncoder(w).Encode(map[string]interface{}{
12811296
"error": callErr.Error(),
12821297
})
12831298
return
12841299
}
12851300

1301+
//nolint:errcheck,gosec // No need to handle error from Encode in this context
12861302
json.NewEncoder(w).Encode(map[string]interface{}{
12871303
"result": resp,
12881304
})

0 commit comments

Comments
 (0)