Skip to content

Commit a988d3d

Browse files
add test AddSessionPromptsUninitialized
1 parent 9a1f382 commit a988d3d

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

server/session_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,97 @@ func TestMCPServer_AddSessionToolsUninitialized(t *testing.T) {
602602
assert.Contains(t, session.GetSessionTools(), "initialized-tool")
603603
}
604604

605+
func TestMCPServer_AddSessionPromptsUninitialized(t *testing.T) {
606+
// This test verifies that adding prompts to an uninitialized session works correctly.
607+
//
608+
// This scenario can occur when prompts are added during the session registration hook,
609+
// before the session is fully initialized. In this case, we should:
610+
// 1. Successfully add the prompts to the session
611+
// 2. Not attempt to send a notification (since the session isn't ready)
612+
// 3. Have the prompts available once the session is initialized
613+
// 4. Not trigger any error hooks when adding prompts to uninitialized sessions
614+
615+
// Set up error hook to track if it's called
616+
errorChan := make(chan error)
617+
hooks := &Hooks{}
618+
hooks.AddOnError(
619+
func(ctx context.Context, id any, method mcp.MCPMethod, message any, err error) {
620+
errorChan <- err
621+
},
622+
)
623+
624+
server := NewMCPServer("test-server", "1.0.0",
625+
WithPromptCapabilities(true),
626+
WithHooks(hooks),
627+
)
628+
ctx := context.Background()
629+
630+
// Create an uninitialized session
631+
sessionChan := make(chan mcp.JSONRPCNotification, 1)
632+
session := &sessionTestClientWithPrompts{
633+
sessionID: "uninitialized-session",
634+
notificationChannel: sessionChan,
635+
initialized: false,
636+
}
637+
638+
// Register the session
639+
err := server.RegisterSession(ctx, session)
640+
require.NoError(t, err)
641+
642+
// Add session-specific tools to the uninitialized session
643+
err = server.AddSessionPrompts(session.SessionID(),
644+
ServerPrompt{Prompt: mcp.NewPrompt("uninitialized-prompt")},
645+
)
646+
require.NoError(t, err)
647+
648+
// Verify no errors
649+
select {
650+
case err := <-errorChan:
651+
t.Error("Expected no errors, but OnError called with: ", err)
652+
case <-time.After(25 * time.Millisecond): // no errors
653+
}
654+
655+
// Verify no notification was sent (channel should be empty)
656+
select {
657+
case <-sessionChan:
658+
t.Error("Expected no notification to be sent for uninitialized session")
659+
default: // no notifications
660+
}
661+
662+
// Verify prompt was added to session
663+
assert.Len(t, session.GetSessionPrompts(), 1)
664+
assert.Contains(t, session.GetSessionPrompts(), "uninitialized-prompt")
665+
666+
// Initialize the session
667+
session.Initialize()
668+
669+
// Now verify that subsequent tool additions will send notifications
670+
err = server.AddSessionPrompts(session.SessionID(),
671+
ServerPrompt{Prompt: mcp.NewPrompt("initialized-prompt")},
672+
)
673+
require.NoError(t, err)
674+
675+
// Verify no errors
676+
select {
677+
case err := <-errorChan:
678+
t.Error("Expected no errors, but OnError called with:", err)
679+
case <-time.After(200 * time.Millisecond): // No errors
680+
}
681+
682+
// Verify notification was sent for the initialized session
683+
select {
684+
case notification := <-sessionChan:
685+
assert.Equal(t, "notifications/prompts/list_changed", notification.Method)
686+
case <-time.After(100 * time.Millisecond):
687+
t.Error("Timeout waiting for expected notifications/prompts/list_changed notification")
688+
}
689+
690+
// Verify both tools are available
691+
assert.Len(t, session.GetSessionPrompts(), 2)
692+
assert.Contains(t, session.GetSessionPrompts(), "uninitialized-prompt")
693+
assert.Contains(t, session.GetSessionPrompts(), "initialized-prompt")
694+
}
695+
605696
func TestMCPServer_DeleteSessionToolsUninitialized(t *testing.T) {
606697
// This test verifies that deleting tools from an uninitialized session works correctly.
607698
//

0 commit comments

Comments
 (0)