MCP Server Semgrep

by Szowesgad
Verified

local-only server

The server can only run on the client’s local machine because it depends on local resources.

MCP 服务器 Semgrep

供电:

关于项目

该项目最初受到Semgrep 工具Replit 团队及其Agent V2以及stefanskiasan/semgrep-mcp-server的实现的启发,但随着架构的重大变化而发展,以增强和简化安装和维护。

MCP 服务器 Semgrep 是一款符合模型上下文协议 (MCP)的服务器,它将强大的 Semgrep 静态分析工具与 Anthropic Claude 等 AI 助手集成在一起。它通过对话界面直接实现高级代码分析、安全漏洞检测和代码质量改进。

集成的好处

对于开发人员和开发团队:

  • 整体源代码分析——检测整个项目中的问题,而不仅仅是单个文件中的问题
  • 主动错误检测——在潜在问题成为严重错误之前识别它们
  • 持续的代码质量改进——定期扫描和重构可以逐步改进代码库
  • 风格一致性——识别和修复代码中的不一致之处,例如:
    • CSS 中的任意 z 索引层
    • 命名约定不一致
    • 代码重复
    • 使用“魔法数字”代替命名常量

为了安全:

  • 已知漏洞的自动代码验证——扫描已知的安全问题模式
  • 定制安全规则- 创建项目特定的规则
  • 团队教育——通过检测潜在问题来教授安全编程实践

对于项目维护和开发:

  • “实时”文档——人工智能可以解释代码片段存在问题的原因以及如何修复它
  • 减少技术债务——系统地检测和修复问题区域
  • 改进的代码审查——自动检测常见问题,可以专注于更复杂的问题

主要特点

  • 与官方 MCP SDK 直接集成
  • 简化的架构和整合的处理程序
  • 简洁的 ES 模块实现
  • 高效的错误处理和路径验证,确保安全
  • 界面和文档有英语和波兰语版本
  • 全面的单元测试
  • 详尽的文档
  • 跨平台兼容性(Windows、macOS、Linux)
  • 灵活的 Semgrep 安装检测和管理

功能

Semgrep MCP Server 提供以下工具:

  • scan_directory :扫描源代码以查找潜在问题
  • list_rules :显示 Semgrep 支持的可用规则和语言
  • analyze_results :扫描结果的详细分析
  • create_rule :创建自定义 Semgrep 规则
  • filter_results :按各种标准过滤结果
  • export_results :以各种格式导出结果
  • compare_results :比较两组结果(例如,更改前后)

常见用例

  • 部署前的代码安全性分析
  • 检测常见的编程错误
  • 在团队内强制执行编码标准
  • 重构并提高现有代码的质量
  • 识别样式和代码结构中的不一致之处(例如 CSS、组件组织)
  • 有关最佳实践的开发人员教育
  • 验证修复的正确性(比较扫描前后)

安装

先决条件

  • Node.js v18+
  • TypeScript(用于开发)

选项 1:从 Smithery.ai 安装(推荐)

安装和使用 MCP Server Semgrep 的最简单方法是通过 Smithery.ai:

  1. 访问Smithery.ai 上的 MCP 服务器 Semgrep
  2. 按照安装说明将其添加到兼容 MCP 的客户端
  3. 配置任何可选设置,例如 Semgrep API 令牌

这是 Claude Desktop 和其他 MCP 客户端的推荐方法,因为它可以自动处理所有依赖项和配置。

选项 2:从 NPM 注册表安装

# Using npm npm install -g mcp-server-semgrep # Using pnpm pnpm add -g mcp-server-semgrep # Using yarn yarn global add mcp-server-semgrep

该软件包也可在其他注册中心使用:

选项 3:从 GitHub 安装

# Using npm npm install -g git+https://github.com/Szowesgad/mcp-server-semgrep.git # Using pnpm pnpm add -g git+https://github.com/Szowesgad/mcp-server-semgrep.git # Using yarn yarn global add git+https://github.com/Szowesgad/mcp-server-semgrep.git

