MCP Java Testing Agent

MCP Java Testing Agent

Automates Java/Maven test generation, JaCoCo coverage analysis, and Checkstyle checking through MCP tools. Enables iterative improvement of test coverage and code style until quality goals are met.

Category
访问服务器

README

MCP Java Testing Agent

This repository provides an MCP (Model Context Protocol) server that exposes tools to automate test generation, coverage analysis, and style checking for Java/Maven projects. It is designed to be driven by an MCP-aware client using the tester.prompt.md prompt.


Overview

The MCP server (in main.py) exposes tools that:

  • Discover public methods in a Java codebase
  • Generate skeleton JUnit tests
  • Run Maven (mvn clean test)
  • Analyze JaCoCo coverage reports
  • Analyze Checkstyle reports

The higher-level loop (iterating until coverage/style goals are met) is described in tester.prompt.md and executed by the LLM client, not hard-coded in Python.


Requirements

  • Python 3.x
  • Java JDK installed and on PATH
  • Maven (mvn) installed and on PATH
  • Maven project configured to:
    • Place sources under something like A2/src/main/java
    • Generate JaCoCo XML at A2/target/site/jacoco/jacoco.xml
    • Generate Checkstyle XML at A2/target/checkstyle-result.xml
  • An MCP-capable editor/client (e.g., VS Code with MCP integration)

Installation & Configuration

  1. Clone and install:

    git clone <your-repo-url>
    cd <your-repo-folder>
    pip install fastmcp
    
     Verify:
    
     java -version
     mvn -v
    
     Ensure your Maven pom.xml is set up to:
    
         Run tests
    
         Produce JaCoCo and Checkstyle reports at the paths above
    
    

Running the MCP Server

From the project root:

python server.py

This starts the MCP server over SSE:

if name == "main": mcp.run(transport="sse")

Your MCP client should be configured to connect to this server. Using the tester Prompt

Assuming you’re in an editor like VS Code with MCP enabled:

Open the project folder (containing main.py and tester.prompt.md).

Open tester.prompt.md (or tester.md) in the editor.

Press Ctrl+Shift+P to open the Command Palette.

Select “Run Prompt” (or the equivalent command).

Choose the tester prompt.

The MCP client will then:

Call the tools defined in tester.prompt.md:

- mcp-final/generate-tests
- mcp-final/run_tests
- mcp-final/analyze-coverage
- mcp-final/get_all_public_methods
- mcp-final/analyze-checkstyle
- mcp-final/run-checkstyle
- mcp-final/git-add-all
- mcp-final/git-commit
- mcp-final/git_pull_request
- mcp-final/git_push
- mcp-final/git_status

Iterate to generate tests, run coverage/style checks, and produce a final summary.

Project Structure (Typical)

├── main.py # MCP server and tools ├── .github/prompts/ │ └── tester.prompt.md # Agent configuration/prompt ├── A2/ │ ├── pom.xml │ ├── src/main/java/... # Java sources │ └── target/ │ ├── site/jacoco/jacoco.xml │ └── checkstyle-result.xml

Adjust paths as needed; defaults are used in the tool implementations. MCP Tools generate_tests(source_file: str) -> str

Generate a skeleton JUnit test class for a given .java file.

Extracts the class name and public method names via regex.

Creates {ClassName}Test with stub methods:

@Test
void test_methodName() {
    // TODO: implement test
}

Writes to: codebase/src/test/java/{ClassName}Test.java.

Returns: Path message on success, or an error string if the file/class/methods can’t be found. get_all_public_methods(dir: str = "A2/src/main/java") -> List[str]

Recursively scan dir for .java files and return all lines starting with public.

Uses os.walk to find .java files.

Reads each file line-by-line and keeps line.strip().startswith("public").

Returns: List of raw lines (method signatures, constructors, or public fields). analyze_coverage(xml_path: str = "A2/target/site/jacoco/jacoco.xml") -> dict

Parse a JaCoCo XML report and list methods with <100% instruction coverage.

Output structure:

{ "uncovered_methods": [ { "class": "com/example/MyClass", "method": "myMethod", "coverage": 0.75, "missed": 10, "covered": 30 }, ... ], "count": 3 }

Returns an "error" key if the XML file is missing. run_checkstyle() -> str

Run the Maven build and tests:

mvn clean test

Assumes your Maven config runs Checkstyle and JaCoCo as part of the build.

Returns: stdout from the Maven process (errors are visible in this text). analyze_checkstyle(xml_path: str = "A2/target/checkstyle-result.xml") -> dict

Parse a Checkstyle XML report and list all style violations.

Output structure:

{ "violations": [ { "file": "/path/to/Foo.java", "line": "42", "column": "13", "severity": "warning", "message": "Line is longer than 100 characters", "source": "com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck" }, ... ], "count": 5 }

Returns an "error" key if the XML file is missing. Agent Workflow Summary

A typical workflow implemented in tester.prompt.md:

Call get_all_public_methods to discover public methods.

Call generate_tests to create initial JUnit tests.

Call run_checkstyle to run mvn clean test and produce JaCoCo + Checkstyle reports.

Call analyze_coverage to find uncovered methods.

Call analyze_checkstyle to find style violations.

Update tests and code (by the agent using file edits), then repeat steps 3–5.

Stop when:

    100% coverage and zero Checkstyle issues are reached, or

    A maximum iteration count (e.g., 10) is reached.

Produce a final summary report (tests created, coverage, style status, recommendations).

Notes & Limitations

Regex-based parsing is simple and may not handle all Java edge cases (complex generics, annotations, inner classes).

Default paths (A2/src/main/java, codebase/src/test/java, etc.) can be customized; keep tool defaults in sync with your project layout.

run_checkstyle only runs mvn clean test; ensure your pom.xml actually binds JaCoCo and Checkstyle to this phase.

The iterative “improve coverage & style” loop is implemented by the MCP client/LLM using these tools, not within server.py itself.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选