LTspice电路模拟服务

一个MCP服务器,为LLM提供直接访问LTspice电路模拟的工具,包括读取和修改原理图、运行模拟、解析结果和生成图表。

  • by daviditkin
  • Python
  • 本地部署
  • 6 星标
  • 更新于
在 GitHub 查看 daviditkin/ltspice-mcp

使用说明

ltspice mcp

我想了解MCP、斜线命令和claude.md文件。DSL是过去的事了吗?我和克劳德一起创建了以下内容:

一个MCP(模型上下文协议)服务器,它为LLM提供了对LTspice电路仿真的直接、基于工具的访问——读取原理图、设置元件值、运行仿真、解析结果和生成图,所有这些都来自克劳德对话。


它做什么

  1. 阅读并修改示意图 --解析 .asc 文件、检查组件和参数、补丁值
  2. 运行模拟 --通过Wine无意识地调用LTspice(-b 批处理模式),获取日志和输出文件
  3. 分析结果 --解析 .raw / .op.raw 二进制输出:波形、节点电压、设备电流
  4. 生成图表 --自动生成的matplotlib脚本作为可复制记录保存在原理图旁边

MCP工具

工具 说明
list_schematics 发现 .asc 目录中的文件和相关输出
read_schematic 解析组件、参数、包含、指令 .asc
read_parameters 阅读 .param a的值 .inc 或 .asc 文件
write_parameters 写入/覆盖 .param a中的值 .inc 文件
run_simulation 通过葡萄酒运行LTspice(-b 批处理),返回日志+输出文件列表
read_waveforms 解析瞬态/AC .raw 输出——返回波形
read_op_point 解析直流工作点 .op.raw --返回节点电压/电流
modify_component 补丁 SYMATTR Value 直接在 .asc
write_schematic 生成新 .asc 元件/网表描述的原理图

/analyze-circuit 斜杠命令

旗舰工作流程。从Claude Code运行它 .asc 原理图:

/analyze-circuit examples/rc_filter/mystery_circuit.asc
/analyze-circuit /absolute/path/to/my_filter.asc
/analyze-circuit          ← lists available schematics and prompts

它将:

  1. 通过以下方式运行模拟 run_simulation
  2. 读取直流工作点——晶体管偏置,工作区域
  3. 发现所有波形(名称、范围、模拟类型)
  4. 检测高频信号并自动切换到全分辨率渲染
  5. 编写并执行 <stem>_analysis.py 原理图旁边的绘图脚本
  6. 在线显示图表并提供书面电路说明

示例分析输出:AM调制器(mystery_circuit.asc)

围绕单个2N3904 NPN BJT构建的集电极调制AM发射机。

; Sources
V1  30 V DC                        ; supply
V2  SINE(0 30m 600k)               ; RF carrier — 30 mV, 600 kHz
V3  SINE(0 3.5 1k)                 ; audio modulating signal — 3.5 V, 1 kHz

; Bias network
R1  56k    ; base voltage divider (top)
R2  15k    ; base voltage divider (bottom)
R3  10k    ; collector load
R4  4.7k   ; emitter resistor — V3 (audio) connects at its bottom terminal

; Signal coupling
C1  100n   ; AC-couples carrier (V2) into base
C2  100n   ; emitter bypass (shorts R4 at RF frequencies)
C3  470p   ; output coupling cap — collector → V(n002) load output

Q1  2N3904 ; common-emitter RF amplifier
R5  1k     ; output load

它是如何工作的: V3(音频,3.5V)与R4串联在发射极和地之间。随着音频信号的摆动,它将有效发射极电压移动到1 kHz,从而改变Vbe和gm。由于RF载波在基极同时被放大,其增益随音频速率而变化,从而产生AM调制。C3 AC将集电极输出耦合到负载。

直流偏压(Q1在有源区):

参数 值
Vcc 30.0伏
Vc(集电极) 18.0 V
VBE 0.66v
Vce 12.4伏
Ic 1.20毫安
β 334

/analyze-circuit 输出:

顶部面板使用LTspice风格的每像素最小/最大渲染,以2ms的分辨率产生AM“足球”形状——绿色填充带是600kHz载波,其幅度包络跟踪1kHz音频信号(红色虚线)。底部面板缩放到10–30µs以显示单个载波周期。


设置

# Install in editable mode
pip install -e .

# LTspice runs via Wine — set path if different from default:
export LTSPICE_EXE="/home/user/.wine/drive_c/Program Files/LTC/LTspiceXVII/XVIIx64.exe"

MCP配置(~/.config/claude/claude_desktop_config.json)

{
  "mcpServers": {
    "ltspice": {
      "command": "ltspice-mcp"
    }
  }
}

或者不安装软件包:

{
  "mcpServers": {
    "ltspice": {
      "command": "python",
      "args": ["-m", "ltspice_mcp.server"],
      "cwd": "/path/to/ltspice-mcp/src"
    }
  }
}

工作流程示例:RC过滤器优化

list_schematics(directory="examples/rc_filter/")
read_schematic(path="rc_filter.asc")           # inspect components
read_parameters(path="params.inc")             # current R=1k, C=100n → fc≈1.6kHz
write_parameters(path="params.inc", parameters={"R": 2200, "C": 47e-9})
run_simulation(schematic_path="rc_filter.asc")
read_waveforms(raw_path="rc_filter.raw", signals=["V(out)"])
# adjust values, repeat

项目结构

src/ltspice_mcp/
  server.py           — MCP server, all tool definitions
  raw_parser.py       — .raw file parser (binary UTF-16-LE and ASCII)
  runner.py           — Wine subprocess runner (two-step: netlist → sim)
  schematic_writer.py — .asc generator (auto-layout + wire routing)
examples/
  rc_filter/
    rc_filter.asc                   — RC low-pass filter demo
    mystery_circuit.asc             — AM modulator (this example)
    mystery_circuit_analysis.py     — auto-generated plot script
    mystery_circuit_analysis.png    — analysis output
.claude/commands/
  analyze-circuit.md  — /analyze-circuit slash command definition

需求

  • Python 3.10+
  • 葡萄酒 安装了LTspice XVII
  • matplotlib, numpy (用于生成的绘图脚本)
  • MCP兼容客户端(克劳德桌面或克劳德代码)