|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test script for integrating Medikode MCP Server with ChatGPT |
| 4 | +This script demonstrates how to use the MCP server tools with OpenAI's function calling |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import requests |
| 9 | +import json |
| 10 | +import openai |
| 11 | +from typing import Dict, Any, List |
| 12 | + |
| 13 | +# Configuration |
| 14 | +MCP_SERVER_URL = "http://localhost:3000" |
| 15 | +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "your-openai-api-key-here") |
| 16 | +MEDIKODE_API_KEY = os.getenv("MEDIKODE_API_KEY", "your-medikode-api-key-here") |
| 17 | + |
| 18 | +class MedikodeMCPClient: |
| 19 | + """Client for interacting with Medikode MCP Server""" |
| 20 | + |
| 21 | + def __init__(self, mcp_url: str, api_key: str): |
| 22 | + self.mcp_url = mcp_url |
| 23 | + self.api_key = api_key |
| 24 | + self.headers = { |
| 25 | + "Content-Type": "application/json", |
| 26 | + "x-api-key": api_key |
| 27 | + } |
| 28 | + |
| 29 | + def get_tools(self) -> List[Dict[str, Any]]: |
| 30 | + """Get OpenAI-compatible tool definitions from MCP server""" |
| 31 | + try: |
| 32 | + response = requests.get(f"{self.mcp_url}/capabilities/openai-tools") |
| 33 | + response.raise_for_status() |
| 34 | + return response.json()["tools"] |
| 35 | + except requests.RequestException as e: |
| 36 | + print(f"Error fetching tools: {e}") |
| 37 | + return [] |
| 38 | + |
| 39 | + def process_chart(self, text: str, specialty: str = None, taxonomy_code: str = None, insurance: str = None) -> Dict[str, Any]: |
| 40 | + """Process patient chart and get code suggestions""" |
| 41 | + data = {"text": text} |
| 42 | + if specialty: |
| 43 | + data["specialty"] = specialty |
| 44 | + if taxonomy_code: |
| 45 | + data["taxonomy_code"] = taxonomy_code |
| 46 | + if insurance: |
| 47 | + data["insurance"] = insurance |
| 48 | + |
| 49 | + try: |
| 50 | + response = requests.post( |
| 51 | + f"{self.mcp_url}/mcp/tools/process_chart", |
| 52 | + headers=self.headers, |
| 53 | + json=data |
| 54 | + ) |
| 55 | + response.raise_for_status() |
| 56 | + return response.json() |
| 57 | + except requests.RequestException as e: |
| 58 | + return {"error": str(e)} |
| 59 | + |
| 60 | + def validate_codes(self, patient_chart: str, human_coded_output: str, specialty: str = None) -> Dict[str, Any]: |
| 61 | + """Validate medical codes against patient chart""" |
| 62 | + data = { |
| 63 | + "patient_chart": patient_chart, |
| 64 | + "human_coded_output": human_coded_output |
| 65 | + } |
| 66 | + if specialty: |
| 67 | + data["specialty"] = specialty |
| 68 | + |
| 69 | + try: |
| 70 | + response = requests.post( |
| 71 | + f"{self.mcp_url}/mcp/tools/validate_codes", |
| 72 | + headers=self.headers, |
| 73 | + json=data |
| 74 | + ) |
| 75 | + response.raise_for_status() |
| 76 | + return response.json() |
| 77 | + except requests.RequestException as e: |
| 78 | + return {"error": str(e)} |
| 79 | + |
| 80 | + def calculate_raf(self, demographics: str, illnesses: str, model: str = "V28") -> Dict[str, Any]: |
| 81 | + """Calculate RAF score""" |
| 82 | + data = { |
| 83 | + "demographics": demographics, |
| 84 | + "illnesses": illnesses, |
| 85 | + "model": model |
| 86 | + } |
| 87 | + |
| 88 | + try: |
| 89 | + response = requests.post( |
| 90 | + f"{self.mcp_url}/mcp/tools/calculate_raf", |
| 91 | + headers=self.headers, |
| 92 | + json=data |
| 93 | + ) |
| 94 | + response.raise_for_status() |
| 95 | + return response.json() |
| 96 | + except requests.RequestException as e: |
| 97 | + return {"error": str(e)} |
| 98 | + |
| 99 | + def qa_validate_codes(self, coded_input: str) -> Dict[str, Any]: |
| 100 | + """QA validate coded medical input""" |
| 101 | + data = {"coded_input": coded_input} |
| 102 | + |
| 103 | + try: |
| 104 | + response = requests.post( |
| 105 | + f"{self.mcp_url}/mcp/tools/qa_validate_codes", |
| 106 | + headers=self.headers, |
| 107 | + json=data |
| 108 | + ) |
| 109 | + response.raise_for_status() |
| 110 | + return response.json() |
| 111 | + except requests.RequestException as e: |
| 112 | + return {"error": str(e)} |
| 113 | + |
| 114 | + def parse_eob(self, content: str) -> Dict[str, Any]: |
| 115 | + """Parse EOB document""" |
| 116 | + data = {"content": content} |
| 117 | + |
| 118 | + try: |
| 119 | + response = requests.post( |
| 120 | + f"{self.mcp_url}/mcp/tools/parse_eob", |
| 121 | + headers=self.headers, |
| 122 | + json=data |
| 123 | + ) |
| 124 | + response.raise_for_status() |
| 125 | + return response.json() |
| 126 | + except requests.RequestException as e: |
| 127 | + return {"error": str(e)} |
| 128 | + |
| 129 | +def test_mcp_server_health(): |
| 130 | + """Test if MCP server is running and healthy""" |
| 131 | + try: |
| 132 | + response = requests.get(f"{MCP_SERVER_URL}/health") |
| 133 | + response.raise_for_status() |
| 134 | + health_data = response.json() |
| 135 | + print("✅ MCP Server is healthy") |
| 136 | + print(f" Status: {health_data['status']}") |
| 137 | + print(f" Version: {health_data['version']}") |
| 138 | + print(f" Environment: {health_data['environment']}") |
| 139 | + return True |
| 140 | + except requests.RequestException as e: |
| 141 | + print(f"❌ MCP Server health check failed: {e}") |
| 142 | + return False |
| 143 | + |
| 144 | +def test_direct_mcp_tools(): |
| 145 | + """Test MCP tools directly without ChatGPT""" |
| 146 | + print("\n🧪 Testing MCP Tools Directly") |
| 147 | + print("=" * 40) |
| 148 | + |
| 149 | + client = MedikodeMCPClient(MCP_SERVER_URL, MEDIKODE_API_KEY) |
| 150 | + |
| 151 | + # Test 1: Process Chart |
| 152 | + print("\n1. Testing process_chart...") |
| 153 | + chart_text = "65-year-old male presents with chest pain radiating to left arm. EKG shows ST elevation in leads II, III, aVF. Troponin I elevated at 15.2 ng/mL." |
| 154 | + result = client.process_chart(chart_text, specialty="Cardiology") |
| 155 | + if "error" not in result: |
| 156 | + print("✅ process_chart successful") |
| 157 | + print(f" Suggested codes: {result.get('suggested_codes', 'N/A')}") |
| 158 | + else: |
| 159 | + print(f"❌ process_chart failed: {result['error']}") |
| 160 | + |
| 161 | + # Test 2: Validate Codes |
| 162 | + print("\n2. Testing validate_codes...") |
| 163 | + validation_result = client.validate_codes( |
| 164 | + patient_chart=chart_text, |
| 165 | + human_coded_output="I21.9, I25.10, Z95.1", |
| 166 | + specialty="Cardiology" |
| 167 | + ) |
| 168 | + if "error" not in validation_result: |
| 169 | + print("✅ validate_codes successful") |
| 170 | + print(f" Validation result: {validation_result.get('validation_result', 'N/A')}") |
| 171 | + else: |
| 172 | + print(f"❌ validate_codes failed: {validation_result['error']}") |
| 173 | + |
| 174 | + # Test 3: Calculate RAF |
| 175 | + print("\n3. Testing calculate_raf...") |
| 176 | + raf_result = client.calculate_raf( |
| 177 | + demographics="65-year-old male, Medicare beneficiary", |
| 178 | + illnesses="Acute myocardial infarction, Coronary artery disease, Diabetes mellitus type 2", |
| 179 | + model="V28" |
| 180 | + ) |
| 181 | + if "error" not in raf_result: |
| 182 | + print("✅ calculate_raf successful") |
| 183 | + print(f" RAF Score: {raf_result.get('raf', {}).get('final', 'N/A')}") |
| 184 | + else: |
| 185 | + print(f"❌ calculate_raf failed: {raf_result['error']}") |
| 186 | + |
| 187 | + # Test 4: QA Validate Codes |
| 188 | + print("\n4. Testing qa_validate_codes...") |
| 189 | + qa_result = client.qa_validate_codes("I21.9, I25.10, Z95.1, 99213") |
| 190 | + if "error" not in qa_result: |
| 191 | + print("✅ qa_validate_codes successful") |
| 192 | + print(f" Denial risk: {qa_result.get('denial_risk', 'N/A')}") |
| 193 | + else: |
| 194 | + print(f"❌ qa_validate_codes failed: {qa_result['error']}") |
| 195 | + |
| 196 | +def test_chatgpt_integration(): |
| 197 | + """Test integration with ChatGPT using OpenAI function calling""" |
| 198 | + print("\n🤖 Testing ChatGPT Integration") |
| 199 | + print("=" * 40) |
| 200 | + |
| 201 | + if OPENAI_API_KEY == "your-openai-api-key-here": |
| 202 | + print("⚠️ Please set your OpenAI API key in the script to test ChatGPT integration") |
| 203 | + return |
| 204 | + |
| 205 | + client = openai.OpenAI(api_key=OPENAI_API_KEY) |
| 206 | + mcp_client = MedikodeMCPClient(MCP_SERVER_URL, MEDIKODE_API_KEY) |
| 207 | + |
| 208 | + # Get MCP tools for ChatGPT |
| 209 | + tools = mcp_client.get_tools() |
| 210 | + if not tools: |
| 211 | + print("❌ Failed to get MCP tools") |
| 212 | + return |
| 213 | + |
| 214 | + print(f"✅ Retrieved {len(tools)} MCP tools for ChatGPT") |
| 215 | + |
| 216 | + # Test ChatGPT with MCP tools |
| 217 | + try: |
| 218 | + response = client.chat.completions.create( |
| 219 | + model="gpt-4", |
| 220 | + messages=[ |
| 221 | + { |
| 222 | + "role": "user", |
| 223 | + "content": "Please process this patient chart and suggest appropriate medical codes: '65-year-old male presents with chest pain radiating to left arm. EKG shows ST elevation in leads II, III, aVF. Troponin I elevated at 15.2 ng/mL.'" |
| 224 | + } |
| 225 | + ], |
| 226 | + tools=tools, |
| 227 | + tool_choice="auto" |
| 228 | + ) |
| 229 | + |
| 230 | + print("✅ ChatGPT integration successful") |
| 231 | + print(f" Response: {response.choices[0].message.content}") |
| 232 | + |
| 233 | + # Check if tools were called |
| 234 | + if response.choices[0].message.tool_calls: |
| 235 | + print(f" Tools called: {len(response.choices[0].message.tool_calls)}") |
| 236 | + for tool_call in response.choices[0].message.tool_calls: |
| 237 | + print(f" - {tool_call.function.name}") |
| 238 | + |
| 239 | + except Exception as e: |
| 240 | + print(f"❌ ChatGPT integration failed: {e}") |
| 241 | + |
| 242 | +def main(): |
| 243 | + """Main test function""" |
| 244 | + print("🚀 Medikode MCP Server - ChatGPT Integration Test") |
| 245 | + print("=" * 60) |
| 246 | + |
| 247 | + # Test 1: MCP Server Health |
| 248 | + if not test_mcp_server_health(): |
| 249 | + print("\n❌ MCP Server is not running. Please start it first:") |
| 250 | + print(" cd /Users/muthuka/root/medikode.ai/allnew/mcp-server") |
| 251 | + print(" node index.js") |
| 252 | + return |
| 253 | + |
| 254 | + # Test 2: Direct MCP Tools |
| 255 | + test_direct_mcp_tools() |
| 256 | + |
| 257 | + # Test 3: ChatGPT Integration |
| 258 | + test_chatgpt_integration() |
| 259 | + |
| 260 | + print("\n📋 Next Steps:") |
| 261 | + print("1. Update OPENAI_API_KEY and MEDIKODE_API_KEY in this script") |
| 262 | + print("2. Run the script again to test ChatGPT integration") |
| 263 | + print("3. Use the MCP server with your ChatGPT application on localhost:8004") |
| 264 | + |
| 265 | +if __name__ == "__main__": |
| 266 | + main() |
0 commit comments