Skip to content

feat(mcp): add context length guard to prevent oversized requests#1416

Merged
shinchan-zhai merged 2 commits intoNoFxAiOS:devfrom
shinchan-zhai:feat/context-length-guard
Mar 18, 2026
Merged

feat(mcp): add context length guard to prevent oversized requests#1416
shinchan-zhai merged 2 commits intoNoFxAiOS:devfrom
shinchan-zhai:feat/context-length-guard

Conversation

@shinchan-zhai
Copy link
Collaborator

Problem

When nofx calls AI models via claw402, messages can exceed model context limits (e.g. DeepSeek 131K), causing 400 errors:

This model's maximum context length is 131072 tokens. However, you requested 154604 tokens

Solution

  • New MaxContext config field (default 0 = no limit, backward compatible)
  • Token estimation (~3 chars/token heuristic for mixed CJK/English)
  • Auto-truncation of oldest non-system messages when limit exceeded
  • Supports both map[string]string and map[string]any message formats
  • Preserves all system messages + at least 1 non-system message
  • Logs warning when truncation occurs

Usage

client := mcp.NewDeepSeekClient(mcp.WithMaxContext(131072)) // 128K

Files Changed

  • mcp/config.go — add MaxContext field
  • mcp/options.go — add WithMaxContext() option
  • mcp/context_guard.go — token estimation + truncation (both message types)
  • mcp/client.go — integrate guard in BuildMCPRequestBody + BuildRequestBodyFromRequest
  • mcp/context_guard_test.go — 6 test cases, all passing

- 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))
@cla-assistant
Copy link

cla-assistant bot commented Mar 17, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant
Copy link

cla-assistant bot commented Mar 17, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link

🤖 Advisory Check Results

These are advisory checks to help improve code quality. They won't block your PR from being merged.

📋 PR Information

Title Format: ✅ Good - Follows Conventional Commits
PR Size: 🟡 Medium (311 lines: +311 -0)

🔧 Backend Checks

Go Formatting: ⚠️ Needs formatting

Files needing formatting
api/strategy.go
api/utils_test.go
config/config.go
kernel/formatter.go
kernel/grid_engine.go
kernel/validate_test.go
main.go
manager/trader_manager.go
market/data.go
market/types.go

Go Vet: ✅ Good
Tests: ✅ Passed

Fix locally:

go fmt ./...      # Format code
go vet ./...      # Check for issues
go test ./...     # Run tests

⚛️ Frontend Checks

Build & Type Check: ✅ Success

Fix locally:

cd web
npm run build  # Test build (includes type checking)

📖 Resources

Questions? Feel free to ask in the comments! 🙏


These checks are advisory and won't block your PR from being merged. This comment is automatically generated from pr-checks-run.yml.

@shinchan-zhai shinchan-zhai merged commit 16ebe0a into NoFxAiOS:dev Mar 18, 2026
19 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant