feat(mcp): Add context engineering optimizations (#481)

Reduce context window usage by ~80-90% through:

1. Lazy Tool Schema Loading
   - discover_tools(): List tool names only (~500 bytes vs ~15KB)
   - get_tool_info(name): Get specific tool details on-demand

2. Minimal Issue Models
   - IssueMinimal: Lightweight model for list views (~80 bytes vs ~400 bytes)
   - Full Issue model preserved for show() command

3. Result Compaction
   - Auto-compact results with >20 issues
   - Returns preview (5 items) + total count + hint
   - Prevents unbounded context growth

4. Documentation
   - Updated CONTEXT_ENGINEERING.md with patterns and examples

Context savings:
- Tool schemas: 97% reduction (15KB → 500 bytes)
- List 50 issues: 80% reduction (20KB → 4KB)
- Ready work: 80% reduction (4KB → 800 bytes)

Inspired by MCP Bridge (github.com/mahawi1992/mwilliams_mcpbridge)
and Manus context engineering patterns.

Co-authored-by: Heal Smartly <marty@MacBook-Pro.local>
This commit is contained in:
mahawi1992
2025-12-14 14:21:22 -08:00
committed by GitHub
parent 2651620a4c
commit 700dca22b0
3 changed files with 489 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
"""Pydantic models for beads issue tracker types."""
from datetime import datetime
from typing import Literal
from typing import Literal, Any
from pydantic import BaseModel, Field, field_validator
@@ -11,6 +11,53 @@ IssueType = Literal["bug", "feature", "task", "epic", "chore"]
DependencyType = Literal["blocks", "related", "parent-child", "discovered-from"]
# =============================================================================
# CONTEXT ENGINEERING: Minimal Models for List Views
# =============================================================================
# These lightweight models reduce context window usage by ~80% for list operations.
# Use full Issue model only when detailed information is needed (show command).
class IssueMinimal(BaseModel):
"""Minimal issue model for list views (~80% smaller than full Issue).
Use this for ready_work, list_issues, and other bulk operations.
For full details including dependencies, use Issue model via show().
"""
id: str
title: str
status: IssueStatus
priority: int = Field(ge=0, le=4)
issue_type: IssueType
assignee: str | None = None
labels: list[str] = Field(default_factory=list)
dependency_count: int = 0
dependent_count: int = 0
@field_validator("priority")
@classmethod
def validate_priority(cls, v: int) -> int:
if not 0 <= v <= 4:
raise ValueError("Priority must be between 0 and 4")
return v
class CompactedResult(BaseModel):
"""Result container for compacted list responses.
When results exceed threshold, returns preview + metadata instead of full data.
This prevents context window overflow for large issue lists.
"""
compacted: bool = True
total_count: int
preview: list[IssueMinimal]
preview_count: int
hint: str = "Use show(issue_id) for full issue details"
# =============================================================================
# ORIGINAL MODELS (unchanged for backward compatibility)
# =============================================================================
class IssueBase(BaseModel):
"""Base issue model with shared fields."""