This TODO list outlines the roadmap for transforming the tw-stock-agent into a production-ready MCP server. The project currently has architectural inconsistencies with dual FastAPI/FastMCP implementations and lacks several critical production features.
Key Goals:
- Consolidate to pure FastMCP architecture
- Implement comprehensive error handling and validation
- Add database persistence and monitoring
- Achieve production-ready quality standards
-
Remove FastAPI Implementation
- Delete
tw_stock_agent/main.pyFastAPI server - Consolidate all endpoints into
mcp_server.pyusing FastMCP - Remove FastAPI dependencies from
pyproject.toml - Risk: Breaking changes for any existing FastAPI consumers
- Mitigation: Document migration path, provide compatibility layer if needed
- Acceptance Criteria: Single MCP server runs with
uv run python mcp_server.py
- Delete
-
Fix Parameter Naming Consistency
- Standardize on
stock_codeacross all tools (notstock_id) - Update tool schemas and documentation
- Acceptance Criteria: All tools use consistent parameter names
- Standardize on
-
Implement Async/Await Throughout
- Convert all tool functions to async
- Make
StockServicemethods async with proper I/O handling - Use
aiohttpinstead ofrequestsfor external API calls - Acceptance Criteria: No blocking I/O operations in main event loop
-
Implement Proper Tool Schemas
- Add input validation using Pydantic models
- Define proper JSON schemas for all tools
- Acceptance Criteria: All tools have valid JSON schemas and input validation
-
Fix Async Context Management
- Implement proper lifespan context for resource initialization
- Add graceful shutdown handling
- Acceptance Criteria: Server starts/stops cleanly with proper resource management
-
Add Database Layer
-- Suggested schema CREATE TABLE stock_cache ( stock_code VARCHAR(10) PRIMARY KEY, data_type VARCHAR(50), data JSONB, expires_at TIMESTAMP, created_at TIMESTAMP DEFAULT NOW() ); CREATE TABLE api_requests ( id SERIAL PRIMARY KEY, endpoint VARCHAR(255), stock_code VARCHAR(10), timestamp TIMESTAMP DEFAULT NOW(), success BOOLEAN, response_time_ms INTEGER );
- Implement SQLite for development, PostgreSQL for production
- Add database migration scripts
- Acceptance Criteria: Persistent caching with configurable TTL
-
Implement Structured Output
class StockDataResponse(BaseModel): stock_code: str company_name: str industry: str market_cap: Optional[float] current_price: float change_percent: float updated_at: datetime
- Create Pydantic models for all response types
- Support both structured and unstructured output
- Acceptance Criteria: All tools return typed, validated responses
-
Custom Exception Classes
class StockNotFoundError(Exception): pass class APIRateLimitError(Exception): pass class DataSourceUnavailableError(Exception): pass
- Implement centralized error handling
- Return proper MCP error responses
- Acceptance Criteria: All error conditions handled gracefully with meaningful messages
-
Input Validation & Security
- Validate stock codes (4-6 digit format)
- Sanitize all string inputs
- Implement parameter length limits
- Acceptance Criteria: No invalid inputs crash the server
-
Structured Logging Implementation
logger.info("Stock data fetched", extra={ "stock_code": stock_code, "response_time_ms": response_time, "cache_hit": cache_hit })
- Use JSON-formatted logs
- Add request tracing with correlation IDs
- Acceptance Criteria: Comprehensive logging for debugging and monitoring
-
Health Checks & Metrics
- Add
/healthendpoint - Implement basic metrics collection
- Monitor external API response times
- Acceptance Criteria: Observable service health and performance
- Add
- Dynamic Resource Discovery
@mcp.resource("stock://info/{stock_code}") @mcp.resource("stock://price/{stock_code}") @mcp.resource("stock://realtime/{stock_code}")
- Implement resource templates
- Add completion support for stock codes
- Acceptance Criteria: MCP clients can discover and access stock resources
-
Comprehensive Unit Tests
- Test all tools with mocked dependencies
- Achieve >90% test coverage
- Use
anyiofor async testing - Acceptance Criteria: Full test suite passes with high coverage
-
Integration Testing
- Test complete MCP server lifecycle
- Mock external APIs for reliable testing
- Acceptance Criteria: End-to-end tests verify MCP protocol compliance
-
Connection Pooling
- Implement
aiohttpsession pooling - Add background data refresh jobs
- Optimize cache key strategies
- Acceptance Criteria: Improved response times and resource utilization
- Implement
-
Rate Limiting Integration
- Connect existing
RateLimiterto MCP server - Add per-client rate limiting
- Acceptance Criteria: Respectful API usage with proper backoff
- Connect existing
-
Comprehensive Documentation
- Add detailed README with MCP integration examples
- Document all tool parameters and return values
- Create API reference documentation
- Acceptance Criteria: New developers can understand and use the server easily
-
Development Tools
- Create development server script
- Add data seeding scripts for testing
- Implement mock data providers
- Acceptance Criteria: Streamlined development workflow
-
OAuth 2.1 Implementation
- Add
TokenVerifierfor protected resources - Support both public and authenticated endpoints
- Acceptance Criteria: Secure access to premium features
- Add
-
Advanced Security Features
- Implement request signing
- Add audit logging
- Support API key authentication
- Acceptance Criteria: Enterprise-ready security features
-
Prompts Implementation
@mcp.prompt() def analyze_stock(stock_code: str, analysis_type: str = "technical") -> str: """Generate stock analysis prompt""" return f"Analyze {stock_code} using {analysis_type} analysis..."
- Create interactive prompt templates
- Add parameter completion
- Acceptance Criteria: Rich prompt-based interactions
-
Sampling & LLM Integration
- Add market commentary generation
- Implement narrative analysis
- Acceptance Criteria: AI-powered stock insights
- Additional Data Providers
- Integrate multiple Taiwan stock exchanges
- Add economic indicators
- Support cryptocurrency data
- Acceptance Criteria: Comprehensive financial data coverage
- Branch Strategy: Feature branches with PR reviews
- Code Quality: Pre-commit hooks with
ruffandmypy - Testing: TDD approach with automated testing
- Documentation: Update docs with each feature
- FastMCP: Primary MCP server framework
- Pydantic: Data validation and serialization
- SQLAlchemy + Alembic: Database ORM and migrations
- aiohttp: Async HTTP client
- pytest + anyio: Testing framework
- structlog: Structured logging
- Server starts without errors
- All tools return valid responses
- >90% test coverage maintained
- <500ms average response time
- Zero security vulnerabilities
- Complete MCP protocol compliance
-
FastAPI to FastMCP Migration
- Risk: Breaking existing integrations
- Mitigation: Phased migration with compatibility testing
-
Database Schema Changes
- Risk: Data loss during migration
- Mitigation: Backup procedures and rollback plans
-
External API Dependencies
- Risk: Rate limiting and availability
- Mitigation: Robust caching and fallback mechanisms
-
Performance Optimization
- Risk: Premature optimization complexity
- Mitigation: Profile before optimizing, measure improvements
-
Authentication Implementation
- Risk: Security vulnerabilities
- Mitigation: Use proven OAuth libraries, security audits
Weeks 1-2: P0 Critical fixes (2 developers) Weeks 3-6: P1 Production essentials (2-3 developers) Weeks 7-10: P2 Quality & enhancement (2 developers) Weeks 11-14: P3 Advanced features (1-2 developers)
Total Effort: ~20-30 developer weeks for complete implementation
This roadmap prioritizes architectural consistency and production readiness while maintaining a clear path toward advanced MCP features. The consolidation to FastMCP in P0 is critical as it unblocks effective development of all subsequent features.
Regular checkpoints should assess progress against success metrics and adjust priorities based on business requirements and technical discoveries during implementation.