|
| 1 | +// Package mcp implements an MCP server using Streamable HTTP transport. |
| 2 | +package mcp |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/inconshreveable/log15" |
| 10 | +) |
| 11 | + |
| 12 | +// MCP implements an MCP server using Streamable HTTP transport. |
| 13 | +type MCP struct { |
| 14 | + logger log15.Logger |
| 15 | + handler http.Handler |
| 16 | +} |
| 17 | + |
| 18 | +// NewMCP creates a new MCP server instance. |
| 19 | +func NewMCP(handler http.Handler, logger log15.Logger) *MCP { |
| 20 | + return &MCP{ |
| 21 | + handler: handler, |
| 22 | + logger: logger, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// ServeHTTP handles incoming MCP requests over Streamable HTTP. |
| 27 | +func (m *MCP) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 28 | + if r.Method != http.MethodPost { |
| 29 | + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) |
| 30 | + |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + body, err := io.ReadAll(r.Body) |
| 35 | + if err != nil { |
| 36 | + http.Error(w, "Bad Request", http.StatusBadRequest) |
| 37 | + |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + var req Request |
| 42 | + |
| 43 | + err = json.Unmarshal(body, &req) |
| 44 | + if err != nil { |
| 45 | + m.writeError(w, nil, -32700, "Parse error") |
| 46 | + |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // Notifications have no id — respond with 202 Accepted. |
| 51 | + if req.ID == nil { |
| 52 | + w.WriteHeader(http.StatusAccepted) |
| 53 | + |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + var id any |
| 58 | + |
| 59 | + err = json.Unmarshal(*req.ID, &id) |
| 60 | + if err != nil { |
| 61 | + id = nil |
| 62 | + } |
| 63 | + |
| 64 | + switch req.Method { |
| 65 | + case "initialize": |
| 66 | + m.handleInitialize(w, id) |
| 67 | + case "tools/list": |
| 68 | + m.handleToolsList(w, id) |
| 69 | + case "tools/call": |
| 70 | + m.handleToolsCall(w, r, id, req.Params) |
| 71 | + default: |
| 72 | + m.writeError(w, id, -32601, "Method not found") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (m *MCP) handleInitialize(w http.ResponseWriter, id any) { |
| 77 | + result := InitializeResult{ |
| 78 | + ProtocolVersion: "2025-03-26", |
| 79 | + Capabilities: Capabilities{ |
| 80 | + Tools: &ToolsCapability{}, |
| 81 | + }, |
| 82 | + ServerInfo: ServerInfo{ |
| 83 | + Name: "askgod", |
| 84 | + Version: "1.0.0", |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + m.writeResult(w, id, result) |
| 89 | +} |
| 90 | + |
| 91 | +func (m *MCP) handleToolsList(w http.ResponseWriter, id any) { |
| 92 | + result := ListToolsResult{ |
| 93 | + Tools: []Tool{ |
| 94 | + { |
| 95 | + Name: "submit_flag", |
| 96 | + Description: "Submit a CTF flag for your team. Returns whether the flag was valid, already submitted, or invalid.", |
| 97 | + InputSchema: ToolInputSchema{ |
| 98 | + Type: "object", |
| 99 | + Properties: map[string]any{ |
| 100 | + "flag": map[string]any{ |
| 101 | + "type": "string", |
| 102 | + "description": "The flag string to submit", |
| 103 | + }, |
| 104 | + }, |
| 105 | + Required: []string{"flag"}, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + m.writeResult(w, id, result) |
| 112 | +} |
| 113 | + |
| 114 | +func (m *MCP) handleToolsCall(w http.ResponseWriter, r *http.Request, id any, params json.RawMessage) { |
| 115 | + var p CallToolParams |
| 116 | + |
| 117 | + err := json.Unmarshal(params, &p) |
| 118 | + if err != nil { |
| 119 | + m.writeError(w, id, -32602, "Invalid params") |
| 120 | + |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + var result CallToolResult |
| 125 | + |
| 126 | + switch p.Name { |
| 127 | + case "submit_flag": |
| 128 | + result = m.submitFlag(r, p.Arguments) |
| 129 | + default: |
| 130 | + m.writeError(w, id, -32602, "Unknown tool: "+p.Name) |
| 131 | + |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + m.writeResult(w, id, result) |
| 136 | +} |
| 137 | + |
| 138 | +func (m *MCP) writeResult(w http.ResponseWriter, id any, result any) { |
| 139 | + resp := Response{ |
| 140 | + JSONRPC: "2.0", |
| 141 | + ID: id, |
| 142 | + Result: result, |
| 143 | + } |
| 144 | + |
| 145 | + w.Header().Set("Content-Type", "application/json") |
| 146 | + |
| 147 | + err := json.NewEncoder(w).Encode(resp) |
| 148 | + if err != nil { |
| 149 | + m.logger.Error("Failed to encode response", log15.Ctx{"error": err}) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func (m *MCP) writeError(w http.ResponseWriter, id any, code int, message string) { |
| 154 | + resp := Response{ |
| 155 | + JSONRPC: "2.0", |
| 156 | + ID: id, |
| 157 | + Error: &Error{ |
| 158 | + Code: code, |
| 159 | + Message: message, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + w.Header().Set("Content-Type", "application/json") |
| 164 | + |
| 165 | + err := json.NewEncoder(w).Encode(resp) |
| 166 | + if err != nil { |
| 167 | + m.logger.Error("Failed to encode error response", log15.Ctx{"error": err}) |
| 168 | + } |
| 169 | +} |
0 commit comments