AI-native control plane for KLayout — thin RPC bridge + modular external workers
Project description
klink
klink is an AI-native control plane for KLayout. It turns a running KLayout GUI into a scriptable layout kernel that can be controlled from external Python processes, MCP clients, and AI agents.
The project is split into three layers:
klink: the external Python client, MCP bridge, routing/domain logic, and agent-facing workflows.klink_plugin: a thin KLayout-side RPC plugin that exposes selectedpyaand GUI operations.examples_klink/public,tests/public, anddocs/public: examples, validation, and release documentation.
The core Python package pulls in no third-party runtime libraries — its only declared dependency is klink's own Rust acceleration kernel, shipped as a prebuilt wheel. Virtual environments, caches, build outputs, and local test artifacts are intentionally not part of a clean release.
What It Does
- Controls KLayout over local TCP RPC, with batch methods sized for generated layouts (thousands of shapes/instances/PCells per call).
- Reads layout, cell, layer, shape, view, selection, and method metadata.
- Creates and edits shapes, text, cells, instances, PCells, Ports, and Anchors.
- Routes: tapered/steiner/damped/channel backends over Port/Anchor markers, plus a detailed-router → live LVS flow for custom-device circuits.
- Exposes KLayout operations as MCP tools through
klink-mcp, navigable withklink.find_tools; runs controlledpyasnippets as the escape hatch. - Remembers what you SEND: selections sent from the KLayout toolbar become durable ids an agent can resolve ("this area", "the one I just sent").
- Drives many KLayout sessions from one bridge and moves geometry between them with a dry-run-then-commit transfer.
- Records a working session — manual edits and RPC edits — into a replayable
Python script (plus a standalone
pyavariant). - Supports gdsfactory-oriented workflows, including Port markers, component
placement, routing, and klive-compatible
c.show()display. - Keeps the KLayout plugin thin while heavier logic runs in external Python.
Repository Layout
klink/ Python client, MCP bridge, and core logic
klink_plugin/ KLayout salt plugin
examples_klink/public/ Public, open-box-runnable example gallery
tests/public/ Public test suite (no KLayout required)
docs/public/ Release documentation
rust/ Rust acceleration crate (klink_boxmaze)
pyproject.toml Python packaging configuration
README.md English README
README.zh-CN.md Chinese README
CLAUDE.md Claude Code operating rules and project context
LICENSE Apache-2.0 license
THIRD_PARTY_NOTICES.md Third-party notices
Requirements
- KLayout — the layout editor klink controls; for everything live this is
the core prerequisite. Install the standard desktop build from
https://www.klayout.de/build.html. klink is developed and tested against
KLayout 0.30.x; any recent official desktop build (whose macro environment
provides the
pyaQt bindings) should work. Purely offline workflows (the public test suite and the offline demos) run without it. - Python 3.10 or newer (the bundled Rust kernel targets CPython 3.10–3.13).
- Optional: Claude Code or another MCP client.
- Optional: gdsfactory, the
klayoutPython package, NumPy/OpenCV, or detector dependencies depending on the workflow.
Install Python Package
For normal use, install the published package from PyPI:
python -m pip install klayout-klink
pip install klayout-klink installs klink and its Rust acceleration kernel
(klink-boxmaze-rs) — klink's own code, shipped as a pre-built wheel for
Linux / macOS / Windows on CPython 3.10–3.13. You get the fast path
automatically; nothing else to do.
On a platform with no pre-built kernel wheel (an unusual OS / arch / Python), pip falls back to building the kernel from source, which needs a Rust toolchain (rustup). If you don't have one — or want the lightest possible install — use
pip install klayout-klink --no-depsfor the pure-Python core only: everything still works (the kernel has a pure-Python fallback), just slower on large place-and-route jobs.
Third-party scientific libraries (klayout, gdsfactory, numpy, …) are not bundled — install the ones a feature needs yourself, into the same interpreter:
python -m pip install klayout # offline DB / LVS extraction
python -m pip install gdsfactory # silicon-photonics routing
python -m pip install numpy opencv-python-headless # nanodevice flake
python -m pip install scipy scikit-learn scikit-image # flake detectors
When a feature needs a library that is missing, klink returns an error naming
the exact pip install command — you do not have to know these in advance.
From source (development only)
Use an editable install (-e) only when you are working on klink itself — it
points the package at your working tree so source edits take effect immediately.
Normal users do not need this.
Windows PowerShell:
git clone <repo-url> klink
cd klink
py -3.11 -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -U pip
python -m pip install -e .
Linux / macOS:
git clone <repo-url> klink
cd klink
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e .
Install KLayout Plugin
Prerequisite: KLayout itself must be installed first — get the desktop build for your OS from https://www.klayout.de/build.html.
klink_plugin is a KLayout salt package. Once installed, KLayout autoruns
pymacros/klink.lym and starts:
- klink RPC on
127.0.0.1:8765. If the port is busy, it tries the next ports up to8799. - a klive-compatible server on
127.0.0.1:8082for gdsfactory-stylec.show()workflows.
Windows:
cd path\to\klink
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\KLayout\salt" | Out-Null
Copy-Item -Path ".\klink_plugin" -Destination "$env:USERPROFILE\KLayout\salt\" -Recurse -Force
Linux / macOS:
cd /path/to/klink
mkdir -p ~/.klayout/salt
cp -R klink_plugin ~/.klayout/salt/
Either way the plugin ends up at <KLayout salt dir>/klink_plugin/ (the folder
that contains grain.xml).
Restart KLayout after copying the plugin. Each KLayout window runs its own
klink session: it binds the first free port in 8765–8799 and registers as
session klayout-<port>, so with several windows open there is one listener
per window (8765, 8766, …). A successful startup prints:
[klink.server] listening on 127.0.0.1:8765
When upgrading an old plugin copy, close KLayout, remove the old
salt/klink_plugin directory, copy the new klink_plugin folder, then restart
KLayout.
Smoke Test
Start KLayout with the plugin loaded, then connect directly from Python:
from klink import KLinkClient
with KLinkClient() as c:
print(c.ping(nonce=42))
print(c.layout_info(verbosity="summary"))
If KLayout is listening on a non-default port:
from klink import KLinkClient
with KLinkClient(port=8766) as c:
print(c.ping())
For a first end-to-end result with no external geometry, run one of the public
demos (see docs/public/demos.md):
python -m examples_klink.public.demos.ebl_wraparound # fully offline
Claude Code / MCP
After installing the Python package, klink-mcp is available as a command-line
entry point.
Install the agent skills and project memory files:
cd path\to\klink
klink-mcp --setup .
This installs or updates:
.claude/skills/klayout/SKILL.md
.claude/skills/klayout-gdsfactory/SKILL.md
CLAUDE.md
Register the MCP server with Claude Code:
claude mcp add klayout -- python -m klink.mcp --profile read,write,verify,escape --session-id project-klink
Common profiles:
| Profile | Purpose |
|---|---|
read |
Read-only exploration: layout, cell, layer, shape, view, and selection queries. |
write |
Editing tools: create cells, layers, shapes, instances, PCells, and undo records. |
verify |
DRC / LVS verification tools. |
escape |
Escape-hatch tools such as exec.python. |
all |
Expose everything. |
Profiles can be combined:
python -m klink.mcp --profile read,write,verify,escape
Optional libraries must be installed into the same Python environment that
runs klink.mcp. For example, if gdsfactory tools report missing dependencies,
install gdsfactory into that same environment:
python -m pip install gdsfactory
Use the MCP klink.status tool to inspect the active interpreter, detected
capabilities, and KLayout connection state.
Tests
The public test suite is pure-Python and does not require KLayout:
python -m pytest -q tests/public
Integration tests (routing, LVS, recorder) need a live KLayout with the
klink_plugin loaded and are exercised in the development repository.
Publishing to PyPI (maintainers)
One tag publishes TWO PyPI projects: klayout-klink (pure-Python core) and
klink-boxmaze-rs (the Rust kernel, platform wheels for Linux/macOS/Windows ×
CPython 3.10–3.13). The KLayout plugin is shipped separately as a salt package
and is not on PyPI. Publishing is done by CI (.github/workflows/release.yml),
not by hand — it builds everything, gates on a 16-combo pip install smoke
test, then uploads via PyPI Trusted Publishing:
git tag v0.1.0 # version must match klink/_meta.py + rust/klink_boxmaze/pyproject.toml
git push origin v0.1.0 # triggers the Release workflow
One-time setup: on pypi.org add a pending publisher (GitHub) for each of the
two project names, pointing at this repo and release.yml.
After publishing, users install with pip install klayout-klink — no clone and
no -e. The editable (-e) install is only for developing klink itself.
Troubleshooting
Connecting from Python fails
Check that:
- KLayout is running.
klink_pluginwas copied into the KLayout salt directory.- KLayout was restarted after plugin installation.
- Port
8765is available, or use the actual session port such as8766. - Firewall or security software is not blocking localhost TCP.
MCP tools exist but return KLayout connection errors
The MCP server is running, but KLayout is not reachable. Start KLayout with the
plugin loaded, then call klink.reconnect or restart the MCP client. Use
klink.status to inspect the last connection error.
gdsfactory or detector tools report missing dependencies
Install the missing library in the same environment used by the script or MCP server (the error names the exact one):
python -m pip install gdsfactory
python -m pip install scipy scikit-learn scikit-image
PowerShell blocks .venv activation
Allow script execution for the current PowerShell process:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\.venv\Scripts\Activate.ps1
Further Reading
docs/public/getting-started.md: install, configure, and first result.docs/public/architecture.md: the three tiers and the control path.docs/public/demos.md: the demos and what each requires.docs/public/control-plane.md: the typed RPC surface, the MCP tool catalogue, batch authoring.docs/public/interactive-workflows.md: SEND selection memory, multi-session transfer, the recorder.CLAUDE.md: Claude Code operating rules and project context.klink/mcp/README.md: MCP bridge details.
Contributing
Contributions are welcome. A few project-specific rules keep klink coherent:
- Process purity.
klink/is pure mechanism and holds zero process data (no hardcoded layers, devices, DRC numbers, ports, or PDK instances). Process facts live in an example or userpdk.pyand are passed explicitly into the APIs. Do not add process constants toklink/. - One intention = one call. New agent-facing tools use one call per user
intention, errors that instruct (carry a
next_action), validate-before-mutate, and state persisted on disk. - Tests must pass. Run the public suite before sending a change:
python -m pytest -q tests/public. Routing/LVS changes count as done only on a live KLayout LVSmatch=True. - Byte-frozen router.
klink/routing/backends/flexdr/and the crate underrust/are byte-parity baselines — the Rust kernel is a speed-only port of the pure-Python reference. Do not alter them casually. - Preflight.
python -m klink.doctorchecks your interpreter, the plugin connection, and the client/plugin version handshake.
Open an issue or a pull request describing the change and how you verified it. Contributions are accepted under the project's Apache-2.0 license.
Want to contribute a PR, discuss a larger change, or co-develop a feature? Reach the maintainers at klinkdev2026@163.com (or open a GitHub issue).
Acknowledgements
klink builds on and borrows from excellent open-source work:
- KLayout — the layout editor and
pya/dbAPIs klink drives and embeds into. - OpenROAD (BSD-3-Clause)
— klink's routing engine contains faithful ports and concept adaptations of
OpenROAD's detailed router
drt(FlexDR / FlexPA / FlexGC) and global routergrt(FastRoute). - gdsfactory — the photonic
component and
route_bundlebackend behind the silicon-photonics workflows. - klive (MIT) — the display protocol
that klink's plugin reimplements on port 8082 so gdsfactory-style
Component.show()works unchanged. - KlayoutClaw (MIT) — nanodevice flake-detection priors and morphological mask helpers.
- Klayout-Router (MIT) — the EBL auto-patching idea behind the writefield patch generator.
Formal third-party copyright and license texts are in
THIRD_PARTY_NOTICES.md.
License
Apache-2.0. See LICENSE. Third-party components retain their own licenses; see
THIRD_PARTY_NOTICES.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file klayout_klink-0.1.0.tar.gz.
File metadata
- Download URL: klayout_klink-0.1.0.tar.gz
- Upload date:
- Size: 467.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc45417c098151cd0a83e6b6b8509ad55caa02fcf24a9a0002081e8e9707450b
|
|
| MD5 |
726b33c24e3387bfaa4d01ce2baf9595
|
|
| BLAKE2b-256 |
fc0f1c9001157b8b83b568cc83ecaf830546a2b6b967b26cde11364dbbf6e0ab
|
Provenance
The following attestation bundles were made for klayout_klink-0.1.0.tar.gz:
Publisher:
release.yml on klinkdev2026/klayout-klink
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
klayout_klink-0.1.0.tar.gz -
Subject digest:
dc45417c098151cd0a83e6b6b8509ad55caa02fcf24a9a0002081e8e9707450b - Sigstore transparency entry: 2077800330
- Sigstore integration time:
-
Permalink:
klinkdev2026/klayout-klink@4ae95614b065980519ddf3b647ff723eec3371b9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/klinkdev2026
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ae95614b065980519ddf3b647ff723eec3371b9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file klayout_klink-0.1.0-py3-none-any.whl.
File metadata
- Download URL: klayout_klink-0.1.0-py3-none-any.whl
- Upload date:
- Size: 547.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84ad06820fb6035e5b90e3490a5df2fec8432de8890fc9d6cee2faee163f1db3
|
|
| MD5 |
ddb2fca2a27468e4660d91e98ad46df6
|
|
| BLAKE2b-256 |
2d95ad97773ca14e470687c8a26ae93b5eb4ff6ce83e6a1fac47643051bd5587
|
Provenance
The following attestation bundles were made for klayout_klink-0.1.0-py3-none-any.whl:
Publisher:
release.yml on klinkdev2026/klayout-klink
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
klayout_klink-0.1.0-py3-none-any.whl -
Subject digest:
84ad06820fb6035e5b90e3490a5df2fec8432de8890fc9d6cee2faee163f1db3 - Sigstore transparency entry: 2077800520
- Sigstore integration time:
-
Permalink:
klinkdev2026/klayout-klink@4ae95614b065980519ddf3b647ff723eec3371b9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/klinkdev2026
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ae95614b065980519ddf3b647ff723eec3371b9 -
Trigger Event:
push
-
Statement type: