AI & AUTOMATION · 2026-06-18
I Built a Self-Improving SolidWorks MCP Server: AI That Writes, Tests & Fixes Its Own CAD Macros
I built a SolidWorks MCP server that lets an AI agent write a VBA macro, run it inside live SolidWorks 2022, read the rebuild errors, fix the code, and re-run — until the model builds clean. The twist: every fixed error becomes a permanent rule, so the same mistake is never made twice. Here's how the self-improving loop works.

A SolidWorks MCP server is a small program that lets an AI assistant control SolidWorks directly — writing a VBA macro, running it inside a live session, reading what broke, and fixing it automatically. I built one, and the part I'm most proud of is that it gets smarter every time it fails: each error it fixes becomes a permanent rule, so the same mistake is never solved twice. This post explains how that self-improving loop actually works.
The problem: AI writes plausible CAD code that doesn't run
Ask any large language model to write a SolidWorks VBA macro and you'll get something that looks right and often isn't. The SolidWorks API is full of subtle preconditions — selection marks, VARIANT_BOOL quirks, objects that are really Variant arrays — that a model can't see from text alone. The code compiles in its head, then fails the moment it touches a real part. Blind generation means you, the engineer, become the debugger.
The fix: a run-and-verify loop instead of blind generation
The MCP server turns that one-shot guess into a closed feedback loop. It connects to a running SolidWorks 2022 over COM, executes the macro on a dedicated worker thread, rebuilds the model, scans the feature tree, and returns a single structured verdict the AI can act on — did it run, were there rebuild errors, which features errored, did the VBA itself throw?
| Blind AI code generation | MCP run-and-verify loop | |
|---|---|---|
| Feedback | None — you find out it's broken | Structured verdict after every run |
| Broken macros | Ship silently, fail on your machine | Caught, diagnosed, and re-run automatically |
| API accuracy | Hallucinated method signatures | Grounded in live API docs lookups |
| Learning | Repeats the same mistakes forever | Every fix saved as a permanent rule |
| Your role | Debug the AI's output by hand | Review the finished, clean model |
How the auto-fix loop works, step by step
- The AI writes a VBA macro and calls run_and_verify.
- The server writes it to a temporary macro file and runs it inside live SolidWorks on a COM worker thread.
- It rebuilds the model and scans for run errors, rebuild errors and errored features.
- It returns one verdict — success or a rich list of exactly what went wrong.
- If it failed, the AI reads the errors, looks up the exact API call in the SolidWorks docs, and rewrites the macro.
- The loop repeats until the model builds clean — no human in the middle.
The part that makes it self-improving
A loop that fixes errors is useful. A loop that remembers how it fixed them is something more. Every time an error is resolved, the server records it as a rule in a persistent knowledge base. The next time a macro fails, the verdict comes back with suggested fixes matched from those past rules — so a mistake solved once becomes a mistake never repeated. Over time the server stops failing on the things it has already learned, and starts each new project further ahead than the last.
A loop that fixes its own errors is automation. A loop that remembers every fix is the start of something that compounds.
Two of those hard-won rules are baked in from the start: never test an API boolean with a bitwise 'Not' (on-the-fly macros return +1, and VBA's Not is bitwise), and treat the rectangle-sketch return as a Variant array, not an object. Those exact bugs cost me hours the first time — now they cost the server nothing, because the lesson is permanent.
What it can actually build
The server ships with verified generators for real geometry — extrudes, fillets, chamfers, shells, ribs, revolves, sweeps, lofts, hole-wizard counterbores, real cut threads, helical springs, linear/mirror/circular patterns, fully-mated assemblies, surface modeling and sheet metal. Anything not yet wrapped as a one-call tool is still buildable with inline VBA running through the same self-correcting loop. It pairs with a verified-style macro-writing approach so the generated code starts correct and the loop keeps it that way.
Why this matters beyond SolidWorks
This is what real agentic engineering looks like: not an AI that spits out code and hopes, but a closed loop with verification and memory, where the tool tells the truth about whether the result actually works. It's the same philosophy behind all my intelligent automation work — and it's exactly how I now approach SolidWorks design automation for clients. If you're curious about the bigger picture of AI agents driving engineering tools, I wrote about MCP for engineers here.
Try it yourself
The full SolidWorks MCP server is open source. You can clone it, register it with Claude Code, and run the self-improving loop against your own SolidWorks 2022 install: github.com/ladla90077-web/solidworks-mcp. If you'd rather have this kind of automation built for your workflow, let's talk.
Frequently Asked Questions
What is a SolidWorks MCP server?
It's a server built on the Model Context Protocol that lets an AI assistant control SolidWorks directly — writing and running VBA macros, rebuilding the model, reading errors, and fixing the code automatically until the model builds correctly.
Can AI actually write working SolidWorks VBA macros?
Not reliably in one shot — the SolidWorks API has too many subtle preconditions. But inside a run-and-verify loop, where the AI runs the macro, reads the real errors, and corrects itself, it converges on working code without a human debugging each attempt.
What makes the loop 'self-improving'?
Every error the server fixes is saved as a permanent rule in a knowledge base. On future failures it returns suggested fixes matched from those past rules, so the same mistake is never solved twice and the system gets more reliable over time.
Do I need to enable VBA trust access or change SolidWorks settings?
No. Inline VBA is written to a temporary macro file and compiled on the fly, so no VBA trust-access setting or pre-authored macro file is required — you just need SolidWorks 2022 and a default template configured.
Does it only work with SolidWorks 2022?
It's built and tested against SolidWorks 2022 over its COM interface, and the bundled API-docs pipeline targets the 2022 documentation. The same approach can be adapted to other versions that expose the same COM API.