@@ -14,6 +14,8 @@ import (
1414 "log/slog"
1515 "maps"
1616 "net/http"
17+ "os"
18+ "strconv"
1719 "strings"
1820 "sync"
1921 "time"
@@ -39,6 +41,20 @@ type mcpRequestContext struct {
3941 perBackendMetricsRecorded bool
4042}
4143
44+ // defaultMaxRequestBodySize is the default maximum allowed POST body size in bytes (4 MiB).
45+ const defaultMaxRequestBodySize = 4 * 1024 * 1024
46+
47+ // getMaxRequestBodySize returns the configured POST body limit from the environment variable,
48+ // falling back to 4 MiB if the variable is unset or invalid.
49+ func getMaxRequestBodySize () int64 {
50+ if v , ok := os .LookupEnv ("MCP_PROXY_MAX_REQUEST_BODY_SIZE" ); ok {
51+ if n , err := strconv .ParseInt (v , 10 , 64 ); err == nil && n > 0 {
52+ return n
53+ }
54+ }
55+ return defaultMaxRequestBodySize
56+ }
57+
4258// NewMCPProxy creates a new MCPProxy instance.
4359func NewMCPProxy (l * slog.Logger , mcpMetrics metrics.MCPMetrics , tracer tracingapi.MCPTracer , sessionCrypto SessionCrypto , logRequestHeaderAttributes map [string ]string ) (* ProxyConfig , * http.ServeMux , error ) {
4460 toolChangeSignaler := newMultiWatcherSignaler () // used to signal changes to all active sessions.
@@ -49,6 +65,7 @@ func NewMCPProxy(l *slog.Logger, mcpMetrics metrics.MCPMetrics, tracer tracingap
4965 l : l ,
5066 client : http.Client {}, // No timeout as it's enforced at Envoy level.
5167 logRequestHeaderAttributes : maps .Clone (logRequestHeaderAttributes ),
68+ maxRequestBodySize : getMaxRequestBodySize (),
5269 }
5370 mux := http .NewServeMux ()
5471 mux .HandleFunc (
0 commit comments