Skip to content

How to use async graph with the add_fastapi_endpoint method? #177

@About-Gleaning

Description

@About-Gleaning

I'm a beginner Python developer working on a project using FastAPI and LangGraph. I encountered an issue while integrating ag-ui:
My graph uses MCP tools, which makes my graph asyn
How can I use an async graph with the add_fastapi_endpoint method?

Here's part of my code - is my solution correct?"

async def get_graph():
    client = MultiServerMCPClient({
        "mcp-server": {
            "url": "http://127.0.0.1:8080/sse",
            "transport": "sse"
        }
    })
    mcp_tools = await client.get_tools()
    ...
    graph_builder = StateGraph(SupplierState)
   ...
    graph = graph_builder.compile(name="demo-graph", checkpointer=checkpointer)
  
    return graph

main.py

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    graph = await get_graph()
    sdk = CopilotKitRemoteEndpoint(
        agents=[
            LangGraphAgent(
                name="demo-graph",
                description="my graph",
                graph=graph
            ),
        ],
    )
    add_fastapi_endpoint(app, sdk, "/copilotkit", use_thread_pool=False)

What is the correct way to register an async graph as a FastAPI endpoint?"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions