diff --git a/packages/mcp-server/README.md b/packages/mcp-server/README.md index 66e38e7..0f5169c 100644 --- a/packages/mcp-server/README.md +++ b/packages/mcp-server/README.md @@ -90,7 +90,7 @@ Use the standard MCP configuration with: - **Natural Language**: Describe diagrams in plain text - flowcharts, architecture diagrams, etc. - **Edit Support**: Modify existing diagrams with natural language instructions - **Export**: Save diagrams as `.drawio` files -- **Self-contained**: Embedded server, works offline (except draw.io UI which loads from embed.diagrams.net) +- **Self-contained**: Embedded server, works offline (except draw.io UI which loads from `embed.diagrams.net` by default, configurable via `DRAWIO_BASE_URL`) ## Available Tools @@ -130,6 +130,33 @@ Use the standard MCP configuration with: | Variable | Default | Description | |----------|---------|-------------| | `PORT` | `6002` | Port for the embedded HTTP server | +| `DRAWIO_BASE_URL` | `https://embed.diagrams.net` | Base URL for the draw.io embed. Set this to use a self-hosted draw.io instance for private deployments. | + +### Private Deployment (Self-hosted draw.io) + +For security-sensitive environments that require private deployment of draw.io: + +```json +{ + "mcpServers": { + "drawio": { + "command": "npx", + "args": ["@next-ai-drawio/mcp-server@latest"], + "env": { + "DRAWIO_BASE_URL": "https://drawio.your-company.com" + } + } + } +} +``` + +You can deploy your own draw.io instance using the official Docker image: + +```bash +docker run -d -p 8080:8080 jgraph/drawio +``` + +Then set `DRAWIO_BASE_URL=http://localhost:8080` (or your server's URL). ## Troubleshooting diff --git a/packages/mcp-server/src/http-server.ts b/packages/mcp-server/src/http-server.ts index 6721599..5e037e0 100644 --- a/packages/mcp-server/src/http-server.ts +++ b/packages/mcp-server/src/http-server.ts @@ -13,6 +13,28 @@ import { } from "./history.js" import { log } from "./logger.js" +// Configurable draw.io embed URL for private deployments +const DRAWIO_BASE_URL = + process.env.DRAWIO_BASE_URL || "https://embed.diagrams.net" + +// Extract origin (scheme + host + port) from URL for postMessage security check +function getOrigin(url: string): string { + try { + const parsed = new URL(url) + return `${parsed.protocol}//${parsed.host}` + } catch { + return url // Fallback if parsing fails + } +} + +const DRAWIO_ORIGIN = getOrigin(DRAWIO_BASE_URL) + +// Normalize URL for iframe src - ensure no double slashes +function normalizeUrl(url: string): string { + // Remove trailing slash to avoid double slashes + return url.replace(/\/$/, '') +} + interface SessionState { xml: string version: number @@ -403,7 +425,7 @@ function getHtmlPage(sessionId: string): string {