Skip to content

Initial implementation#1

Merged
brnorris03 merged 33 commits intomainfrom
initial-implementation
Dec 8, 2025
Merged

Initial implementation#1
brnorris03 merged 33 commits intomainfrom
initial-implementation

Conversation

@brnorris03
Copy link
Copy Markdown
Owner

@brnorris03 brnorris03 commented Dec 7, 2025

This pull request introduces comprehensive documentation and configuration improvements for the MLIR MCP Server project. The changes focus on making setup, development, and usage much clearer for new contributors and users, with detailed guides, configuration examples, and workflow instructions. The most important updates are grouped below:

Documentation Enhancements:

  • Added a detailed project overview, feature list, installation instructions, configuration options, and usage guide to README.md, making it much easier for users to get started and understand the server’s capabilities.
  • Created CONTRIBUTING.md with step-by-step development setup, code style guidelines, commit message conventions, and project structure, streamlining the onboarding process for new contributors.
  • Added docs/mlir_python_bindings.md, a comprehensive guide for building, installing, and troubleshooting MLIR Python bindings, which are required for parsing and manipulating MLIR code.
  • Added docs/phase2_summary.md, summarizing the implementation of core parsing and validation tools, error handling, test coverage, and next development steps, providing transparency and technical insight into the project’s progress.

Configuration and Setup Improvements:

  • Added .env.example with clear instructions and examples for configuring MLIR toolchain detection, supporting both single and multiple installations, and explaining how environment variables are auto-configured for Python and library paths.

Continuous Integration Setup:

  • Introduced .github/workflows/ci.yml to automate testing, MLIR toolchain building, coverage reporting, and PR comments for both Ubuntu and macOS, ensuring code quality and reliability across platforms.

brnorris03 and others added 4 commits December 6, 2025 21:58
Implement Phase 1 (Project Setup & Infrastructure) and Phase 2 (Core
Parsing & Validation) of the MLIR MCP Server.

## Phase 1: Project Setup & Infrastructure

- Add pyproject.toml with project configuration and dependencies
- Implement configuration system with auto-detection (config.py)
  * Auto-detects MLIR toolchain from common locations
  * Support for .env files and environment variables
  * Relaxed validation (only core tools required)
- Create MCP server skeleton with FastMCP (server.py)
  * Health check tools: ping, toolchain_info
- Add comprehensive test suite for configuration
- Create helper scripts: run_server.sh, run_tests.sh
- Add development documentation: CONTRIBUTING.md

## Phase 2: Core Parsing & Validation

- Implement error handling module (utils/error_handling.py)
  * MLIRError dataclass for structured errors
  * parse_mlir_diagnostic() for error parsing
- Implement parsing tools (tools/parser.py)
  * parse_mlir() - Parse MLIR code and return module info
  * validate_mlir() - Validate MLIR syntax and semantics
  * get_module_info() - Extract detailed structural information
- Add MLIR test fixtures (valid and invalid)
- Create comprehensive parser test suite (9 tests, all passing)
- Register parsing tools in MCP server

## Documentation

- Update README.md with installation and setup instructions
  * Python version matching guidance for MLIR bindings
  * Platform-specific configuration (macOS, Linux, Windows)
- Add MLIR Python bindings setup guide
- Add Phase 2 implementation summary

## Key Features

- Zero-configuration setup with auto-detection
- Flexible configuration system
- Graceful error handling (works without MLIR bindings)
- Platform-agnostic design
- Comprehensive test coverage (19 tests, all passing)

## Server Status

5 MCP tools available:
1. ping - Health check
2. toolchain_info - Toolchain details
3. parse_mlir - Parse MLIR code
4. validate_mlir - Validate MLIR
5. get_module_info - Extract module information

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Implement Phase 3 (Generation Tools) with IR builders, templates, and
generation capabilities.

## Generation Tools

- Implement IR builder wrappers (mlir_wrapper/ir_builder.py)
  * build_function() - Build MLIR functions with signatures
  * build_operation() - Build individual operations
  * build_module_from_operations() - Combine operations into modules

