# โ
Cloudflare Workers Deployment - READY
**Date**: 2025-11-12
**Project**: dadosbr-mcp
**Status**: ๐ข READY FOR DEPLOYMENT
---
## โ
Critical Issues Fixed
### 1. โ
Worker Entry Point Configured
- **Fixed**: Updated wrangler.toml to point to `worker.js`
- **Before**: `main = "build/lib/workers/mcp-agent.js"` (Durable Objects only)
- **After**: `main = "build/lib/workers/worker.js"` (Full routing)
- **Result**: All endpoints now accessible
### 2. โ
TAVILY_API_KEY Injection Added
- **Fixed**: Added environment variable injection in 3 locations:
- `lib/workers/worker.ts` (main entry point)
- `lib/adapters/cloudflare.ts` (handleMCPEndpoint)
- `lib/adapters/cloudflare.ts` (handleMCPRequest)
- **Result**: cnpj_intelligence and cnpj_search tools will work correctly
### 3. โ
Durable Objects Configuration Removed
- **Fixed**: Removed unnecessary Durable Objects bindings
- **Reason**: Not needed for traditional worker approach
- **Result**: Simplified configuration, lower costs
### 4. โ
Build Successful
- **Build time**: 2025-11-12 02:09
- **Output**: `build/lib/workers/worker.js` (6.2KB)
- **Compilation**: No errors
---
## ๐ Available Endpoints
All endpoints are now accessible:
### Core MCP Endpoints
- โ
**POST /mcp** - MCP JSON-RPC protocol
- โ
**GET/POST /sse** - Server-Sent Events streaming
- โ
**GET /health** - Health check
### OAuth Endpoints (ChatGPT Integration)
- โ
**GET /.well-known/oauth-authorization-server** - OAuth discovery
- โ
**GET /.well-known/openid_configuration** - OpenID configuration
- โ
**GET /oauth/authorize** - OAuth authorization
- โ
**POST /oauth/token** - OAuth token exchange
- โ
**GET /oauth/userinfo** - User information
- โ
**GET /.well-known/jwks.json** - JSON Web Key Set
### REST API Endpoints
- โ
**GET /cnpj/{cnpj}** - CNPJ lookup
- โ
**GET /cep/{cep}** - CEP lookup
- โ
**POST /search** - Web search
- โ
**POST /intelligence** - Intelligence search
- โ
**POST /thinking** - Sequential thinking
- โ
**GET /openapi.json** - OpenAPI schema
### Root Endpoint
- โ
**GET /** - Service information
---
## ๐ Required Secrets
Before deploying, you MUST configure the TAVILY_API_KEY secret:
```bash
# Set the Tavily API key (required for cnpj_intelligence and cnpj_search)
wrangler secret put TAVILY_API_KEY
# When prompted, enter your Tavily API key
# Get your key from: https://tavily.com
```
### Optional Secrets
```bash
# Optional: API key for authentication (if you want to protect your worker)
wrangler secret put MCP_API_KEY
```
---
## ๐ Deployment Commands
### Option 1: Deploy to Production
```bash
# Deploy to production
npm run deploy
# Or directly with wrangler
wrangler deploy
```
### Option 2: Deploy to Staging (Recommended First)
```bash
# Deploy to staging environment
npm run deploy:staging
# Or directly with wrangler
wrangler deploy --env staging
```
### View Logs
```bash
# Tail logs from production
wrangler tail
# Tail logs from staging
wrangler tail --env staging
```
---
## ๐ KV Namespaces
Already configured in wrangler.toml:
### Default/Production
- **MCP_CACHE**: `9e3f2b99610b4c67abb1080b5f927efa`
- **MCP_KV**: `9e3f2b99610b4c67abb1080b5f927efa`
### Staging
- **MCP_CACHE**: `69470c08bd184c7ba625f09690c8a1e9`
- **MCP_KV**: `69470c08bd184c7ba625f09690c8a1e9`
---
## ๐งช Testing After Deployment
### 1. Health Check
```bash
# Replace with your worker URL
curl https://mcp-dadosbr-aredes.cristianocosta.workers.dev/health
```
Expected response:
```json
{
"status": "healthy",
"service": "mcp-dadosbr",
"version": "1.0.0",
"timestamp": "2025-11-12T02:09:00.000Z",
"runtime": "cloudflare-workers"
}
```
### 2. Service Info
```bash
curl https://mcp-dadosbr-aredes.cristianocosta.workers.dev/
```
Expected: JSON with service information and available endpoints
### 3. CNPJ Lookup (REST API)
```bash
curl https://mcp-dadosbr-aredes.cristianocosta.workers.dev/cnpj/11222333000181
```
Expected: Company data from Receita Federal
### 4. MCP JSON-RPC
```bash
curl -X POST https://mcp-dadosbr-aredes.cristianocosta.workers.dev/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'
```
Expected: List of 5 available tools
### 5. Intelligence Search (requires TAVILY_API_KEY)
```bash
curl -X POST https://mcp-dadosbr-aredes.cristianocosta.workers.dev/intelligence \
-H "Content-Type: application/json" \
-d '{
"cnpj": "11222333000181",
"categories": ["news", "legal"],
"max_results_per_query": 3
}'
```
Expected: Company data + web search results
---
## ๐ฏ MCP Tools Available
After deployment, these tools will be available:
1. **cnpj_lookup** - Look up Brazilian company by CNPJ
2. **cep_lookup** - Look up Brazilian postal code
3. **cnpj_search** - Search companies with web queries
4. **sequentialthinking** - Sequential thinking processor
5. **cnpj_intelligence** - Intelligent automatic company research
---
## โ๏ธ Environment Variables
Configured in wrangler.toml (no changes needed):
```toml
MCP_TRANSPORT = "http"
MCP_HTTP_PORT = "8787"
MCP_CACHE_SIZE = "256"
MCP_CACHE_TTL = "60000"
```
---
## ๐ Cloudflare Workers Limits
### Free Tier (100,000 requests/day)
- โ
CPU Time: 10ms per request
- โ
Memory: 128MB
- โ
Subrequests: 50 per request
### Paid Tier (Recommended for production)
- โ
CPU Time: 50,000ms per request
- โ
Memory: 128MB
- โ
Subrequests: 1,000 per request
**Note**: Intelligence search with multiple concurrent searches may need paid tier for optimal performance.
---
## ๐ Deployment Workflow
### First Time Deployment
1. **Set secrets**:
```bash
wrangler secret put TAVILY_API_KEY
```
2. **Deploy to staging**:
```bash
npm run deploy:staging
```
3. **Test all endpoints** (see Testing section above)
4. **Deploy to production**:
```bash
npm run deploy
```
5. **Monitor logs**:
```bash
wrangler tail
```
### Subsequent Deployments
1. **Make changes** to code
2. **Rebuild**:
```bash
npm run build:worker
```
3. **Deploy**:
```bash
npm run deploy:staging # Test in staging first
npm run deploy # Then deploy to production
```
---
## ๐ Troubleshooting
### Issue: "TAVILY_API_KEY not configured"
**Solution**: Run `wrangler secret put TAVILY_API_KEY`
### Issue: 404 on all endpoints
**Solution**: Check that `main = "build/lib/workers/worker.js"` in wrangler.toml
### Issue: Tools not working
**Solution**:
1. Verify build succeeded: `npm run build:worker`
2. Check logs: `wrangler tail`
3. Verify secrets: `wrangler secret list`
### Issue: Timeout errors
**Solution**:
- Free tier has 10ms CPU limit
- Upgrade to paid tier for production workloads
- Reduce max_queries in intelligence search
---
## ๐ Monitoring
### View Logs
```bash
# Production logs
wrangler tail
# Staging logs
wrangler tail --env staging
# Filter by status
wrangler tail --status error
```
### Metrics
View metrics in Cloudflare Dashboard:
- Requests per second
- CPU time usage
- Error rate
- KV read/write operations
---
## โ
Pre-Deployment Checklist
- [x] Build successful
- [x] Entry point configured correctly
- [x] TAVILY_API_KEY injection added
- [x] Durable Objects removed
- [x] KV namespaces configured
- [ ] TAVILY_API_KEY secret deployed
- [ ] Staging deployment tested
- [ ] All endpoints verified
- [ ] Production deployment completed
---
## ๐ Next Steps
1. **Deploy TAVILY_API_KEY secret** (REQUIRED):
```bash
wrangler secret put TAVILY_API_KEY
```
2. **Deploy to staging**:
```bash
npm run deploy:staging
```
3. **Test endpoints**:
```bash
curl https://mcp-dadosbr-aredes-staging.cristianocosta.workers.dev/health
```
4. **Deploy to production**:
```bash
npm run deploy
```
5. **Set up monitoring**:
- Configure alerts in Cloudflare Dashboard
- Monitor error rates and response times
---
## ๐ Documentation
- **MCP Protocol**: https://modelcontextprotocol.io
- **Cloudflare Workers**: https://developers.cloudflare.com/workers/
- **Project README**: README.md
- **Code Review**: CLOUDFLARE_WORKERS_REVIEW.md
---
## ๐ Support
- **GitHub Issues**: https://github.com/cristianoaredes/mcp-dadosbr/issues
- **Cloudflare Community**: https://community.cloudflare.com
- **MCP Discord**: https://discord.gg/modelcontextprotocol
---
**Status**: โ
READY FOR DEPLOYMENT
**Last Updated**: 2025-11-12 02:09 BRT