AWS Diagram MCP Server

1.0.23 · active · verified Thu Apr 16

The AWS Diagram MCP Server is a Python application that hosts an API for generating architectural diagrams. It leverages the popular `diagrams` Python package, allowing users to submit Diagram as Code (DaC) definitions (using the `diagrams` DSL) via HTTP endpoints and receive image outputs. It is currently at version 1.0.23 and follows an active, irregular release cadence driven by feature development and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to interact with the AWS Diagram MCP Server's API. First, ensure the server is installed (`pip install awslabs-aws-diagram-mcp-server`) and running in a separate terminal (`python -m mcp_server`). The client code then uses the `requests` library (`pip install requests`) to send a `diagrams` DSL string to the server's `/diagram` endpoint and saves the generated image locally.

import os
import requests # pip install requests

# --- Step 1: Run the MCP Server (in a separate terminal/process) ---
# The server application must be running for the client code below to work.
# In your terminal, run:
# pip install awslabs-aws-diagram-mcp-server
# python -m mcp_server
# By default, it listens on http://127.0.0.1:8000.
# You can configure host/port via environment variables (e.g., before running 'python -m mcp_server'):
# export MCP_SERVER_PORT=8001
# export MCP_SERVER_HOST=0.0.0.0

# --- Step 2: Client interaction example ---

server_host = os.environ.get('MCP_SERVER_HOST', '127.0.0.1')
server_port = os.environ.get('MCP_SERVER_PORT', '8000')
server_url = f"http://{server_host}:{server_port}"

print(f"Attempting to connect to MCP Server at {server_url}")

try:
    # Check if the server is healthy
    health_response = requests.get(f"{server_url}/health")
    health_response.raise_for_status()
    print("Server is healthy and reachable!")

    # Example of sending a diagram DSL to generate an image
    diagram_dsl = '''
from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import VPC

with Diagram("Simple Web Service", show=False, filename="simple_web_service"):
    with Cluster("Web Servers"):
        web = EC2("Web Instance")
    vpc = VPC("MyVPC")
    web >> vpc
'''

    headers = {"Content-Type": "application/json"}
    # The /diagram endpoint accepts a POST request with the DSL and desired output format.
    # The `filename` in Diagram() constructor is respected by the server.
    payload = {"diagram_source_code": diagram_dsl, "output_format": "png"}

    diagram_response = requests.post(f"{server_url}/diagram", json=payload)
    diagram_response.raise_for_status()

    # The server returns the image content directly
    output_filename = "simple_web_service.png"
    with open(output_filename, "wb") as f:
        f.write(diagram_response.content)
    print(f"Diagram '{output_filename}' generated successfully!")

except requests.exceptions.ConnectionError:
    print(f"Error: Could not connect to MCP server at {server_url}. Make sure it is running (e.g., 'python -m mcp_server') and accessible.")
except requests.exceptions.HTTPError as e:
    print(f"HTTP Error from server: {e.response.status_code} - {e.response.text}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →