# HTTP Server for MCP Sequence Diagram Server
The Docker container now includes an HTTP server that serves generated diagrams via REST API endpoints.
## Quick Start
### 1. Start the Server
```bash
# Using Docker Compose (recommended)
docker-compose up -d
# Or using Docker directly
docker run --rm -d \
-p 8080:8080 \
-v $(pwd)/diagrams:/usr/src/app/diagrams \
-v $(pwd)/logs:/usr/src/app/logs \
mcp-sequence-diagram-server:latest
```
### 2. Access the API
- **API Documentation**: http://localhost:8080/
- **Health Check**: http://localhost:8080/api/health
- **List Diagrams**: http://localhost:8080/api/diagrams
## API Endpoints
### 1. Root Endpoint
```
GET /
```
Returns API documentation and available endpoints.
**Response:**
```json
{
"message": "MCP Sequence Diagram Server",
"version": "1.0.0",
"endpoints": {
"health": "/api/health",
"listDiagrams": "/api/diagrams",
"getDiagram": "/api/diagrams/:filename",
"downloadDiagram": "/api/diagrams/:filename/download",
"staticFiles": "/diagrams/:filename"
}
}
```
### 2. Health Check
```
GET /api/health
```
Returns server health status.
**Response:**
```json
{
"success": true,
"status": "healthy",
"timestamp": "2025-07-04T21:15:00.000Z",
"diagramsDirectory": "/usr/src/app/diagrams"
}
```
### 3. List Diagrams
```
GET /api/diagrams
```
Returns a list of all generated diagrams.
**Response:**
```json
{
"success": true,
"diagrams": [
{
"name": "login-flow.svg",
"url": "/api/diagrams/login-flow.svg",
"downloadUrl": "/api/diagrams/login-flow.svg/download"
}
]
}
```
### 4. Get Diagram
```
GET /api/diagrams/:filename
```
Returns the diagram file with appropriate content type.
**Parameters:**
- `filename`: Name of the diagram file (e.g., `login-flow.svg`)
**Response:** The diagram file (SVG or PNG)
### 5. Download Diagram
```
GET /api/diagrams/:filename/download
```
Downloads the diagram file with proper headers.
**Parameters:**
- `filename`: Name of the diagram file (e.g., `login-flow.svg`)
**Response:** File download with `Content-Disposition: attachment`
### 6. Static File Access
```
GET /diagrams/:filename
```
Direct access to diagram files in the diagrams directory.
**Parameters:**
- `filename`: Name of the diagram file
## Usage Examples
### Using curl
```bash
# Get API documentation
curl http://localhost:8080/
# Check server health
curl http://localhost:8080/api/health
# List all diagrams
curl http://localhost:8080/api/diagrams
# View a specific diagram
curl http://localhost:8080/api/diagrams/login-flow.svg
# Download a diagram
curl -O http://localhost:8080/api/diagrams/login-flow.svg/download
```
### Using JavaScript/Fetch
```javascript
// List diagrams
const response = await fetch('http://localhost:8080/api/diagrams');
const data = await response.json();
console.log(data.diagrams);
// Get diagram as image
const diagramResponse = await fetch('http://localhost:8080/api/diagrams/login-flow.svg');
const svgContent = await diagramResponse.text();
document.getElementById('diagram-container').innerHTML = svgContent;
```
### Using Python
```python
import requests
# List diagrams
response = requests.get('http://localhost:8080/api/diagrams')
diagrams = response.json()['diagrams']
# Download a diagram
for diagram in diagrams:
if diagram['name'].endswith('.svg'):
download_url = f"http://localhost:8080{diagram['downloadUrl']}"
response = requests.get(download_url)
with open(diagram['name'], 'wb') as f:
f.write(response.content)
```
## Integration with MCP Client
When you generate a diagram using the MCP server, it will now return HTTP URLs for accessing the generated files:
```
Sequence diagram generated successfully!
Saved to: /usr/src/app/diagrams/login-flow.svg
Format: SVG
HTTP Access:
- View: http://localhost:8080/api/diagrams/login-flow.svg
- Download: http://localhost:8080/api/diagrams/login-flow.svg/download
- Direct: http://localhost:8080/diagrams/login-flow.svg
```
## Testing
Run the test script to verify the HTTP server is working:
```bash
# Install test dependencies
npm install
# Run the test
node test-http-server.js
```
## Configuration
### Environment Variables
- `PORT`: HTTP server port (default: 8080)
- `NODE_ENV`: Environment mode (production/development)
### Docker Configuration
The HTTP server is automatically started when the container runs. The MCP server runs on stdio, while the HTTP server runs on port 8080.
### CORS
CORS is enabled by default to allow cross-origin requests from web applications.
## Security Notes
- The HTTP server only serves files from the `diagrams` directory
- No authentication is implemented (add if needed for production)
- CORS is enabled for development (configure appropriately for production)
- File access is restricted to the diagrams directory
## Troubleshooting
### Common Issues
1. **Port 8080 already in use**
```bash
# Check what's using the port
lsof -i :8080
# Use a different port
docker run -p 8081:8080 mcp-sequence-diagram-server
```
2. **Cannot access diagrams**
- Ensure the diagrams directory is properly mounted
- Check file permissions
- Verify the container is running
3. **CORS errors**
- The server includes CORS headers by default
- If issues persist, check your client's CORS configuration
### Logs
Check container logs for HTTP server information:
```bash
docker-compose logs -f
```