@@ -16,20 +16,29 @@ import (
1616func TestSSEServer (t * testing.T ) {
1717 t .Run ("Can instantiate" , func (t * testing.T ) {
1818 mcpServer := NewMCPServer ("test" , "1.0.0" )
19- sseServer := NewSSEServer (mcpServer , WithBaseURL ("http://localhost:8080" ))
19+ sseServer := NewSSEServer (mcpServer ,
20+ WithBaseURL ("http://localhost:8080" ),
21+ WithBasePath ("/mcp" ),
22+ )
2023
2124 if sseServer == nil {
2225 t .Error ("SSEServer should not be nil" )
2326 }
2427 if sseServer .server == nil {
2528 t .Error ("MCPServer should not be nil" )
2629 }
27- if sseServer .baseURL != "http://localhost:8080" {
30+ if sseServer .baseURL != "http://localhost:8080/mcp " {
2831 t .Errorf (
29- "Expected baseURL http://localhost:8080, got %s" ,
32+ "Expected baseURL http://localhost:8080/mcp , got %s" ,
3033 sseServer .baseURL ,
3134 )
3235 }
36+ if sseServer .basePath != "/mcp" {
37+ t .Errorf (
38+ "Expected basePath /mcp, got %s" ,
39+ sseServer .basePath ,
40+ )
41+ }
3342 })
3443
3544 t .Run ("Can send and receive messages" , func (t * testing.T ) {
@@ -405,4 +414,58 @@ func TestSSEServer(t *testing.T) {
405414 // Clean up SSE connection
406415 cancel ()
407416 })
417+
418+ t .Run ("works as http.Handler with custom basePath" , func (t * testing.T ) {
419+ mcpServer := NewMCPServer ("test" , "1.0.0" )
420+ sseServer := NewSSEServer (mcpServer , WithBasePath ("/mcp" ))
421+
422+ ts := httptest .NewServer (sseServer )
423+ defer ts .Close ()
424+
425+ // Test 404 for unknown path first (simpler case)
426+ resp , err := http .Get (fmt .Sprintf ("%s/sse" , ts .URL ))
427+ if err != nil {
428+ t .Fatalf ("Failed to make request: %v" , err )
429+ }
430+ defer resp .Body .Close ()
431+ if resp .StatusCode != http .StatusNotFound {
432+ t .Errorf ("Expected status 404, got %d" , resp .StatusCode )
433+ }
434+
435+ // Test SSE endpoint with proper cleanup
436+ ctx , cancel := context .WithCancel (context .Background ())
437+ defer cancel ()
438+
439+ sseURL := fmt .Sprintf ("%s/sse" , ts .URL + sseServer .basePath )
440+ req , err := http .NewRequestWithContext (ctx , "GET" , sseURL , nil )
441+ if err != nil {
442+ t .Fatalf ("Failed to create request: %v" , err )
443+ }
444+
445+ resp , err = http .DefaultClient .Do (req )
446+ if err != nil {
447+ t .Fatalf ("Failed to connect to SSE endpoint: %v" , err )
448+ }
449+ defer resp .Body .Close ()
450+
451+ if resp .StatusCode != http .StatusOK {
452+ t .Errorf ("Expected status 200, got %d" , resp .StatusCode )
453+ }
454+
455+ // Read initial message in goroutine
456+ done := make (chan struct {})
457+ go func () {
458+ defer close (done )
459+ buf := make ([]byte , 1024 )
460+ _ , err := resp .Body .Read (buf )
461+ if err != nil && err .Error () != "context canceled" {
462+ t .Errorf ("Failed to read from SSE stream: %v" , err )
463+ }
464+ }()
465+
466+ // Wait briefly for initial response then cancel
467+ time .Sleep (100 * time .Millisecond )
468+ cancel ()
469+ <- done
470+ })
408471}
0 commit comments