anil988 commited on
Commit
b62e12f
Β·
1 Parent(s): 6cbbabf

Compliance: Added MCP Server Support for Path A Requirements

Browse files
Files changed (1) hide show
  1. backend/main.py +126 -0
backend/main.py CHANGED
@@ -180,6 +180,132 @@ def agent_action_protocol(record: Dict, triage: Dict, contra: Dict, labs: Dict,
180
  r = _gemini(prompt, override_key=override_key)
181
  return r or {"primary_directive": "Stabilize patient.", "action_mapping": [], "clinical_summary": "Offline.", "agent_reasoning": ["Failed to connect."]}
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  # ── API Endpoints ─────────────────────────────────────────────────────────────
184
 
185
  @app.get("/patients")
 
180
  r = _gemini(prompt, override_key=override_key)
181
  return r or {"primary_directive": "Stabilize patient.", "action_mapping": [], "clinical_summary": "Offline.", "agent_reasoning": ["Failed to connect."]}
182
 
183
+ # ── A2A Standard Protocol (for Prompt Opinion Marketplace) ────────────────────
184
+
185
+ @app.get("/.well-known/a2a-card.json")
186
+ def get_a2a_card():
187
+ """Agent Discovery Endpoint (A2A Standard)"""
188
+ return {
189
+ "name": "CodeBlue Context",
190
+ "version": "1.0.0",
191
+ "description": "Specialized ER diagnostic and safety synthesis agent. Orchestrates 5 specialized agents for 'Condition-First' clinical priority.",
192
+ "capabilities": ["clinical_synthesis", "safety_verification", "triage_ranking"],
193
+ "endpoints": {
194
+ "invoke": "/a2a/invoke"
195
+ },
196
+ "author": "Anil Chowdary",
197
+ "data_safety": "strictly_synthetic_data"
198
+ }
199
+
200
+ @app.post("/a2a/invoke")
201
+ async def a2a_invoke(request: Dict[str, Any], x_gemini_key: str = Header(None)):
202
+ """JSON-RPC 2.0 Invocation Endpoint (A2A Standard)"""
203
+ # A2A Message Format: { "jsonrpc": "2.0", "method": "...", "params": {...}, "id": 1 }
204
+ method = request.get("method")
205
+ params = request.get("params", {})
206
+ msg_id = request.get("id")
207
+
208
+ if method == "analyze_patient":
209
+ text = params.get("text")
210
+ if not text:
211
+ return {"jsonrpc": "2.0", "error": {"code": -32602, "message": "Missing 'text' param"}, "id": msg_id}
212
+
213
+ # Run core orchestration
214
+ result = await process_patient_synthesis(text, x_gemini_key)
215
+
216
+ # Return A2A compliant response
217
+ return {
218
+ "jsonrpc": "2.0",
219
+ "result": {
220
+ "directive": result["agents"]["action_protocol"].get("primary_directive"),
221
+ "severity": result["agents"]["triage"].get("severity_label"),
222
+ "risks": result["agents"]["triage"].get("critical_risks"),
223
+ "summary": result["agents"]["action_protocol"].get("clinical_summary")
224
+ },
225
+ "id": msg_id
226
+ }
227
+
228
+ return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": msg_id}
229
+
230
+ async def process_patient_synthesis(text: str, key: str):
231
+ # Internal helper to reuse the synthesis logic for A2A requests
232
+ from .main import agent_triage, agent_contraindication, agent_labs, agent_differentials, agent_action_protocol
233
+
234
+ record = {"raw_text": text}
235
+ triage = agent_triage(record, override_key=key)
236
+ contra = agent_contraindication(record, triage=triage, override_key=key)
237
+ labs = agent_labs(record, triage=triage, override_key=key)
238
+ diffs = agent_differentials(record, triage=triage, labs=labs, override_key=key)
239
+ action = agent_action_protocol(record, triage, contra, labs, diffs, override_key=key)
240
+
241
+ return {
242
+ "agents": {
243
+ "triage": triage,
244
+ "contraindication": contra,
245
+ "labs": labs,
246
+ "differentials": diffs,
247
+ "action_protocol": action
248
+ }
249
+ }
250
+
251
+ # ── MCP Standard Protocol (for Prompt Opinion Marketplace) ───────────────────
252
+
253
+ @app.get("/mcp")
254
+ async def mcp_sse_endpoint():
255
+ """MCP SSE Discovery Endpoint"""
256
+ # This signals the transport for MCP
257
+ return {"transport": "sse", "endpoint": "/mcp/message"}
258
+
259
+ @app.post("/mcp/message")
260
+ async def mcp_message(request: Dict[str, Any], x_gemini_key: str = Header(None)):
261
+ """MCP JSON-RPC Tool Invocation"""
262
+ method = request.get("method")
263
+ params = request.get("params", {})
264
+ msg_id = request.get("id")
265
+
266
+ if method == "list_tools":
267
+ return {
268
+ "jsonrpc": "2.0",
269
+ "result": {
270
+ "tools": [
271
+ {
272
+ "name": "analyze_clinical_record",
273
+ "description": "Performs a 5-agent clinical synthesis to identify life-threatening risks and generate stabilization directives.",
274
+ "input_schema": {
275
+ "type": "object",
276
+ "properties": {
277
+ "text": {"type": "string", "description": "The raw clinical record or lab data."}
278
+ },
279
+ "required": ["text"]
280
+ }
281
+ }
282
+ ]
283
+ },
284
+ "id": msg_id
285
+ }
286
+
287
+ if method == "call_tool":
288
+ tool_name = params.get("name")
289
+ tool_args = params.get("arguments", {})
290
+
291
+ if tool_name == "analyze_clinical_record":
292
+ text = tool_args.get("text")
293
+ result = await process_patient_synthesis(text, x_gemini_key)
294
+ return {
295
+ "jsonrpc": "2.0",
296
+ "result": {
297
+ "content": [
298
+ {
299
+ "type": "text",
300
+ "text": f"DIRECTIVE: {result['agents']['action_protocol'].get('primary_directive')}\nSEVERITY: {result['agents']['triage'].get('severity_label')}\nSUMMARY: {result['agents']['action_protocol'].get('clinical_summary')}"
301
+ }
302
+ ]
303
+ },
304
+ "id": msg_id
305
+ }
306
+
307
+ return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": msg_id}
308
+
309
  # ── API Endpoints ─────────────────────────────────────────────────────────────
310
 
311
  @app.get("/patients")