Files
copilotkit__copilotkit/showcase/starters/ms-agent-python/agent_server.py
Jordan Ritter 59c073c610 fix: add agent_server.py to all Python starters, matching demo packages
Python starters were trying to run uvicorn with agent.<module>:app but
those modules don't export a FastAPI app. The demo packages use
agent_server.py as the FastAPI wrapper, so starters need it too.

Changes:
- Copy agent_server.py from each demo package into starter root,
  rewriting "from agents." to "from agent." for the starter layout
- Update all non-langgraph Python devScripts to use agent_server:app
- Update Dockerfile.python to COPY agent_server.py for non-langgraph
- Update getEntrypointBlock() generic Python to use agent_server:app
- Make langgraph-fastapi use langgraph_cli dev like langgraph-python
  (it was incorrectly configured as a uvicorn-based starter)
- Regenerate all starters
2026-04-14 15:41:34 -07:00

74 lines
1.8 KiB
Python
Generated

"""
Agent Server for MS Agent Framework (Python)
FastAPI server that hosts the Microsoft Agent Framework agent backend.
The Next.js CopilotKit runtime proxies requests here via AG-UI protocol.
"""
from __future__ import annotations
import os
import uvicorn
from agent_framework import BaseChatClient
from agent_framework.openai import OpenAIChatClient
from agent_framework_ag_ui import add_agent_framework_fastapi_endpoint
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from agent.agent import create_agent
load_dotenv()
def _build_chat_client() -> BaseChatClient:
try:
if bool(os.getenv("OPENAI_API_KEY")):
return OpenAIChatClient(
model=os.getenv("OPENAI_CHAT_MODEL_ID", "gpt-4o-mini"),
api_key=os.getenv("OPENAI_API_KEY"),
)
raise ValueError("OPENAI_API_KEY environment variable is required")
except Exception as exc:
raise RuntimeError(
"Unable to initialize the chat client. Double-check your API credentials."
) from exc
chat_client = _build_chat_client()
my_agent = create_agent(chat_client)
app = FastAPI(title="CopilotKit + Microsoft Agent Framework (Python)")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
add_agent_framework_fastapi_endpoint(
app=app,
agent=my_agent,
path="/",
)
@app.get("/health")
async def health():
return {"status": "ok"}
def main():
"""Run the uvicorn server."""
host = os.getenv("AGENT_HOST", "0.0.0.0")
port = int(os.getenv("AGENT_PORT", "8000"))
uvicorn.run("agent_server:app", host=host, port=port, reload=True)
if __name__ == "__main__":
main()