|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + ghcontext "github.com/github/github-mcp-server/pkg/context" |
| 10 | +) |
| 11 | + |
| 12 | +// mcpJSONRPCRequest represents the structure of an MCP JSON-RPC request. |
| 13 | +// We only parse the fields needed for routing and optimization. |
| 14 | +type mcpJSONRPCRequest struct { |
| 15 | + JSONRPC string `json:"jsonrpc"` |
| 16 | + Method string `json:"method"` |
| 17 | + Params struct { |
| 18 | + // For tools/call |
| 19 | + Name string `json:"name,omitempty"` |
| 20 | + Arguments json.RawMessage `json:"arguments,omitempty"` |
| 21 | + // For prompts/get |
| 22 | + // Name is shared with tools/call |
| 23 | + // For resources/read |
| 24 | + URI string `json:"uri,omitempty"` |
| 25 | + } `json:"params"` |
| 26 | +} |
| 27 | + |
| 28 | +// WithMCPParse creates a middleware that parses MCP JSON-RPC requests early in the |
| 29 | +// request lifecycle and stores the parsed information in the request context. |
| 30 | +// This enables: |
| 31 | +// - Registry filtering via ForMCPRequest (only register needed tools/resources/prompts) |
| 32 | +// - Avoiding duplicate JSON parsing in downstream middlewares |
| 33 | +// - Access to owner/repo for secret-scanning middleware |
| 34 | +// |
| 35 | +// The middleware reads the request body, parses it, restores the body for downstream |
| 36 | +// handlers, and stores the parsed MCPMethodInfo in the request context. |
| 37 | +func WithMCPParse() func(http.Handler) http.Handler { |
| 38 | + return func(next http.Handler) http.Handler { |
| 39 | + fn := func(w http.ResponseWriter, r *http.Request) { |
| 40 | + ctx := r.Context() |
| 41 | + |
| 42 | + // Skip health check endpoints |
| 43 | + if r.URL.Path == "/_ping" { |
| 44 | + next.ServeHTTP(w, r) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // Only parse POST requests (MCP uses JSON-RPC over POST) |
| 49 | + if r.Method != http.MethodPost { |
| 50 | + next.ServeHTTP(w, r) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // Read the request body |
| 55 | + body, err := io.ReadAll(r.Body) |
| 56 | + if err != nil { |
| 57 | + // Log but continue - don't block requests on parse errors |
| 58 | + next.ServeHTTP(w, r) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Restore the body for downstream handlers |
| 63 | + r.Body = io.NopCloser(bytes.NewReader(body)) |
| 64 | + |
| 65 | + // Skip empty bodies |
| 66 | + if len(body) == 0 { |
| 67 | + next.ServeHTTP(w, r) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Parse the JSON-RPC request |
| 72 | + var mcpReq mcpJSONRPCRequest |
| 73 | + err = json.Unmarshal(body, &mcpReq) |
| 74 | + if err != nil { |
| 75 | + // Log but continue - could be a non-MCP request or malformed JSON |
| 76 | + next.ServeHTTP(w, r) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Skip if not a valid JSON-RPC 2.0 request |
| 81 | + if mcpReq.JSONRPC != "2.0" || mcpReq.Method == "" { |
| 82 | + next.ServeHTTP(w, r) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Build the MCPMethodInfo |
| 87 | + methodInfo := &ghcontext.MCPMethodInfo{ |
| 88 | + Method: mcpReq.Method, |
| 89 | + } |
| 90 | + |
| 91 | + // Extract item name based on method type |
| 92 | + |
| 93 | + switch mcpReq.Method { |
| 94 | + case "tools/call": |
| 95 | + methodInfo.ItemName = mcpReq.Params.Name |
| 96 | + // Parse arguments if present |
| 97 | + if len(mcpReq.Params.Arguments) > 0 { |
| 98 | + var args map[string]any |
| 99 | + err := json.Unmarshal(mcpReq.Params.Arguments, &args) |
| 100 | + if err == nil { |
| 101 | + methodInfo.Arguments = args |
| 102 | + // Extract owner and repo if present |
| 103 | + if owner, ok := args["owner"].(string); ok { |
| 104 | + methodInfo.Owner = owner |
| 105 | + } |
| 106 | + if repo, ok := args["repo"].(string); ok { |
| 107 | + methodInfo.Repo = repo |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + case "prompts/get": |
| 112 | + methodInfo.ItemName = mcpReq.Params.Name |
| 113 | + case "resources/read": |
| 114 | + methodInfo.ItemName = mcpReq.Params.URI |
| 115 | + default: |
| 116 | + // Whatever |
| 117 | + } |
| 118 | + |
| 119 | + // Store the parsed info in context |
| 120 | + ctx = ghcontext.ContextWithMCPMethodInfo(ctx, methodInfo) |
| 121 | + |
| 122 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 123 | + } |
| 124 | + return http.HandlerFunc(fn) |
| 125 | + } |
| 126 | +} |
0 commit comments