Skip to content

Commit cb78c5c

Browse files
committed
feat(chat): expose mcp for whole chat api under /mcp/sse
- uses sse as transport
1 parent 88b4628 commit cb78c5c

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

‎src/askui/chat/api/app.py‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,32 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: # noqa: ARG001
3838
dependencies=[SetEnvFromHeadersDep],
3939
)
4040

41-
# Add CORS middleware
42-
app.add_middleware(
43-
CORSMiddleware,
44-
allow_origins=["*"],
45-
allow_credentials=True,
46-
allow_methods=["*"],
47-
allow_headers=["*"],
48-
)
41+
42+
# Include routers
43+
v1_router = APIRouter(prefix="/v1")
44+
v1_router.include_router(assistants_router)
45+
v1_router.include_router(threads_router)
46+
v1_router.include_router(messages_router)
47+
v1_router.include_router(runs_router)
48+
v1_router.include_router(mcp_configs_router)
49+
v1_router.include_router(files_router)
50+
v1_router.include_router(health_router)
51+
app.include_router(v1_router)
52+
53+
mcp = FastMCP.from_fastapi(app=app, name="AskUI Chat MCP")
54+
mcp_app = mcp.http_app("/sse", transport="sse")
55+
56+
57+
@asynccontextmanager
58+
async def combined_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
59+
async with lifespan(app):
60+
async with mcp_app.lifespan(app):
61+
yield
62+
63+
64+
app = FastAPI(title=app.title, version=app.version, lifespan=combined_lifespan)
65+
app.mount("/mcp", mcp_app)
66+
app.include_router(v1_router)
4967

5068

5169
@app.exception_handler(NotFoundError)
@@ -103,13 +121,10 @@ def catch_all_exception_handler(
103121
)
104122

105123

106-
# Include routers
107-
v1_router = APIRouter(prefix="/v1")
108-
v1_router.include_router(assistants_router)
109-
v1_router.include_router(threads_router)
110-
v1_router.include_router(messages_router)
111-
v1_router.include_router(runs_router)
112-
v1_router.include_router(mcp_configs_router)
113-
v1_router.include_router(files_router)
114-
v1_router.include_router(health_router)
115-
app.include_router(v1_router)
124+
app.add_middleware(
125+
CORSMiddleware,
126+
allow_origins=["*"],
127+
allow_credentials=True,
128+
allow_methods=["*"],
129+
allow_headers=["*"],
130+
)

0 commit comments

Comments
 (0)