MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / ServeHTTP

Method ServeHTTP

mcp/sse.go:140–172  ·  view source on GitHub ↗

ServeHTTP handles POST requests to the transport endpoint.

(w http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

138
139// ServeHTTP handles POST requests to the transport endpoint.
140func (t *SSEServerTransport) ServeHTTP(w http.ResponseWriter, req *http.Request) {
141 if t.incoming == nil {
142 http.Error(w, "session not connected", http.StatusInternalServerError)
143 return
144 }
145
146 // Read and parse the message.
147 data, err := io.ReadAll(req.Body)
148 if err != nil {
149 http.Error(w, "failed to read body", http.StatusBadRequest)
150 return
151 }
152 // Optionally, we could just push the data onto a channel, and let the
153 // message fail to parse when it is read. This failure seems a bit more
154 // useful
155 msg, err := jsonrpc2.DecodeMessage(data)
156 if err != nil {
157 http.Error(w, "failed to parse body", http.StatusBadRequest)
158 return
159 }
160 if req, ok := msg.(*jsonrpc.Request); ok {
161 if _, err := checkRequest(req, serverMethodInfos); err != nil {
162 http.Error(w, err.Error(), http.StatusBadRequest)
163 return
164 }
165 }
166 select {
167 case t.incoming <- msg:
168 w.WriteHeader(http.StatusAccepted)
169 case <-t.done:
170 http.Error(w, "session closed", http.StatusBadRequest)
171 }
172}
173
174// Connect sends the 'endpoint' event to the client.
175// See [SSEServerTransport] for more details on the [Connection] implementation.

Callers

nothing calls this directly

Calls 3

DecodeMessageFunction · 0.92
checkRequestFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected