ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered. It also tracks inflight requests.
(rw http.ResponseWriter, r *http.Request)
| 358 | // ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered. |
| 359 | // It also tracks inflight requests. |
| 360 | func (b *RequestBridge) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 361 | select { |
| 362 | case <-b.closed: |
| 363 | http.Error(rw, "server closed", http.StatusInternalServerError) |
| 364 | return |
| 365 | default: |
| 366 | } |
| 367 | |
| 368 | // We want to abide by the context passed in without losing any of its |
| 369 | // functionality, but we still want to link our shutdown context to each |
| 370 | // request. |
| 371 | ctx := mergeContexts(r.Context(), b.inflightCtx) |
| 372 | |
| 373 | b.inflightReqs.Add(1) |
| 374 | b.inflightWG.Add(1) |
| 375 | defer func() { |
| 376 | b.inflightReqs.Add(-1) |
| 377 | b.inflightWG.Done() |
| 378 | }() |
| 379 | |
| 380 | // Enforce the request body size limit. MaxBytesReader counts bytes as |
| 381 | // they are read from the connection and fails when the limit is exceeded. |
| 382 | r.Body = http.MaxBytesReader(rw, r.Body, maxRequestBodyBytes) |
| 383 | b.mux.ServeHTTP(rw, r.WithContext(ctx)) |
| 384 | } |
| 385 | |
| 386 | // Shutdown will attempt to gracefully shutdown. This entails waiting for all requests to |
| 387 | // complete, and shutting down the MCP server proxier. |