Skip to main content
Glama
Lekssays

Joern MCP Server

by Lekssays

🦡 codebadger

一个容器化的模型上下文协议 (MCP) 服务器,利用 Joern 的代码属性图 (CPG) 技术提供静态代码分析,支持 Java、C/C++、JavaScript、Python、Go、Kotlin、C#、Ghidra、Jimple、PHP、Ruby 和 Swift。

新闻

codebadger 及其配套论文——Bridging Code Property Graphs and Language Models for Program Analysis——已被 ICSE 2026 软件漏洞管理研讨会录用。🎉

Related MCP server: Smart Code Reviewer

引用

@article{lekssays2026bridging,
  title={Bridging Code Property Graphs and Language Models for Program Analysis},
  author={Lekssays, Ahmed},
  journal={arXiv preprint arXiv:2603.24837},
  year={2026}
}

使用 codebadger 发现了漏洞?

如果 codebadger 帮助您发现了现实世界中的漏洞,我们非常乐意了解。请提交一个 Pull Request 将其添加到 TROPHIES.md 中——包括 CVE ID、项目名称、一行描述和日期。

前置要求

在开始之前,请确保您已安装:

  • DockerDocker Compose

  • Python 3.10+ (推荐 Python 3.13)

  • pip (Python 包管理器)

验证您的设置:

docker --version
docker-compose --version
python --version

快速开始

1. 安装 Python 依赖

# Create a virtual environment (optional but recommended)
python -m venv venv

# Install dependencies
pip install -r requirements.txt

2. 启动 Docker 服务 (Joern)

docker compose up -d

这将启动:

  • Joern Server:静态代码分析引擎(运行 CPG 生成和查询)

验证服务是否正在运行:

docker compose ps

3. 启动 MCP 服务器

# Start the server
python main.py &

MCP 服务器将在 http://localhost:4242 可用。

4. 停止所有服务

# Stop MCP server (Ctrl+C in terminal)

# Stop Docker services
docker-compose down
# Optional: Clean up everything
bash cleanup.sh

清理脚本

使用提供的清理脚本重置您的环境:

bash cleanup.sh

这将:

  • 停止并删除 Docker 容器

  • 终止孤立的 Joern/MCP 进程

  • 清除 Python 缓存 (__pycache__, .pytest_cache)

  • 可选地清除 playground 目录(CPG 和缓存的代码库)

集成

GitHub Copilot 集成

编辑 VS Code (GitHub Copilot) 的 MCP 配置文件:

路径:

~/.config/Code/User/mcp.json

配置示例:

{
  "inputs": [],
  "servers": {
    "codebadger": {
      "url": "http://localhost:4242/mcp",
      "type": "http"
    }
  }
}

Claude Code 集成

要将 codebadger 集成到 Claude Desktop,请编辑:

路径:

Claude → Settings → Developer → Edit Config → claude_desktop_config.json

添加以下内容:

{
  "mcpServers": {
    "codebadger": {
      "url": "http://localhost:4242/mcp",
      "type": "http"
    }
  }
}

可用工具

核心工具

  • generate_cpg:为代码库(本地路径或 GitHub URL)生成代码属性图 (CPG)。

  • get_cpg_status:检查 CPG 是否存在并获取状态元数据。

  • run_cpgql_query:针对 CPG 执行原始 CPGQL 查询并返回结构化结果。

  • get_cpgql_syntax_help:显示 CPGQL 语法助手、提示和常见错误修复。

代码浏览

  • list_methods:列出方法/函数,支持可选的正则表达式/文件过滤器。

  • list_files:以分页树状视图显示源文件。

  • get_method_source:获取指定方法的源代码。

  • list_calls:列出函数之间的调用点(调用者 → 被调用者)。

  • get_call_graph:构建人类可读的调用图(入站或出站)。

  • list_parameters:获取方法的参数名称、类型和顺序。

  • get_codebase_summary:高级指标(文件、方法、调用、语言)。

  • get_code_snippet:通过起始/结束行号返回文件片段。

语义分析

  • get_cfg:生成方法的控制流图(节点和边)。

  • get_type_definition:检查结构体/类类型及其成员。

  • get_macro_expansion:启发式检测可能的宏展开调用。

污点与漏洞分析

  • find_taint_sources:查找可能的外部输入点(源)。

  • find_taint_sinks:定位污点数据可能流向的危险接收点(汇)。

  • find_taint_flows:检测从源到汇的数据流(污点分析)。

  • get_program_slice:为调用构建向后/向前程序切片。

  • get_variable_flow:追踪特定位置变量的数据依赖关系。

  • find_bounds_checks:搜索缓冲区访问附近的边界检查。

  • find_use_after_free:启发式检测释放后使用模式。

  • find_double_free:检测潜在的重复释放问题。

  • find_null_pointer_deref:查找可能的空指针解引用。

  • find_integer_overflow:检测整数溢出模式。

  • find_format_string_vulns:检测格式化字符串漏洞 (CWE-134),即非字面量格式参数被传递给 printf 系列函数。

  • find_heap_overflow:检测堆溢出漏洞 (CWE-122),即对堆缓冲区的写入可能超过其分配的大小。

  • find_stack_overflow:检测栈缓冲区溢出漏洞 (CWE-121),即对固定大小局部数组(例如 char buf[64])的写入可能超过其声明的维度。

  • find_toctou:检测检查时-使用时 (Time-of-Check-Time-of-Use) 竞态条件 (CWE-367),即文件先通过 access()/stat() 检查,然后在单独的步骤中打开或操作。

  • find_uninitialized_reads:检测未初始化变量读取 (CWE-457),即局部变量在赋值前被使用。

自定义工具

您无需修改核心代码库即可添加自己的检测器:

  1. src/tools/queries/your_query.scala 中编写 Scala 查询模板。

  2. src/tools/custom_tools.py 中注册 Python 工具函数。

  3. 重启服务器——该工具会自动出现在每个 MCP 客户端中。

请参阅 CUSTOM_TOOLS_GUIDE.md 获取完整的逐步指南、CPGQL 参考和设计决策。

贡献与测试

感谢您的贡献!以下是开始运行测试和贡献代码的快速指南。

前置要求

  • Python 3.10+ (CI 中使用 3.13)

  • Docker 和 Docker Compose (用于集成测试)

本地开发设置

  1. 创建虚拟环境并安装依赖

python -m venv venv
pip install -r requirements.txt
  1. 启动 Docker 服务 (用于集成测试)

docker-compose up -d
  1. 运行单元测试

pytest tests/ -q
  1. 运行集成测试 (需要运行 Docker Compose)

# Start MCP server in background
python main.py &

# Run integration tests
pytest tests/integration -q

# Stop MCP server
pkill -f "python main.py"
  1. 运行所有测试

pytest tests/ -q
  1. 测试后清理

bash cleanup.sh
docker-compose down

代码贡献

贡献时请遵循以下准则:

  1. 遵循仓库约定

  2. 为行为变更编写测试

  3. 在提交 PR 前确保所有测试通过

  4. 在 PR 描述中包含清晰的变更日志

  5. 如有需要,更新文档

配置

MCP 服务器可以通过环境变量或 config.yaml 进行配置。

环境变量

关键设置(可选 - 显示为默认值):

# Server
MCP_HOST=0.0.0.0
MCP_PORT=4242

# Joern
JOERN_BINARY_PATH=joern
JOERN_JAVA_OPTS="-Xmx4G -Xms2G -XX:+UseG1GC -Dfile.encoding=UTF-8"

# CPG Generation
CPG_GENERATION_TIMEOUT=600
MAX_REPO_SIZE_MB=500

# Query
QUERY_TIMEOUT=30
QUERY_CACHE_ENABLED=true
QUERY_CACHE_TTL=300

# Telemetry (OpenTelemetry)
OTEL_ENABLED=false
OTEL_SERVICE_NAME=codebadger
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
OTEL_EXPORTER_OTLP_PROTOCOL=grpc

配置文件

config.example.yaml 创建 config.yaml

cp config.example.yaml config.yaml

然后根据需要进行自定义。

遥测 (OpenTelemetry)

CodeBadger 内置了用于分布式追踪的 OpenTelemetry 支持。启用后,所有 MCP 工具调用都会被自动追踪,此外还包括 CPG 生成、Joern 服务器管理和查询执行的自定义跨度 (spans)。

快速开始

  1. 安装遥测依赖(包含在 requirements.txt 中):

pip install opentelemetry-sdk opentelemetry-exporter-otlp
  1. 通过环境变量启用:

export OTEL_ENABLED=true
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
python main.py

或通过 config.yaml

telemetry:
  enabled: true
  service_name: codebadger
  otlp_endpoint: http://localhost:4317
  otlp_protocol: grpc  # or "http/protobuf"

使用 Jaeger 进行本地开发

# Start Jaeger (provides UI at http://localhost:16686)
docker run -d --name jaeger \
  -p 16686:16686 \
  -p 4317:4317 \
  jaegertracing/all-in-one:latest

# Start CodeBadger with telemetry
OTEL_ENABLED=true python main.py

追踪内容

跨度 (Span)

描述

tools/call {name}

每次 MCP 工具调用(通过 FastMCP 自动实现)

cpg.generate

完整的 CPG 生成流水线

cpg.joern_cli_exec

在 Docker 内执行 Joern CLI 命令

cpg.spawn_server

Joern 服务器实例创建

cpg.load_cpg

将 CPG 文件加载到 Joern 服务器

query.execute

带有计时和成功属性的 CPGQL 查询执行

配置参考

设置

环境变量

默认值

描述

enabled

OTEL_ENABLED

false

启用/禁用遥测

service_name

OTEL_SERVICE_NAME

codebadger

追踪中的服务名称

otlp_endpoint

OTEL_EXPORTER_OTLP_ENDPOINT

http://localhost:4317

OTLP 收集器端点

otlp_protocol

OTEL_EXPORTER_OTLP_PROTOCOL

grpc

导出协议 (grpchttp/protobuf)

当遥测被禁用(默认)时,所有追踪均为无操作 (no-op),零开销。

A
license - permissive license
Not graded
quality - not tested
A
maintenance

Maintenance

Maintainers
9hResponse time
2wRelease cycle
20Releases (12mo)
Commit activity
Issues opened vs closed

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    A
    quality
    D
    maintenance
    Enables AI assistants to understand and navigate codebases through structural analysis. Provides code mapping, symbol search, and impact analysis using ast-grep for accurate parsing of Python, JavaScript, TypeScript, and Go projects.
    4
    52
    MIT
  • A
    license
    A
    quality
    D
    maintenance
    Enables comprehensive code analysis including quality assessment, security vulnerability detection, refactoring suggestions, complexity calculations, and automatic documentation generation for multiple programming languages.
    5
    10
    MIT
  • F
    license
    C
    quality
    D
    maintenance
    Enables AI assistants to analyze codebases through semantic search, call graph generation, and function metadata extraction. Provides real-time code analysis with persistent vector storage for understanding complex code structures and relationships.
    13
    5
  • F
    license
    B
    quality
    Not graded
    maintenance
    Provides comprehensive codebase analysis and semantic understanding through integrated knowledge graphs, enabling AI assistants to understand project structure, patterns, dependencies, and context through multiple analysis tools and format generators.
    9

View all related MCP servers

Related MCP Connectors

View all MCP Connectors

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/Lekssays/codebadger'

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