HANA Cloud MCP Server
by HatriGt
"""
HANA Cloud MCP Server Setup Script
---------------------------------
This script helps setup the Model Context Protocol (MCP) server
for HANA Cloud DB integration with Cursor IDE.
"""
import os
import argparse
import json
import requests
from getpass import getpass
def setup_environment():
"""Setup the environment variables for MCP server"""
print("\n===== HANA Cloud MCP Server Setup =====\n")
# Get HANA Cloud connection details
print("Enter HANA Cloud connection details:")
host = input("HANA Host (e.g., your-hana-host.hanacloud.ondemand.com): ")
port = input("HANA Port [443]: ") or "443"
user = input("HANA Username [DBADMIN]: ") or "DBADMIN"
password = getpass("HANA Password: ")
# Get schema configuration
print("\nEnter schema configuration (press Enter for defaults):")
model_schema = input("Model Schema [MCP_MODELS]: ") or "MCP_MODELS"
context_schema = input("Context Schema [MCP_CONTEXTS]: ") or "MCP_CONTEXTS"
protocol_schema = input("Protocol Schema [MCP_PROTOCOLS]: ") or "MCP_PROTOCOLS"
# Get server configuration
print("\nEnter server configuration:")
port_number = input("Server Port [5000]: ") or "5000"
max_connections = input("Max Database Connections [10]: ") or "10"
# Create environment file
env_content = f"""# HANA Cloud MCP Server Environment Configuration
# Generated by setup script
# HANA Cloud Connection
HANA_HOST={host}
HANA_PORT={port}
HANA_USER={user}
HANA_PASSWORD={password}
# Schema Configuration
MODEL_SCHEMA={model_schema}
CONTEXT_SCHEMA={context_schema}
PROTOCOL_SCHEMA={protocol_schema}
# Server Configuration
PORT={port_number}
MAX_CONNECTIONS={max_connections}
"""
# Write to .env file
with open('.env', 'w') as f:
f.write(env_content)
print("\nEnvironment configuration saved to .env file.")
return {
'hana_host': host,
'hana_port': port,
'server_port': port_number
}
def create_sample_protocol(base_url, protocol_type):
"""Create a sample protocol of the specified type"""
if protocol_type == 'inference':
protocol_name = 'sklearn_inference'
implementation = {
'handler_type': 'python',
'active': True,
'description': 'Default scikit-learn inference protocol',
'supported_models': ['classification', 'regression'],
'version': '1.0.0'
}
else: # training
protocol_name = 'sklearn_training'
implementation = {
'handler_type': 'python',
'active': True,
'description': 'Default scikit-learn training protocol',
'supported_models': ['classification', 'regression'],
'version': '1.0.0'
}
payload = {
'protocol_name': protocol_name,
'protocol_type': f'sklearn_{protocol_type}',
'implementation': implementation
}
try:
response = requests.post(f"{base_url}/api/protocols", json=payload)
response.raise_for_status()
print(f"Created sample {protocol_type} protocol: {protocol_name}")
return response.json()
except requests.exceptions.RequestException as e:
print(f"Failed to create sample {protocol_type} protocol: {e}")
return None
def create_sample_data(base_url):
"""Create sample data (models, contexts, and protocols)"""
print("\nCreating sample data for testing...")
# Create sample protocols
inference_protocol = create_sample_protocol(base_url, 'inference')
training_protocol = create_sample_protocol(base_url, 'training')
if not inference_protocol or not training_protocol:
print("Failed to create sample protocols. Skipping sample model and context creation.")
return
# Create sample model
model_payload = {
'model_name': 'sample_classifier',
'model_metadata': {
'version': '1.0.0',
'type': 'classification',
'framework': 'sklearn',
'description': 'Sample classification model for testing',
'features': ['feature1', 'feature2', 'feature3'],
'target': 'target_class',
'metrics': {
'accuracy': 0.92,
'f1_score': 0.91
},
'active': True
}
}
try:
response = requests.post(f"{base_url}/api/models", json=model_payload)
response.raise_for_status()
print(f"Created sample model: {model_payload['model_name']}")
# Create sample context
context_payload = {
'context_name': 'sample_inference_context',
'model_name': 'sample_classifier',
'context_config': {
'type': 'inference',
'batch_size': 100,
'timeout_seconds': 30,
'description': 'Sample inference context for testing',
'active': True
}
}
response = requests.post(f"{base_url}/api/contexts", json=context_payload)
response.raise_for_status()
print(f"Created sample context: {context_payload['context_name']}")
except requests.exceptions.RequestException as e:
print(f"Failed to create sample model or context: {e}")
def test_server_connection(host, port):
"""Test connection to the MCP server"""
try:
base_url = f"http://{host}:{port}"
response = requests.get(f"{base_url}/health")
response.raise_for_status()
print("\nMCP server is running and healthy!")
print(f"Server URL: {base_url}")
return base_url
except requests.exceptions.RequestException:
print("\nCould not connect to MCP server. Make sure the server is running.")
return None
def generate_cursor_config(server_url):
"""Generate Cursor IDE configuration for MCP server"""
cursor_config = {
"name": "HANA Cloud MCP",
"type": "rest",
"base_url": server_url,
"endpoints": {
"models": {
"list": "/api/models",
"get": "/api/models/{model_name}",
"create": "/api/models",
"delete": "/api/models/{model_name}"
},
"contexts": {
"list": "/api/contexts",
"get": "/api/contexts/{context_name}",
"create": "/api/contexts",
"execute": "/api/contexts/{context_name}/execute"
},
"protocols": {
"list": "/api/protocols",
"get": "/api/protocols/{protocol_name}"
}
},
"auth": {
"type": "none" # Update as needed
}
}
# Save to cursor-config.json
with open('cursor-config.json', 'w') as f:
json.dump(cursor_config, f, indent=2)
print("\nCursor IDE configuration saved to cursor-config.json")
print("Import this file in Cursor IDE to connect to your MCP server.")
def main():
parser = argparse.ArgumentParser(description="HANA Cloud MCP Server Setup")
parser.add_argument("--env-only", action="store_true", help="Only setup environment variables")
parser.add_argument("--test-server", action="store_true", help="Test server connection")
parser.add_argument("--create-samples", action="store_true", help="Create sample data")
args = parser.parse_args()
if args.env_only:
setup_environment()
return
if args.test_server:
host = input("Server Host [localhost]: ") or "localhost"
port = input("Server Port [5000]: ") or "5000"
base_url = test_server_connection(host, port)
if base_url and args.create_samples:
create_sample_data(base_url)
generate_cursor_config(base_url)
return
# Full setup
config = setup_environment()
print("\nNext steps:")
print("1. Start the MCP server using 'python mcp_server.py'")
print("2. Run this script with --test-server to test connection and create sample data")
print(" Example: python setup.py --test-server --create-samples")
if __name__ == "__main__":
main()