Skip to main content
Glama
wwwzhouhui

Mermaid MCP Server

by wwwzhouhui

validate_mermaid_syntax

Validate Mermaid diagram syntax by testing conversion to ensure code correctness before rendering images.

Instructions

通过尝试简单转换来验证 Mermaid 图表语法。

参数:
    mermaid_code: 要验证的 Mermaid 图表语法代码

返回:
    包含验证结果的字典

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mermaid_codeYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Implements the validate_mermaid_syntax tool by cleaning the mermaid code, base64 encoding it, and performing a HEAD request to mermaid.ink/svg/ endpoint to check if the syntax is valid based on HTTP status.
    @mcp.tool()
    def validate_mermaid_syntax(mermaid_code: str) -> Dict[str, Any]:
        """
        通过尝试简单转换来验证 Mermaid 图表语法。
        
        参数:
            mermaid_code: 要验证的 Mermaid 图表语法代码
        
        返回:
            包含验证结果的字典
        """
        try:
            if not mermaid_code or not mermaid_code.strip():
                return {
                    "valid": False,
                    "error": "Mermaid 代码是必需的,不能为空"
                }
            
            # 清理代码
            cleaned_code = mermaid_code.replace("```mermaid", "").replace("```", "").strip()
            
            # 尝试编码
            try:
                encoded_diagram = base64.urlsafe_b64encode(cleaned_code.encode('utf-8')).decode('ascii')
            except Exception as e:
                return {
                    "valid": False,
                    "error": f"编码图表失败:{str(e)}"
                }
            
            # 使用 SVG 格式进行快速验证
            url = f"https://mermaid.ink/svg/{encoded_diagram}"
            
            try:
                response = requests.head(url, timeout=10)
                
                if response.status_code == 200:
                    return {
                        "valid": True,
                        "message": "Mermaid 语法有效"
                    }
                elif response.status_code == 400:
                    return {
                        "valid": False,
                        "error": "无效的 Mermaid 语法"
                    }
                else:
                    return {
                        "valid": False,
                        "error": f"验证失败:HTTP {response.status_code}"
                    }
                    
            except requests.Timeout:
                return {
                    "valid": False,
                    "error": "验证超时"
                }
            except requests.ConnectionError:
                return {
                    "valid": False,
                    "error": "连接错误:无法访问验证服务"
                }
            except Exception as e:
                return {
                    "valid": False,
                    "error": f"验证错误:{str(e)}"
                }
                
        except Exception as e:
            return {
                "valid": False,
                "error": f"验证过程中发生意外错误:{str(e)}"
            }
  • Registers the validate_mermaid_syntax function as an MCP tool using the FastMCP decorator.
    @mcp.tool()
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions '尝试简单转换' (attempting simple conversion) as the validation method, which implies a read-only, non-destructive operation, but doesn't clarify error handling, performance implications, or what '简单转换' entails. For a tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise and well-structured: a purpose statement followed by clear parameter and return sections in bullet-like format. Every sentence earns its place without redundancy, and it's front-loaded with the core functionality. The bilingual presentation (Chinese purpose, English labels) is efficient for clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (single parameter, no nested objects) and the presence of an output schema (which handles return values), the description is reasonably complete. It covers purpose, parameter semantics, and return type at a high level. However, it lacks usage guidelines and detailed behavioral context, which are minor gaps in this simple validation context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description explicitly documents the single parameter 'mermaid_code' as '要验证的 Mermaid 图表语法代码' (Mermaid diagram syntax code to validate), adding meaning beyond the schema's basic title 'Mermaid Code'. However, with schema description coverage at 0%, it doesn't provide format details, constraints, or examples. The baseline is 3 since it compensates somewhat but not fully for the schema's lack of descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose as '验证 Mermaid 图表语法' (validate Mermaid diagram syntax) and specifies the method '通过尝试简单转换' (by attempting simple conversion). It distinguishes from sibling tools like 'convert_mermaid_to_image' by focusing on validation rather than conversion to image format. However, it doesn't explicitly differentiate from 'get_supported_options' which might relate to syntax options.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools 'convert_mermaid_to_image' or 'get_supported_options', nor does it specify scenarios where validation is preferred over direct conversion or option checking. There's no indication of prerequisites or exclusions for usage.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wwwzhouhui/mermaid_mcp_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server