Development Guide
Welcome to the tai-mcp development guide. This documentation covers architecture, development setup, testing strategies, and contribution guidelines for developers working on tai-mcp.
Project Overview
tai-mcp is a TypeScript-based Model Context Protocol (MCP) server that provides email functionality to AI agents. The project emphasizes clean architecture, comprehensive testing, and developer-friendly workflows.
Key Design Principles
- Modular Architecture - Clean separation of concerns across layers
- Type Safety - Full TypeScript coverage with strict typing
- Testing First - Comprehensive integration and unit test coverage
- Security Focus - Secure authentication and input validation
- Developer Experience - Clear APIs, good logging, and debugging tools
Technology Stack
Core Technologies
- TypeScript 5.3+ - Type-safe JavaScript with modern features
- Node.js 18+ - Runtime environment with ES modules support
- Zod - Runtime type validation and schema definition
- Jest - Testing framework with ES modules support
Email & Content Processing
- node-fetch - HTTP client for API communication
- marked - Markdown to HTML conversion
- turndown - HTML to markdown conversion
Developer Tools
- ESLint - Code linting with TypeScript rules
- VitePress - Documentation site generation
- MCP Inspector - Tool debugging and introspection
Architecture Overview
tai-mcp follows a layered architecture pattern:
┌─────────────────────────────────────────┐
│ CLI Interface │
│ (src/index.ts) │
├─────────────────────────────────────────┤
│ Command Layer │
│ (src/commands/) │
├─────────────────────────────────────────┤
│ MCP Server │
│ (src/server.ts) │
├─────────────────────────────────────────┤
│ Tool Layer │
│ (src/tools/) │
├─────────────────────────────────────────┤
│ Service Layer │
│ (src/services/) │
├─────────────────────────────────────────┤
│ Utility Layer │
│ (src/utils/) │
└─────────────────────────────────────────┘Layer Responsibilities
CLI Interface (src/index.ts)
- Command-line argument parsing
- Environment validation
- Process lifecycle management
- Error handling and graceful shutdown
Command Layer (src/commands/)
- User registration workflows
- Live mode email polling
- Command-specific logic and validation
MCP Server (src/server.ts)
- MCP protocol implementation
- Tool registration and exposure
- Server resources and capabilities
Tool Layer (src/tools/)
- Individual MCP tool implementations
- Parameter validation with Zod schemas
- Response formatting for AI agents
Service Layer (src/services/)
- Core business logic
- External API communication
- Authentication and token management
Utility Layer (src/utils/)
- Shared functionality
- Configuration management
- Logging and debugging utilities
Development Setup
Prerequisites
- Node.js 18.0.0 or higher
- npm or yarn package manager
- Git for version control
Initial Setup
Clone Repository
bashgit clone https://github.com/BMPixel/tai-mcp.git cd tai-mcpInstall Dependencies
bashnpm installEnvironment Configuration
bash# Create .env file for development cp .env.example .env # Edit with your development credentials NAME=dev-username PASSWORD=dev-password INSTANCE=dev LOG_LEVEL=debugBuild Project
bashnpm run buildVerify Setup
bashnpm test
Development Workflow
Development Mode with Watch:
npm run dev- TypeScript compilation with watch mode
- Automatic rebuilds on file changes
- Real-time error reporting
Testing During Development:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run only unit tests
npm run test:unit
# Run only integration tests
npm run test:integrationCode Quality:
# Lint code
npm run lint
# Fix auto-fixable lint issues
npm run lint -- --fixDocumentation:
# Start documentation dev server
npm run docs:dev
# Build documentation
npm run docs:buildProject Structure
src/
├── index.ts # Main entry point & CLI router
├── server.ts # MCP server implementation
├── commands/ # CLI command implementations
│ ├── register.ts # User registration command
│ └── live.ts # Live polling command
├── tools/ # MCP tool implementations
│ ├── send-email.ts # send_email tool
│ ├── fetch-email.ts # fetch_email tool
│ ├── list-inbox.ts # list_inbox tool
│ └── reply-email.ts # reply_email tool
├── services/ # Core business logic
│ ├── auth.ts # Authentication service
│ ├── api-client.ts # API communication
│ └── email-poller.ts # Email polling for live mode
├── utils/ # Utility modules
│ ├── logger.ts # Logging utility
│ ├── config.ts # Configuration management
│ ├── html-to-markdown.ts # Content conversion
│ └── mcp-setup.ts # MCP configuration helpers
└── types/ # TypeScript type definitions
├── api.ts # API response types
└── tools.ts # Tool parameter schemasKey Files Explained
src/index.ts - Application entry point
- Parses command-line arguments
- Validates environment variables
- Routes to appropriate command handlers
- Manages process lifecycle and signals
src/server.ts - MCP server core
- Implements MCP protocol
- Registers all email tools
- Handles server capabilities and resources
- Manages tool execution context
src/services/api-client.ts - API communication hub
- Centralized HTTP client with authentication
- Automatic JWT token management
- Request/response logging and error handling
- Retry logic and timeout management
src/types/tools.ts - Type definitions and validation
- Zod schemas for all tool parameters
- TypeScript interfaces for type safety
- Runtime validation and error messages
- Parameter documentation for AI agents
Development Best Practices
Code Style
TypeScript Configuration:
- Strict mode enabled for maximum type safety
- ES modules with modern JavaScript features
- Explicit return types for public functions
- Comprehensive type definitions
Naming Conventions:
// Use descriptive, self-documenting names
const authenticatedApiClient = new ApiClient(config);
const emailValidationSchema = z.object({...});
// Functions should be verbs
async function sendEmailToRecipient(params: SendEmailParams): Promise<void>
// Classes should be nouns
class EmailPollingService implements PollingServiceError Handling:
// Use structured error logging
try {
await apiClient.sendEmail(emailRequest);
} catch (error) {
logger.error('Failed to send email', {
error: error instanceof Error ? error.message : error,
emailRequest: { to: emailRequest.to, subject: emailRequest.subject }
});
throw error;
}Testing Strategy
tai-mcp uses a dual testing approach:
Integration Tests (Primary)
- Test complete workflows with real API calls
- Two-account testing pattern (sender/receiver)
- End-to-end validation of email functionality
- Real network conditions and API behavior
Unit Tests (Secondary)
- Test individual components in isolation
- Mock external dependencies
- Fast execution for development workflow
- Business logic validation
Test Structure:
tests/
├── integration/ # End-to-end tests
│ └── api.test.ts # Complete email workflows
├── unit/ # Component tests
│ ├── services/ # Service layer tests
│ ├── tools/ # Tool implementation tests
│ └── utils/ # Utility function tests
└── mocks/ # Test fixtures and mocks
└── api-responses.ts # Mock API responsesDevelopment Tools
MCP Inspector:
npm run inspector- Interactive tool debugging
- Parameter validation testing
- Response format verification
- Real-time MCP protocol inspection
Debug Logging:
LOG_LEVEL=debug npm run dev- Detailed operation logging
- API request/response details
- Authentication flow tracking
- Error context and stack traces
Type Checking:
# TypeScript compilation check
npx tsc --noEmit
# Watch mode for continuous checking
npx tsc --noEmit --watchContributing Guidelines
Getting Started
Fork the Repository
bashgit clone https://github.com/your-username/tai-mcp.git cd tai-mcp git remote add upstream https://github.com/BMPixel/tai-mcp.gitCreate Feature Branch
bashgit checkout -b feature/your-feature-nameMake Changes
- Follow existing code patterns
- Add tests for new functionality
- Update documentation as needed
- Ensure type safety
Test Changes
bashnpm test npm run lint npm run buildSubmit Pull Request
- Clear description of changes
- Reference related issues
- Include test coverage
- Update documentation
Code Review Process
Required Checks:
- All tests passing (integration + unit)
- ESLint validation without errors
- TypeScript compilation successful
- Documentation updates included
Review Criteria:
- Code follows project patterns
- Appropriate test coverage
- Security considerations addressed
- Performance impact assessed
Common Development Tasks
Adding New MCP Tools
Create Tool Implementation:
typescript// src/tools/new-tool.ts export function registerNewTool(server: McpServer, apiClient: ApiClient, config: Config): void { server.tool( 'new_tool', 'Description of the new tool', newToolSchema.shape, async (params): Promise<ToolResponse> => { // Implementation } ); }Define Parameter Schema:
typescript// src/types/tools.ts export const newToolSchema = z.object({ parameter: z.string().describe('Parameter description') });Register Tool:
typescript// src/server.ts import { registerNewTool } from './tools/new-tool.js'; // In createServer function registerNewTool(server, apiClient, config);Add Tests:
typescript// tests/unit/tools/new-tool.test.ts describe('new_tool', () => { test('should handle valid parameters', async () => { // Test implementation }); });
Extending API Client
// src/services/api-client.ts
export class ApiClient {
async newApiMethod(params: NewApiParams): Promise<NewApiResponse> {
const response = await this.makeRequest('POST', '/api/v1/new-endpoint', params);
return response.json();
}
}Adding Configuration Options
Update Config Interface:
typescript// src/types/tools.ts export interface Config { // existing fields... newOption: string; }Add Environment Variable Loading:
typescript// src/utils/config.ts export function loadConfig(): Config { return { // existing fields... newOption: process.env.NEW_OPTION || 'default-value' }; }Update Documentation:
- Add to configuration guide
- Update environment variable reference
- Include usage examples
Debugging and Troubleshooting
Common Development Issues
TypeScript Compilation Errors:
# Check for type errors
npx tsc --noEmit
# Common issues:
# - Missing type imports
# - Incorrect interface implementations
# - Import path issues with .js extensionsTest Failures:
# Run specific test file
npm test -- tests/integration/api.test.ts
# Debug test with verbose output
npm test -- --verbose
# Run tests in debug mode
node --inspect-brk node_modules/.bin/jestRuntime Issues:
# Enable debug logging
LOG_LEVEL=debug npm start
# Check environment variables
env | grep -E "(NAME|PASSWORD|INSTANCE)"
# Verify API connectivity
curl https://tai.chat/api/v1/healthPerformance Profiling
# Profile memory usage
node --inspect build/src/index.js
# Monitor API performance
LOG_LEVEL=debug npm start 2>&1 | grep "API request"
# Check bundle size
npm run build && du -h build/Release Process
Version Management
tai-mcp follows semantic versioning:
- Major (1.0.0) - Breaking changes
- Minor (0.1.0) - New features, backward compatible
- Patch (0.0.1) - Bug fixes, backward compatible
Release Checklist
Pre-release Testing
bashnpm test npm run lint npm run build npm run docs:buildVersion Update
bashnpm version patch|minor|majorDocumentation Update
- Update CHANGELOG.md
- Verify documentation accuracy
- Update API documentation if needed
Publish Release
bashgit push origin main --tags npm publish
Next Steps
Explore specific development topics:
- Architecture Guide - Detailed architectural patterns
- Testing Guide - Comprehensive testing strategies
- API Reference - Tool implementation patterns
- GitHub Repository - Source code and issues