Skip to main content

MCP server suite for physics laboratory instrumentation control

Project description

InstrMCP: Instrumentation Control MCP Server

PyPI version instrmcp Python 3.10+ License: MIT MCP Documentation Status Tests Lint

MCP server suite for quantum device physics laboratory's instrumentation control, enabling Large Language Models to interact directly with physics instruments and measurement systems through QCodes and JupyterLab.

https://github.com/user-attachments/assets/e7d0a441-36b2-4fec-9c54-1427310b7698

Features

  • Full QCodes Integration: Built-in support for all QCodes instrument drivers
  • Database Integration: Read-only access to QCodes databases with intelligent code generation
  • MeasureIt Templates: Comprehensive measurement pattern library and code generation
  • JupyterLab Native: Seamless integration with JupyterLab
  • Dynamic Tool Creation: Create custom MCP tools at runtime using LLM-powered tool registration
  • Safe mode: Read-only mode with optional unsafe execution
  • CLI: Easy server management with instrmcp command
  • MCP: Standard Model Context Protocol for LLM integration
  • The MCP has been tested to work with Claude Desktop, Claude Code, and Codex CLI

Quick Start

Installation

From PyPI (Recommended):

pip install instrmcp

That's it! QCodes, JupyterLab, and all dependencies are automatically installed. The JupyterLab extension is automatically enabled (no Node.js or rebuild required).

From Source (For Development):

git clone https://github.com/caidish/instrMCP.git
cd instrMCP
pip install -e .

# Run setup to enable JupyterLab extension (only needed for editable install)
instrmcp-setup

# Set required environment variable for development
# For macOS/Linux:
export instrMCP_PATH="$(pwd)"
echo 'export instrMCP_PATH="'$(pwd)'"' >> ~/.zshrc  # or ~/.bashrc
source ~/.zshrc

# For Windows (PowerShell):
$env:instrMCP_PATH = (Get-Location).Path
[System.Environment]::SetEnvironmentVariable('instrMCP_PATH', (Get-Location).Path, 'User')

Extension: MeasureIt

MeasureIt provides comprehensive measurement pattern templates for common physics experiments.

Installation:

pip install qmeasure

Important Notes:

  • Import as measureit (not qmeasure): import measureit
  • Python 3.8+ required
  • For source installation or advanced configuration, see the MeasureIt GitHub repository

Enable in InstrMCP:

# In Jupyter notebook
%mcp_option add measureit
%mcp_restart

Usage

Loading InstrMCP in Jupyter

# Start JupyterLab
jupyter lab

In a Jupyter notebook cell:

# Load InstrMCP extension
%load_ext instrmcp.extensions

# Start MCP server
%mcp_start

# Check status
%mcp_status

# Enable unsafe mode (code execution)
%mcp_unsafe

# Enable optional features (restart required)
%mcp_option add measureit database
%mcp_restart

CLI Server Management

# Start standalone servers
instrmcp jupyter --port 3000                # Jupyter MCP server
instrmcp jupyter --port 3000 --unsafe       # With unsafe mode
instrmcp qcodes --port 3001                 # QCodes station server

# Configuration and info
instrmcp config    # Show configuration paths
instrmcp version   # Show version
instrmcp --help    # Show all commands

Documentation

Configuration Example

Station configuration uses standard YAML format:

# instrmcp/config/data/default_station.yaml
instruments:
  mock_dac:
    driver: qcodes.instrument_drivers.mock.MockDAC
    name: mock_dac_1
    enable: true

Configuration is automatic! The system auto-detects installation paths. For custom setups:

# View current configuration
instrmcp config

# Custom config file (optional)
mkdir -p ~/.instrmcp
echo "custom_setting: value" > ~/.instrmcp/config.yaml

Claude Desktop Integration

InstrMCP provides seamless integration with Claude Desktop for AI-assisted laboratory instrumentation control.

Quick Setup (2 Steps)

  1. Run Automated Setup:
cd /path/to/your/instrMCP
./claudedesktopsetting/setup_claude.sh
  1. Restart Claude Desktop completely and test with: "What MCP tools are available?"

Manual Setup Alternative:

# 1. Copy and edit configuration
cp claudedesktopsetting/claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json

# 2. Edit the copied file - replace placeholders with actual paths:
#    /path/to/your/python3 → $(which python3)
#    /path/to/your/instrMCP → $(pwd)

See claudedesktopsetting/README.md for detailed setup instructions and troubleshooting.

Claude Code Integration

Claude Code supports local MCP servers via STDIO. Use the provided launcher to connect:

# Add instrMCP as MCP Server
claude mcp add instrMCP --env instrMCP_PATH=$instrMCP_PATH \
  --env PYTHONPATH=$instrMCP_PATH \
  -- $instrMCP_PATH/venv/bin/python \
  $instrMCP_PATH/claudedesktopsetting/claude_launcher.py

# Verify connection
/mcp

Prerequisites:

  • Ensure instrMCP_PATH environment variable is set
  • Have a Jupyter server running with the instrMCP extension loaded
  • MCP server started in Jupyter with %mcp_start

Codex CLI Integration

Codex expects MCP servers over STDIO. Use the Codex launcher to proxy STDIO calls to your HTTP MCP server.

Configuration:

  • command: python
  • args: ["/path/to/your/instrMCP/codexsetting/codex_launcher.py"]
  • env:
    • JUPYTER_MCP_HOST=127.0.0.1
    • JUPYTER_MCP_PORT=8123

V2.0.0 Features (Current Release)

1. Resource Discovery Tool

The mcp_list_resources() tool helps LLMs discover and effectively use MCP resources:

