Skip to content

Commit 7bb1fd2

Browse files
authored
ci: add golangci-lint (#282)
1 parent 0aa3bbd commit 7bb1fd2

File tree

10 files changed

+74
-16
lines changed

10 files changed

+74
-16
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
golangci:
13+
name: lint
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-go@v5
18+
with:
19+
go-version: stable
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v8
22+
with:
23+
version: v2.1

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
linters:
3+
exclusions:
4+
presets:
5+
- std-error-handling
6+

client/transport/streamable_http_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ func startMockStreamableHTTPServer() (string, func()) {
4646
w.Header().Set("Mcp-Session-Id", sessionID)
4747
w.Header().Set("Content-Type", "application/json")
4848
w.WriteHeader(http.StatusAccepted)
49-
json.NewEncoder(w).Encode(map[string]any{
49+
if err := json.NewEncoder(w).Encode(map[string]any{
5050
"jsonrpc": "2.0",
5151
"id": request["id"],
5252
"result": "initialized",
53-
})
53+
}); err != nil {
54+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
55+
return
56+
}
5457

5558
case "debug/echo":
5659
// Check session ID
@@ -62,11 +65,14 @@ func startMockStreamableHTTPServer() (string, func()) {
6265
// Echo back the request as the response result
6366
w.Header().Set("Content-Type", "application/json")
6467
w.WriteHeader(http.StatusOK)
65-
json.NewEncoder(w).Encode(map[string]any{
68+
if err := json.NewEncoder(w).Encode(map[string]any{
6669
"jsonrpc": "2.0",
6770
"id": request["id"],
6871
"result": request,
69-
})
72+
}); err != nil {
73+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
74+
return
75+
}
7076

7177
case "debug/echo_notification":
7278
// Check session ID
@@ -104,14 +110,17 @@ func startMockStreamableHTTPServer() (string, func()) {
104110
w.Header().Set("Content-Type", "application/json")
105111
w.WriteHeader(http.StatusOK)
106112
data, _ := json.Marshal(request)
107-
json.NewEncoder(w).Encode(map[string]any{
113+
if err := json.NewEncoder(w).Encode(map[string]any{
108114
"jsonrpc": "2.0",
109115
"id": request["id"],
110116
"error": map[string]any{
111117
"code": -1,
112118
"message": string(data),
113119
},
114-
})
120+
}); err != nil {
121+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
122+
return
123+
}
115124
}
116125
})
117126

examples/custom_context/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NewMCPServer() *MCPServer {
125125
func (s *MCPServer) ServeSSE(addr string) *server.SSEServer {
126126
return server.NewSSEServer(s.server,
127127
server.WithBaseURL(fmt.Sprintf("http://%s", addr)),
128-
server.WithSSEContextFunc(authFromRequest),
128+
server.WithHTTPContextFunc(authFromRequest),
129129
)
130130
}
131131

examples/everything/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func handleLongRunningOperationTool(
392392
for i := 1; i < int(steps)+1; i++ {
393393
time.Sleep(time.Duration(stepDuration * float64(time.Second)))
394394
if progressToken != nil {
395-
server.SendNotificationToClient(
395+
err := server.SendNotificationToClient(
396396
ctx,
397397
"notifications/progress",
398398
map[string]any{
@@ -402,6 +402,9 @@ func handleLongRunningOperationTool(
402402
"message": fmt.Sprintf("Server progress %v%%", int(float64(i)*100/steps)),
403403
},
404404
)
405+
if err != nil {
406+
return nil, fmt.Errorf("failed to send notification: %w", err)
407+
}
405408
}
406409
}
407410

mcp/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type URITemplate struct {
6767
}
6868

6969
func (t *URITemplate) MarshalJSON() ([]byte, error) {
70-
return json.Marshal(t.Template.Raw())
70+
return json.Marshal(t.Raw())
7171
}
7272

7373
func (t *URITemplate) UnmarshalJSON(data []byte) error {

server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func TestMCPServer_Tools(t *testing.T) {
368368
"id": 1,
369369
"method": "tools/list"
370370
}`))
371-
tt.validate(t, notifications, toolsList.(mcp.JSONRPCMessage))
371+
tt.validate(t, notifications, toolsList)
372372
})
373373
}
374374
}

server/session_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ func TestMCPServer_NotificationChannelBlocked(t *testing.T) {
573573
require.NoError(t, err)
574574

575575
// Fill the buffer first to ensure it gets blocked
576-
server.SendNotificationToSpecificClient(session.SessionID(), "first-message", nil)
576+
err = server.SendNotificationToSpecificClient(session.SessionID(), "first-message", nil)
577+
require.NoError(t, err)
577578

578579
// This will cause the buffer to block
579580
err = server.SendNotificationToSpecificClient(session.SessionID(), "blocked-message", nil)

server/sse.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func WithSSEEndpoint(endpoint string) SSEOption {
185185
// WithSSEContextFunc sets a function that will be called to customise the context
186186
// to the server using the incoming request.
187187
//
188-
// Deprecated: Use WithContextFunc instead. This will be removed in a future version.
188+
// Deprecated: Use WithHTTPContextFunc instead. This will be removed in a future version.
189189
//
190190
//go:deprecated
191191
func WithSSEContextFunc(fn SSEContextFunc) SSEOption {
@@ -297,7 +297,11 @@ func (s *SSEServer) handleSSE(w http.ResponseWriter, r *http.Request) {
297297
defer s.sessions.Delete(sessionID)
298298

299299
if err := s.server.RegisterSession(r.Context(), session); err != nil {
300-
http.Error(w, fmt.Sprintf("Session registration failed: %v", err), http.StatusInternalServerError)
300+
http.Error(
301+
w,
302+
fmt.Sprintf("Session registration failed: %v", err),
303+
http.StatusInternalServerError,
304+
)
301305
return
302306
}
303307
defer s.server.UnregisterSession(r.Context(), sessionID)
@@ -480,7 +484,14 @@ func (s *SSEServer) writeJSONRPCError(
480484
response := createErrorResponse(id, code, message)
481485
w.Header().Set("Content-Type", "application/json")
482486
w.WriteHeader(http.StatusBadRequest)
483-
json.NewEncoder(w).Encode(response)
487+
if err := json.NewEncoder(w).Encode(response); err != nil {
488+
http.Error(
489+
w,
490+
fmt.Sprintf("Failed to encode response: %v", err),
491+
http.StatusInternalServerError,
492+
)
493+
return
494+
}
484495
}
485496

486497
// SendEventToSession sends an event to a specific SSE session identified by sessionID.
@@ -621,7 +632,11 @@ func (s *SSEServer) MessageHandler() http.Handler {
621632
// ServeHTTP implements the http.Handler interface.
622633
func (s *SSEServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
623634
if s.dynamicBasePathFunc != nil {
624-
http.Error(w, (&ErrDynamicPathConfig{Method: "ServeHTTP"}).Error(), http.StatusInternalServerError)
635+
http.Error(
636+
w,
637+
(&ErrDynamicPathConfig{Method: "ServeHTTP"}).Error(),
638+
http.StatusInternalServerError,
639+
)
625640
return
626641
}
627642
path := r.URL.Path

server/sse_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestSSEServer(t *testing.T) {
2929

3030
if sseServer == nil {
3131
t.Error("SSEServer should not be nil")
32+
return
3233
}
3334
if sseServer.server == nil {
3435
t.Error("MCPServer should not be nil")
@@ -1247,7 +1248,7 @@ func TestSSEServer(t *testing.T) {
12471248
WithHooks(&Hooks{
12481249
OnAfterInitialize: []OnAfterInitializeFunc{
12491250
func(ctx context.Context, id any, message *mcp.InitializeRequest, result *mcp.InitializeResult) {
1250-
result.Result.Meta = map[string]any{"invalid": func() {}} // marshal will fail
1251+
result.Meta = map[string]any{"invalid": func() {}} // marshal will fail
12511252
},
12521253
},
12531254
}),

0 commit comments

Comments
 (0)