# Deploying CompanyIQ MCP Server to Railway
This guide walks you through deploying the CompanyIQ MCP server to Railway for HTTP access.
## Prerequisites
- [Railway account](https://railway.app) (free tier available)
- Git repository with the project (GitHub, GitLab, or Railway CLI)
- API keys for external services (optional but recommended)
## Quick Deploy via Railway Dashboard
### 1. Connect Your Repository
1. Go to [railway.app](https://railway.app) and sign in
2. Click **"New Project"**
3. Select **"Deploy from GitHub repo"**
4. Authorize Railway to access your repository
5. Select `companyiq-mcp`
### 2. Configure Environment Variables
In the Railway dashboard, go to your service and click **Variables**. Add:
**Required:**
```
COMPANYIQ_API_KEY=<generate-a-secure-key>
```
**Recommended:**
```
BRREG_API_BASE_URL=https://data.brreg.no/enhetsregisteret/api
SSB_API_BASE_URL=https://data.ssb.no/api/pxwebapi/v2
CACHE_TTL_HOURS=24
```
**Optional (for PDF parsing):**
```
OPENAI_API_KEY=sk-your-key-here
```
> **Tip:** Generate a secure API key with: `openssl rand -hex 32`
### 3. Deploy
Railway will automatically:
1. Detect the Node.js project
2. Run `npm ci && npm run build`
3. Start the server with `npm start`
### 4. Get Your URL
Once deployed, Railway provides a public URL like:
```
https://companyiq-mcp-production.up.railway.app
```
## Alternative: Railway CLI Deploy
```bash
# Install Railway CLI
npm install -g @railway/cli
# Login to Railway
railway login
# Initialize project (first time only)
railway init
# Link to existing project
railway link
# Deploy
railway up
# Set environment variables
railway variables set COMPANYIQ_API_KEY=your-secret-key
railway variables set BRREG_API_BASE_URL=https://data.brreg.no/enhetsregisteret/api
```
## Testing Your Deployment
### Health Check
```bash
curl https://your-app.up.railway.app/health
```
Expected response:
```json
{
"status": "healthy",
"service": "companyiq-mcp",
"version": "1.0.0",
"timestamp": "2024-01-15T10:30:00.000Z",
"tools_available": 15
}
```
### List Available Tools
```bash
curl -H "x-api-key: YOUR_API_KEY" \
https://your-app.up.railway.app/api/tools
```
### Execute a Tool (REST API)
```bash
# Search for companies
curl -X POST \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"industry": "62", "limit": 5}' \
https://your-app.up.railway.app/api/tools/search_companies
# Get company info
curl -X POST \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"org_nr": "923609016"}' \
https://your-app.up.railway.app/api/tools/get_company
```
## API Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/health` | Health check (no auth required) |
| GET | `/` | Service info (no auth required) |
| GET | `/api/tools` | List all available tools |
| POST | `/api/tools/:toolName` | Execute a specific tool |
| GET | `/sse` | MCP SSE connection |
| POST | `/messages` | MCP message endpoint |
## Available Tools
| Tool | Description |
|------|-------------|
| `get_company` | Get company info by org number or name |
| `search_companies` | Search companies by industry, region, size |
| `analyze_growth` | Find high-growth companies |
| `analyze_ownership` | Analyze ownership structure |
| `track_board` | Track board composition |
| `analyze_financials` | Financial analysis with risk assessment |
| `market_landscape` | Competitive landscape analysis |
| `consolidation_trends` | M&A and consolidation trends |
| `economic_context` | Macro statistics from SSB |
| `fetch_financials` | Fetch financial data from API |
| `get_financial_link` | Get links to annual reports |
| `import_financials` | Manually import financial data |
| `search_bankrupt_companies` | Find bankrupt companies |
| `build_financial_history` | Build financial history |
| `auto_scrape_financials` | Auto-scrape annual reports |
## Using with MCP Clients
### SSE Connection
For MCP-compatible clients, connect via SSE:
```javascript
// Example with EventSource
const eventSource = new EventSource(
'https://your-app.up.railway.app/sse',
{
headers: {
'x-api-key': 'YOUR_API_KEY'
}
}
);
eventSource.onmessage = (event) => {
console.log('MCP message:', JSON.parse(event.data));
};
```
## Troubleshooting
### Build Fails
1. Check Railway build logs
2. Ensure `better-sqlite3` compiles (needs build tools)
3. Verify Node.js version >= 18
### Native Dependencies (better-sqlite3)
Railway's Nixpacks should handle this automatically. If issues occur:
```bash
# In railway.json, try adding:
{
"build": {
"nixpacksVersion": "latest"
}
}
```
### Database Persistence
The SQLite database is stored locally and will reset on each deploy. For persistent data:
1. Use Railway's PostgreSQL or MySQL add-on
2. Or mount a volume for SQLite persistence
### PDF Parsing Issues
The auto-scrape feature requires:
- Chromium (bundled with Puppeteer)
- OpenAI API key for Vision parsing
On Railway, Puppeteer should work with default Nixpacks. If not, you may need to add:
```json
{
"build": {
"aptPkgs": ["chromium"]
}
}
```
## Local Development
```bash
# Install dependencies
npm install
# Build
npm run build
# Run locally
npm start
# Or in development mode
npm run dev
```
## Security Notes
- Always set `COMPANYIQ_API_KEY` in production
- Never commit `.env` files
- Use Railway's environment variable encryption
- Consider rate limiting for production use
## Support
- Railway docs: https://docs.railway.app
- MCP SDK: https://github.com/modelcontextprotocol/sdk
- Issues: [Your GitHub repo issues]