This guide documents the complete implementation of Coffee Engine v2.0 following the Modular Development Architecture specification. All phases have been successfully implemented.
- Configuration-driven provider registry (
config/providers.ts) - Event routing configuration (
config/events.ts) - Zod schema validation for normalized events (
lib/schemas.ts) - Type-safe enums for events (
lib/enums.ts) - Singleton pattern with exported interfaces for all services
- Auth middleware (
lib/auth-middleware.ts) - Verification service with strict typing
- Complete OpenAPI specification at
/api/openapi - Webhook handler at
/api/webhooks/[providerId] - Verification endpoint at
/api/coffee/verify
- New File:
mcp/server.ts- Standalone MCP server exposing verification tool - New File:
app/api/mcp/route.ts- HTTP-based MCP endpoint for remote connections - Zod schema validation for MCP inputs
- Zero logic duplication - MCP tools call the same
VerificationService - Configured for Claude Desktop and other MCP clients
- New Component:
components/verification-card.tsx- Transaction verification UI - New Component:
components/chat-interface.tsx- Simple chat with verification gate - New Component:
components/ai-chat.tsx- Full-featured AI chat with provider support - New Library:
lib/api-client.ts- Frontend API abstraction - New Endpoint:
app/api/chat/route.ts- AI chat backend with verification - New Page:
app/premium/page.tsx- Complete premium flow demo
- providers.ts - List of enabled providers
- events.ts - Event-to-handler mappings
- index.ts - Centralized config export
- Verification Service - Pure business logic
- Event Router Service - Provider-agnostic routing
- Token Store Service - Zod-validated storage
- Provider Registry Service - Dynamic registration
- Auth Middleware - Reusable authentication
- BMC Provider - Buy Me a Coffee webhook handler
- Implements
IWebhookProviderinterface - Self-contained with own normalization logic
- HTTP Endpoints - REST API (webhooks, verification, health)
- MCP Endpoint - Model Context Protocol support
- Chat Endpoint - AI provider integration
- OpenAPI - Auto-documented specification
- Verification UI - Payment verification flow
- Chat Components - Simple and AI-powered chat
- Premium Page - Full feature showcase
- API Client - Type-safe frontend requests
mcp/server.ts - Standalone MCP server for Claude Desktop
app/api/mcp/route.ts - HTTP MCP endpoint (remote connections)
lib/api-client.ts - Frontend API abstraction layer
components/verification-card.tsx - Payment verification UI component
components/chat-interface.tsx - Simple chat with verification gate
components/ai-chat.tsx - AI-powered chat component
app/api/chat/route.ts - Chat API with AI provider support
app/premium/page.tsx - Premium feature demo page
lib/schemas.ts - Zod validation schemas
lib/enums.ts - TypeScript enums for type safety
lib/auth-middleware.ts - Auth HOF for protecting routes
config/providers.ts - Enabled providers list
config/events.ts - Event routing configuration
config/index.ts - Config export
import { apiClient } from '@/lib/api-client';
// In a React component
const result = await apiClient.verifyTransaction('TXN_12345ABC', 'bmc');
if (result.valid) {
console.log(`Verified ${result.payerEmail} paid ${result.amountMinor / 100} ${result.currency}`);
}import { ChatInterface } from '@/components/chat-interface';
export function MyApp() {
return <ChatInterface onVerified={(result) => console.log(result)} />;
}import { AIChat } from '@/components/ai-chat';
export function PremiumChat() {
const [apiKey, setApiKey] = useState('');
return (
<AIChat
apiKey={apiKey}
systemPrompt="You are a premium AI assistant"
/>
);
}Edit ~/.../Claude/claude_desktop_config.json:
{
"mcpServers": {
"coffee-engine": {
"command": "node",
"args": ["/path/to/coffee-engine/mcp/server.js"]
}
}
}Then in Claude, use the "verify_transaction" tool:
User: "Verify transaction TXN_ABC123"
Claude: Uses the verify_transaction MCP tool
Result: "✅ Transaction verified for user@example.com, Amount: $5.00"
- API Key Protection: All endpoints requiring authentication use
withAuth()middleware - Header-based Auth:
x-coffee-api-keyheader for API requests - Session Tokens: Frontend maintains optional session after verification
- Zod Schemas: All inputs validated at route entry points
- Type Safety: TypeScript strict mode throughout
- Schema Validation: Token storage validates against NormalizedEventSchema
- Client-Side Keys: User's OpenAI/Anthropic keys stay in browser
- Never Logged: API keys are not stored or logged by Coffee Engine
- Optional: Server Proxy: For users who prefer server-side proxying
- No Data Sharing: User's AI API keys never sent to Coffee Engine
- Create Provider Class (
providers/new-provider.ts):
import type { IWebhookProvider } from '@/types';
export class NewProvider implements IWebhookProvider {
readonly providerId = 'new-provider';
async verifyRequest(request: Request): Promise<boolean> {
// Implement signature verification
}
async normalizePayload(payload: unknown): Promise<NormalizedEvent> {
// Normalize to standard event format
}
}- Register in Config (
config/providers.ts):
import { NewProvider } from '@/providers/new-provider';
export const ENABLED_PROVIDERS = [
new BmcProvider(),
new NewProvider(), // Add here
];- Add Event Handlers (
config/events.ts):
export const EVENT_MAP = [
{ provider: 'new-provider', event: 'payment.created', handler: handleDonation },
// ...
];NEXT_PUBLIC_BASE_URL=https://your-domain.com
COFFEE_API_KEY=your_secret_api_key
KV_REST_API_URL=your_vercel_kv_url
KV_REST_API_TOKEN=your_vercel_kv_tokenBMC_WEBHOOK_SECRET=your_bmc_secret
STRIPE_API_KEY=your_stripe_keyNEXT_PUBLIC_COFFEE_API_KEY=frontend_api_key (if needed)git push origin main
# Auto-deploys via Vercel GitHub integrationFor production MCP usage, either:
-
As Subprocess (recommended for Claude Desktop):
- Run as standalone Node process with stdio transport
- Requires environment variables accessible to process
-
As HTTP Server (recommended for remote):
- Use
/api/mcpendpoint - Accessible via standard HTTPS
- Use
curl -X POST http://localhost:3000/api/coffee/verify \
-H "x-coffee-api-key: test-key" \
-H "Content-Type: application/json" \
-d '{"transactionId":"TXN_TEST"}'curl http://localhost:3000/api/openapi | jq .- Configure in
claude_desktop_config.json - Restart Claude Desktop
- MCP icon appears in bottom toolbar
- Use verify_transaction tool
- Check Node.js version (16+)
- Verify path in claude_desktop_config.json
- Check environment variables
- Run with
node mcp/server.jsto see errors
- Confirm transaction exists in KV store
- Check provider ID is correct
- Verify API key if using auth
- Ensure API key is set for chosen provider
- Check NEXT_PUBLIC_BASE_URL
- Verify /api/chat endpoint is accessible
- Check browser console for CORS errors
Potential future enhancements:
- Phase 5: Advanced Analytics & Dashboards
- Phase 6: Multi-provider Payment Processing
- Phase 7: Subscription Management
- Phase 8: Custom Branded AI Agents