File size: 8,900 Bytes
86f402d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
SkinProAI MCP Server - Pure JSON-RPC 2.0 stdio server (no mcp library required).

Uses sys.executable (venv Python) so all ML packages (torch, transformers, etc.)
are available. Tools are loaded lazily on first call.

Run standalone: python mcp_server/server.py
(Should start silently, waiting on stdin.)
"""

import sys
import json
import os

# Ensure project root is on path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from mcp_server.tool_registry import get_monet, get_convnext, get_gradcam, get_rag


# ---------------------------------------------------------------------------
# Tool implementations
# ---------------------------------------------------------------------------

def _monet_analyze(arguments: dict) -> dict:
    from PIL import Image
    image = Image.open(arguments["image_path"]).convert("RGB")
    return get_monet().analyze(image)


def _classify_lesion(arguments: dict) -> dict:
    from PIL import Image
    image = Image.open(arguments["image_path"]).convert("RGB")
    monet_scores = arguments.get("monet_scores")
    return get_convnext().classify(
        clinical_image=image,
        derm_image=None,
        monet_scores=monet_scores,
    )


def _generate_gradcam(arguments: dict) -> dict:
    from PIL import Image
    import tempfile
    image = Image.open(arguments["image_path"]).convert("RGB")
    result = get_gradcam().analyze(image)

    gradcam_file = tempfile.NamedTemporaryFile(suffix="_gradcam.png", delete=False)
    gradcam_path = gradcam_file.name
    gradcam_file.close()
    result["overlay"].save(gradcam_path)

    return {
        "gradcam_path": gradcam_path,
        "predicted_class": result["predicted_class"],
        "predicted_class_full": result["predicted_class_full"],
        "confidence": result["confidence"],
    }


def _search_guidelines(arguments: dict) -> dict:
    query = arguments.get("query", "")
    diagnosis = arguments.get("diagnosis") or ""
    rag = get_rag()
    context, references = rag.get_management_context(diagnosis, query)
    references_display = rag.format_references_for_display(references)
    return {
        "context": context,
        "references": references,
        "references_display": references_display,
    }


def _compare_images(arguments: dict) -> dict:
    from PIL import Image
    import tempfile
    image1 = Image.open(arguments["image1_path"]).convert("RGB")
    image2 = Image.open(arguments["image2_path"]).convert("RGB")

    from models.overlay_tool import get_overlay_tool
    comparison = get_overlay_tool().generate_comparison_overlay(
        image1, image2, label1="Previous", label2="Current"
    )
    comparison_path = comparison["path"]

    monet = get_monet()
    prev_result = monet.analyze(image1)
    curr_result = monet.analyze(image2)

    monet_deltas = {}
    for name in curr_result["features"]:
        prev_val = prev_result["features"].get(name, 0.0)
        curr_val = curr_result["features"][name]
        delta = curr_val - prev_val
        if abs(delta) > 0.1:
            monet_deltas[name] = {
                "previous": prev_val,
                "current": curr_val,
                "delta": delta,
            }

    # Generate GradCAM for both images so the frontend can show a side-by-side comparison
    prev_gradcam_path = None
    curr_gradcam_path = None
    try:
        gradcam = get_gradcam()
        prev_gc = gradcam.analyze(image1)
        curr_gc = gradcam.analyze(image2)

        f1 = tempfile.NamedTemporaryFile(suffix="_gradcam.png", delete=False)
        prev_gradcam_path = f1.name
        f1.close()
        prev_gc["overlay"].save(prev_gradcam_path)

        f2 = tempfile.NamedTemporaryFile(suffix="_gradcam.png", delete=False)
        curr_gradcam_path = f2.name
        f2.close()
        curr_gc["overlay"].save(curr_gradcam_path)
    except Exception:
        pass  # GradCAM comparison is best-effort

    return {
        "comparison_path": comparison_path,
        "monet_deltas": monet_deltas,
        "prev_gradcam_path": prev_gradcam_path,
        "curr_gradcam_path": curr_gradcam_path,
    }


TOOLS = {
    "monet_analyze": _monet_analyze,
    "classify_lesion": _classify_lesion,
    "generate_gradcam": _generate_gradcam,
    "search_guidelines": _search_guidelines,
    "compare_images": _compare_images,
}

TOOLS_LIST = [
    {
        "name": "monet_analyze",
        "description": "Extract MONET concept-presence scores from a skin lesion image.",
        "inputSchema": {
            "type": "object",
            "properties": {"image_path": {"type": "string"}},
            "required": ["image_path"],
        },
    },
    {
        "name": "classify_lesion",
        "description": "Classify a skin lesion using ConvNeXt dual-encoder.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "image_path": {"type": "string"},
                "monet_scores": {"type": "array"},
            },
            "required": ["image_path"],
        },
    },
    {
        "name": "generate_gradcam",
        "description": "Generate a Grad-CAM attention overlay for a skin lesion image.",
        "inputSchema": {
            "type": "object",
            "properties": {"image_path": {"type": "string"}},
            "required": ["image_path"],
        },
    },
    {
        "name": "search_guidelines",
        "description": "Search clinical guidelines RAG for management context.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "query": {"type": "string"},
                "diagnosis": {"type": "string"},
            },
            "required": ["query"],
        },
    },
    {
        "name": "compare_images",
        "description": "Generate comparison overlay and MONET deltas for two lesion images.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "image1_path": {"type": "string"},
                "image2_path": {"type": "string"},
            },
            "required": ["image1_path", "image2_path"],
        },
    },
]


# ---------------------------------------------------------------------------
# JSON-RPC 2.0 dispatcher
# ---------------------------------------------------------------------------

def handle_request(request: dict):
    method = request.get("method")
    req_id = request.get("id")  # None for notifications
    params = request.get("params", {})

    if method == "initialize":
        return {
            "jsonrpc": "2.0",
            "id": req_id,
            "result": {
                "protocolVersion": "2024-11-05",
                "capabilities": {"tools": {"listChanged": False}},
                "serverInfo": {"name": "SkinProAI", "version": "1.0.0"},
            },
        }

    if method in ("notifications/initialized",):
        return None  # notification — no response

    if method == "tools/list":
        return {
            "jsonrpc": "2.0",
            "id": req_id,
            "result": {"tools": TOOLS_LIST},
        }

    if method == "tools/call":
        name = params.get("name")
        arguments = params.get("arguments", {})
        if name not in TOOLS:
            return {
                "jsonrpc": "2.0",
                "id": req_id,
                "error": {"code": -32601, "message": f"Unknown tool: {name}"},
            }
        try:
            result = TOOLS[name](arguments)
            return {
                "jsonrpc": "2.0",
                "id": req_id,
                "result": {
                    "content": [{"type": "text", "text": json.dumps(result)}],
                    "isError": False,
                },
            }
        except Exception as e:
            return {
                "jsonrpc": "2.0",
                "id": req_id,
                "result": {
                    "content": [{"type": "text", "text": f"Tool error: {e}"}],
                    "isError": True,
                },
            }

    # Unknown method with id → method not found
    if req_id is not None:
        return {
            "jsonrpc": "2.0",
            "id": req_id,
            "error": {"code": -32601, "message": f"Method not found: {method}"},
        }

    return None  # unknown notification — ignore


# ---------------------------------------------------------------------------
# Main loop
# ---------------------------------------------------------------------------

def main():
    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue
        try:
            request = json.loads(line)
        except json.JSONDecodeError:
            continue
        response = handle_request(request)
        if response is not None:
            sys.stdout.write(json.dumps(response) + "\n")
            sys.stdout.flush()


if __name__ == "__main__":
    main()