- Implement generation tools (tools/generator.py)
  * create_function() - Generate functions with custom signatures
  * create_operation() - Generate specific MLIR operations
  * generate_from_template() - Generate from predefined templates
  * list_templates() - List available templates with parameters

## Template Library

Six MLIR templates for common patterns:
1. simple_function - Basic function skeleton
2. add_function - Integer addition
3. mul_function - Integer multiplication
4. constant - Constant value function
5. if_else - Conditional with scf.if
6. for_loop - Loop with accumulation

All templates support parameter substitution for types and values.

## Testing

- Add comprehensive test suite (tests/test_generator.py)
  * 13 tests for all generation tools
  * Template generation tests
  * Error handling tests
- All 32 tests passing (100% success rate)

## Configuration

- Update helper scripts to use generic placeholder paths
- Add .env to .gitignore for local configuration
- Scripts now serve as templates users customize for their environment

## Server Integration

- Register 4 new MCP tools in server
- Total of 9 MCP tools now available

## Server Status

9 MCP tools now available:
1-5. Previous parsing tools
6. create_function - Generate MLIR functions
7. create_operation - Generate operations
8. generate_from_template - Use templates
9. list_templates - List available templates

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Update configuration tests to properly isolate from .env files in the
repository by changing to temporary directories during tests that check
auto-detection and error handling.

Fixes:
- test_auto_detect_finds_toolchain: Add monkeypatch.chdir(tmp_path)
- test_config_with_auto_detection: Add monkeypatch.chdir(tmp_path)
- test_config_raises_when_toolchain_not_found: Add monkeypatch.chdir(tmp_path)

All 32 tests now passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Reorganize the Features section to prominently display the 9 MCP tools
available in the server, grouped by functionality:
- Server Management (2 tools)
- Parsing & Validation (3 tools)
- Code Generation (4 tools)

This makes it immediately clear what capabilities the server provides.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
@continue
Copy link
Copy Markdown

continue bot commented Dec 7, 2025

Keep this PR in a mergeable state →

Learn more

All Green is an AI agent that automatically:

✅ Addresses code review comments

✅ Fixes failing CI checks

✅ Resolves merge conflicts

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR provides the initial implementation of an MLIR MCP Server, enabling AI assistants like Claude to parse, generate, transform, and analyze MLIR code through the Model Context Protocol.

Key Changes:

  • Complete server infrastructure with configuration management and auto-detection of MLIR toolchain
  • Parser tools for validating and extracting information from MLIR code
  • Generator tools for creating MLIR functions, operations, and template-based code generation
  • Comprehensive test suite with fixtures and configuration for testing

Reviewed changes

Copilot reviewed 24 out of 31 changed files in this pull request and generated 17 comments.

