Skip to content

feat: add native Model Context Protocol (MCP) server support (--mcp) - #910

Merged
eyal0 merged 3 commits into
pcb2gcode:masterfrom
nando256:master
Aug 11, 2026
Merged

feat: add native Model Context Protocol (MCP) server support (--mcp)#910
eyal0 merged 3 commits into
pcb2gcode:masterfrom
nando256:master

Conversation

@nando256

Copy link
Copy Markdown
Contributor

Summary

This PR adds native Model Context Protocol (MCP) server support to pcb2gcode via a new --mcp command-line flag.

This enables AI coding assistants and LLM integration tools (such as Claude Desktop, Cursor, Antigravity, etc.) to directly interact with and control pcb2gcode over standard input/output (stdio).

Key Changes

  • New --mcp Flag: Added --mcp option to options.cpp and main.cpp.
  • C++ Native MCP Implementation: Implemented a JSON-RPC 2.0 stdio server in src/mcp_server.cpp and src/mcp_server.hpp.
  • Header-only JSON Parser: Included single-header nlohmann/json.hpp to avoid introducing heavy external library build dependencies.
  • Process Isolation: Tool executions (pcb2gcode_run) spawn child processes to maintain clean execution state and memory safety.
  • Helper Script & Documentation: Added setup-ubuntu.sh for streamlined building and auto-generating mcp-config.json, and updated README.md with MCP documentation.

Tested Environment & Real-World Results

  • OS & Platform: Windows 11 + WSL2 (Ubuntu 24.04 LTS)
  • MCP Client: Antigravity IDE (Windows host connecting to WSL via wsl.exe)
  • Verification Result: Successfully invoked pcb2gcode via the MCP server to generate valid G-code files from KiCad-generated Gerber files (.gbr).

How to Test

  1. Build with MCP support:
    chmod +x setup-ubuntu.sh
    ./setup-ubuntu.sh

Add native C++ stdio MCP server implementation to pcb2gcode. Allows AI coding assistants (Antigravity, Cursor, Claude Desktop) to execute pcb2gcode directly.

- Add --mcp flag to main.cpp and options.cpp

- Implement C++ JSON-RPC 2.0 stdio MCP server in src/mcp_server.cpp and src/mcp_server.hpp

- Include single-header nlohmann/json.hpp

- Add setup-ubuntu.sh script with WSL/Linux auto-detection and Ubuntu 24.04 libgerbv.pc workaround

- Update README.md and .gitignore
@eyal0

eyal0 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

I'm not sure why this is necessary. How does it help?

Coverage was decreased. Can you add tests?

Comment thread src/nlohmann/json.hpp Outdated
@nando256

nando256 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review! I've addressed all three points in the follow-up commit 060fb51.

  1. "I'm not sure why this is necessary. How does it help?"

The goal is to enable AI coding assistants to fully automate the PCB-to-G-code workflow without manual steps.

A concrete use case: tools like KiCad-MCP-Server allow an LLM (e.g. Claude) to design a PCB directly in KiCad. Once the design is done, the same AI session can call pcb2gcode --mcp to generate G-code from the Gerber files — all without the user ever opening a terminal. This closes the loop from schematic → PCB layout → G-code in a single AI-driven session.

I tested this personally on Windows 11 + WSL2 (Ubuntu 24.04) using Antigravity IDE, successfully generating G-code from KiCad-exported Gerber files via the MCP interface.

  1. "Coverage was decreased. Can you add tests?"

Added the following in commit 060fb51:

  • tests/mcp_server_tests.cpp — 3 Boost unit test cases:
    • mcp_server_initialize_test: verifies initialize returns correct serverInfo
    • mcp_server_ping_test: verifies ping returns a valid JSON-RPC 2.0 response
    • mcp_server_tools_list_test: verifies tools/list returns exactly 3 tools
  • tests/options_tests.cpp — added mcp_option test case to verify the --mcp CLI flag is accepted
  • tests/CMakeLists.txt — registered mcp_server_tests as a CMake test target
    All tests pass (*** No errors detected) on Ubuntu 24.04 with GEOS 3.12.1 and Boost 1.83.
  1. "Rather than including this file, can you add it to the cmake?"

Removed the vendored src/nlohmann/json.hpp (24,765 lines). Now handled in CMakeLists.txt:

find_package(nlohmann_json 3.11 QUIET)
if(NOT nlohmann_json_FOUND)
  FetchContent_Declare(nlohmann_json
    URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
  FetchContent_MakeAvailable(nlohmann_json)
endif()

Uses the system-installed version if available, otherwise downloads automatically. No manual installation required.

@eyal0

eyal0 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thanks for those improvements!

I am on holiday now so I can't fully read them. I'm also not an expert on MCPs so I don't really know what they do...

Why can't AI just run pcb2gcode with the correct command line and generate the command line? I don't understand why an mcp is needed. This is not to say that you did it incorrectly! This is just because I don't know what an MCP is!

Anyway, I'll be able to look at it in about 10 days.

@eyal0

eyal0 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

I'm understanding it better. It seems like a nice improvement. It also provides a nice foothold for the mcp. In the future, maybe there will be more mcp commands.

I have added another test to improve test coverage.

@coveralls

coveralls commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 60.874% (-3.4%) from 64.253% — nando256:master into pcb2gcode:master

@eyal0

eyal0 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

I'm still working on it. I'll try to keep the majority of the coverage done from cpp tests and only do what I have to from the python test.

- Introduced a new Python script `run_mcp_stdin_test.py` to perform end-to-end tests for the `pcb2gcode --mcp` command, validating the JSON-RPC communication and server lifecycle.
- Updated `CMakeLists.txt` to define the test binary path for `mcp_server_tests` and added a new integration test `mcp_stdio_shutdown_test`.
- Enhanced `mcp_server_tests.cpp` with additional test cases to cover various tool calls and error handling scenarios.
@eyal0

eyal0 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@nando256 I'm happy with the coverage now. All my changes are here. If you're good with them, I'm ready to merge this.

Any thoughts?

@nando256

Copy link
Copy Markdown
Contributor Author

Thank you for the thoughtful improvements! I've reviewed your changes and they look great to me:

  • Extracting the stream-redirect boilerplate into run_mcp_with_input() is a clean improvement — much easier to read.
  • The tools/call tests using the real binary path (PCB2GCODE_TEST_BINARY) are a great addition, covering the fork/exec path that the unit tests couldn't reach.
  • The end-to-end stdio shutdown test via Python is a nice integration layer on top.

I'm happy with all the changes. Please go ahead and merge whenever you're ready!

@eyal0
eyal0 merged commit 372b80e into pcb2gcode:master Aug 11, 2026
19 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants