Skip to content

Commit cdaa48b

Browse files
authored
Handle requests with initialize method (#49)
1 parent 021b02d commit cdaa48b

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

mcp/server.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ func (s *Server) HandleRequest(request jsonrpc.Request) *jsonrpc.Response {
153153
s.logger.Info("handling request", "method", request.Method)
154154
}
155155

156-
if strings.HasPrefix(request.Method, "notifications/") {
156+
// Handle notifications first
157+
if strings.HasPrefix(request.Method, "notifications/") || request.Method == "initialized" {
157158
s.logger.Info("received notification", "method", request.Method)
158159
return nil
159160
}
@@ -217,18 +218,6 @@ func handleRequest[Req, Resp any](request jsonrpc.Request, handler func(*Req) (*
217218
return jsonrpc.NewResponse(request.ID, result, nil)
218219
}
219220

220-
// handleNotification is a helper to unmarshal params and call a notification handler
221-
func handleNotification[Req any](request jsonrpc.Request, handler func(*Req)) {
222-
var req Req
223-
if request.Params != nil {
224-
if err := json.Unmarshal(request.Params, &req); err != nil {
225-
return
226-
}
227-
}
228-
229-
handler(&req)
230-
}
231-
232221
func (s *Server) handleInitialize(request *InitializeRequest) (*InitializeResponse, error) {
233222
response := &InitializeResponse{
234223
ProtocolVersion: Version,

mcp/server_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"io"
14+
"log/slog"
15+
1316
"github.com/loopwork-ai/emcee/internal"
1417
"github.com/loopwork-ai/emcee/jsonrpc"
1518
"github.com/stretchr/testify/assert"
@@ -930,3 +933,21 @@ func TestFindOperationByToolName(t *testing.T) {
930933
})
931934
}
932935
}
936+
937+
func TestHandleInitializedNotification(t *testing.T) {
938+
// Create a test server with a logger
939+
server, ts := setupTestServer(t)
940+
defer ts.Close()
941+
942+
// Add a logger to the server
943+
server.logger = slog.New(slog.NewTextHandler(io.Discard, nil))
944+
945+
// Create an initialized notification
946+
notification := jsonrpc.NewRequest("initialized", nil, 1)
947+
948+
// Handle the notification
949+
response := server.HandleRequest(notification)
950+
951+
// Verify that notifications don't generate a response
952+
assert.Nil(t, response, "notifications should not generate a response")
953+
}

0 commit comments

Comments
 (0)