Skip to content

Commit 81aba8c

Browse files
committed
Add MCP inference workflow example for OAI
1 parent fba1295 commit 81aba8c

6 files changed

Lines changed: 1998 additions & 0 deletions

File tree

fuse_examples/imaging/oai_example/README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,111 @@ 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+
### Required Packages
144+
145+
From the repository root, install the project and example dependencies with:
146+
147+
```bash
148+
pip install -e .[examples]
149+
pip install "mcp[cli]"
150+
```
151+
152+
For this workflow specifically:
153+
154+
- `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`
155+
- `pip install "mcp[cli]"` adds the MCP server/client SDK used by the interactive CLI and Streamable HTTP server
156+
- `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]`
157+
158+
Launch it with:
159+
160+
```bash
161+
python fuse_examples/imaging/oai_example/mcp_inference/inference_cli.py
162+
```
163+
164+
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.
165+
166+
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.
167+
168+
You can optionally point to a different config or device:
169+
170+
```bash
171+
python fuse_examples/imaging/oai_example/mcp_inference/inference_cli.py \
172+
--inference-config fuse_examples/imaging/oai_example/mcp_inference/inference_config.yaml \
173+
--device auto
174+
```
175+
176+
Under the hood, the workflow exposes these MCP tools over Streamable HTTP:
177+
178+
- `get_inference_settings`
179+
- `process_case`
180+
- `process_batch`
181+
182+
Typical MCP inputs:
183+
184+
- `path` for `process_case`
185+
- `batch_path` for `process_batch`
186+
- `task`: one of `segmentation`, `classification`, or `all`
187+
- `qc_visualization`: optional boolean
188+
- `output_dir`: optional output root
189+
- `log_to_csv`: optional boolean
190+
191+
At startup the session shows the current defaults and offers:
192+
193+
1. Process input
194+
2. Change settings
195+
3. Reset to defaults
196+
4. View current settings
197+
5. Exit
198+
199+
The interactive terminal view looks like this:
200+
201+
```text
202+
Background MCP server ready at http://127.0.0.1:8000/mcp
203+
204+
Interactive inference workflow
205+
Defaults: mode=single, task=all, input=nifti, qc=on, logging=on
206+
1. Process input
207+
2. Change settings
208+
3. Reset to defaults
209+
4. View current settings
210+
5. Exit
211+
Choose an option [1]:
212+
```
213+
214+
Input handling:
215+
216+
- single mode accepts one `.nii` / `.nii.gz` volume path, and also supports a DICOM folder if needed
217+
- 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
218+
- batch processing continues if one case fails and records the failure in the run log
219+
220+
Supported tasks:
221+
222+
- segmentation only
223+
- classification only
224+
- all tasks in sequence
225+
226+
Outputs:
227+
228+
- `segmentation_mask.nii.gz` when segmentation is enabled
229+
- `segmentation_qc.png` when QC is enabled
230+
- `classification.json` when classification is enabled
231+
- `inference_metadata.json` with input and preprocessing metadata
232+
- `inference_log.csv` with per-case status and output paths

0 commit comments

Comments
 (0)