pt4046-mcp
by SMEAC
README.md
# pt4046-mcp
An MCP server that lets AI assistants drive a **PeakTech 4046** two-channel 160 MHz DDS arbitrary
function generator over **LAN**. It is a sibling of
[mcp_msox2014a](https://github.com/SMEAC/mcp_msox2014a) (oscilloscope) and
[mcp_ur](https://github.com/SMEAC/mcp_ur) (robot) and uses the same architecture: transport →
driver → session → FastMCP tools, with a built-in simulator.
On the bench it replaces the MSO-X 2014A's built-in WaveGen, which needs a licence (DSOX2WAVEGEN)
that the scope does not have.
## Features
| Area | What you get |
|---|---|
| Connection | Raw SCPI socket or VXI-11 over LAN (USB VISA also works); static IP from `.mcp.json`; port scan and auto-detection; simulator (`SIM`) |
| Channels | Sine, square, ramp, pulse, noise and 145 built-in waveforms by number or name; frequency, amplitude + offset or high + low levels, phase, duty, symmetry, pulse width and edges, load, polarity, output on/off |
| Modulation | AM, FM, PM, PWM, SUM, FSK, QFSK, 4FSK, PSK, QPSK, 4PSK, ASK, OSK; internal or external source |
| Sweep / burst | Linear, log and list (up to 128 points) sweeps; triggered and gated bursts; `*TRG` |
| Dual channel | Frequency coupling (ratio/offset), amplitude/offset coupling, waveform combine, phase sync |
| Arbitrary | Upload samples in volts (16k points per channel, 1M on channel A) in the binary frame format from the Programmer's Guide |
| Safety | Every setting is checked against the instrument limits for waveform, frequency and load before anything is sent; optional bench limit `PT4046_MAX_VOLTAGE` on the unloaded output voltage; outputs only change when asked |
| Escape hatch | `fgen_scpi` for any SCPI command |
The command set follows the *PeakTech P 4046 Programmer's Guide* (05/2023, from the product
page downloads). It is Agilent 33xxx-style SCPI: `SOURce1:`/`OUTPut1` address channel A and
`SOURce2:`/`OUTPut2` channel B.
## Install
Requires Python 3.11+ and [uv](https://docs.astral.sh/uv/).
```powershell
git clone https://github.com/SMEAC/mcp_pt4046.git
cd mcp_pt4046
uv sync
uv run pt4046-mcp --simulate # quick smoke run (stdio; Ctrl+C to exit)
uv run pytest
```
No VISA installation is needed for the raw-socket LAN connection. VXI-11 uses pyvisa-py
(included), or Keysight IO Libraries / NI-VISA if installed.
## Generator LAN setup
1. On the generator: **Utility → Interface → Network**. Set DHCP off, then the IP address,
subnet mask and default gateway, and press **Confirm**.
2. Put the IP in `.mcp.json` as `PT4046_HOST`. The PC needs an address in the same subnet.
3. The PeakTech manuals do not say which TCP port the LAN interface listens on. Find it once:
```
fgen_device(operation='scan', host='192.168.1.30')
```
This tries the common raw-SCPI ports (5025 first) with `SYST:ERR?` and checks for a VXI-11
portmapper. Put the port it reports in `.mcp.json` as `PT4046_PORT`. Without `PT4046_PORT`,
every connect repeats the probe; that works but is slower.
## Configuration (`.mcp.json`)
```json
{
"mcpServers": {
"pt4046": {
"command": "uv",
"args": ["run", "--directory", "C:\\dev\\mcp_stuff\\mcp_pt4046", "pt4046-mcp"],
"env": {
"PT4046_HOST": "192.168.1.30",
"PT4046_PORT": "5025"
}
}
}
}
```
| Variable | Meaning |
|---|---|
| `PT4046_HOST` | Generator IP, `IP:port`, a VISA resource (`TCPIP0::192.168.1.30::inst0::INSTR`, `USB0::…::INSTR`) or `SIM`. Tools auto-connect to it. |
| `PT4046_PORT` | Raw-socket port. If unset, the port is probed on connect. |
| `PT4046_MAX_VOLTAGE` | Optional bench limit, in volts, on the peak voltage at an *unloaded* output (\|offset\| + amplitude/2, doubled if the load setting is 50 Ω). Settings above it are refused. |
| `PT4046_TIMEOUT_S` | I/O timeout (default 5 s). |
| `PT4046_VISA_LIBRARY` | Force a VISA backend (`@py`, `@ivi` or a DLL path). |
| `PT4046_LOG_LEVEL` | Logging level on stderr (default WARNING). |
## Tools
| Tool | Operations |
|---|---|
| `fgen_device` | `scan`, `connect`, `disconnect`, `status`, `identify`, `errors`, `clear`, `reset`, `all_off`, `local`, `save_state`, `recall_state` |
| `fgen_channel` | `get`, `set`, `on`, `off`, `phase_sync`, `sync_output`, `trigger_output` |
| `fgen_modulation` | `get`, `set`, `off` |
| `fgen_sweep` | `get`, `set`, `off`, `trigger` |
| `fgen_burst` | `get`, `set`, `off`, `trigger` |
| `fgen_dual` | `get`, `set` |
| `fgen_arb` | `list_builtin`, `select_builtin`, `select_user`, `upload` |
| `fgen_scpi` | raw write/query |
| `fgen_help` | `quickstart`, `tool_help`, `hardware_guide`, `faq`, `discover` |
Resource: `pt4046://status`. Prompt: `frequency_response`, a gain/phase sweep with the
generator driving the circuit and the MSO-X 2014A measuring.
Every result that follows a write includes `instrument_errors` when the generator queued any.
`SYSTem:ERRor?` is also the synchronisation point, because the command set has no `*OPC?`.
```python
fgen_channel(operation='set', channel=1, function='sine', frequency=1e3, amplitude=1, offset=0)
fgen_channel(operation='set', channel=2, function='square', frequency=10e3, low=0, high=3.3)
fgen_channel(operation='on', channel=1)
fgen_device(operation='all_off')
```
## Things to know
- **Load setting.** The generator has a 50 Ω source impedance and scales its output so that the
*configured* load sees the set amplitude. After `*RST` the setting is High Z. If you set 50 Ω
but connect a high-impedance circuit, the circuit sees **twice** the set voltage. The limit
checks always use the unloaded (worst-case) voltage.
- **Megahertz.** In PeakTech SCPI, `MHz` means *milli*hertz and `MAHz` means megahertz. The tools
send plain numbers in Hz and never use unit suffixes. Remember this when you use `fgen_scpi`.
- **`*IDN?`** is not in the PeakTech command list. `identify` reports it if the instrument does not
answer, and connecting does not depend on it.
- **Mode exclusivity.** Only one of modulation, sweep or burst can be active per channel.
- **Coupling order.** Coupling parameters are only accepted while that coupling is on, and the
trigger source only while sweep or burst is on. The tools send the commands in that order.
## Not yet verified on hardware
The simulator and the localhost socket tests follow the Programmer's Guide. The following still
need checking on the real instrument:
- the LAN port number (`fgen_device(operation='scan')`)
- the exact `*IDN?`, `SYST:ERR?`, `FUNC?` and `OUTP:LOAD?` response formats (the parsers accept
the likely variants)
- arbitrary upload (`ARB:SHRT<n>` / `ARB:DATA`): the guide's frame format is implemented
exactly, but which waveform slot the data lands in is not documented. Check the result on the
scope.
## Licence
MIT