|
| 1 | +""" |
| 2 | +Example: Deploying a Django REST Framework application as an MCP server. |
| 3 | +
|
| 4 | +This example shows how to deploy a Django REST Framework application |
| 5 | +using the Tadata SDK. This is a complete working example that sets up |
| 6 | +a minimal Django configuration with drf-spectacular. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import django |
| 11 | +from django.conf import settings |
| 12 | +from tadata_sdk import deploy |
| 13 | + |
| 14 | + |
| 15 | +def setup_django(): |
| 16 | + """Set up a minimal Django configuration for demonstration.""" |
| 17 | + if not settings.configured: |
| 18 | + settings.configure( |
| 19 | + DEBUG=True, |
| 20 | + SECRET_KEY="demo-secret-key-not-for-production", |
| 21 | + INSTALLED_APPS=[ |
| 22 | + "django.contrib.contenttypes", |
| 23 | + "django.contrib.auth", |
| 24 | + "rest_framework", |
| 25 | + "drf_spectacular", |
| 26 | + ], |
| 27 | + REST_FRAMEWORK={ |
| 28 | + "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", |
| 29 | + }, |
| 30 | + SPECTACULAR_SETTINGS={ |
| 31 | + "TITLE": "Demo Django API", |
| 32 | + "DESCRIPTION": "A demonstration Django REST Framework API", |
| 33 | + "VERSION": "1.0.0", |
| 34 | + }, |
| 35 | + ROOT_URLCONF=__name__, # Use this module as the URL config |
| 36 | + USE_TZ=True, |
| 37 | + ) |
| 38 | + django.setup() |
| 39 | + |
| 40 | + |
| 41 | +def create_demo_api(): |
| 42 | + """Create a simple Django REST API for demonstration.""" |
| 43 | + from django.urls import path, include |
| 44 | + from rest_framework import serializers, viewsets, routers |
| 45 | + from rest_framework.decorators import api_view |
| 46 | + from rest_framework.response import Response |
| 47 | + |
| 48 | + # Simple serializer |
| 49 | + class ItemSerializer(serializers.Serializer): |
| 50 | + id = serializers.IntegerField() |
| 51 | + name = serializers.CharField(max_length=100) |
| 52 | + description = serializers.CharField(max_length=500, required=False) |
| 53 | + |
| 54 | + # Simple viewset |
| 55 | + class ItemViewSet(viewsets.ViewSet): |
| 56 | + """A simple ViewSet for managing items.""" |
| 57 | + |
| 58 | + def list(self, request): |
| 59 | + """List all items.""" |
| 60 | + items = [ |
| 61 | + {"id": 1, "name": "Item 1", "description": "First item"}, |
| 62 | + {"id": 2, "name": "Item 2", "description": "Second item"}, |
| 63 | + ] |
| 64 | + serializer = ItemSerializer(items, many=True) |
| 65 | + return Response(serializer.data) |
| 66 | + |
| 67 | + def retrieve(self, request, pk=None): |
| 68 | + """Retrieve a specific item.""" |
| 69 | + item = {"id": int(pk), "name": f"Item {pk}", "description": f"Item number {pk}"} |
| 70 | + serializer = ItemSerializer(item) |
| 71 | + return Response(serializer.data) |
| 72 | + |
| 73 | + # Simple API view |
| 74 | + @api_view(["GET"]) |
| 75 | + def hello_world(request): |
| 76 | + """A simple hello world endpoint.""" |
| 77 | + return Response({"message": "Hello from Django!"}) |
| 78 | + |
| 79 | + # Setup URL routing |
| 80 | + router = routers.DefaultRouter() |
| 81 | + router.register(r"items", ItemViewSet, basename="item") |
| 82 | + |
| 83 | + # URL patterns (this module serves as ROOT_URLCONF) |
| 84 | + global urlpatterns |
| 85 | + urlpatterns = [ |
| 86 | + path("api/", include(router.urls)), |
| 87 | + path("hello/", hello_world, name="hello"), |
| 88 | + ] |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + """Deploy Django REST Framework application.""" |
| 93 | + print("Setting up Django configuration...") |
| 94 | + setup_django() |
| 95 | + |
| 96 | + print("Creating demo API...") |
| 97 | + create_demo_api() |
| 98 | + |
| 99 | + print("Django setup complete! Now deploying to Tadata...") |
| 100 | + |
| 101 | + # Get API key (you would set this in your environment) |
| 102 | + api_key = os.getenv("TADATA_API_KEY") |
| 103 | + if not api_key: |
| 104 | + print("⚠️ TADATA_API_KEY environment variable not set.") |
| 105 | + print(" For a real deployment, you would need to set this.") |
| 106 | + print(" For this demo, we'll show what the call would look like:") |
| 107 | + print() |
| 108 | + print(" result = deploy(") |
| 109 | + print(" use_django=True,") |
| 110 | + print(" api_key='your-api-key-here',") |
| 111 | + print(" name='my-django-api',") |
| 112 | + print(" base_url='https://api.example.com'") |
| 113 | + print(" )") |
| 114 | + print() |
| 115 | + print("Let's test the Django schema extraction instead...") |
| 116 | + |
| 117 | + # Test the schema extraction without actually deploying |
| 118 | + from tadata_sdk.openapi.source import OpenAPISpec |
| 119 | + |
| 120 | + try: |
| 121 | + spec = OpenAPISpec.from_django() |
| 122 | + print("✅ Django schema extraction successful!") |
| 123 | + print(f" API Title: {spec.info.title}") |
| 124 | + print(f" API Version: {spec.info.version}") |
| 125 | + print(f" Available paths: {list(spec.paths.keys())}") |
| 126 | + print() |
| 127 | + print("This OpenAPI specification would be deployed to Tadata as an MCP server.") |
| 128 | + except Exception as e: |
| 129 | + print(f"❌ Schema extraction failed: {e}") |
| 130 | + return |
| 131 | + |
| 132 | + # Deploy using Django schema extraction |
| 133 | + try: |
| 134 | + result = deploy( |
| 135 | + use_django=True, # Extract schema from configured Django application |
| 136 | + api_key=api_key, |
| 137 | + base_url="https://api.example.com", # Your Django API base URL |
| 138 | + ) |
| 139 | + |
| 140 | + print("✅ Deployment successful!") |
| 141 | + print(f" MCP Server ID: {result.id}") |
| 142 | + print(f" Created at: {result.created_at}") |
| 143 | + if result.updated: |
| 144 | + print(" Status: New deployment created") |
| 145 | + else: |
| 146 | + print(" Status: No changes detected, deployment skipped") |
| 147 | + |
| 148 | + except Exception as e: |
| 149 | + print(f"❌ Deployment failed: {e}") |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments