### Start Streamable HTTP Server Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Example of how to start the MCP streamable HTTP server using the `startHTTPStreamableServer` function. This server handles JSON-RPC requests over HTTP POST. ```typescript import { createServer } from "./server.js"; import { startHTTPStreamableServer } from "./services/index.js"; await startHTTPStreamableServer(createServer, "/mcp", 1122); // Server running at http://localhost:1122/mcp // Clients POST JSON-RPC requests to this endpoint ``` -------------------------------- ### Development: Install Dependencies Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Install the necessary dependencies for developing mcp-mermaid. ```bash npm install ``` -------------------------------- ### Docker Run Command for MCP-Mermaid Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Example Docker run command to start the MCP-Mermaid service, specifying the SSE transport, port, and host. ```bash docker run -p 3033:3033 susuperli/mcp-mermaid:latest \ --transport sse \ --port 3033 \ --host 0.0.0.0 ``` -------------------------------- ### Service Transport Setup Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/INDEX.md Functions to set up and start MCP server instances with specific transport protocols. ```APIDOC ## startStdioMcpServer(server) ### Description Sets up and starts the MCP server using STDIO transport. ### Method `startStdioMcpServer(server) ### Parameters #### Path Parameters - **server** (McpServer) - Required - The MCP server instance. ## startSSEMcpServer(server, endpoint?, port?, host?) ### Description Sets up and starts the MCP server using SSE transport. ### Method `startSSEMcpServer(server, endpoint?, port?, host?) ### Parameters #### Path Parameters - **server** (McpServer) - Required - The MCP server instance. #### Query Parameters - **endpoint** (string) - Optional - The SSE endpoint. - **port** (number) - Optional - The port to run the server on. - **host** (string) - Optional - The host to run the server on. ## startHTTPStreamableServer(createServer, endpoint?, port?, host?) ### Description Sets up and starts the MCP server using Streamable HTTP transport. ### Method `startHTTPStreamableServer(createServer, endpoint?, port?, host?) ### Parameters #### Path Parameters - **createServer** (function) - Required - Function to create the MCP server instance. #### Query Parameters - **endpoint** (string) - Optional - The streamable HTTP endpoint. - **port** (number) - Optional - The port to run the server on. - **host** (string) - Optional - The host to run the server on. ``` -------------------------------- ### Global Installation and Streamable Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Install mcp-mermaid globally and run the server using the Streamable transport protocol. This allows for custom endpoints. ```bash npm install -g mcp-mermaid mcp-mermaid -t streamable ``` -------------------------------- ### Development: Start MCP Server (Debugging) Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Start the mcp-mermaid server using the MCP Inspector for debugging purposes. ```bash npm run start ``` -------------------------------- ### Local Development: Clone and Build Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Clone the mcp-mermaid repository, install dependencies, and build the project for local development. ```bash git clone https://github.com/hustcc/mcp-mermaid.git cd mcp-mermaid npm install npm run build ``` -------------------------------- ### Logger.serverStartup() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs server startup information with a ๐Ÿš€ emoji prefix, including server type, port, and endpoint. It also displays formatted test endpoints. ```typescript Logger.serverStartup("SSE", 3033, "/sse"); // Output: // [MCP-Mermaid] ๐Ÿš€ SSE running on: http://localhost:3033/sse // // Test endpoints: // โ€ข Health check: http://localhost:3033/health // โ€ข Ping test: http://localhost:3033/ping ``` -------------------------------- ### Install MCP Mermaid Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Install the mcp-mermaid package using npm. This can be done for local project use or globally. ```bash npm install mcp-mermaid # or globally npm install -g mcp-mermaid ``` -------------------------------- ### Run STDIO Transport (Default) Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Starts the MCP Mermaid server using the default STDIO transport. No additional configuration is needed. ```bash node build/index.js # or explicitly node build/index.js --transport stdio ``` -------------------------------- ### Global Installation and SSE Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Install mcp-mermaid globally and run the server using the SSE transport protocol. The default endpoint for SSE is /sse. ```bash npm install -g mcp-mermaid mcp-mermaid -t sse ``` -------------------------------- ### SSE Connection Response Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md Example of a successful HTTP 200 OK response when establishing an SSE connection. The Mcp-Session-Id header is crucial for subsequent message correlation. ```http HTTP/1.1 200 OK Content-Type: text/event-stream Mcp-Session-Id: f8d7a9c2-1234-5678-abcd-ef1234567890 (streaming events follow) ``` -------------------------------- ### Start MCP Server with SSE HTTP Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using Server-Sent Events (SSE) over HTTP. It sets up an Express server to handle streaming connections. Clients connect via GET to an endpoint and send messages via POST. ```typescript export const startSSEMcpServer = async ( server: McpServer, endpoint = "/sse", port = 3033, host?: string, ): Promise ``` ```typescript import { createServer } from "./server.js"; import { startSSEMcpServer } from "./services/index.js"; const server = createServer(); await startSSEMcpServer(server, "/sse", 3033); // Server running at http://localhost:3033/sse // Clients connect via GET /sse, send messages via POST /messages?sessionId=... ``` -------------------------------- ### Run MCP Mermaid Server (SSE HTTP) Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Start the MCP Mermaid server with the SSE HTTP transport on a specified port. ```bash mcp-mermaid --transport sse --port 3033 # or with npm npm run start:sse ``` -------------------------------- ### Run MCP Mermaid Globally Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Installs and runs the MCP Mermaid server as a global CLI command. ```bash npm install -g mcp-mermaid # Run with options mcp-mermaid --transport sse --port 3033 # View help mcp-mermaid --help ``` -------------------------------- ### Local Development: Start Streamable Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Run the mcp-mermaid server locally using the Streamable transport protocol. This will start the server on port 1122. ```bash npm run start:streamable ``` -------------------------------- ### Local Development: Start SSE Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Run the mcp-mermaid server locally using the SSE transport protocol. This will start the server on port 3033. ```bash npm run start:sse ``` -------------------------------- ### POST /messages Request Body Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md Example of a JSON-RPC 2.0 request message sent to the /messages endpoint. It includes the method 'tools/call' with arguments for generating a mermaid diagram. ```json { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "graph TD; A-->B;", "outputType": "base64" } } } ``` -------------------------------- ### Example Usage of createMermaidInkUrl Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/mermaid-url.md Demonstrates how to use the `createMermaidInkUrl` function to generate both SVG and PNG URLs with different themes and styles. ```typescript import { createMermaidInkUrl } from "./utils/index.js"; // Generate SVG URL const svgUrl = createMermaidInkUrl( "graph TD; A[Start] --> B[Process] --> C[End]", "svg", "dark", "classic" ); // Returns: https://mermaid.ink/svg/pako:eJ4rLiqpVEopLEmVKkktLlGyUlAqys... // Generate PNG URL with hand-drawn style const imgUrl = createMermaidInkUrl( "pie title Expenses\n\"Rent\" : 40\n\"Food\" : 30\n\"Other\" : 30", "img", "forest", "handDrawn" ); // Returns: https://mermaid.ink/img/pako:eJ4rLiqpVEopLEmVKkktLlGyUlAqys... ``` -------------------------------- ### Redirecting Server Output to File Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Example of redirecting all server output, including standard output and error streams, to a file named 'server.log'. ```bash # Redirect all output to a file node build/index.js --transport sse > server.log 2>&1 ``` -------------------------------- ### MCP Server Integration with withRetry() Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/retry.md Example of how the MCP Server integrates with withRetry() to handle potential transient failures during the renderMermaid operation. ```typescript const { id, svg, screenshot } = await withRetry( () => renderMermaid(mermaid, theme, backgroundColor, look), { maxAttempts: 3, delayMs: 500 }, ); ``` -------------------------------- ### Saving Diagram to File Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/tools.md Example of generating a simple graph and requesting the output to be saved as a PNG file. ```typescript { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "graph TD;\n A --> B --> C", "outputType": "file" } } ``` -------------------------------- ### SSE Error Response Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md Example of an error response (5xx) when establishing an SSE stream fails. This JSON object indicates a server-side issue. ```json { "error": "Error establishing SSE stream" } ``` -------------------------------- ### Basic Diagram Request Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/tools.md Example of a basic request to generate a Mermaid diagram with default settings and base64 output. ```typescript { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "graph TD;\n A[Start] --> B[Process] --> C[End]" } } ``` -------------------------------- ### NPM Script to Start MCP Inspector Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Launches the MCP Mermaid server with the MCP Inspector for debugging purposes using an npm script. ```bash npm start ``` -------------------------------- ### Run SSE Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Starts the MCP Mermaid server with the SSE transport protocol on a specified port and endpoint. ```bash node build/index.js --transport sse --port 3033 --endpoint /sse ``` -------------------------------- ### Example Usage of MermaidLook Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/types.md Demonstrates how to specify the 'handDrawn' look when rendering a Mermaid diagram for a sketchy style. ```typescript const result = await renderMermaid( "graph TD; A --> B;", "dark", "white", "handDrawn" // Use sketchy rendering ); ``` -------------------------------- ### Generate Mermaid Diagram Tool Usage Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Example of calling the generate_mermaid_diagram tool with specific arguments for Mermaid syntax, theme, and output type. ```json { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "graph TD; A[Start] --> B[Process] --> C[End]", "theme": "dark", "outputType": "base64" } } ``` -------------------------------- ### Usage of RetryOptions with withRetry Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/types.md Demonstrates how to use the `RetryOptions` interface to configure retry behavior when calling the `withRetry()` function. This example customizes the number of attempts, delay, and retryable status codes. ```typescript import { withRetry } from "./utils/index.js"; const result = await withRetry( () => renderMermaid(mermaid, theme, bgColor, look), { maxAttempts: 5, delayMs: 1000, exponential: true, retryableStatusCodes: [408, 429, 503, 504] } ); ``` -------------------------------- ### POST /messages Success Response Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md Example of a successful JSON-RPC 2.0 response from the /messages endpoint. The 'result' field contains the generated content, such as a base64 encoded image. ```json { "jsonrpc": "2.0", "id": 1, "result": { "content": [ { "type": "image", "data": "iVBORw0KGgo...", "mimeType": "image/png" } ] } } ``` -------------------------------- ### Send Tool Call with HTTP Client (Streamable Transport) Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md This example demonstrates how to send a tool call using an HTTP client with streamable transport. The request is made to http://localhost:1122/mcp with a JSON payload specifying the tool and its arguments. ```bash curl -X POST http://localhost:1122/mcp \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "pie title Sales\n\"A\" : 40\n\"B\" : 60", "outputType": "svg_url" } } }' ``` -------------------------------- ### Decoded Payload Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/mermaid-url.md Illustrates the structure of the JSON payload after it has been decoded from the mermaid.ink URL. ```json { "code": "graph TD;\nA[Start] --> B[End]", "mermaid": { "theme": "dark", "look": "classic" } } ``` -------------------------------- ### Start MCP Server with STDIO Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using standard input/output for communication. Ideal for subprocess integration. The server listens on stdin and writes responses to stdout. ```typescript export async function startStdioMcpServer(server: McpServer): Promise ``` ```typescript import { createServer } from "./server.js"; import { startStdioMcpServer } from "./services/index.js"; const server = createServer(); await startStdioMcpServer(server); // Server now listens on stdin/stdout ``` -------------------------------- ### Diagram with Theme and SVG Output Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/tools.md Example of generating a pie chart with a dark theme and requesting SVG output. ```typescript { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "pie title Sales Distribution\n\"Product A\" : 40\n\"Product B\" : 30\n\"Product C\" : 30", "theme": "dark", "outputType": "svg" } } ``` -------------------------------- ### Bind SSE Transport to All Interfaces Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Starts the MCP Mermaid server with SSE transport, binding to all network interfaces on a specified port. ```bash node build/index.js --transport sse --host 0.0.0.0 --port 3033 ``` -------------------------------- ### Run Streamable HTTP Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Starts the MCP Mermaid server with the streamable HTTP transport protocol on a specified port and endpoint. ```bash node build/index.js --transport streamable --port 1122 --endpoint /mcp ``` -------------------------------- ### Start MCP Server with HTTP Streamable Transport Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using a stateless HTTP-based transport. A factory function is used to create a new MCP server instance for each request. ```typescript export const startHTTPStreamableServer = async ( createServer: () => McpServer, endpoint = "/mcp", port = 1122, host?: string, ): Promise ``` -------------------------------- ### Example Usage of renderMermaid Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/render.md Demonstrates how to use the renderMermaid function to render a Mermaid diagram with custom theme and background color. Imports the function and logs the result's ID, SVG, and screenshot. ```typescript import { renderMermaid } from "./utils/index.js"; const result = await renderMermaid( "graph TD; A[Start] --> B[Process] --> C[End]", "dark", "#1a1a1a", "classic" ); console.log(result.id); console.log(result.svg); console.log(result.screenshot); ``` -------------------------------- ### Hand-drawn Style Diagram with URL Output Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/tools.md Example of creating a flowchart with a hand-drawn look, custom background color, and requesting a public SVG URL. ```typescript { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "flowchart LR\n A[Input] --> B{Decision}\n B -->|Yes| C[Output]\n B -->|No| D[Error]", "look": "handDrawn", "backgroundColor": "#f0f0f0", "outputType": "svg_url" } } ``` -------------------------------- ### TypeScript Fetch Example for MCP POST Endpoint Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md This TypeScript code demonstrates how to send a POST request to the MCP endpoint using the Fetch API. It includes setting the correct headers and stringifying the JSON-RPC request body. ```typescript const response = await fetch("http://localhost:1122/mcp", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "generate_mermaid_diagram", arguments: { mermaid: "graph TD; A[Start] --> B[End]" } } }) }); const result = await response.json(); console.log(result); ``` -------------------------------- ### Streamable HTTP Transport Example for Tool Call Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md This curl command demonstrates invoking the 'generate_mermaid_diagram' tool using the Streamable HTTP transport. It sends a POST request with the JSON-RPC payload, specifying the mermaid syntax, theme, and output type. ```bash curl -X POST http://localhost:1122/mcp \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "generate_mermaid_diagram", "arguments": { "mermaid": "pie title Expenses\n\"Rent\" : 40\n\"Food\" : 30\n\"Other\" : 30", "theme": "forest", "outputType": "svg_url" } } }' ``` -------------------------------- ### Logger.serverStartup() Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs server startup information with the ๐Ÿš€ emoji prefix, including server type, port, and endpoint. It also displays test endpoints for health checks and ping tests. ```APIDOC ## Logger.serverStartup() ### Description Logs server startup information with the ๐Ÿš€ emoji prefix. Displays the server URL and test endpoints with formatted output. ### Method Signature `export function serverStartup( serverType: string, port: number, endpoint: string, ): void` ### Parameters #### Path Parameters - **serverType** (string) - Required - The type of server (e.g., "SSE", "Streamable HTTP", "Stdio"). - **port** (number) - Required - The port the server is listening on. - **endpoint** (string) - Required - The endpoint path (e.g., "/sse", "/mcp"). ### Example ```typescript Logger.serverStartup("SSE", 3033, "/sse"); // Output: // [MCP-Mermaid] ๐Ÿš€ SSE running on: http://localhost:3033/sse // // Test endpoints: // โ€ข Health check: http://localhost:3033/health // โ€ข Ping test: http://localhost:3033/ping ``` ### Additional Information Outputs formatted server startup information including: - Server type and listening address (underlined and colored green) - Health check endpoint URL (underlined) - Ping test endpoint URL (underlined) ``` -------------------------------- ### Development: Build Server Source: https://github.com/hustcc/mcp-mermaid/blob/main/README.md Build the mcp-mermaid server for development. ```bash npm run build ``` -------------------------------- ### startHTTPStreamableServer Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using HTTP Streamable transport. This is a stateless HTTP-based transport where each request is self-contained, requiring a factory function to create a new MCP server instance for each request. ```APIDOC ## startHTTPStreamableServer() ### Description Initializes an MCP server using HTTP Streamable transport. This is a stateless HTTP-based transport where each request is self-contained. ### Parameters #### Path Parameters - **createServer** (`() => McpServer`) - Required - A factory function that returns a new MCP server instance. Called once per request. - **endpoint** (string) - Optional - The HTTP endpoint path where MCP requests are accepted. Defaults to "/mcp". - **port** (number) - Optional - The port the HTTP server listens on. Defaults to 1122. - **host** (string | undefined) - Optional - The host to bind to. If undefined, uses "localhost". Pass "0.0.0.0" for all interfaces. ### Return Type - `Promise` โ€” Resolves when the HTTP server is initialized and listening. ``` -------------------------------- ### Run MCP Mermaid Server (Streamable HTTP) Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Initialize the MCP Mermaid server using the Streamable HTTP transport on a designated port. ```bash mcp-mermaid --transport streamable --port 1122 # or with npm npm run start:streamable ``` -------------------------------- ### createServer() Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/server.md Creates and configures an MCP (Model Context Protocol) server instance for mermaid diagram generation. This is the main factory function for instantiating the server. ```APIDOC ## createServer() ### Description Creates and configures an MCP (Model Context Protocol) server instance for mermaid diagram generation. This is the main factory function for instantiating the server. ### Return Type - `McpServer` โ€” An MCP server instance from `@modelcontextprotocol/sdk/server/mcp.js` configured with the `generate_mermaid_diagram` tool and appropriate error handlers. ### Configuration - Server name: `"mcp-mermaid"` - Server version: `"0.1.3"` - Capabilities: `tools` (for tool definition and execution) - Sets up request handlers for `ListToolsRequest` and `CallToolRequest` - Configures a global error handler that logs MCP errors ### Example Usage ```typescript import { createServer } from "./server.js"; const server = createServer(); // The server is now ready to be connected to a transport (stdio, SSE, etc.) ``` ``` -------------------------------- ### CLI Options Reference Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Displays the available command-line options for the mcp-mermaid executable, including transport selection, port, host, and endpoint configuration. ```bash mcp-mermaid [OPTIONS] ``` -------------------------------- ### POST /messages Error Response Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md Example of a JSON-RPC 2.0 error response from the /messages endpoint. The 'error' object details the failure, including a code and message. ```json { "jsonrpc": "2.0", "id": 1, "error": { "code": -32603, "message": "Failed to generate mermaid: ..." } } ``` -------------------------------- ### Launch MCP Mermaid Server Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Launches the MCP Mermaid server with specified options. Use this for direct execution. ```bash node build/index.js [OPTIONS] ``` -------------------------------- ### CLI Options Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/SUMMARY.txt Documentation for the command-line interface options provided by MCP Mermaid. ```APIDOC ## CLI Options This section details the 5 documented CLI options for MCP Mermaid. ### Option Documentation Each CLI option is fully documented, explaining its purpose and usage. Refer to the detailed documentation within the project for specifics on each option. ``` -------------------------------- ### Logger.cleanup() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs a cleanup message with a ๐Ÿงน emoji prefix. ```typescript Logger.cleanup("Temporary CSS file removed"); // Output: [MCP-Mermaid] ๐Ÿงน Temporary CSS file removed ``` -------------------------------- ### Display Help Message Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Displays the help message showing all available command-line options for the MCP Mermaid server. ```bash node build/index.js --help ``` -------------------------------- ### startSSEMcpServer Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using Server-Sent Events (SSE) HTTP transport. This function sets up an Express HTTP server to handle streaming connections, allowing clients to connect via SSE and send messages through a separate POST endpoint. ```APIDOC ## startSSEMcpServer() ### Description Initializes an MCP server using Server-Sent Events (SSE) HTTP transport. Creates an Express HTTP server that handles streaming connections. Sets up an Express HTTP server with two endpoints: **GET `{endpoint}`** โ€” Establishes SSE stream - Creates a new `SSEServerTransport` for each client - Returns `Content-Type: text/event-stream` - Registers session ID and cleans up on disconnect - Calls `server.connect(transport)` to bind the MCP server to this transport **POST `/messages`** โ€” Receives client messages - Requires `sessionId` query parameter - Routes messages to the correct transport based on session ID - Returns 400 if sessionId is missing - Returns 404 if session not found ### Parameters #### Path Parameters - **server** (McpServer) - Required - An MCP server instance from `createServer()`. - **endpoint** (string) - Optional - The HTTP GET endpoint where clients connect to establish SSE stream. Defaults to "/sse". - **port** (number) - Optional - The port the HTTP server listens on. Defaults to 3033. - **host** (string | undefined) - Optional - The host to bind to. If undefined, uses "localhost". Pass "0.0.0.0" for all interfaces. ### Return Type - `Promise` โ€” Resolves when the HTTP server is initialized and listening. ### Example Usage ```typescript import { createServer } from "./server.js"; import { startSSEMcpServer } from "./services/index.js"; const server = createServer(); await startSSEMcpServer(server, "/sse", 3033); // Server running at http://localhost:3033/sse // Clients connect via GET /sse, send messages via POST /messages?sessionId=... ``` ### HTTP Endpoints #### GET {endpoint} - **Description**: Upgrade connection to SSE stream, return session ID in header #### POST /messages - **Description**: Send JSON-RPC message for a specific session - **Query Parameters** - **sessionId** (string) - Required - The session ID for the message. ### Error Handling - **SSE stream establishment error**: Returns HTTP 500 "Error establishing SSE stream" if not already sent headers - **Missing sessionId**: Returns HTTP 400 "Missing sessionId parameter" - **Invalid session**: Returns HTTP 404 "Session not found" - **Message handling error**: Returns HTTP 500 "Error handling request" ``` -------------------------------- ### startStdioMcpServer Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md Initializes an MCP server using STDIO (standard input/output) transport. This is the default transport mechanism for MCP client-server communication. It creates a StdioServerTransport and connects the provided server to it, ideal for subprocess integration. ```APIDOC ## startStdioMcpServer() ### Description Initializes an MCP server using STDIO (standard input/output) transport. This is the default transport mechanism for MCP client-server communication. Creates a `StdioServerTransport` from the MCP SDK and connects the provided server to it. This transport communicates via standard input/output streams, making it ideal for subprocess integration with applications like Claude, VSCode, and Cline. The server will read JSON-RPC messages from stdin and write responses to stdout. ### Parameters #### Path Parameters - **server** (McpServer) - Required - An MCP server instance from `createServer()`. ### Return Type - `Promise` โ€” Resolves when the transport is fully connected and server is ready to receive requests. ### Example Usage ```typescript import { createServer } from "./server.js"; import { startStdioMcpServer } from "./services/index.js"; const server = createServer(); await startStdioMcpServer(server); // Server now listens on stdin/stdout ``` ``` -------------------------------- ### runSSEServer() Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/server.md Launches the MCP server using Server-Sent Events (SSE) HTTP transport. This allows remote clients to connect via HTTP with streaming event support. ```APIDOC ## runSSEServer(endpoint = "/sse", port = 3033, host?: string) ### Description Launches the MCP server using Server-Sent Events (SSE) HTTP transport. This allows remote clients to connect via HTTP with streaming event support. ### Parameters #### Path Parameters - **endpoint** (string) - Optional - The HTTP endpoint path where the SSE stream will be served. Defaults to `"/sse"`. - **port** (number) - Optional - The port number the HTTP server will listen on. Defaults to `3033`. - **host** (string | undefined) - Optional - The host address to bind to. If undefined, defaults to `"localhost"`. Pass `"0.0.0.0"` to listen on all interfaces. ### Return Type - `Promise` โ€” Resolves when the HTTP server is fully initialized and listening for connections. ### Usage Creates an MCP server and starts an Express HTTP server that handles SSE connections. Each client that connects to the SSE endpoint establishes a persistent stream for receiving server-sent events. Clients send subsequent messages via POST to `/messages` with their session ID. ### Example Usage ```typescript import { runSSEServer } from "./server.js"; // Start SSE server on port 3033 await runSSEServer("/sse", 3033); // Clients can now connect to: http://localhost:3033/sse ``` ``` -------------------------------- ### Server Creation and Transport Launchers Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/INDEX.md Functions for creating an MCP server instance and launching it with different transport mechanisms (STDIO, SSE, HTTP Streamable). ```APIDOC ## createServer() ### Description Creates an MCP server instance. ### Method `createServer()` ## runStdioServer(port?, host?) ### Description Launches the MCP server using STDIO transport. ### Method `runStdioServer(port?, host?) ### Parameters #### Query Parameters - **port** (number) - Optional - The port to run the server on. - **host** (string) - Optional - The host to run the server on. ## runSSEServer(endpoint?, port?, host?) ### Description Launches the MCP server using SSE (Server-Sent Events) HTTP transport. ### Method `runSSEServer(endpoint?, port?, host?) ### Parameters #### Query Parameters - **endpoint** (string) - Optional - The specific endpoint for SSE. - **port** (number) - Optional - The port to run the server on. - **host** (string) - Optional - The host to run the server on. ## runHTTPStreamableServer(createServer, endpoint?, port?, host?) ### Description Launches the MCP server using Streamable HTTP transport. ### Method `runHTTPStreamableServer(createServer, endpoint?, port?, host?) ### Parameters #### Path Parameters - **createServer** (function) - Required - The function to create the MCP server instance. #### Query Parameters - **endpoint** (string) - Optional - The specific endpoint for the streamable HTTP server. - **port** (number) - Optional - The port to run the server on. - **host** (string) - Optional - The host to run the server on. ``` -------------------------------- ### Logger.success() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs a success message with a โœ… emoji prefix. Additional data can be passed for context. ```typescript Logger.success("Mermaid diagram saved", { filepath: "/path/to/file.png" }); // Output: [MCP-Mermaid] โœ… Mermaid diagram saved { filepath: "/path/to/file.png" } ``` -------------------------------- ### Logger.warn() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs a warning message with a โš ๏ธ emoji prefix. Additional data can be passed for context. ```typescript Logger.warn("Attempt 1/3 failed. Retrying in 500msโ€ฆ", error); // Output: [MCP-Mermaid] โš ๏ธ Attempt 1/3 failed. Retrying in 500msโ€ฆ ``` -------------------------------- ### Redirecting Server Error Stream to File Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Example of redirecting only the standard error stream from the server to a file named 'errors.log'. ```bash # Redirect only stderr to file node build/index.js --transport sse 2> errors.log ``` -------------------------------- ### Usage of McpServer in MCP Mermaid Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/types.md Demonstrates the creation of an `McpServer` instance using `createServer()` and its subsequent connection to a transport mechanism like stdio via `startStdioMcpServer()`. ```typescript import { createServer } from "./server.js"; import { startStdioMcpServer } from "./services/index.js"; const server = createServer(); await startStdioMcpServer(server); ``` -------------------------------- ### Logger.error() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs an error message with a โŒ emoji prefix. An optional error object or details can be provided. ```typescript Logger.error("MCP Error", errorObject); // Output: [MCP-Mermaid] โŒ MCP Error ``` -------------------------------- ### NPM Scripts Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Lists common NPM scripts for building, testing, and running the mcp-mermaid project with different transport configurations. ```bash npm start npm run start:sse npm run start:streamable npm run build npm test ``` -------------------------------- ### withRetry() with Custom Options Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/retry.md Shows how to configure withRetry() with custom parameters like maxAttempts, delayMs, exponential backoff, and specific retryableStatusCodes. ```typescript const result = await withRetry( async () => { return await someFlakeyOperation(); }, { maxAttempts: 5, delayMs: 1000, exponential: true, retryableStatusCodes: [408, 429, 503, 504], // Custom status codes } ); ``` -------------------------------- ### withRetry() Immediate Failure Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/retry.md Illustrates a scenario where withRetry() throws an error immediately without retrying because the error is not considered retryable. ```typescript try { await withRetry( async () => { throw new Error("Invalid input"); }, { maxAttempts: 3, delayMs: 100 } ); } catch (error) { // Error is thrown immediately without retry // error.message === "Invalid input" } ``` -------------------------------- ### Logger.info() Usage Example Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/logger.md Logs an informational message with an โ„น๏ธ emoji prefix. Additional data can be passed and will be spread via console.log. ```typescript Logger.info("Rendering diagram", { width: 800, height: 600 }); // Output: [MCP-Mermaid] โ„น๏ธ Rendering diagram { width: 800, height: 600 } ``` -------------------------------- ### Basic withRetry() Usage Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/retry.md Demonstrates basic usage of withRetry() with default options to fetch data, automatically retrying on common HTTP error statuses. ```typescript import { withRetry } from "./utils/index.js"; const result = await withRetry(async () => { const response = await fetch("https://api.example.com/data"); if (!response.ok) throw new Error(`HTTP ${response.status}`); return response.json(); }); // Will retry up to 3 times on 503, 429, 502, or 504 errors ``` -------------------------------- ### Claude Integration Configuration Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Shows how to configure mcp-mermaid as an MCP server within Claude Desktop's configuration file. ```json { "mcpServers": { "mcp-mermaid": { "command": "mcp-mermaid" } } } ``` -------------------------------- ### Method Not Allowed Response Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md The JSON-RPC error response returned for disallowed HTTP methods (GET, DELETE) on the MCP streamable HTTP endpoint. ```json { "jsonrpc": "2.0", "error": { "code": -32000, "message": "Method not allowed" }, "id": null } ``` -------------------------------- ### Create MCP Server Instance Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/server.md Use `createServer` to instantiate an MCP server configured with the `generate_mermaid_diagram` tool. This function sets up necessary handlers and error configurations for the server. ```typescript export function createServer(): McpServer ``` ```typescript import { createServer } from "./server.js"; const server = createServer(); // The server is now ready to be connected to a transport (stdio, SSE, etc.) ``` -------------------------------- ### Successful Response Body for generate_mermaid_diagram Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md This is an example of a successful response body containing the generated SVG content. The 'content' array holds the result, which can be of different types. ```json { "jsonrpc": "2.0", "id": 1, "result": { "content": [ { "type": "text", "text": "..." } ] } } ``` -------------------------------- ### runHTTPStreamableServer() Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/server.md Launches the MCP server using HTTP Streamable transport. This is an HTTP-based transport that supports bi-directional JSON-RPC communication in a single HTTP POST request. ```APIDOC ## runHTTPStreamableServer(endpoint = "/mcp", port = 3033, host?: string) ### Description Launches the MCP server using HTTP Streamable transport. This is an HTTP-based transport that supports bi-directional JSON-RPC communication in a single HTTP POST request. ### Parameters #### Path Parameters - **endpoint** (string) - Optional - The HTTP endpoint path where the MCP streamable handler will be mounted. Defaults to `"/mcp"`. - **port** (number) - Optional - The port number the HTTP server will listen on. Defaults to `3033`. - **host** (string | undefined) - Optional - The host address to bind to. If undefined, defaults to `"localhost"`. Pass `"0.0.0.0"` to listen on all interfaces. ### Return Type - `Promise` โ€” Resolves when the HTTP server is fully initialized and listening for connections. ### Usage Creates an MCP server and starts an Express HTTP server that handles streamable HTTP transport. This transport creates a new server instance per request and handles the JSON-RPC protocol over HTTP POST. CORS is enabled to allow cross-origin requests. ### Supported HTTP methods: - **POST** โ€” Handles MCP streamable requests (accepted) - **GET** โ€” Returns 405 Method Not Allowed - **DELETE** โ€” Returns 405 Method Not Allowed ### Example Usage ```typescript import { runHTTPStreamableServer } from "./server.js"; // Start streamable HTTP server on port 1122 await runHTTPStreamableServer("/mcp", 1122); // Clients can now POST to: http://localhost:1122/mcp ``` ``` -------------------------------- ### Method Not Allowed Response Body (GET/DELETE) Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/endpoints.md When using GET or DELETE methods on the MCP endpoint, a 'Method not allowed' error is returned. This JSON structure details the error. ```json { "jsonrpc": "2.0", "error": { "code": -32000, "message": "Method not allowed" }, "id": null } ``` -------------------------------- ### Transport Service Initializers Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Functions for setting up different transport services for the MCP server. ```APIDOC ## Transport Service Initializers ### Description Functions to set up and start specific transport services for the MCP server. ### Methods - `startStdioMcpServer()` โ€” Sets up the STDIO transport. - `startSSEMcpServer()` โ€” Sets up the SSE HTTP transport. - `startHTTPStreamableServer()` โ€” Sets up the Streamable HTTP transport. ``` -------------------------------- ### Mermaid Rendering Configuration Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/configuration.md Configure the mermaid-isomorphic renderer with theme and look parameters. ```typescript { mermaidConfig: { theme: theme, // From tool parameter look: look, // From tool parameter } } ``` -------------------------------- ### Server Factory Functions and Transport Initializers Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Functions for creating MCP server instances and initializing various transport mechanisms. ```APIDOC ## createServer() ### Description Creates an MCP server instance. ### Method Not specified (likely a function call in a programming language). ## runStdioServer() ### Description Launches the STDIO transport for the MCP server. ### Method Not specified (likely a function call in a programming language). ## runSSEServer() ### Description Launches the SSE (Server-Sent Events) HTTP transport for the MCP server. ### Method Not specified (likely a function call in a programming language). ## runHTTPStreamableServer() ### Description Launches the Streamable HTTP transport for the MCP server. ### Method Not specified (likely a function call in a programming language). ``` -------------------------------- ### Check Directory Permissions Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/README.md Lists the permissions and ownership of a directory. Use this to diagnose file output errors by verifying write permissions. ```bash ls -ld $(pwd) # Check permissions ``` -------------------------------- ### MCP Streamable HTTP Server Endpoint Source: https://github.com/hustcc/mcp-mermaid/blob/main/_autodocs/api-reference/services.md The MCP streamable HTTP server accepts JSON-RPC messages via POST requests to a specified endpoint. Other HTTP methods (GET, DELETE) are not allowed. ```APIDOC ## POST {endpoint} ### Description Accepts MCP streamable HTTP request (JSON-RPC) for processing. ### Method POST ### Endpoint {endpoint} ### Request Body - **jsonrpc** (string) - Required - Specifies the JSON-RPC protocol version. - **id** (number) - Required - The ID of the request. - **method** (string) - Required - The name of the method to be invoked. - **params** (object) - Optional - Parameters for the method call. ### Request Example ```json { "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} } ``` ### Response Streamable HTTP response containing JSON-RPC response(s). #### Success Response (200) - **jsonrpc** (string) - The JSON-RPC protocol version. - **result** (any) - The result of the method call (if successful). - **error** (object) - An error object (if an error occurred). - **code** (number) - The error code. - **message** (string) - A description of the error. - **id** (number) - The ID of the request. #### Response Example ```json { "jsonrpc": "2.0", "result": {}, "id": 1 } ``` ### Error Handling If an error occurs during request processing and headers haven't been sent, returns HTTP 500 with JSON-RPC error: ```json { "jsonrpc": "2.0", "error": { "code": -32000, "message": "Method not allowed" }, "id": null } ``` ```