The MCP (Model-Context-Protocol) server provides a set of tools for interacting with OMOP CDM databases and generating SQL queries using natural language processing. The server is built using FastMCP and integrates with Ollama for LLM capabilities.
Execute_SQL_Query
: Execute SQL queries against an OMOP database and return results in CSV formatTest_Connection
: Test if a database connection is validGet_OMOP_Schema
: Get the OMOP CDM schema information for prompting
Validate_SQL_Query
: Validate SQL queries against OMOP CDM validation rules
Generate_SQL
: Generate SQL from natural language using an LLM, incorporating medical concept codesGenerate_Explanation
: Generate an explanation for an SQL queryGenerate_Answer
: Generate a natural language answer based on query, SQL, and resultsList_Available_Models
: List available LLM models from Ollama
The server uses a configuration file (config/config.json
) that specifies:
- Database connection strings
- Schema directory location
- Ollama API settings
- OMOP CDM validation rules and schema files
The SQL generation tool accepts medical concepts in the following format:
{
"conditions": [
{
"concept_id": 12345,
"concept_name": "Diabetes",
"vocabulary_id": "SNOMED"
}
],
"drugs": [
{
"concept_id": 11111,
"concept_name": "Metformin",
"vocabulary_id": "RxNorm"
}
],
"measurements": [
{
"concept_id": 22222,
"concept_name": "Blood Pressure",
"vocabulary_id": "LOINC"
}
]
}
- Python 3.13 or higher
- PostgreSQL database with OMOP CDM schema
- Ollama running locally for LLM capabilities
- mcp
- httpx
- sqlalchemy
- pydantic
- pydantic-settings
- python-multipart
- sse-starlette
The MCP server can be used as a standalone service or integrated into other applications. To use it:
- Ensure all dependencies are installed:
uv pip install -e .
-
Configure the database connection and other settings in
config/config.json
-
Start the server:
from src.unified_mcp import mcp
mcp.run(transport="stdio")
# Generate SQL with medical concepts
medical_concepts = {
"conditions": [
{"concept_id": 12345, "concept_name": "Diabetes", "vocabulary_id": "SNOMED"}
]
}
schema = mcp.tools["Get_OMOP_Schema"]()
sql_query, confidence = await mcp.tools["Generate_SQL"](
prompt="Find all patients with diabetes",
medical_concepts=medical_concepts,
schema=schema
)
# Validate the generated SQL
validation_result = mcp.tools["Validate_SQL_Query"](sql_query)
# Execute the query if valid
if validation_result["is_valid"]:
results = mcp.tools["Execute_SQL_Query"](sql_query)