Understanding Model Context Protocol (MCP) sampling through hands-on examples and debugging real library issues.
- MCP Protocol - How sampling should work in theory
- Library Analysis - What's broken in mcp-go implementation
- Reference Implementation - Working examples for comparison
- Debugging Techniques - Tools and methods for protocol analysis
SSE Fundamentals Required: This project assumes you understand Server-Sent Events and bidirectional communication patterns. If you're new to SSE, start with: learn-sse-bidirectional
# Required: Anthropic API key for real LLM integration
export ANTHROPIC_API_KEY="your-api-key-here"
# Optional: Enable verbose logging
export MCP_DEBUG=1
# Terminal 1: Start enhanced server with file analysis
go run mcp-implementations/cmd/enhanced-server/main.go
# Terminal 2: Connect enhanced client with real LLM
go run mcp-implementations/cmd/enhanced-client/main.go
# Terminal 3: Run workflow test
go run debugging-tools/cmd/test-workflow/main.go
# See how SSE should work (from working-examples/)
go run working-examples/cmd/basic-sse-server/main.go
The mcp-go library has fundamental issues with SSE implementation that prevent MCP sampling from working correctly. This project:
- Identifies specific bugs (GitHub issue #530)
- Provides working reference implementations
- Offers debugging tools and techniques
Learn MCP through progressively complex examples:
- Working SSE patterns (baseline)
- Mock MCP implementations (learning)
- Real LLM integration (production-ready)
- Debugging tools (troubleshooting)
Help fix the mcp-go library by providing:
- Clear problem reproduction
- Working alternative implementations
- Detailed analysis documentation
- Test workflows for validation
learn-mcp-sampling/
├── README.md # This overview
├── MCP_PROTOCOL_EXPLAINED.md # MCP-specific concepts
├── working-examples/ # SSE reference implementations
│ ├── cmd/
│ │ ├── basic-sse-server/ # How SSE should work
│ │ └── basic-sse-client/ # Clean connection example
│ └── README.md # SSE comparison guide
├── mcp-implementations/ # MCP protocol examples
│ ├── cmd/
│ │ ├── enhanced-server/ # Real file analysis server
│ │ ├── enhanced-client/ # Anthropic API integration
│ │ ├── sampling-http-server/ # Basic MCP server
│ │ ├── sampling-http-client/ # Mock sampling client
│ │ └── simulate-sampling/ # Working simulation
│ └── files/ # Sample files for analysis
├── debugging-tools/ # Analysis and testing tools
│ ├── cmd/
│ │ ├── test-workflow/ # End-to-end testing
│ │ ├── check-sampling-clients/# Connection diagnostics
│ │ ├── debug-server/ # SSE debugging server
│ │ └── all-in-one-client/ # Session testing
│ └── analysis/
│ ├── SAMPLING_ISSUE_ANALYSIS.md # Bug documentation
│ └── LIBRARY_BUGS.md # GitHub issues summary
├── go.mod # Go module definition
└── go.sum # Dependency checksums
- Read the Analysis: Start with
debugging-tools/analysis/SAMPLING_ISSUE_ANALYSIS.md
- See Working SSE: Run examples in
working-examples/
- Try Broken MCP: Attempt
mcp-implementations/cmd/sampling-http-*
- Compare Results: Identify the difference
- Enhanced Server: Real file analysis with
enhanced-server/main.go
- Anthropic Integration: Live LLM calls with
enhanced-client/main.go
- Simulation: See intended behavior with
simulate-sampling/main.go
- Use Debug Tools: Test connections with tools in
debugging-tools/cmd/
- Analyze Library Code: Understand mcp-go StreamableHTTP issues
- Propose Fixes: Contribute to mcp-go project with evidence
- MCP Client declares sampling capability
- Someone calls a tool that needs LLM analysis
- MCP Server sends sampling request via SSE
- MCP Client processes request using LLM API
- MCP Client returns results via HTTP POST
- MCP Server provides results to tool caller
- SSE Connection Issues: "context deadline exceeded" errors
- Missing Transport Options: Need
WithContinuousListening()
- Session Management: Improper header handling
- Event Parsing: SSE stream processing bugs
Our enhanced implementations prove the pattern works:
- ✅ Clean SSE connections with instant delivery
- ✅ Proper bidirectional communication
- ✅ Real Anthropic API integration
- ✅ Multi-modal content support (text, images, PDFs)
- Server: Serves files from local directory for analysis
- Client: Uses Claude 3.5 Sonnet for content analysis
- Capabilities: Text, image, and binary file support
- Features: Summarization, code analysis, visual understanding
- Authentication: API key management
- Error Handling: Graceful failure modes
- Logging: Comprehensive request/response tracking
- Token Usage: Cost monitoring and optimization
- Security: Path traversal protection
# Test basic connectivity
go run debugging-tools/cmd/check-sampling-clients/main.go
# Debug SSE streams
go run debugging-tools/cmd/debug-server/main.go
# Full workflow test
go run debugging-tools/cmd/test-workflow/main.go
- SSE Stream Analysis: Monitor connection establishment
- Event Delivery Testing: Verify message transmission
- Response Correlation: Check request/response matching
- Session Management: Validate MCP headers
Compare mcp-go behavior with our reference implementations:
- Connection establishment patterns
- Event streaming reliability
- Error handling approaches
- Session lifecycle management
- GitHub Issue #530: SSE connection timeouts
- Missing Features: Continuous listening support
- Documentation: Incomplete sampling examples
- Testing: Limited real-world scenarios
- Clear Reproduction: Demonstrates exact failure conditions
- Working Reference: Shows how it should behave
- Test Cases: Provides validation scenarios
- Documentation: Explains complex concepts clearly
- Use our debugging tools to isolate specific issues
- Reference our working implementations for correct behavior
- Submit PRs to mcp-go with evidence from this project
- Test fixes using our comprehensive test suite
- Text Files: Code, documentation, configuration
- Images: Screenshots, diagrams, photos
- Binary Files: PDFs, archives, executables
- Custom Prompts: Flexible analysis workflows
- Rate Limiting: API call management
- Cost Control: Token usage monitoring
- Error Recovery: Robust failure handling
- Scaling: Multiple client support
- learn-sse-bidirectional - SSE fundamentals (prerequisite)
- mcp-go - The library we're helping to fix
- Model Context Protocol - Official MCP specification
- Hard Way Labs - Hands-on learning methodology
- "context deadline exceeded": Use
WithContinuousListening()
or our enhanced examples - No sampling requests: Check server tool registration and client capability declaration
- Authentication errors: Verify
ANTHROPIC_API_KEY
environment variable - Connection failures: Compare with working SSE examples in this project
- Start with working examples to establish baseline
- Use debugging tools to identify specific failure points
- Consult analysis documentation for known issues
- Compare behavior with reference implementations
MIT License - Use freely for learning, debugging, and contributing!
Debug Responsibly! 🔍
Part of the Hard Way Labs learning methodology - hands-on, progressive, multi-component education for developers.
Contributing to open source by making complex protocols understandable and fixable.