feat: add native Model Context Protocol (MCP) server support (--mcp) - #910
Conversation
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
|
I'm not sure why this is necessary. How does it help? Coverage was decreased. Can you add tests? |
|
Thank you for the review! I've addressed all three points in the follow-up commit 060fb51.
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.
Added the following in commit 060fb51:
Removed the vendored src/nlohmann/json.hpp (24,765 lines). Now handled in CMakeLists.txt: Uses the system-installed version if available, otherwise downloads automatically. No manual installation required. |
|
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. |
|
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. |
|
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.
|
Thank you for the thoughtful improvements! I've reviewed your changes and they look great to me:
I'm happy with all the changes. Please go ahead and merge whenever you're ready! |
Summary
This PR adds native Model Context Protocol (MCP) server support to
pcb2gcodevia a new--mcpcommand-line flag.This enables AI coding assistants and LLM integration tools (such as Claude Desktop, Cursor, Antigravity, etc.) to directly interact with and control
pcb2gcodeover standard input/output (stdio).Key Changes
--mcpFlag: Added--mcpoption tooptions.cppandmain.cpp.src/mcp_server.cppandsrc/mcp_server.hpp.nlohmann/json.hppto avoid introducing heavy external library build dependencies.pcb2gcode_run) spawn child processes to maintain clean execution state and memory safety.setup-ubuntu.shfor streamlined building and auto-generatingmcp-config.json, and updatedREADME.mdwith MCP documentation.Tested Environment & Real-World Results
wsl.exe)pcb2gcodevia the MCP server to generate valid G-code files from KiCad-generated Gerber files (.gbr).How to Test