feat: JuliaMSI MCP server v0.1.0 — 20 tools ready for testing
CI / test (push) Has been cancelled

Implements the Model Context Protocol server for the JuliaMSI mass
spectrometry imaging engine, covering phases 1–3 of the implementation
plan plus the main phase-4 items.

Tools (20):
  session/dataset:   msi_open_dataset, msi_close_dataset, msi_dataset_info,
                     msi_get_dataset_stats
  spectra:           msi_get_spectrum_at_coord, msi_get_spectrum_by_index
  ion images:        msi_get_mz_slice, msi_get_multiple_mz_slices,
                     msi_plan_ion_image, msi_plan_multiple_slices
  preprocessing:     msi_plan_preprocessing, msi_plan_streaming_pipeline,
                     msi_plan_analytics
  execution:         msi_execute_runspec, msi_list_plans,
                     msi_get_task_status, msi_get_task_result,
                     msi_cancel_task, msi_list_tasks
  maintenance:       msi_clean_artifacts

Resources (msi://): dataset/metadata, dataset/stats,
  tasks/{task_id}/status, tasks/{task_id}/result
Prompts: analyze_region_of_interest, standard_pipeline, diagnose_task

- Persistent session: one MSIData kept alive between tool calls
- Plan -> execute -> poll async core (SHA-256 RunSpecs + background tasks)
- Results as on-disk artifacts (CSV/PGM/MatrixMarket) with compact previews
- Structured JSON-RPC error mapping with suggested actions
- stdio and Streamable HTTP transports, optional shared-token auth
- Artifact retention (age + size budgets) and glibc malloc_trim cleanup
- 107 tests + end-to-end stdio smoke test; validated on HR2MSI bladder
  (imzML) and T9_A1/Col_1 (mzML)

Required JuliaMSI engine fixes are tracked in patches/ (already upstream
on the rel/Windows_Mac_Recompatibility branch and GitHub main).
This commit is contained in:
Pixelguy14
2026-11-25 13:25:13 -06:00
commit c3d5daeec5
35 changed files with 7927 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
diff --git a/src/ImageProcessing.jl b/src/ImageProcessing.jl
index d47ea6a..2518406 100644
--- a/src/ImageProcessing.jl
+++ b/src/ImageProcessing.jl
@@ -31,8 +31,15 @@ function load_and_prepare_mask(mask_path::String, target_dims::Tuple{Int, Int})
# Load the image
img = Images.load(mask_path)
- # Convert to grayscale if it's a color image
- gray_img = Gray.(img)
+ # Convert to grayscale. Most images are already Colorant arrays, but TIFFs
+ # with extra samples (e.g. the optical reference images shipped with MSI
+ # datasets) load as TiffImages.WidePixel, for which Gray.(img) has no method.
+ # Fall back to the embedded color channel in that case.
+ gray_img = if eltype(img) <: Colorant
+ Gray.(img)
+ else
+ Gray.([getproperty(p, :color) for p in img])
+ end
# Binarize using Otsu's method - white regions become 'true'
binary_img = binarize(gray_img, Otsu())
@@ -101,7 +108,11 @@ function process_image_pipeline(gray_img;
# --- Step 4: Smooth Edges ---
# A morphological closing with a small disk smooths outlines.
- smoothing_kernel = ones(Bool, (smoothing, smoothing))
+ # ImageMorphology requires odd-sized structuring elements; the historical
+ # default smoothing=2 produced a 2×2 matrix and threw "connectivity must be
+ # odd-sized". Normalize to the next odd size (minimum 3).
+ smooth_size = max(3, isodd(smoothing) ? smoothing : smoothing + 1)
+ smoothing_kernel = ones(Bool, (smooth_size, smooth_size))
smoothed_img = closing(holes_filled_img, smoothing_kernel)
# --- Return all intermediate steps for visualization ---
+40
View File
@@ -0,0 +1,40 @@
diff --git a/src/MSIData.jl b/src/MSIData.jl
index 1e88fa3..ed8851c 100644
--- a/src/MSIData.jl
+++ b/src/MSIData.jl
@@ -1532,9 +1532,12 @@ end
function _iterate_uncompressed_fast(f::Function, data::MSIData, source::ImzMLSource, indices_to_iterate::Union{AbstractVector{Int}, Nothing})
spectrum_indices = (indices_to_iterate === nothing) ? (1:length(data.spectra_metadata)) : indices_to_iterate
- # Intialize local buffers for this thread's sequential iteration
- mz_buf = Vector{Float64}()
- int_buf = Vector{Float64}()
+ # Initialize local buffers for this thread's sequential iteration.
+ # IMPORTANT: the element type must match the on-disk format. Using Float64
+ # buffers here made `read!` consume twice as many bytes for Float32 datasets
+ # and misinterpret them as Float64 garbage (breaking use_mmap=false).
+ mz_buf = Vector{source.mz_format}()
+ int_buf = Vector{source.intensity_format}()
for i in spectrum_indices
meta = data.spectra_metadata[i]
diff --git a/src/imzML.jl b/src/imzML.jl
index a144893..f8071e2 100644
--- a/src/imzML.jl
+++ b/src/imzML.jl
@@ -559,8 +559,13 @@ function load_imzml_lazy(file_path::String; cache_size::Int=100, use_mmap::Bool=
mz_is_compressed, int_is_compressed, global_mode)
@debug "Metadata parsing complete. Saving cache for next time..."
- # We create a temporary MSIData just for save_metadata_cache
- tmp_source = ImzMLSource(ibd_handles, default_mz_format, default_intensity_format, mmap_data, any_comp)
+ # We create a temporary MSIData just for save_metadata_cache.
+ # IMPORTANT: do NOT share the real `ibd_handles` vector here. MSIData
+ # installs a finalizer that closes every handle in `source.ibd_handles`;
+ # when this temporary object is garbage-collected it would close the
+ # handles owned by the real dataset (breaking use_mmap=false reads with
+ # "Bad file descriptor"). The cache writer only needs the formats.
+ tmp_source = ImzMLSource(IO[], default_mz_format, default_intensity_format, nothing, any_comp)
tmp_msi = MSIData(tmp_source, spectra_metadata, instrument_meta, (width, height), nothing, cache_size)
save_metadata_cache(tmp_msi, cache_path)
end