Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Add CORS Middleware to Resolve XMLHttpRequest Error (#26)

hunteraraujo291f7f2b8556
diff --git a/autogpt/sdk/agent.py b/autogpt/sdk/agent.py
index 2ca3378baf..a97bea423f 100644
--- a/autogpt/sdk/agent.py
+++ b/autogpt/sdk/agent.py
@@ -4,6 +4,7 @@ from uuid import uuid4
from fastapi import APIRouter, FastAPI, UploadFile
from fastapi.responses import FileResponse
+from fastapi.middleware.cors import CORSMiddleware
from hypercorn.asyncio import serve
from hypercorn.config import Config
@@ -35,6 +36,21 @@ class Agent:
version="v0.4",
)
+ # Add CORS middleware
+ origins = [
+ "http://localhost:5000",
+ "http://127.0.0.1:5000",
+ # Add any other origins you want to whitelist
+ ]
+
+ app.add_middleware(
+ CORSMiddleware,
+ allow_origins=origins,
+ allow_credentials=True,
+ allow_methods=["*"],
+ allow_headers=["*"],
+ )
+
app.include_router(router)
app.add_middleware(AgentMiddleware, agent=self)
config.loglevel = "ERROR"