Skip to content

Commit bfc5f6f

Browse files
feat(medcat-service): Run MCP server for medcat using gradio (#253)
* feat(medcat-service): Add MCP. Fix requirements.txt * feat(medcat-service): Add MCP. Fix linting
1 parent 94381f5 commit bfc5f6f

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

medcat-service/medcat_service/demo/gradio_demo.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict, List
2-
31
import gradio as gr
42
from pydantic import BaseModel
53

@@ -43,7 +41,7 @@ class EntityResponse(BaseModel):
4341
Expected data format of gradio highlightedtext component
4442
"""
4543

46-
entities: List[EntityAnnotation]
44+
entities: list[EntityAnnotation]
4745
text: str
4846

4947

@@ -75,15 +73,15 @@ def convert_annotation_to_display_model(entity: Entity) -> EntityAnnotationDispl
7573
)
7674

7775

78-
def convert_entity_dict_to_annotations(entity_dict_list: List[Dict[str, Entity]]) -> list[EntityAnnotation]:
76+
def convert_entity_dict_to_annotations(entity_dict_list: list[dict[str, Entity]]) -> list[EntityAnnotation]:
7977
annotations: list[EntityAnnotation] = []
8078
for entity_dict in entity_dict_list:
8179
for key, entity in entity_dict.items():
8280
annotations.append(convert_annotation_to_ner_model(entity, index=int(key)))
8381
return annotations
8482

8583

86-
def convert_entity_dict_to_display_model(entity_dict_list: List[Dict[str, Entity]]) -> list[EntityAnnotationDisplay]:
84+
def convert_entity_dict_to_display_model(entity_dict_list: list[dict[str, Entity]]) -> list[EntityAnnotationDisplay]:
8785
annotations: list[EntityAnnotationDisplay] = []
8886
for entity_dict in entity_dict_list:
8987
for key, entity in entity_dict.items():
@@ -95,7 +93,32 @@ def convert_display_model_to_list_of_lists(entity_display_model: list[EntityAnno
9593
return [[str(getattr(entity, field)) for field in entity.model_fields] for entity in entity_display_model]
9694

9795

98-
def process_input(input_text: str):
96+
def perform_named_entity_resolution(input_text: str):
97+
"""
98+
Performs clinical coding by processing the input text with MedCAT to extract and
99+
annotate medical concepts (entities).
100+
101+
Returns:
102+
1. A dictionary following the NER response model (EntityResponse), containing the original text
103+
and the list of detected entities.
104+
2. A datatable-compatible list of lists, where each sublist represents an entity annotation and
105+
its attributes for display purposes.
106+
107+
This method is used as the main function for the Gradio MedCAT demo and MCP server,
108+
enabling users to input free text and receive automatic annotation and coding of clinical entities.
109+
110+
Args:
111+
input_text (str): The input text to be processed and annotated for medical entities by MedCAT.
112+
113+
Returns:
114+
Tuple:
115+
- dict: A dictionary following the NER response model (EntityResponse), containing the
116+
original text and the list of detected entities.
117+
- list[list[str]]: A datatable-compatible list of lists, where each sublist represents an
118+
entity annotation and its attributes for display purposes.
119+
120+
"""
121+
99122
processor = get_medcat_processor(get_settings())
100123
input = ProcessAPIInputContent(text=input_text)
101124

@@ -136,7 +159,7 @@ def process_input(input_text: str):
136159
""" # noqa: E501
137160

138161
io = gr.Interface(
139-
fn=process_input,
162+
fn=perform_named_entity_resolution,
140163
inputs="text",
141164
outputs=[
142165
gr.HighlightedText(label="Processed Text"),

medcat-service/medcat_service/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
app.include_router(health.router)
3636
app.include_router(process.router)
3737

38-
gr.mount_gradio_app(app, io, path="/demo")
38+
gr.mount_gradio_app(app, io, path="/demo", mcp_server=True)
3939

4040

4141
def configure_observability(settings: Settings, app: FastAPI):

medcat-service/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ medcat[meta-cat,spacy,deid]~=2.2.0
77
transformers>=4.34.0,<5.0.0
88
requests==2.32.4
99
fastapi[standard]==0.115.2
10-
pydantic==2.9.2
10+
pydantic>=2.11.10,<2.12.5
1111
pydantic-settings==2.10.1
12-
gradio==5.38.0
12+
gradio[mcp]==5.38.0
1313
prometheus-fastapi-instrumentator==7.1.0

0 commit comments

Comments
 (0)