File size: 6,620 Bytes
ed1f7cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# Spend Analyzer MCP - Deployment Guide
This guide covers deploying the Spend Analyzer MCP to Modal.com with Claude and SambaNova Cloud API integration.
## Prerequisites
1. **Modal Account**: Sign up at [modal.com](https://modal.com)
2. **API Keys**:
- Anthropic API key for Claude
- SambaNova Cloud API key
3. **Email Credentials**: App-specific passwords for email access
## Setup Instructions
### 1. Install Modal CLI
```bash
pip install modal
```
### 2. Authenticate with Modal
```bash
modal token new
```
### 3. Create Modal Secrets
Create the required secrets in your Modal dashboard or via CLI:
#### Anthropic API Key
```bash
modal secret create anthropic-api-key ANTHROPIC_API_KEY=your_claude_api_key_here
```
#### SambaNova API Key
```bash
modal secret create sambanova-api-key SAMBANOVA_API_KEY=your_sambanova_api_key_here
```
#### Email Credentials
```bash
modal secret create email-credentials \
[email protected] \
EMAIL_PASS=your_app_password \
IMAP_SERVER=imap.gmail.com
```
### 4. Deploy to Modal
```bash
# Deploy the application
modal deploy modal_deployment.py
# Or run locally for testing
modal run modal_deployment.py
```
## API Providers
### Claude (Anthropic)
- **Model**: claude-3-sonnet-20240229
- **Features**: Advanced reasoning, financial analysis
- **Setup**: Get API key from [console.anthropic.com](https://console.anthropic.com)
### SambaNova Cloud
- **Model**: Meta-Llama-3.1-8B-Instruct
- **Features**: Fast inference, cost-effective
- **Setup**: Get API key from [cloud.sambanova.ai](https://cloud.sambanova.ai)
- **API Format**: OpenAI-compatible
## Available Modal Functions
### 1. `process_bank_statements`
Process bank statements from email attachments.
**Parameters:**
- `email_config`: Email configuration dict
- `days_back`: Number of days to look back (default: 30)
- `passwords`: Optional PDF passwords dict
**Returns:**
- Processed statements list
- Transaction analysis
- Error handling for password-protected PDFs
### 2. `analyze_uploaded_statements`
Analyze directly uploaded PDF statements.
**Parameters:**
- `pdf_contents`: Dict of filename -> PDF bytes
- `passwords`: Optional PDF passwords dict
**Returns:**
- Analysis results
- Transaction categorization
- Financial insights
### 3. `get_ai_analysis`
Get AI-powered financial analysis.
**Parameters:**
- `analysis_data`: Financial data dict
- `user_question`: Optional specific question
- `provider`: "claude" or "sambanova" (default: "claude")
**Returns:**
- AI analysis text
- Usage statistics
- Provider information
### 4. `save_user_data` / `load_user_data`
Persistent storage for user analysis data.
**Features:**
- User-specific data isolation
- Timestamp tracking
- JSON serialization
### 5. `mcp_webhook`
MCP protocol endpoint for external integrations.
**Features:**
- Tool registration
- Resource management
- Error handling
## Environment Variables
The following environment variables are automatically available in Modal functions:
```bash
ANTHROPIC_API_KEY=your_claude_key
SAMBANOVA_API_KEY=your_sambanova_key
EMAIL_USER=your_email
EMAIL_PASS=your_app_password
IMAP_SERVER=your_imap_server
```
## Usage Examples
### Basic Deployment Test
```python
import modal
# Test the deployment
app = modal.App.lookup("spend-analyzer-mcp-bmt")
get_ai_analysis = app["get_ai_analysis"]
# Test with sample data
test_data = {
"spending_insights": [
{
"category": "Food & Dining",
"total_amount": 500.0,
"transaction_count": 15
}
],
"recommendations": ["Consider reducing dining expenses"]
}
result = get_ai_analysis.remote(
analysis_data=test_data,
user_question="How can I save money on food?",
provider="claude"
)
print(result)
```
### Email Processing
```python
email_config = {
"email": "your_[email protected]",
"password": "your_app_password",
"imap_server": "imap.gmail.com"
}
result = process_bank_statements.remote(
email_config=email_config,
days_back=30
)
print(f"Processed {result['total_transactions']} transactions")
```
### PDF Analysis
```python
# Read PDF file
with open("statement.pdf", "rb") as f:
pdf_content = f.read()
pdf_contents = {"statement.pdf": pdf_content}
result = analyze_uploaded_statements.remote(
pdf_contents=pdf_contents,
passwords={"statement.pdf": "optional_password"}
)
print(result['analysis'])
```
## Monitoring and Logs
### View Logs
```bash
modal logs spend-analyzer-mcp-bmt
```
### Monitor Functions
```bash
modal stats spend-analyzer-mcp-bmt
```
### View Volumes
```bash
modal volume list
```
## Troubleshooting
### Common Issues
1. **Import Errors**: Ensure all dependencies are in the Modal image
2. **Secret Access**: Verify secrets are created with correct names
3. **PDF Processing**: Check file permissions and password requirements
4. **API Limits**: Monitor usage for both Claude and SambaNova
### Debug Mode
Enable debug logging in Modal functions:
```python
import logging
logging.basicConfig(level=logging.DEBUG)
```
### Local Testing
Test functions locally before deployment:
```bash
modal run modal_deployment.py::main
```
## Security Considerations
1. **API Keys**: Store in Modal secrets, never in code
2. **Email Passwords**: Use app-specific passwords
3. **PDF Data**: Processed in memory, not stored permanently
4. **User Data**: Isolated by user ID in persistent storage
## Cost Optimization
1. **Function Timeouts**: Set appropriate timeouts for each function
2. **Memory Allocation**: Adjust based on PDF processing needs
3. **API Provider**: Choose between Claude (quality) and SambaNova (cost)
4. **Batch Processing**: Process multiple PDFs in single function call
## Scaling
Modal automatically handles scaling based on demand:
- **Cold Starts**: ~2-3 seconds for new containers
- **Warm Containers**: Sub-second response times
- **Concurrent Requests**: Automatically scaled
- **Resource Limits**: Configurable per function
## Integration with MCP
The deployment includes a webhook endpoint for MCP integration:
```
POST https://your-modal-app.modal.run/mcp_webhook
```
This enables integration with Claude Desktop and other MCP clients.
## Support
For deployment issues:
1. Check Modal logs and documentation
2. Verify API key permissions
3. Test with minimal examples
4. Contact Modal support for platform issues
## Next Steps
After successful deployment:
1. Test all functions with real data
2. Set up monitoring and alerts
3. Configure backup strategies
4. Implement additional security measures
5. Scale based on usage patterns
|