-
Notifications
You must be signed in to change notification settings - Fork 14
Support for listening on localhost with http #1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |||||||||||||||||||
| "context" | ||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||
| "fmt" | ||||||||||||||||||||
| "io" | ||||||||||||||||||||
| "net/http" | ||||||||||||||||||||
| "strings" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| "github.com/mark3labs/mcp-go/mcp" | ||||||||||||||||||||
|
|
@@ -602,3 +604,113 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
| return data | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // StartHTTP initializes the MCP server and starts an HTTP server for MCP protocol over HTTP/SSE | ||||||||||||||||||||
| func (s *Server) StartHTTP(port int) error { | ||||||||||||||||||||
| s.logger.Info("Initializing MCP HTTP server on port %d", port) | ||||||||||||||||||||
| if err := s.CreateServer(); err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| http.HandleFunc("/sse", s.handleMCPHTTP) | ||||||||||||||||||||
| addr := fmt.Sprintf(":%d", port) | ||||||||||||||||||||
| s.logger.Info("Listening on http://localhost%s/sse", addr) | ||||||||||||||||||||
| fmt.Printf("MCP HTTP server listening on http://localhost%s/sse\n", addr) | ||||||||||||||||||||
| return http.ListenAndServe(addr, nil) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // handleMCPHTTP handles HTTP POST requests for MCP protocol | ||||||||||||||||||||
| func (s *Server) handleMCPHTTP(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||
| s.logger.Info("New HTTP connection from %s %s %s", r.RemoteAddr, r.Method, r.URL.Path) | ||||||||||||||||||||
| if r.Method != http.MethodPost { | ||||||||||||||||||||
| http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| body, err := io.ReadAll(r.Body) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| http.Error(w, "Failed to read body", http.StatusBadRequest) | ||||||||||||||||||||
| s.logger.Error("Failed to read request body from %s: %v", r.RemoteAddr, err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| s.logger.Info("Request body: %s", string(body)) | ||||||||||||||||||||
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| // Parse the JSON-RPC request | ||||||||||||||||||||
| var req map[string]interface{} | ||||||||||||||||||||
| if err := json.Unmarshal(body, &req); err != nil { | ||||||||||||||||||||
| http.Error(w, "Invalid JSON", http.StatusBadRequest) | ||||||||||||||||||||
| s.logger.Error("Invalid JSON from %s: %v", r.RemoteAddr, err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Intercept "initialize" method | ||||||||||||||||||||
| if method, ok := req["method"].(string); ok && method == "initialize" { | ||||||||||||||||||||
| id := req["id"] | ||||||||||||||||||||
| s.logger.Info("Received 'initialize' from %s (id=%v)", r.RemoteAddr, id) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Extract protocolVersion from params | ||||||||||||||||||||
| protocolVersion := "" | ||||||||||||||||||||
| if params, ok := req["params"].(map[string]interface{}); ok { | ||||||||||||||||||||
| if pv, ok := params["protocolVersion"].(string); ok { | ||||||||||||||||||||
| protocolVersion = pv | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if protocolVersion == "" { | ||||||||||||||||||||
| protocolVersion = "2025-03-26" // fallback, should always be present | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| resp := map[string]interface{}{ | ||||||||||||||||||||
| "jsonrpc": "2.0", | ||||||||||||||||||||
| "id": id, | ||||||||||||||||||||
| "result": map[string]interface{}{ | ||||||||||||||||||||
| "serverInfo": map[string]interface{}{ | ||||||||||||||||||||
| "name": "MCPShell", | ||||||||||||||||||||
| "version": s.version, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| "capabilities": map[string]interface{}{ | ||||||||||||||||||||
| "tools": map[string]interface{}{ | ||||||||||||||||||||
| "allowedTools": s.getAllowedToolNames(), | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| "sessionId": "local", | ||||||||||||||||||||
| "protocolVersion": protocolVersion, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| } | ||||||||||||||||||||
| w.Header().Set("Content-Type", "application/json") | ||||||||||||||||||||
| w.WriteHeader(http.StatusOK) | ||||||||||||||||||||
| respBytes, _ := json.Marshal(resp) | ||||||||||||||||||||
|
Comment on lines
+679
to
+680
|
||||||||||||||||||||
| w.WriteHeader(http.StatusOK) | |
| respBytes, _ := json.Marshal(resp) | |
| respBytes, err := json.Marshal(resp) | |
| if err != nil { | |
| s.logger.Error("Failed to marshal response: %v", err) | |
| http.Error(w, `{"error":"Internal server error"}`, http.StatusInternalServerError) | |
| return | |
| } | |
| w.WriteHeader(http.StatusOK) |
Copilot
AI
Aug 1, 2025
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.
Logging the full response may expose sensitive information in logs. Consider logging only a summary or sanitized version of the response for security purposes.
| s.logger.Info("Response: %s", string(respBytes)) | |
| result := resp["result"].(map[string]interface{}) | |
| serverInfo := result["serverInfo"].(map[string]interface{}) | |
| sessionID := result["sessionId"] | |
| protocolVersion := result["protocolVersion"] | |
| s.logger.Info("Response summary: server=%s version=%s sessionId=%v protocolVersion=%v", serverInfo["name"], serverInfo["version"], sessionID, protocolVersion) |
Copilot
AI
Aug 1, 2025
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.
Error from json.Marshal is being ignored. Consider handling the marshaling error to ensure robust error handling.
| respBytes, _ = json.Marshal(v) | |
| var err error | |
| respBytes, err = json.Marshal(v) | |
| if err != nil { | |
| http.Error(w, "Failed to marshal response", http.StatusInternalServerError) | |
| s.logger.Error("Failed to marshal response for %s: %v", r.RemoteAddr, err) | |
| return | |
| } |
Copilot
AI
Aug 1, 2025
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.
Logging the full response may expose sensitive information in logs. Consider logging only a summary or sanitized version of the response for security purposes.
| s.logger.Info("Response: %s", string(respBytes)) | |
| s.logger.Info("Response sent to %s: type=%T, length=%d bytes", r.RemoteAddr, resp, len(respBytes)) |
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.
The endpoint is named '/sse' but the implementation is a standard HTTP POST handler, not Server-Sent Events. Consider renaming to '/mcp' or implementing actual SSE if that's the intended protocol.