Skip to content

Commit 71862c7

Browse files
authored
Add MCP inference workflow example for OAI (#417)
1 parent fba1295 commit 71862c7

5 files changed

Lines changed: 2008 additions & 0 deletions

File tree

fuse_examples/imaging/oai_example/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,121 @@ You can track the progress of your training/testing using one of the following m
122122
If ClearML is installed and enabled in your config file (`clearml : True`), you can use it to monitor your results.
123123

124124
Choose the method that best suits your workflow and preferences.
125+
126+
## MCP Inference for Segmentation and Classification
127+
128+
The current inference workflow supports downstream segmentation, downstream classification, or both in one run. It can be used as:
129+
130+
- a persistent interactive CLI session backed by a background MCP HTTP server
131+
- a protocol-level MCP server using the official MCP Python SDK
132+
133+
In this example, the workflow is organized as a set of callable tools with clear inputs and outputs:
134+
135+
- preprocessing
136+
- segmentation
137+
- classification
138+
- QC visualization
139+
- result logging
140+
141+
The workflow uses the official MCP Python SDK and serves tool-based inference over Streamable HTTP.
142+
143+
### Code Organization
144+
145+
The inference functionality has been modularized into three main components for better maintainability:
146+
147+
- `inference_utils.py`: Contains utilities, data models, configuration handling, and helper functions
148+
- `inference_tools.py`: Implements the core inference tools (preprocessing, segmentation, classification, visualization) and the MCP inference engine
149+
- `inference_cli.py`: Main entry point, MCP server setup, and interactive CLI interface
150+
151+
### Required Packages
152+
153+
Python 3.10 or newer is required for this MCP workflow because `mcp[cli]` does not support Python 3.9.
154+
155+
From the repository root, install the project and example dependencies with:
156+
157+
```bash
158+
pip install -e .[examples]
159+
pip install "mcp[cli]"
160+
```
161+
162+
For this workflow specifically:
163+
164+
- `pip install -e .[examples]` makes the local `fuse-med-ml` package importable and installs the example runtime dependencies used here, including `torch`, `numpy`, `pandas`, `matplotlib`, `nibabel`, and `monai`
165+
- `pip install "mcp[cli]"` adds the MCP server/client SDK used by the interactive CLI and Streamable HTTP server
166+
- `pydicom` is only needed when your input is a DICOM folder instead of a `.nii` / `.nii.gz` volume, and it is already included in `.[examples]`
167+
168+
Launch it with:
169+
170+
```bash
171+
python fuse_examples/imaging/oai_example/mcp_inference/inference_cli.py
172+
```
173+
174+
By default, this starts a background MCP server and then opens the interactive terminal workflow against that same server. While the session is open, other MCP clients can connect to the same host, port, and path.
175+
176+
The default output root is `fuse_examples/imaging/oai_example/outputs/mcp_inference/`. Each run creates a `session_<timestamp>/` folder, and cases are written to neutral subfolders such as `case_0001/`, `case_0002/`, and so on. The original input-derived `case_id` is still preserved inside the CSV/JSON metadata for traceability. A typical session contains per-case NIfTI/PNG/JSON outputs together with a session-level `inference_log.csv`, as summarized below.
177+
178+
You can optionally point to a different config or device:
179+
180+
```bash
181+
python fuse_examples/imaging/oai_example/mcp_inference/inference_cli.py \
182+
--inference-config fuse_examples/imaging/oai_example/mcp_inference/inference_config.yaml \
183+
--device auto
184+
```
185+
186+
Under the hood, the workflow exposes these MCP tools over Streamable HTTP:
187+
188+
- `get_inference_settings`
189+
- `process_case`
190+
- `process_batch`
191+
192+
Typical MCP inputs:
193+
194+
- `path` for `process_case`
195+
- `batch_path` for `process_batch`
196+
- `task`: one of `segmentation`, `classification`, or `all`
197+
- `qc_visualization`: optional boolean
198+
- `output_dir`: optional output root
199+
- `log_to_csv`: optional boolean
200+
201+
At startup the session shows the current defaults and offers:
202+
203+
1. Process input
204+
2. Change settings
205+
3. Reset to defaults
206+
4. View current settings
207+
5. Exit
208+
209+
The interactive terminal view looks like this:
210+
211+
```text
212+
Background MCP server ready at http://127.0.0.1:8000/mcp
213+
214+
Interactive inference workflow
215+
Defaults: mode=single, task=all, input=nifti, qc=on, logging=on
216+
1. Process input
217+
2. Change settings
218+
3. Reset to defaults
219+
4. View current settings
220+
5. Exit
221+
Choose an option [1]:
222+
```
223+
224+
Input handling:
225+
226+
- single mode accepts one `.nii` / `.nii.gz` volume path, and also supports a DICOM folder if needed
227+
- batch mode accepts either a folder of cases or a `.csv`, `.tsv`, `.txt`, or `.jsonl` manifest with a `path`, `input_path`, or `img_path` column
228+
- batch processing continues if one case fails and records the failure in the run log
229+
230+
Supported tasks:
231+
232+
- segmentation only
233+
- classification only
234+
- all tasks in sequence
235+
236+
Outputs:
237+
238+
- `segmentation_mask.nii.gz` when segmentation is enabled
239+
- `segmentation_qc.png` when QC is enabled
240+
- `classification.json` when classification is enabled
241+
- `inference_metadata.json` with input and preprocessing metadata
242+
- `inference_log.csv` with per-case status and output paths

0 commit comments

Comments
 (0)