File size: 22,994 Bytes
71cadf5 |
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
import os
import logging
import structlog
from contextlib import asynccontextmanager
from typing import Optional, Union, Dict, Any, List, Annotated
from fastapi import FastAPI, Request, Depends, HTTPException, Header, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from dotenv import load_dotenv
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from src.main import mcp
from src.tools.base_tool import BaseTool
from src.dependencies import (
get_cache_service,
get_usage_service,
get_fda_tool,
get_pubmed_tool,
get_healthfinder_tool,
get_clinical_trials_tool,
get_medical_terminology_tool
)
# Set up structured logging
logging.basicConfig(level=logging.DEBUG)
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
logger = structlog.get_logger("healthcare-mcp")
# Load environment variables
load_dotenv()
# Add lifespan event handlers
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events"""
# Startup: Initialize services
logger.info("Starting Healthcare MCP Server")
# Initialize services if needed
try:
from src.services.cache_service import CacheService
cache = CacheService()
await cache.init()
logger.info("Cache service initialized")
except Exception as e:
logger.error("Failed to initialize cache service", error=str(e))
try:
from src.services.usage_service import UsageService
usage = UsageService()
await usage.init()
logger.info("Usage service initialized")
except Exception as e:
logger.error("Failed to initialize usage service", error=str(e))
yield # Server is running
# Shutdown: Clean up resources
logger.info("Shutting down Healthcare MCP Server")
# Close the shared HTTP client
try:
from src.tools.base_tool import BaseTool
await BaseTool.close_http_client()
logger.info("HTTP client closed")
except Exception as e:
logger.error("Failed to close HTTP client", error=str(e))
# Close services
try:
from src.services.cache_service import CacheService
cache = CacheService()
await cache.close()
logger.info("Cache service closed")
except Exception as e:
logger.error("Failed to close cache service", error=str(e))
try:
from src.services.usage_service import UsageService
usage = UsageService()
await usage.close()
logger.info("Usage service closed")
except Exception as e:
logger.error("Failed to close usage service", error=str(e))
# Set up rate limiter
limiter = Limiter(key_func=get_remote_address)
# Create FastAPI app with lifespan
app = FastAPI(
title="Healthcare MCP API",
description="Healthcare MCP server for medical information access",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan
)
# Add rate limiter to app
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
# Add OpenTelemetry instrumentation if enabled
if os.getenv("ENABLE_TELEMETRY", "false").lower() == "true":
try:
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
FastAPIInstrumentor.instrument_app(app)
logger.info("OpenTelemetry instrumentation enabled")
except ImportError:
logger.warning("OpenTelemetry packages not installed, skipping instrumentation")
# Import necessary components for request handling
from fastapi import Body, Query, Path, BackgroundTasks
from pydantic import BaseModel, Field, ConfigDict
from typing import Dict, Any, Optional, List, Union, Annotated
# Define tool request model
class ToolRequest(BaseModel):
"""Request model for tool execution"""
model_config = ConfigDict(extra="forbid")
name: str = Field(..., description="Name of the tool to execute")
arguments: Dict[str, Any] = Field(..., description="Arguments to pass to the tool")
session_id: Optional[str] = Field(None, description="Session ID for tracking usage")
# Define error response model
class ErrorResponse(BaseModel):
"""Standard error response"""
model_config = ConfigDict(frozen=True)
status: str = Field("error", description="Status of the response")
error_message: str = Field(..., description="Error message")
error_code: Optional[str] = Field(None, description="Error code for categorization")
# Define success response model
class SuccessResponse(BaseModel):
"""Base success response"""
model_config = ConfigDict(extra="allow")
status: str = Field("success", description="Status of the response")
# Mount SSE endpoint (but don't mount it at the same path as other APIs)
app.mount("/mcp/sse", mcp.sse_app())
# Define API endpoints for each tool
@app.get("/api/fda",
summary="Look up drug information from the FDA database",
description="Search for drug information in the FDA database by drug name and search type",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Drug Information"])
@limiter.limit("60/minute")
async def api_fda_drug_lookup(
request: Request,
drug_name: Annotated[str, Query(description="Name of the drug to search for")],
search_type: Annotated[str, Query(description="Type of information to retrieve: 'label', 'adverse_events', or 'general'")] = "general",
session_id: Annotated[Optional[str], Header(description="Session ID for tracking usage")] = None
):
"""
Look up drug information from the FDA database
- **drug_name**: Name of the drug to search for
- **search_type**: Type of information to retrieve: 'label', 'adverse_events', or 'general'
- **session_id**: Optional session ID for tracking usage
"""
try:
from src.main import fda_drug_lookup
logger.info("FDA drug lookup request", drug_name=drug_name, search_type=search_type, session_id=session_id)
return await fda_drug_lookup(session_id, drug_name, search_type)
except Exception as e:
logger.error("Error in FDA drug lookup", error=str(e), drug_name=drug_name)
return ErrorResponse(error_message=f"Error looking up drug information: {str(e)}")
@app.get("/api/pubmed",
summary="Search for medical literature in PubMed database",
description="Search for medical literature in PubMed database by query, with options for max results and date range",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Medical Literature"])
@limiter.limit("30/minute")
async def api_pubmed_search(
request: Request,
query: Annotated[str, Query(description="Search query for medical literature")],
max_results: Annotated[int, Query(description="Maximum number of results to return", ge=1, le=50)] = 5,
date_range: Annotated[str, Query(description="Limit to articles published within years (e.g. '5' for last 5 years)")] = "",
session_id: Annotated[Optional[str], Header(description="Session ID for tracking usage")] = None
):
"""
Search for medical literature in PubMed database
- **query**: Search query for medical literature
- **max_results**: Maximum number of results to return (1-50)
- **date_range**: Limit to articles published within years (e.g. '5' for last 5 years)
- **session_id**: Optional session ID for tracking usage
"""
try:
from src.main import pubmed_search
logger.info("PubMed search request", query=query, max_results=max_results, date_range=date_range, session_id=session_id)
return await pubmed_search(session_id, query, max_results, date_range)
except Exception as e:
logger.error("Error in PubMed search", error=str(e), query=query)
return ErrorResponse(error_message=f"Error searching PubMed: {str(e)}")
@app.get("/api/health_finder",
summary="Get evidence-based health information on various topics",
description="Access evidence-based health information from Health.gov",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Health Information"])
@limiter.limit("60/minute")
async def api_health_topics(
request: Request,
topic: Annotated[str, Query(description="Health topic to search for information")],
language: Annotated[str, Query(description="Language for content (en or es)")] = "en",
session_id: Annotated[Optional[str], Header(description="Session ID for tracking usage")] = None
):
"""
Get evidence-based health information on various topics
- **topic**: Health topic to search for information
- **language**: Language for content (en or es)
- **session_id**: Optional session ID for tracking usage
"""
try:
from src.main import health_topics
logger.info("Health topics request", topic=topic, language=language, session_id=session_id)
return await health_topics(session_id, topic, language)
except Exception as e:
logger.error("Error in health topics", error=str(e), topic=topic)
return ErrorResponse(error_message=f"Error fetching health information: {str(e)}")
@app.get("/api/clinical_trials",
summary="Search for clinical trials by condition and status",
description="Search for clinical trials by medical condition, status, and other parameters",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Clinical Trials"])
@limiter.limit("30/minute")
async def api_clinical_trials(
request: Request,
condition: Annotated[str, Query(description="Medical condition or disease to search for")],
status: Annotated[str, Query(description="Trial status (recruiting, completed, active, not_recruiting, or all)")] = "recruiting",
max_results: Annotated[int, Query(description="Maximum number of results to return", ge=1, le=100)] = 10,
session_id: Annotated[Optional[str], Header(description="Session ID for tracking usage")] = None,
clinical_trials_tool = Depends(get_clinical_trials_tool),
usage_service = Depends(get_usage_service)
):
"""
Search for clinical trials by condition, status, and other parameters
- **condition**: Medical condition or disease to search for
- **status**: Trial status (recruiting, completed, active, not_recruiting, or all)
- **max_results**: Maximum number of results to return (1-100)
- **session_id**: Optional session ID for tracking usage
"""
try:
logger.info("Clinical trials search request",
condition=condition,
status=status,
max_results=max_results,
session_id=session_id)
# Track usage
if session_id:
await usage_service.track_usage(session_id, "clinical_trials_search", {
"condition": condition,
"status": status,
"max_results": max_results
})
# Call the tool directly
result = await clinical_trials_tool.search_trials(condition, status, max_results)
return result
except Exception as e:
logger.error("Error in clinical trials search", error=str(e), condition=condition)
return ErrorResponse(error_message=f"Error searching clinical trials: {str(e)}")
@app.get("/api/medical_terminology",
summary="Look up ICD-10 codes by code or description",
description="Look up ICD-10 codes and medical terminology definitions",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Medical Terminology"])
@limiter.limit("60/minute")
async def api_lookup_icd_code(
request: Request,
code: Annotated[Optional[str], Query(description="ICD-10 code to look up (optional if description is provided)")] = None,
description: Annotated[Optional[str], Query(description="Medical condition description to search for (optional if code is provided)")] = None,
max_results: Annotated[int, Query(description="Maximum number of results to return", ge=1, le=50)] = 10,
session_id: Annotated[Optional[str], Header(description="Session ID for tracking usage")] = None
):
"""
Look up ICD-10 codes by code or description
- **code**: ICD-10 code to look up (optional if description is provided)
- **description**: Medical condition description to search for (optional if code is provided)
- **max_results**: Maximum number of results to return (1-50)
- **session_id**: Optional session ID for tracking usage
"""
try:
from src.main import lookup_icd_code
logger.info("ICD code lookup request",
code=code,
description=description,
max_results=max_results,
session_id=session_id)
return await lookup_icd_code(session_id, code, description, max_results)
except Exception as e:
logger.error("Error in ICD code lookup", error=str(e), code=code, description=description)
return ErrorResponse(error_message=f"Error looking up ICD-10 code: {str(e)}")
@app.get("/api/usage_stats",
summary="Get usage statistics for the current session",
description="Get a summary of API usage for the current session",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Monitoring"])
@limiter.limit("120/minute")
async def api_usage_stats(
request: Request,
session_id: Annotated[Optional[str], Query(description="Session ID to get usage statistics for")] = None
):
"""
Get usage statistics for the current session
- **session_id**: Session ID to get usage statistics for (optional)
Returns a summary of API usage for the specified session or all sessions if no session ID is provided
"""
try:
from src.main import get_usage_stats
logger.info("Usage stats request", session_id=session_id)
return await get_usage_stats(session_id)
except Exception as e:
logger.error("Error in usage stats", error=str(e), session_id=session_id)
return ErrorResponse(error_message=f"Error getting usage statistics: {str(e)}")
@app.get("/api/all_usage_stats",
summary="Get overall usage statistics",
description="Get a summary of API usage across all sessions",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Monitoring"])
@limiter.limit("30/minute")
async def api_all_usage_stats(
request: Request,
days: Annotated[int, Query(description="Number of days to include in the statistics", ge=1, le=365)] = 30
):
"""
Get overall usage statistics
- **days**: Number of days to include in the statistics (1-365, default: 30)
Returns a summary of API usage across all sessions for the specified time period
"""
try:
from src.main import get_all_usage_stats
logger.info("All usage stats request", days=days)
return await get_all_usage_stats(None, days=days)
except Exception as e:
logger.error("Error in all usage stats", error=str(e))
return ErrorResponse(error_message=f"Error getting all usage statistics: {str(e)}")
# Add the specific call-tool endpoint
@app.post("/mcp/call-tool",
summary="Call a specific tool by name",
description="Generic endpoint to call any tool by name with arguments",
response_model=Union[SuccessResponse, ErrorResponse],
tags=["Tool Execution"])
@limiter.limit("60/minute")
async def call_tool(
request: Request,
tool_request: ToolRequest = Body(...),
):
"""
Call a specific tool by name
- **name**: Name of the tool to call
- **arguments**: Arguments to pass to the tool
- **session_id**: Optional session ID for tracking usage
"""
try:
from src.main import fda_drug_lookup, pubmed_search, health_topics, clinical_trials_search, lookup_icd_code, get_usage_stats, get_all_usage_stats
tool_name = tool_request.name
arguments = tool_request.arguments
session_id = tool_request.session_id
logger.info("Tool call request",
tool_name=tool_name,
session_id=session_id)
# Map tool names to their corresponding functions
tool_mapping = {
"fda_drug_lookup": lambda args: fda_drug_lookup(session_id, **args),
"pubmed_search": lambda args: pubmed_search(session_id, **args),
"health_topics": lambda args: health_topics(session_id, **args),
"clinical_trials_search": lambda args: clinical_trials_search(session_id, **args),
"lookup_icd_code": lambda args: lookup_icd_code(session_id, **args),
"get_usage_stats": lambda _: get_usage_stats(session_id),
"get_all_usage_stats": lambda args: get_all_usage_stats(session_id, **args)
}
if tool_name not in tool_mapping:
logger.warning("Tool not found", tool_name=tool_name)
return ErrorResponse(
error_message=f"Tool '{tool_name}' not found",
error_code="TOOL_NOT_FOUND"
)
# Call the appropriate tool function
result = await tool_mapping[tool_name](arguments)
return result
except Exception as e:
logger.error("Error in tool call", error=str(e), tool_name=tool_request.name)
return ErrorResponse(
error_message=f"Error calling tool: {str(e)}",
error_code="TOOL_EXECUTION_ERROR"
)
# Health check endpoint
@app.get("/health",
summary="Health check endpoint",
description="Check if the server is running properly",
tags=["Monitoring"])
@limiter.limit("300/minute")
async def health_check(request: Request):
"""
Health check endpoint
Returns the status and version of the server along with service health information
"""
logger.info("Health check request")
# Check if cache service is available
cache_status = "ok"
try:
from src.services.cache_service import CacheService
cache = CacheService()
await cache.init()
# Just check if we can access the cache
cache_stats = await cache.get_stats()
await cache.close()
if isinstance(cache_stats, dict) and "error" in cache_stats:
cache_status = f"error: {cache_stats['error']}"
except Exception as e:
cache_status = f"error: {str(e)}"
# Check if usage service is available
usage_status = "ok"
try:
from src.services.usage_service import UsageService
usage = UsageService()
await usage.init()
# Just check if we can access the usage stats
usage_stats = await usage.get_usage_stats()
await usage.close()
if isinstance(usage_stats, dict) and "error" in usage_stats:
usage_status = f"error: {usage_stats['error']}"
except Exception as e:
usage_status = f"error: {str(e)}"
# Get current timestamp in ISO format
from datetime import datetime, timezone
timestamp = datetime.now(timezone.utc).isoformat()
return {
"status": "healthy",
"version": "1.0.0",
"timestamp": timestamp,
"services": {
"cache": cache_status,
"usage": usage_status
}
}
# Redirect root to docs
@app.get("/",
summary="Redirect to API documentation",
description="Redirects to the Swagger UI documentation",
tags=["Documentation"])
@limiter.limit("300/minute")
async def redirect_to_docs(request: Request):
"""
Redirect to API documentation
Redirects to the Swagger UI documentation
"""
from fastapi.responses import RedirectResponse
logger.info("Root request, redirecting to docs")
return RedirectResponse(url="/docs")
# Information about premium version
@app.get("/premium-info",
summary="Get information about the premium version",
description="Get information about the premium version of the Healthcare MCP Server",
response_model=SuccessResponse,
tags=["Documentation"])
@limiter.limit("120/minute")
async def premium_info(request: Request):
"""
Get information about the premium version
Returns information about the premium version of the Healthcare MCP Server including features,
pricing, and contact information
"""
logger.info("Premium info request")
return {
"status": "success",
"message": "This is the free version of Healthcare MCP Server.",
"premium_url": "https://healthcaremcp.com",
"features": [
"Unlimited API calls (500 requests per minute)",
"Advanced healthcare data tools (SNOMED CT, RxNorm, LOINC)",
"Custom integrations with EHR systems",
"Priority support with 24/7 availability",
"Advanced analytics and usage reporting",
"Private deployment options"
],
"pricing": {
"basic": "$99/month",
"professional": "$299/month",
"enterprise": "Contact for pricing"
},
"contact": "[email protected]"
}
# End of API endpoints
if __name__ == "__main__":
import uvicorn
import argparse
parser = argparse.ArgumentParser(description='Healthcare MCP Server')
parser.add_argument('--port', type=int, default=int(os.getenv("PORT", "8000")), help='Port to run the server on')
parser.add_argument('--host', type=str, default="0.0.0.0", help='Host to run the server on')
args = parser.parse_args()
port = args.port
logger.info("Starting server", port=port)
uvicorn.run(app,
host=args.host,
port=port,
log_level="info",
access_log=True,
workers=int(os.getenv("WORKERS", "1")))
|