- Notifications
You must be signed in to change notification settings - Fork1.2k
Add multi-user HTTP mode: per-request GitHub token, docs, and tests#489
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
thomcost wants to merge2 commits intogithub:mainChoose a base branch fromthomcost:multi-user-http
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build e2e | ||
package e2e_test | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
) | ||
func TestMultiUserHTTPServer_Integration(t *testing.T) { | ||
// Start the server in multi-user mode on a random port (e.g. 18080) | ||
cmd := exec.Command("../cmd/github-mcp-server/github-mcp-server", "multi-user", "--port", "18080") | ||
cmd.Stdout = nil | ||
cmd.Stderr = nil | ||
if err := cmd.Start(); err != nil { | ||
t.Fatalf("failed to start server: %v", err) | ||
} | ||
defer cmd.Process.Kill() | ||
// Wait for server to start | ||
time.Sleep(2 * time.Second) | ||
// Make a request without Authorization header | ||
resp, err := http.Post("http://localhost:18080/v1/mcp", "application/json", bytes.NewBufferString(`{"test":"noauth"}`)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if resp.StatusCode != http.StatusUnauthorized { | ||
t.Errorf("expected 401 Unauthorized, got %d", resp.StatusCode) | ||
} | ||
// Make a request with Authorization header | ||
body, _ := json.Marshal(map[string]string{"test": "authed"}) | ||
req, _ := http.NewRequest("POST", "http://localhost:18080/v1/mcp", bytes.NewBuffer(body)) | ||
req.Header.Set("Authorization", "Bearer testtoken123") | ||
req.Header.Set("Content-Type", "application/json") | ||
resp2, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if resp2.StatusCode == http.StatusUnauthorized { | ||
t.Errorf("expected not 401, got 401 (token should be accepted if server is running and token is valid)") | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package ghmcp | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
type dummyRequest struct { | ||
Test string `json:"test"` | ||
} | ||
func TestMultiUserHTTPServer_TokenRequired(t *testing.T) { | ||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token := extractTokenFromRequest(r) | ||
if token == "" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
_, _ = w.Write([]byte(`{"error":"missing GitHub token in Authorization header"}`)) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok":true}`)) | ||
}) | ||
ts := httptest.NewServer(h) | ||
defer ts.Close() | ||
// No Authorization header | ||
resp, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(`{"test":"noauth"}`)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if resp.StatusCode != http.StatusUnauthorized { | ||
t.Errorf("expected 401 Unauthorized, got %d", resp.StatusCode) | ||
} | ||
} | ||
func TestMultiUserHTTPServer_ValidToken(t *testing.T) { | ||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token := extractTokenFromRequest(r) | ||
if token == "" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
_, _ = w.Write([]byte(`{"error":"missing GitHub token in Authorization header"}`)) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok":true,"token":"` + token + `"}`)) | ||
}) | ||
ts := httptest.NewServer(h) | ||
defer ts.Close() | ||
// With Authorization header | ||
body, _ := json.Marshal(dummyRequest{Test: "authed"}) | ||
req, _ := http.NewRequest("POST", ts.URL, bytes.NewBuffer(body)) | ||
req.Header.Set("Authorization", "Bearer testtoken123") | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("expected 200 OK, got %d", resp.StatusCode) | ||
} | ||
data, _ := io.ReadAll(resp.Body) | ||
if !bytes.Contains(data, []byte("testtoken123")) { | ||
t.Errorf("expected token in response, got %s", string(data)) | ||
} | ||
} | ||
func TestExtractTokenFromRequest_StrictValidation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
header string | ||
expected string | ||
}{ | ||
{"valid bearer token", "Bearer ghp_1234567890abcdef", "ghp_1234567890abcdef"}, | ||
{"valid long token", "Bearer github_pat_11AAAAAAA0AAAAAAAAAAAAAAAAA", "github_pat_11AAAAAAA0AAAAAAAAAAAAAAAAA"}, | ||
{"missing header", "", ""}, | ||
{"malformed - no Bearer", "ghp_1234567890abcdef", ""}, | ||
{"malformed - wrong case", "bearer ghp_1234567890abcdef", ""}, | ||
{"too short token", "Bearer abc", ""}, | ||
{"empty token", "Bearer ", ""}, | ||
{"only Bearer", "Bearer", ""}, | ||
{"spaces in token", "Bearer ghp_123 456", "ghp_123 456"}, // This should work | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := &http.Request{ | ||
Header: http.Header{}, | ||
} | ||
if tt.header != "" { | ||
req.Header.Set("Authorization", tt.header) | ||
} | ||
result := extractTokenFromRequest(req) | ||
if result != tt.expected { | ||
t.Errorf("expected %q, got %q", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
func TestMultiUserHandler_ContextInjection(t *testing.T) { | ||
// Mock MCP server that verifies token is in context | ||
mockMCP := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token, ok := r.Context().Value("github_token").(string) | ||
if !ok { | ||
t.Error("token not found in context") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
if token != "test_token_123456" { | ||
t.Errorf("wrong token in context: %s", token) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"success":true}`)) | ||
}) | ||
handler := &multiUserHandler{mcpServer: mockMCP} | ||
req := httptest.NewRequest("POST", "/", strings.NewReader("{}")) | ||
req.Header.Set("Authorization", "Bearer test_token_123456") | ||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, req) | ||
if w.Code != http.StatusOK { | ||
t.Errorf("expected 200, got %d. Body: %s", w.Code, w.Body.String()) | ||
} | ||
} | ||
func TestMultiUserHandler_MissingToken(t *testing.T) { | ||
// Mock MCP server (should not be called) | ||
mockMCP := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Error("MCP server should not be called when token is missing") | ||
}) | ||
handler := &multiUserHandler{mcpServer: mockMCP} | ||
req := httptest.NewRequest("POST", "/", strings.NewReader("{}")) | ||
// No Authorization header | ||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, req) | ||
if w.Code != http.StatusUnauthorized { | ||
t.Errorf("expected 401, got %d", w.Code) | ||
} | ||
contentType := w.Header().Get("Content-Type") | ||
if contentType != "application/json" { | ||
t.Errorf("expected Content-Type: application/json, got %s", contentType) | ||
} | ||
if !strings.Contains(w.Body.String(), "missing GitHub token") { | ||
t.Errorf("expected error message about missing token, got: %s", w.Body.String()) | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.