Skip to content

Commit 8ba14c7

Browse files
authored
fix: allow session to be used while half-open (#34)
1 parent 02c0a50 commit 8ba14c7

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

internal/engine/engine.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,6 @@ func (e *Engine) InitializeSession(ctx context.Context, userID string, req *mcp.
264264
}
265265

266266
func (e *Engine) HandleRequest(ctx context.Context, sess *SessionHandle, req *jsonrpc.Request) (*jsonrpc.Response, error) {
267-
// Require session to be open before serving requests (except initialize, which
268-
// doesn't reach here).
269-
if st := sess.State(); st != "" && st != sessions.SessionStateOpen {
270-
return jsonrpc.NewErrorResponse(req.ID, jsonrpc.ErrorCodeInvalidRequest, "session not initialized", nil), nil
271-
}
272-
273267
switch req.Method {
274268
case string(mcp.ToolsListMethod):
275269
return e.handleToolsList(ctx, sess, req)

stdio/handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,17 @@ func TestTools_ListAndCall(t *testing.T) {
338338
}
339339
}
340340

341-
// Handshake gating: requests must fail until client sends notifications/initialized.
342-
func TestHandshake_PendingRejectsRequests(t *testing.T) {
341+
// Half-open sessions: requests are now allowed (no gating InvalidRequest error) before client sends notifications/initialized.
342+
func TestHandshake_PreInitializedRequestsAllowed(t *testing.T) {
343343
srv := mcpservice.NewServer(
344344
mcpservice.WithToolsCapability(mcpservice.NewToolsContainer()),
345345
)
346346
th := newHarness(t, srv)
347347

348-
// Initialize session
348+
// Initialize session (server responds) but do NOT send notifications/initialized yet.
349349
_ = th.initialize(t, "init-1", defaultInitializeRequest())
350350

351-
// Immediately send a ping request before initialized; expect InvalidRequest error
351+
// Send a ping request before initialized; it should NOT fail with "session not initialized".
352352
ping := &jsonrpc.Request{JSONRPCVersion: jsonrpc.ProtocolVersion, Method: string(mcp.PingMethod), ID: jsonrpc.NewRequestID("1")}
353353
if err := th.send(ping); err != nil {
354354
t.Fatal(err)
@@ -357,8 +357,8 @@ func TestHandshake_PendingRejectsRequests(t *testing.T) {
357357
if err != nil {
358358
t.Fatal(err)
359359
}
360-
if res.Error == nil || res.Error.Code != jsonrpc.ErrorCodeInvalidRequest {
361-
t.Fatalf("expected invalid request error before initialized, got: %+v", res.Error)
360+
if res.Error != nil && res.Error.Message == "session not initialized" {
361+
t.Fatalf("did not expect gating error before initialized: %+v", res.Error)
362362
}
363363
}
364364

0 commit comments

Comments
 (0)