Show a summary per file
File Description
src/mlir_mcp_server/__init__.py Package initialization with version info
src/mlir_mcp_server/__main__.py Entry point for running the server
src/mlir_mcp_server/config.py Configuration system with auto-detection and validation
src/mlir_mcp_server/server.py MCP server implementation with tool registration
src/mlir_mcp_server/tools/parser.py MLIR parsing and validation tools
src/mlir_mcp_server/tools/generator.py MLIR code generation tools and templates
src/mlir_mcp_server/utils/error_handling.py Structured error handling for MLIR diagnostics
src/mlir_mcp_server/mlir_wrapper/ir_builder.py Wrappers for MLIR Python IR builders
tests/test_config.py Tests for configuration module
tests/test_parser.py Tests for parser tools
tests/test_generator.py Tests for generator tools
tests/fixtures/*.mlir Test fixtures for valid and invalid MLIR code
pyproject.toml Project configuration and dependencies
README.md Project documentation and usage guide
CONTRIBUTING.md Contribution guidelines
run_server.sh, run_tests.sh Helper scripts for running server and tests
.gitignore, .env.example Git configuration and example environment file
docs/*.md Phase 2 summary and MLIR Python bindings documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

brnorris03 and others added 17 commits December 7, 2025 07:28
Address key review comments from GitHub Copilot:

1. Fix body_builder docstring in ir_builder.py
   - Add detailed description of callable signature
   - Explain parameter behavior

2. Standardize error message format
   - Change "Missing template parameter" to "Template parameter not provided"
   - Maintain consistency with other error messages

3. Add comprehensive error handling tests
   - New test_error_handling.py with 11 tests
   - Test MLIRError dataclass
   - Test parse_mlir_diagnostic with various formats
   - Coverage: error_handling.py now at 89%

4. Update README project status
   - Mark parsing and generation tools as implemented (✅)
   - Add multiple installations support to status
   - Update "Coming soon" section

5. Remove unused PyYAML dependency
   - Not currently used in implementation
   - Can be added back if YAML config support is added later

6. Remove unused imports and variables
   - Remove unused MLIRError import from parser.py
   - Remove unused 'parsed' variable from ir_builder.py

Test Results: 58/58 passing (100%), 72% coverage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Add comprehensive MCP server tests and update documentation to reflect
current implementation status.

Changes:
- Add test_server.py with 3 tests for server creation and tool registration
- Update README.md "Project Status" section to mark features as implemented
- Remove PyYAML dependency (unused)

Coverage improvements:
- server.py: 0% → 75%
- Total coverage: 72% → 79%

Test results: 61/61 passing (100%)

Features now marked as implemented:
✅ Multiple MLIR installations support
✅ Automatic Python bindings and library path setup
✅ MLIR parsing tools
✅ MLIR generation tools
✅ Comprehensive test suite

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Add 14 tests for MLIR IR builder wrapper functions to improve coverage.

Tests added:
- build_function with various signatures (simple, no args, multiple results, f64, index)
- build_operation with operands, attributes, and edge cases
- build_module_from_operations with empty, single, and multiple operations
- Error handling tests for invalid inputs

Coverage improvements:
- ir_builder.py: 61% → 86% (+25%)
- Total: 79% → 83% (+4%)

All 75 tests passing (100%)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Address PR comment about temporary implementation markers.

Changes:
- Add comprehensive TODO comment to build_operation explaining
  what a complete implementation would require
- Clarify that current implementation generates textual representations
  suitable for templates and simple generation
- Add TODO for build_function empty body generation
- Reference MLIR Python bindings documentation

This makes it clear that:
1. Current implementation is intentionally simplified
2. Future enhancements are documented
3. Use cases for current implementation are explicit

All tests still passing (75/75, 83% coverage)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Address final PR comments:

1. Fix type annotations in error_handling.py
   - Change to_dict return type to dict[str, Any] for accuracy
   - Change current_error type to dict[str, Any]
   - Properly handle None checks when converting to int
   - Add detailed docstring for to_dict return value

2. Add Bash requirement comments to helper scripts
   - Add "IMPORTANT: This script requires Bash" to run_server.sh
   - Add "IMPORTANT: This script requires Bash" to run_tests.sh

All 75 tests passing, 83% coverage maintained.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Note: CI tests will run without MLIR bindings (graceful degradation).
Tests that require MLIR will be skipped automatically.

All 75 tests passing locally (83% coverage)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Apply automatic linting fixes from ruff and remove unused variables.

Changes:
- Convert Optional[X] to X | None (modern Python type syntax)
- Remove unused ctx variables in parser.py
- Fix exception chaining (raise ... from e)
- Remove unused import in ir_builder.py
- Fix import organization

All 75 tests passing, 83% coverage maintained.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Add .vscode/, IMPLEMENTATION_PLAN.md to gitignore to prevent
accidental commits.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Create shared test utilities module and update tests to work in CI without MLIR.

Changes:
1. Create tests/utils.py with shared helpers:
   - is_mlir_available() - Check if MLIR bindings exist
   - setup_mlir_environment() - Initialize MLIR paths (skips in CI)
   - create_mock_toolchain() - Create mock toolchain for testing
   - get_real_mlir_config() - Get real config or skip

2. Update generator tests to handle missing MLIR gracefully:
   - Tests check is_mlir_available() and assert appropriate result
   - In CI without MLIR: tests pass by expecting error responses
   - Locally with MLIR: tests pass by expecting success

3. Refactor duplicated code across test files:
   - test_generator.py, test_ir_builder.py, test_parser.py, test_server.py
   - test_integration.py now uses shared utilities

All 75 tests passing locally (83% coverage)
CI tests will now pass without MLIR installation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Allow manual workflow dispatch for CI runs.

Workflow triggers:
- Push to main branch
- Pull requests to main
- Manual dispatch via GitHub UI

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Add comprehensive tests for server entry point to improve coverage.

Tests:
- main() with valid configuration
- main() with configuration errors
- main() with validation errors

Coverage improvements:
- __main__.py: 0% → 95% (+95%)
- Total: 83% → 88% (+5%)

All 78 tests passing (100%)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Build minimal MLIR toolchain from LLVM in CI to enable full test coverage.

Implementation:
- Build LLVM 19.1.7 with MLIR project
- Configure minimal build (Native target only, Release mode)
- Enable Python bindings (MLIRPythonModules)
- Build only essential tools: mlir-opt, mlir-translate
- Cache toolchain at /opt/mlir-toolchain
- 150 minute timeout for builds

Cache strategy:
- Key: ${{ runner.os }}-mlir-llvm-19.1.7-python-3.11
- First run: ~90-120 minutes (build + test)
- Cached runs: ~5 minutes (test only)

Testing:
- Set MLIR_INSTALLATIONS=/opt/mlir-toolchain
- Auto-configures PYTHONPATH and library paths
- All 78 tests will run in CI (100%)

This enables full MLIR functionality testing in CI without pre-installed toolchain.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Extract LLVM version to workflow-level environment variable for easier updates.

Changes:
- Add env.LLVM_VERSION: "19.1.7" at workflow level
- Use ${{ env.LLVM_VERSION }} in cache key and git clone
- Single place to update LLVM version

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
@brnorris03 brnorris03 force-pushed the initial-implementation branch from db43133 to 5872a20 Compare December 7, 2025 17:19
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 32 out of 39 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

brnorris03 and others added 11 commits December 7, 2025 09:58
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Implement Phase 4 transformation tools with mlir-opt integration.

New modules:
- utils/subprocess_runner.py - Safe subprocess execution for MLIR tools
  * MLIRToolRunner class with timeout and error handling
  * run_mlir_opt with proper pass pipeline wrapping
  * run_mlir_translate for translation
  * Generic run_tool method

- tools/transformer.py - Transformation tool implementations
  * apply_pass - Apply single MLIR pass
  * apply_pass_pipeline - Apply pass pipeline
  * canonicalize, cse, inline_functions - Convenience functions

Testing:
- test_transformer.py with 9 comprehensive tests
- All transformation tools tested with real mlir-opt
- Test coverage: transformer.py 81%, subprocess_runner.py 48%

Server integration:
- 3 new MCP tools registered (apply_pass, apply_pass_pipeline, canonicalize)
- Total tools: 12

Other changes:
- Add numpy to dependencies (required for MLIR Python bindings)
- Update .gitignore to exclude dev docs
- Update test utilities for better reuse
- Fix test_server to expect 12 tools

Test results: 87/87 passing (100%), 83% coverage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Implement Phase 5 analysis tools for MLIR code inspection.

New module:
- tools/analyzer.py - Analysis tool implementations
  * count_operations - Count operations by type with recursive walk
  * extract_function - Extract specific function by symbol name
  * get_operation_operands - Get operand information with optional filtering

Testing:
- test_analyzer.py with 9 comprehensive tests
- Test coverage: analyzer.py 78%
- All analysis functions tested with real MLIR

Server integration:
- 3 new MCP tools registered (count_operations, extract_function, get_operation_operands)
- Total tools: 15

Dependencies:
- Add PyYAML back (required for MLIR linalg dialect Python bindings during build)

Test results: 96/96 passing (100%), 82% coverage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Add 27 new tests across multiple modules to improve overall test coverage
from 82% to 88%. Focus on subprocess runner, analyzer edge cases, and
generator templates.

New test file:
- tests/test_subprocess_runner.py: Complete test suite for MLIRToolRunner
  with 16 tests covering initialization, mlir-opt, mlir-translate, and
  generic tool execution with timeout and error handling (48% → 96%)

Enhanced existing tests:
- tests/test_analyzer.py: Add 7 edge case tests for invalid MLIR,
  operation filtering, nested operations, and multi-function extraction
  (78% → 92%)
- tests/test_generator.py: Add 4 tests for template coverage including
  mul_function, simple_function, and None parameter handling
- tests/test_server.py: Simplify by removing redundant tool handler tests
- tests/test_main.py: Add type annotations for consistency

Test results:
- 123/123 tests passing (100%)
- Coverage: 88% (up from 82%)
- No regressions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Implement translation between MLIR and LLVM IR using mlir-translate.
Add comprehensive test suite with round-trip translation support.
Fix CI test execution by removing CI environment check.

New translation module (src/mlir_mcp_server/tools/translator.py):
- translate_to_llvmir: Convert MLIR (LLVM dialect) to LLVM IR
- translate_from_llvmir: Import LLVM IR to MLIR (LLVM dialect)
- get_translation_info: Query available translation capabilities

Server updates (src/mlir_mcp_server/server.py):
- Register 3 new translation tools (18 total tools)
- Full integration with mlir-translate subprocess

Test coverage (tests/test_translator.py):
- 14 comprehensive tests covering:
  * Simple and complex MLIR to LLVM IR translation
  * LLVM IR import to MLIR
  * Round-trip translation verification
  * Error handling for invalid inputs
  * Translation capability queries

CI fix (tests/utils.py):
- Remove CI environment check in setup_mlir_environment()
- Now correctly loads MLIR config in CI with toolchain available
- All 137 tests will run in CI (no unnecessary skips)

Test results:
- 137/137 tests passing (100%)
- Coverage: 87% overall
- translator.py: 78% coverage (new module)

Phase 6 (Translation & Optimization) complete:
✅ translate_to_llvmir, translate_from_llvmir
⏳ optimize_for_performance (future: custom optimization passes)
⏳ reduce_testcase (future: mlir-reduce integration)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Fix code quality issues identified by Copilot code review:

1. Use importlib.util.find_spec() instead of try/except import
   - More efficient check for MLIR Python bindings availability
   - Avoids importing unused module just for checking

2. Fix unused variables in tests
   - tests/test_installation.py: Use _ for intentionally unused config
   - tests/test_main.py: Remove unused exc_info variable

3. Update README.md with current project status
   - Mark all implemented features with ✅ checkmarks
   - Update tool count from 9 to 18 tools
   - Update test count from 47 to 137 tests
   - Update coverage from 71% to 87%
   - Add transformation, analysis, and translation tools to implemented list
   - Update 'Coming soon' section to reflect completed work

All tests still pass (137/137) with 87% coverage.
Add CodeCoverageSummary action to provide:
- Detailed coverage metrics in PR comments
- Coverage badge with indicators
- Automatic updates on each push
- Thresholds: 60% (warning), 80% (good)

The summary will be automatically posted as a sticky comment on PRs,
showing coverage breakdown by file and overall statistics.

Uses:
- irongut/CodeCoverageSummary@v1.3.0 for generating coverage summary
- marocchino/sticky-pull-request-comment@v2 for posting to PRs
Add pull-requests: write permission to allow the Code Coverage Summary
action to post comments on pull requests.

Without this permission, the sticky-pull-request-comment action fails with:
'Resource not accessible by integration'

Permissions added:
- contents: read (default, for checking out code)
- pull-requests: write (for posting coverage comments)
Set @brnorris03 as the default owner for all files in the repository.
This ensures automatic review requests on pull requests.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 8, 2025

Code Coverage

Package Line Rate Health
. 89%
mlir_wrapper 88%
prompts 100%
resources 100%
tools 84%
utils 93%
Summary 87% (562 / 644)

@brnorris03 brnorris03 merged commit b9e8425 into main Dec 8, 2025
2 checks passed
@brnorris03 brnorris03 deleted the initial-implementation branch December 8, 2025 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants