mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-16 09:11:03 +08:00
feat(mcp): add context length guard to prevent oversized requests
* feat: add X-Client-ID header for claw402 monitoring * feat(mcp): add context length guard to prevent oversized requests - Add MaxContext field to Config (default 0 = no limit) - Add WithMaxContext() option for setting model context limits - Add context_guard.go: token estimation + message truncation - Integrate guard into both BuildMCPRequestBody and BuildRequestBodyFromRequest - Support both map[string]string and map[string]any message formats - Truncates oldest non-system messages when estimated tokens exceed limit - Always preserves system messages and keeps at least 1 non-system message - Logs warning when truncation occurs for debugging Usage: mcp.NewDeepSeekClient(mcp.WithMaxContext(131072))
This commit is contained in:
@@ -86,6 +86,19 @@ func WithMaxTokens(maxTokens int) ClientOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxContext sets the model's max context window in tokens.
|
||||
// When set (> 0), the client will automatically truncate oldest non-system
|
||||
// messages if the estimated token count exceeds this limit.
|
||||
//
|
||||
// Usage example:
|
||||
//
|
||||
// client := mcp.NewClient(mcp.WithMaxContext(131072)) // DeepSeek 128K
|
||||
func WithMaxContext(maxContext int) ClientOption {
|
||||
return func(c *Config) {
|
||||
c.MaxContext = maxContext
|
||||
}
|
||||
}
|
||||
|
||||
// WithTemperature sets temperature parameter
|
||||
//
|
||||
// Usage example:
|
||||
|
||||
Reference in New Issue
Block a user