Features:

  • Comprehensive Resource Listing: All available MCP resources with URIs, descriptions, and use cases
  • Context-Aware: Only shows resources based on enabled options (core, MeasureIt, database)
  • Resources vs Tools Guidance: Educates LLMs on when to use read-only resources vs active tools
  • Common Patterns: Examples like "Check available_instruments → Use qcodes_instrument_info"
  • First-Use Recommendation: Use this tool FIRST to discover what context is available

Example Response:

{
  "total_resources": 8,
  "resources": [
    {
      "uri": "resource://available_instruments",
      "name": "Available Instruments",
      "use_when": "Need to know what instruments exist BEFORE calling qcodes_instrument_info",
      "example": "Check this first to see instrument names..."
    }
  ],
  "guidance": {
    "resources_vs_tools": {
      "resources": "Provide READ-ONLY reference data, templates, and documentation",
      "tools": "Perform ACTIVE operations like reading live values, executing code"
    },
    "when_to_use_resources": [
      "Before using tools - check available_instruments first",
      "For code templates - get MeasureIt examples",
      "For configuration - check database_config"
    ]
  }
}

2. Consent System for Cell Modifications

Cell modification tools now require user consent in unsafe mode:

Tools requiring consent:

  • notebook_update_editing_cell - Shows old/new content comparison before replacing entire cell
  • notebook_apply_patch - Shows visual diff dialog with exact changes

Features:

  • Visual Diff Display: Red deletions, green additions, context lines
  • Pattern Warning: Prominent alert if old_text not found in cell
  • Change Statistics: Shows chars removed/added and delta
  • Consent Workflow: "Decline" | "Allow" | "Always Allow" buttons
  • Battle-Tested Diffing: Uses industry-standard diff library (v8.0.2) from GitHub/GitLab

Example: When LLM calls notebook_apply_patch(old_text="x = 10", new_text="x = 20"), user sees:

- x = 10  ← Red background with strikethrough
+ x = 20  ← Green background

3. Line Range Parameters for Context Management

Control LLM context window consumption with line range selection:

Features:

  • line_start / line_end parameters (default: lines 1-100)
  • Automatic Bounds Clamping: Invalid ranges safely handled
  • Truncation Metadata: Returns total_lines, truncated flag
  • Context Window Savings: Prevents large cells from consuming excessive tokens

Example:

# Get only first 50 lines of a large cell
get_editing_cell(line_start=1, line_end=50)

# Get lines 100-200 for focused analysis
get_editing_cell(line_start=100, line_end=200)

4. Dynamic Tool Creation

Create custom MCP tools at runtime using LLM-powered tool registration:

# In Jupyter with instrMCP loaded in unsafe mode
# LLM can create tools dynamically using meta-tools:
dynamic_register_tool(
    name="analyze_data",
    source_code="def analyze_data(x): return x * 2",
    capabilities=["cap:numpy", "cap:custom.analysis"],  # Freeform labels
    parameters=[{"name": "x", "type": "number", "description": "Input", "required": true}]
)

Features:

  • 6 Meta-Tools: register, update, revoke, list, inspect, registry_stats
  • Consent UI: User approval required for tool registration/updates (shows full source code)
  • Freeform Capability Labels: Tag tools with descriptive capabilities for discovery
  • Persistent Registry: Tools saved to ~/.instrmcp/registry/ and reloaded on server start
  • Audit Trail: All tool operations logged to ~/.instrmcp/audit/tool_audit.log
  • Auto JSON Correction: Optional LLM-powered JSON error fixing (opt-in via %mcp_option auto_correct_json)

Capability Labels (v2.0.0): Capabilities are freeform documentation labels - NOT enforced security boundaries. Use any descriptive string:

  • Suggested format: cap:library.action (e.g., cap:numpy.array, cap:qcodes.read)
  • Used for discovery, filtering, and transparency in consent UI
  • No validation - flexibility for LLMs to describe tool dependencies
  • Future: Enforcement layer planned for v3.0.0

See Dynamic Tools Quickstart for details.

Testing & Quality

  • 464 tests (463 passed, 1 skipped)
  • Zero linter errors across all modules
  • Code formatted with black
  • CI/CD passing on all workflows

V3.0.0 Roadmap

  • Capability Enforcement: Security boundaries based on capability taxonomy
  • Support RedPitaya
  • Support Raspberry Pi for outdated instruments
  • Integrating lab wiki knowledge base for safety rails
  • More LLM integration examples

License

MIT License - see LICENSE file.

Contributing

We welcome contributions! See our Development Guide for details on:

  • Setting up development environment
  • Running tests
  • Code quality standards
  • Contribution guidelines

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

instrmcp-2.2.0.tar.gz (3.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

instrmcp-2.2.0-py3-none-any.whl (4.3 MB view details)

Uploaded Python 3

File details

Details for the file instrmcp-2.2.0.tar.gz.

File metadata

  • Download URL: instrmcp-2.2.0.tar.gz
  • Upload date:
  • Size: 3.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for instrmcp-2.2.0.tar.gz
Algorithm Hash digest
SHA256 b51ea20ce922e1be35f9f246b64c0dad2a3604f555b5e9ba12f10af37348b1ff
MD5 1de84b8f5c1d3fcab76545e17fb65bf3
BLAKE2b-256 bc4b2f65703f0665c72a8a522baad8722ffc3675e57a24046157ce06449e0342

See more details on using hashes here.

File details

Details for the file instrmcp-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: instrmcp-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for instrmcp-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59de147042370721447cae21f5d298b4869d1b166e01afa995717d544c887467
MD5 79c4ac40ddebad2aa40857b3c5ab6368
BLAKE2b-256 cc1411d422907da9909f6453c856622ed7af26947ed8b129e55f1e8bc280fb1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page