Generic SCPI Instrument

v0.1.3
io.github.K-Dense-AI/labmcp-scpi Checked 1 day ago Published 2 months ago

Generic MCP server for any SCPI instrument over VISA, TCP or serial, with command allow/deny lists.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

get_connection_info Get Connection Info

Report which instrument is connected (identity, address, simulated or real), whether the server is read-only, and the active safety limits. Call this first.

get_command_log Get Command Log

Return the most recent raw commands sent to / replies received from the instrument (newest last). Useful for debugging and for recording what was done.

ParameterTypeDescription
limitinteger

reconnect Reconnect

Close and re-open the connection to the instrument (e.g. after it was power cycled or a cable was re-plugged).

scpi_primer Scpi Primer

Concise SCPI syntax guide (long/short forms, queries, compound commands, common commands, error queue, binary blocks) plus this server's active command policy. Works without a connection.

identify Identify

Ask the instrument who it is (*IDN?): manufacturer, model, serial number, firmware.

list_visa_resources List Visa Resources

List GPIB / USBTMC / LAN (VXI-11, HiSLIP) instruments visible to VISA on this computer. Works without --address. Raw-socket instruments (tcp://host:5025) are not discoverable.

ParameterTypeDescription
backendstring@py = pyvisa-py, @ivi = NI-VISA / Keysight / R&S VISA
querystringVISA resource filter

scpi_query Scpi Query

Send ONE read-only SCPI query and return the instrument's text reply. Only single queries are accepted: the header must end with '?' (parameters such as 'VOLT? MAX' are allowed), with no ';' and no line breaks. Queries with known side effects (*TST?, *CAL?, CALibration, DIAGnostic) and anything in the lab's query denylist are refused - use `scpi_write` for those. An unknown query gets no reply (timeout); the error queue is then reported.

ParameterTypeDescription
command *stringOne SCPI query, e.g. '*IDN?', 'MEAS:VOLT:DC?', 'VOLT? MAX'
max_charsintegerTruncate longer replies
timeout_sReply timeout (default: server's)

query_binary_block Query Binary Block

Read an IEEE 488.2 definite-length binary block (waveforms, trace data, screenshots, FORMat REAL/INTeger readings). The query must pass the same read-only checks as `scpi_query`. Returns length, SHA-256 and (optionally) decoded values with summary statistics, downsampled to max_points; use save_path for the full data. Blocks larger than the `max_block_bytes` limit are discarded.

ParameterTypeDescription
command *stringOne SCPI query answering with #<n><len><data>
byte_orderstringbig = SCPI FORMat:BORDer NORMal, little = SWAPped
decode_asstringInterpret the payload as an array of this type (see the instrument's FORMat)
max_inline_bytesintegerReturn the raw payload as base64 only up to this size
max_pointsintegerMax decoded values returned inline
overwritebooleanAllow replacing an existing save_path
save_pathWrite the full data here: .csv with decode_as writes index,value rows; .bin/.dat/.raw/.txt/.png/.bmp/.jpg/.gif/.tif (or .csv with decode_as none) get the raw payload
timeout_snumberTimeout for the whole transfer

get_errors Get Errors

Read and clear the instrument's error/event queue (SYSTem:ERRor? until 0,"No error"). Entries are returned oldest first; each can only be read once. An empty list means no errors.

ParameterTypeDescription
max_errorsintegerStop after this many entries

wait_operation_complete Wait Operation Complete

Wait until the instrument has finished all pending operations (*OPC? returns 1), e.g. after starting a sweep, an acquisition or a settling source.

ParameterTypeDescription
timeout_snumberLongest time to wait

scpi_write Scpi Write

Send any SCPI program message (settings, compound 'A;B' messages, queries with side effects) and then read the error queue. If the message contains a query, its reply is returned. This can change the instrument's state, including switching outputs on; say what the command does before calling it. Commands in the lab's denylist, or outside its allowlist, are refused before anything is sent.

ParameterTypeDescription
command *stringSCPI program message, e.g. 'VOLT 5;CURR 0.1' or 'OUTP ON'
timeout_sReply timeout if it contains a query

scpi_batch Scpi Batch

Run a short sequence of SCPI commands and queries in order, checking the error queue after every step. Every step is checked against the command policy BEFORE the first one is sent, so a refused step means nothing was sent. Returns per-step replies and errors. Steps not started within ~14 minutes are not sent (status not_run).

ParameterTypeDescription
steps *arrayCommands/queries to send in order
delay_between_snumberPause between steps
stop_on_errorbooleanStop at the first step that reports an error
timeout_sReply timeout per query

reset_instrument Reset Instrument

Reset the instrument to its default settings (*RST, then *CLS), wait for completion and check errors. SCPI requires outputs OFF after *RST, but every other setting (levels, ranges, triggers, limits) returns to its default and some instruments differ: check the manual before resetting anything connected to a device under test.

ParameterTypeDescription
clear_statusbooleanAlso send *CLS (clear status and error queue)

device_clear Device Clear

Recover a stuck or confused instrument: VISA device clear (or discard unread input on socket/serial links), report the error queue, optionally ABORt, then *CLS. This does NOT make an arbitrary instrument safe (outputs stay as they are): use `apply_safe_state` if the lab configured one, or the instrument's own controls.

ParameterTypeDescription
send_abortbooleanAlso send ABORt (stop sweeps / measurements)