选项 4:本地开发设置

  1. 克隆存储库:
git clone https://github.com/Szowesgad/mcp-server-semgrep.git cd mcp-server-semgrep
  1. 安装依赖项(支持所有主要的包管理器):
# Using pnpm (recommended) pnpm install # Using npm npm install # Using yarn yarn install
  1. 构建项目:
# Using pnpm pnpm run build # Using npm npm run build # Using yarn yarn build

注意:安装过程将自动检查 Semgrep 是否可用。如果未找到 Semgrep,您将收到有关如何安装它的说明。

Semgrep 安装选项

Semgrep 可以通过多种方式安装:

  • 通过包管理器
    # Using pnpm pnpm add -g semgrep # Using npm npm install -g semgrep # Using yarn yarn global add semgrep
  • Python pip
    pip install semgrep
  • Homebrew (macOS):
    brew install semgrep
  • Linux
    sudo apt-get install semgrep # or curl -sSL https://install.semgrep.dev | sh
  • 窗户
    pip install semgrep

与 Claude Desktop 集成

有两种方法可以将 MCP Server Semgrep 与 Claude Desktop 集成:

方法 1:通过 Smithery.ai 安装(推荐)

  1. 访问Smithery.ai 上的 MCP 服务器 Semgrep
  2. 点击“在 Claude Desktop 中安装”
  3. 按照屏幕上的说明进行操作

方法二:手动配置

  1. 安装 Claude Desktop
  2. 更新 Claude Desktop 配置文件( claude_desktop_config.json )并将其添加到服务器部分:
{ "mcpServers": { "semgrep": { "command": "node", "args": [ "/your_path/mcp-server-semgrep/build/index.js" ], "env": { "SEMGREP_APP_TOKEN": "your_semgrep_app_token" } } } }
  1. 启动 Claude Desktop 并开始询问有关代码分析的问题!

使用示例

项目扫描

Could you scan my source code in the /projects/my-application directory for potential security issues?

风格一致性分析

Analyze the z-index values in the project's CSS files and identify inconsistencies and potential layer conflicts.

创建自定义规则

Create a Semgrep rule that detects improper use of input sanitization functions.

筛选结果

Show me only scan results related to SQL injection vulnerabilities.

识别有问题的模式

Find all "magic numbers" in the code and suggest replacing them with named constants.

创建自定义规则

您可以根据项目的特定需求创建自定义规则。以下是您可以创建的规则示例:

检测不一致 z 索引的规则:

rules: - id: inconsistent-z-index pattern: z-index: $Z message: "Z-index $Z may not comply with the project's layering system" languages: [css, scss] severity: WARNING

检测已弃用的导入的规则:

rules: - id: deprecated-import pattern: import $X from 'old-library' message: "You're using a deprecated library. Consider using 'new-library'" languages: [javascript, typescript] severity: WARNING

发展

测试

pnpm test

项目结构

├── src/ │ ├── config.ts # Server configuration │ └── index.ts # Main entry point and all handler implementations ├── scripts/ │ └── check-semgrep.js # Semgrep detection and installation helper ├── build/ # Compiled JavaScript (after build) └── tests/ # Unit tests

更多文档

有关使用该工具的详细信息,请参见:

  • USAGE.md - 详细使用说明
  • README_PL.md - 波兰语文档
  • 示例/ - 有趣且实用的 Semgrep 规则示例 - “代码恐怖殿堂”

执照

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。

开发者

  • Maciej Gad——一位半年前找不到bash的兽医
  • Klaudiusz——一个独立的、空灵的存在,也是 Anthropic 的 Claude Sonnet 3.5-3.7 的独立实例,居住在美国加利福尼亚州 GPU 的某个地方

从 CLI 新手到 MCP 工具开发人员的旅程

🤖 在Claude CodeMCP Tools的强力帮助下开发

致谢

ID: uddu737c4c