-
Notifications
You must be signed in to change notification settings - Fork 702
ci: add golangci-lint #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: golangci-lint | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| golangci: | ||
| name: lint | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: stable | ||
| - name: golangci-lint | ||
| uses: golangci/golangci-lint-action@v8 | ||
| with: | ||
| version: v2.1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| version: "2" | ||
| linters: | ||
| exclusions: | ||
| presets: | ||
| - std-error-handling |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,7 +125,7 @@ func NewMCPServer() *MCPServer { | |
| func (s *MCPServer) ServeSSE(addr string) *server.SSEServer { | ||
| return server.NewSSEServer(s.server, | ||
| server.WithBaseURL(fmt.Sprintf("http://%s", addr)), | ||
| server.WithSSEContextFunc(authFromRequest), | ||
| server.WithHTTPContextFunc(authFromRequest), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deprecated, examples should always use the recommended way. |
||
| ) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,7 +185,7 @@ func WithSSEEndpoint(endpoint string) SSEOption { | |
| // WithSSEContextFunc sets a function that will be called to customise the context | ||
| // to the server using the incoming request. | ||
| // | ||
| // Deprecated: Use WithContextFunc instead. This will be removed in a future version. | ||
| // Deprecated: Use WithHTTPContextFunc instead. This will be removed in a future version. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ref: #253 |
||
| // | ||
| //go:deprecated | ||
| func WithSSEContextFunc(fn SSEContextFunc) SSEOption { | ||
|
|
@@ -297,7 +297,11 @@ func (s *SSEServer) handleSSE(w http.ResponseWriter, r *http.Request) { | |
| defer s.sessions.Delete(sessionID) | ||
|
|
||
| if err := s.server.RegisterSession(r.Context(), session); err != nil { | ||
| http.Error(w, fmt.Sprintf("Session registration failed: %v", err), http.StatusInternalServerError) | ||
| http.Error( | ||
| w, | ||
| fmt.Sprintf("Session registration failed: %v", err), | ||
| http.StatusInternalServerError, | ||
| ) | ||
| return | ||
| } | ||
| defer s.server.UnregisterSession(r.Context(), sessionID) | ||
|
|
@@ -480,7 +484,14 @@ func (s *SSEServer) writeJSONRPCError( | |
| response := createErrorResponse(id, code, message) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| json.NewEncoder(w).Encode(response) | ||
| if err := json.NewEncoder(w).Encode(response); err != nil { | ||
| http.Error( | ||
| w, | ||
| fmt.Sprintf("Failed to encode response: %v", err), | ||
| http.StatusInternalServerError, | ||
| ) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // SendEventToSession sends an event to a specific SSE session identified by sessionID. | ||
|
|
@@ -621,7 +632,11 @@ func (s *SSEServer) MessageHandler() http.Handler { | |
| // ServeHTTP implements the http.Handler interface. | ||
| func (s *SSEServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| if s.dynamicBasePathFunc != nil { | ||
| http.Error(w, (&ErrDynamicPathConfig{Method: "ServeHTTP"}).Error(), http.StatusInternalServerError) | ||
| http.Error( | ||
| w, | ||
| (&ErrDynamicPathConfig{Method: "ServeHTTP"}).Error(), | ||
| http.StatusInternalServerError, | ||
| ) | ||
| return | ||
| } | ||
| path := r.URL.Path | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ func TestSSEServer(t *testing.T) { | |
|
|
||
| if sseServer == nil { | ||
| t.Error("SSEServer should not be nil") | ||
| return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added critical early return to prevent nil dereference. This change adds a necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks to the linter in the CI, we won't miss such issues in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pottekkat Absolutely! Integrating golangci-lint into your CI pipeline is a great move that will help catch these kinds of issues automatically before they make it into the codebase. Early detection of potential nil dereferences and similar bugs will save debugging time and improve code quality. Looking forward to seeing the linter in action across the project! If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
||
| } | ||
| if sseServer.server == nil { | ||
| t.Error("MCPServer should not be nil") | ||
|
|
@@ -1247,7 +1248,7 @@ func TestSSEServer(t *testing.T) { | |
| WithHooks(&Hooks{ | ||
| OnAfterInitialize: []OnAfterInitializeFunc{ | ||
| func(ctx context.Context, id any, message *mcp.InitializeRequest, result *mcp.InitializeResult) { | ||
| result.Result.Meta = map[string]any{"invalid": func() {}} // marshal will fail | ||
| result.Meta = map[string]any{"invalid": func() {}} // marshal will fail | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed test marshal error simulation. The assignment now correctly sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pottekkat Thanks for confirming! The fix ensures that your test correctly simulates a JSON marshal error by directly setting |
||
| }, | ||
| }, | ||
| }), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.