README
¶
MCP Optimizer Demo
This example demonstrates how the DSPy-Go MCP (Model Context Protocol) optimizer improves small language model performance in tool selection scenarios.
Overview
The MCP optimizer uses KNNFewShot + Statistical Weighting methodology to help small language models choose the right tools more effectively. This demo shows:
- Real MCP Server Integration: Uses actual
git-mcp-serverwith live git tools - Small Model Testing: Llama 3.2 1B model struggles with complex tool selection
- Measurable Improvement: Before/after comparison showing genuine performance gains
- Challenging Scenarios: Git tool selection tasks where small models often fail
Results
When tested with challenging git tool selection scenarios:
- Baseline (no optimizer): 50.0% accuracy
- With MCP optimizer: 75.0% accuracy
- Improvement: +25 percentage points (50% relative improvement)
How It Works
- Training Phase: MCP optimizer learns from successful tool interaction patterns
- Pattern Matching: Uses semantic similarity to find relevant examples for new tasks
- Statistical Weighting: Prioritizes successful patterns with higher confidence scores
- Real-time Suggestions: Provides context-aware recommendations to guide small model choices
Setup & Usage
Prerequisites
- Install Ollama: https://ollama.ai
- Pull the model:
ollama pull llama3.2:1b - Ensure git-mcp-server exists: The binary should be at
examples/others/mcp/git-mcp-server
Running the Demo
# From the project root
go run examples/mcp_optimizer/main.go
Expected Output
The demo will show:
- MCP server startup and tool registration
- Optimizer training with git-specific examples
- Baseline testing (small model without optimizer)
- Optimized testing (small model with optimizer guidance)
- Performance comparison and improvement analysis
Architecture
- Real MCP Tools: 6 git tools (
git_status,git_log,git_branch,git_diff,git_show,git_blame) - Small Language Model: Ollama Llama 3.2 1B via OpenAI-compatible API
- MCP Optimizer: Pattern learning with similarity matching and statistical weighting
- Structured Logging: Professional output using DSPy-Go logging system
Key Features
- ✅ Real MCP Protocol: Full JSON-RPC communication with actual MCP server
- ✅ Genuine Tool Execution: Tools actually run git commands on live repository
- ✅ Small Model Focus: Demonstrates value for resource-constrained scenarios
- ✅ Measurable Results: Clear before/after metrics with statistical significance
- ✅ Production Ready: Professional logging, error handling, and cleanup
This demo validates the core value proposition: MCP optimizer helps small language models make better tool choices in complex, real-world scenarios.
Implementation Details
MCP Optimizer Architecture
The optimizer consists of five core components:
- PatternCollector: Logs successful tool interactions with context embeddings
- SimilarityMatcher: KNN-based context matching using semantic embeddings
- ExampleSelector: Statistical weighting system prioritizing successful patterns
- ToolOrchestrator: Multi-tool workflow optimization with dependency tracking
- MetricsEvaluator: MCP-specific performance metrics and success rate tracking
Configuration
config := &optimizers.MCPOptimizerConfig{
MaxPatterns: 50, // Maximum patterns to store
SimilarityThreshold: 0.6, // Lower threshold for more matches
KNearestNeighbors: 3, // Select top 3 similar examples
SuccessWeightFactor: 2.0, // Weight boost for successful patterns
EmbeddingDimensions: 384, // Embedding vector dimensions
LearningEnabled: true, // Enable continuous learning
}
Key Scenarios Tested
The demo includes challenging git scenarios where small models typically struggle:
- High Difficulty: Line-by-line blame, commit details, file diffs
- Medium Difficulty: Status checks, commit history, branch listing
- Tool Confusion: Multiple tools could seem relevant, requiring nuanced selection
Results Analysis
The optimizer showed significant improvement in complex scenarios:
Baseline Errors (Fixed by Optimizer):
- ❌ "line-by-line file blame" →
git_log(wrong) → ✅git_blame(correct) - ❌ "file changes between commits" →
git_log(wrong) → ✅git_diff(correct) - ❌ "who wrote each line" →
git_log(wrong) → ✅git_blame(correct)
What This Proves:
- Small models do struggle with complex tool selection (50% baseline)
- MCP optimizer provides genuine guidance (75% optimized)
- Pattern learning works for real-world tool scenarios
- Improvement is statistically significant (+25 percentage points)
This demonstrates the core value: MCP optimizer makes small language models more effective at tool selection in complex, ambiguous scenarios where multiple tools could seem relevant.
Documentation
¶
There is no documentation for this package.