-
Notifications
You must be signed in to change notification settings - Fork 619
Add option to StreamableHTTPServer to allow custom http server instance #347
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -670,6 +670,56 @@ func TestStreamableHTTP_SessionWithTools(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestStreamableHTTPServer_WithOptions(t *testing.T) { | ||
t.Run("WithStreamableHTTPServer sets httpServer field", func(t *testing.T) { | ||
mcpServer := NewMCPServer("test", "1.0.0") | ||
customServer := &http.Server{Addr: ":9999"} | ||
httpServer := NewStreamableHTTPServer(mcpServer, WithStreamableHTTPServer(customServer)) | ||
|
||
if httpServer.httpServer != customServer { | ||
t.Errorf("Expected httpServer to be set to custom server instance, got %v", httpServer.httpServer) | ||
} | ||
}) | ||
|
||
t.Run("Start with conflicting address returns error", func(t *testing.T) { | ||
mcpServer := NewMCPServer("test", "1.0.0") | ||
customServer := &http.Server{Addr: ":9999"} | ||
httpServer := NewStreamableHTTPServer(mcpServer, WithStreamableHTTPServer(customServer)) | ||
|
||
err := httpServer.Start(":8888") | ||
if err == nil { | ||
t.Error("Expected error for conflicting address, got nil") | ||
} else if !strings.Contains(err.Error(), "conflicting listen address") { | ||
t.Errorf("Expected error message to contain 'conflicting listen address', got '%s'", err.Error()) | ||
} | ||
}) | ||
|
||
t.Run("Options consistency test", func(t *testing.T) { | ||
mcpServer := NewMCPServer("test", "1.0.0") | ||
endpointPath := "/test-mcp" | ||
customServer := &http.Server{} | ||
|
||
// Options to test | ||
options := []StreamableHTTPOption{ | ||
WithEndpointPath(endpointPath), | ||
WithStreamableHTTPServer(customServer), | ||
} | ||
|
||
// Apply options multiple times and verify consistency | ||
for i := 0; i < 10; i++ { | ||
server := NewStreamableHTTPServer(mcpServer, options...) | ||
|
||
if server.endpointPath != endpointPath { | ||
t.Errorf("Expected endpointPath %s, got %s", endpointPath, server.endpointPath) | ||
} | ||
|
||
if server.httpServer != customServer { | ||
t.Errorf("Expected httpServer to match, got %v", server.httpServer) | ||
} | ||
} | ||
}) | ||
} | ||
Comment on lines
+673
to
+721
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. 🛠️ Refactor suggestion Good test coverage but missing some important edge cases. The current tests cover the basic functionality well:
However, consider adding tests for these scenarios to improve coverage: Add these missing test cases: + t.Run("Start with custom server and no address sets address", func(t *testing.T) {
+ mcpServer := NewMCPServer("test", "1.0.0")
+ customServer := &http.Server{} // No address set
+ httpServer := NewStreamableHTTPServer(mcpServer, WithStreamableHTTPServer(customServer))
+
+ // This should work and set the address
+ go func() {
+ time.Sleep(10 * time.Millisecond)
+ httpServer.Shutdown(context.Background())
+ }()
+
+ err := httpServer.Start(":0") // Use :0 for random port
+ if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
+ t.Errorf("Expected no error or connection closed error, got: %v", err)
+ }
+
+ if customServer.Addr != ":0" {
+ t.Errorf("Expected custom server address to be set to ':0', got '%s'", customServer.Addr)
+ }
+ })
+
+ t.Run("Start with custom server and matching address succeeds", func(t *testing.T) {
+ mcpServer := NewMCPServer("test", "1.0.0")
+ customServer := &http.Server{Addr: ":0"}
+ httpServer := NewStreamableHTTPServer(mcpServer, WithStreamableHTTPServer(customServer))
+
+ go func() {
+ time.Sleep(10 * time.Millisecond)
+ httpServer.Shutdown(context.Background())
+ }()
+
+ err := httpServer.Start(":0") // Same address
+ if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
+ t.Errorf("Expected no error or connection closed error, got: %v", err)
+ }
+ }) 🤖 Prompt for AI Agents
|
||
|
||
func postJSON(url string, bodyObject any) (*http.Response, error) { | ||
jsonBody, _ := json.Marshal(bodyObject) | ||
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Should we document in this scenario that routing must be handled by the user? In the default/previous case, routing was handled by the
Start
method, but for the custom HTTP server instance, it is not. Maybe document it in a user-facing way with theWithStreamableHTTPServer
function?It can be a brief note because we can reasonably expect someone who wants to use a custom HTTP server to do this, regardless. But if they don't, the server would still start, but it wouldn't be able to do anything, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, makes sense, will update for both sse and streamable http.