Skip to main content
Glama
SongJiangzhou

C++ Style Guide MCP Server

check_include_guard

Verify C++ header file include guards for correctness by analyzing code and suggesting proper macro names based on file paths.

Instructions

检查 C++ 头文件的包含保护是否正确

参数:
    code: 头文件的完整代码
    file_path: 可选的文件路径,用于生成建议的保护宏名

返回:
    检查结果,包含是否符合规范、详细说明和建议

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes
file_pathNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • MCP tool handler for 'check_include_guard'. Decorated with @mcp.tool(), it implements the tool by invoking the IncludeGuardChecker instance.
    @mcp.tool()
    def check_include_guard(code: str, file_path: str = "") -> str:
        """
        检查 C++ 头文件的包含保护是否正确
    
        参数:
            code: 头文件的完整代码
            file_path: 可选的文件路径,用于生成建议的保护宏名
    
        返回:
            检查结果,包含是否符合规范、详细说明和建议
        """
        checker = get_include_guard_checker()
    
        file_path_param = file_path if file_path else None
        is_valid, details, suggestions = checker.check_include_guard(code, file_path_param)
    
        result = details
        if suggestions and not is_valid:
            result += f"\n建议的保护宏名:\n"
            for sug in suggestions:
                result += f"  • {sug}\n"
    
        return result
  • Core logic implementation of the include guard check within the IncludeGuardChecker class. Performs detailed analysis for #pragma once, traditional guards, name validation, and generates suggestions.
    def check_include_guard(
        self,
        code: str,
        file_path: Optional[str] = None
    ) -> Tuple[bool, str, List[str]]:
        """
        检查头文件的包含保护
    
        Args:
            code: 头文件代码
            file_path: 可选的文件路径,用于生成建议的保护宏名
    
        Returns:
            (是否符合规范, 详细说明, 建议列表)
        """
        lines = code.strip().split('\n')
        if len(lines) < 3:
            return False, "文件太短,无法包含有效的包含保护", []
    
        # 检查 #pragma once
        has_pragma_once = self._check_pragma_once(code)
    
        # 检查传统的 #ifndef/#define/#endif 保护
        has_traditional_guard, guard_name = self._check_traditional_guard(code)
    
        suggestions = []
        details = ""
    
        if has_pragma_once:
            details = "✓ 使用了 #pragma once\n\n"
            details += "说明: #pragma once 是现代编译器广泛支持的简洁方式。\n"
            details += "优点: 简洁、避免宏名冲突\n"
            details += "注意: 大多数编译器支持,但不是 C++ 标准的一部分\n"
            return True, details, []
    
        if has_traditional_guard:
            # 检查保护宏命名是否合理
            is_valid_name, name_message = self._validate_guard_name(guard_name, file_path)
    
            details = f"✓ 使用了传统的包含保护\n"
            details += f"保护宏名: {guard_name}\n\n"
    
            if is_valid_name:
                details += f"命名检查: ✓ 符合规范\n"
                details += f"{name_message}\n"
            else:
                details += f"命名检查: ✗ 可以改进\n"
                details += f"{name_message}\n\n"
                # 生成建议的宏名
                if file_path:
                    suggested_names = self._generate_guard_names(file_path)
                    suggestions = suggested_names
                    details += "建议的保护宏名:\n"
                    for name in suggested_names:
                        details += f"  • {name}\n"
    
            details += "\n包含保护规范:\n"
            details += "  • 宏名应全大写,使用下划线分隔\n"
            details += "  • 包含文件路径或项目名作为前缀,避免冲突\n"
            details += "  • 以 _H、_HPP 或 _H_ 结尾\n"
            details += "  • 避免以下划线开头(保留给编译器)\n"
    
            return True, details, suggestions
    
        # 没有任何保护
        details = "✗ 缺少包含保护!\n\n"
        details += "头文件应使用以下方式之一防止重复包含:\n\n"
    
        details += "方式1: #pragma once (推荐)\n"
        details += "```cpp\n"
        details += "#pragma once\n\n"
        details += "// 头文件内容\n"
        details += "```\n\n"
    
        details += "方式2: 传统包含保护\n"
        details += "```cpp\n"
        if file_path:
            guard_name = self._generate_guard_names(file_path)[0]
            suggestions = self._generate_guard_names(file_path)
        else:
            guard_name = "MY_HEADER_H"
        details += f"#ifndef {guard_name}\n"
        details += f"#define {guard_name}\n\n"
        details += "// 头文件内容\n\n"
        details += f"#endif // {guard_name}\n"
        details += "```\n"
    
        return False, details, suggestions
  • Factory function to get the singleton IncludeGuardChecker instance, used by the MCP tool handler.
    def get_checker() -> IncludeGuardChecker:
        """获取全局包含保护检查器实例"""
        global _checker
        if _checker is None:
            _checker = IncludeGuardChecker()
        return _checker
  • Registration of the 'check_include_guard' tool using the @mcp.tool() decorator.
    @mcp.tool()
    def check_include_guard(code: str, file_path: str = "") -> str:
        """
        检查 C++ 头文件的包含保护是否正确
    
        参数:
            code: 头文件的完整代码
            file_path: 可选的文件路径,用于生成建议的保护宏名
    
        返回:
            检查结果,包含是否符合规范、详细说明和建议
        """
        checker = get_include_guard_checker()
    
        file_path_param = file_path if file_path else None
        is_valid, details, suggestions = checker.check_include_guard(code, file_path_param)
    
        result = details
        if suggestions and not is_valid:
            result += f"\n建议的保护宏名:\n"
            for sug in suggestions:
                result += f"  • {sug}\n"
    
        return result
Behavior3/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 describes what the tool does (checks include guard correctness) and what it returns (results with compliance details, explanations, and suggestions), but doesn't mention performance characteristics, error handling, or specific implementation details that would help an agent understand behavioral traits beyond basic functionality.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded with the main purpose first, followed by parameter and return sections. Every sentence adds value, though the structure could be slightly more polished (e.g., using bullet points or clearer formatting).

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 moderate complexity (2 parameters, no annotations, but with output schema), the description is reasonably complete. It explains the purpose, parameters, and return value, and since an output schema exists, it doesn't need to detail return structure. However, it could benefit from more context on error cases or edge scenarios.

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

Parameters4/5

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

With 0% schema description coverage, the description compensates well by explaining both parameters: 'code' as the complete header file code and 'file_path' as an optional path for generating suggested guard macro names. This adds meaningful context beyond the bare schema, though it doesn't detail format requirements or examples.

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

Purpose5/5

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

The description clearly states the specific action ('检查' meaning 'check') and resource ('C++ 头文件的包含保护' meaning 'C++ header file include guard'), distinguishing it from sibling tools like analyze_memory_safety or check_naming by focusing on include guard validation rather than memory safety or naming conventions.

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

Usage Guidelines3/5

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

The description implies usage for C++ header files, but doesn't explicitly state when to use this tool versus alternatives like check_const_correctness or suggest_modern_cpp. It provides basic context but lacks explicit guidance on exclusions or comparisons with sibling tools.

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/SongJiangzhou/cpp_guidelines'

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