]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blobdiff - skill.py
Add MCP Skill - CORS, Sessionless
[matrix-shop-bot.git] / skill.py
index 355b9b210006db56a81967ecb8524bd6c7d9cc59..bc99032ddda8b11ba82f00f903429e52c5c5f4f5 100644 (file)
--- a/skill.py
+++ b/skill.py
@@ -3,9 +3,13 @@ from matrix_shop_bot.db import Data
 import os
 import uvicorn
 
 import os
 import uvicorn
 
+# CORS
 from starlette.middleware import Middleware
 from starlette.middleware.cors import CORSMiddleware
 from starlette.middleware import Middleware
 from starlette.middleware.cors import CORSMiddleware
-import uvicorn
+
+# HTTP
+from starlette.applications import Starlette
+from starlette.routing import Mount
 
 
 MY_IP=os.environ.get('MY_IP')
 
 
 MY_IP=os.environ.get('MY_IP')
@@ -15,16 +19,24 @@ mcp = FastMCP("Shop Bot")
 def add_item(name: str) -> str:
     return '\n'.join(Data().add_item(name))
 
 def add_item(name: str) -> str:
     return '\n'.join(Data().add_item(name))
 
-# Define custom middleware
-custom_middleware = [
-    Middleware(CORSMiddleware, allow_origins=["*"],
-            allow_methods=["*"],
-            allow_headers=["*"],),
+ 
+# Configure CORS for browser-based clients
+middleware = [
+    Middleware(
+        CORSMiddleware,
+        allow_origins=["*"],  # Allow all origins; use specific origins for security
+        allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
+        allow_headers=[
+            "mcp-protocol-version",
+            "mcp-session-id",
+            "Authorization",
+            "Content-Type",
+        ],
+        expose_headers=["mcp-session-id"],
+    )
 ]
 
 ]
 
-if __name__ == "__main__":
-    http_app = mcp.http_app(transport="streamable-http",path="/",middleware=custom_middleware)
-    uvicorn.run(http_app, host=MY_IP, port=9000)
+# if __name__ == "__main__":
 
 
 
 
 
 
@@ -62,5 +74,31 @@ if __name__ == "__main__":
 #def list_purchases(store: str) -> 'list[str]':
 #    return Data().list_purchases(store)
 
 #def list_purchases(store: str) -> 'list[str]':
 #    return Data().list_purchases(store)
 
-# if __name__ == "__main__":
-#    mcp.run(transport="http", host=MY_IP, port=9000)
+if __name__ == "__main__":
+#   mcp.run(
+#           transport="http", 
+#           host=MY_IP, 
+#           stateless_http=True,
+#           port=9000)
+
+
+       # Create the ASGI app
+    mcp_app = mcp.http_app(
+            path="/mcp",
+            # middleware=middleware
+            )
+
+    # Create a Starlette app and mount the MCP server
+    app = Starlette(
+        routes=[
+            Mount("/mcp-server", app=mcp_app),
+            # Add other routes as needed
+        ],
+        lifespan=mcp_app.lifespan,
+    )
+
+    # http_app = mcp.http_app(transport="streamable-http",path="/",middleware=custom_middleware)
+    http_app = mcp.http_app(transport="streamable-http",path="/",middleware=middleware)
+    # http_app = mcp.http_app(transport="streamable-http",path="/")
+    uvicorn.run(http_app, host=MY_IP, port=9000)
+