# Async graph creation function

**URL:** <https://forum.langchain.com/t/async-graph-creation-function/4767>\
**Category:** LangChain\
**Tags:** python-help\
**Created:** [October 16, 2026, 1:46pm UTC](https://forum.langchain.com/t/async-graph-creation-function/4767 "2026-10-16T13:46:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric-burel](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/eric-burel/32/712_2.png) [@eric-burel](https://forum.langchain.com/u/eric-burel)\
**Post date:** [October 16, 2026, 1:46pm UTC](https://forum.langchain.com/t/async-graph-creation-function/4767/1 "2026-10-16T13:46:31Z")

</div>

Hey folks, I fail to figure how you’re expected to build a graph from an async function, in the context of a LangGraph app.

Documentation often shows using `ainvoke` etc. but when creating an actual LangGraph Server you don’t control how the graph is invoked, what you do is exposing a graph object from a module.

```auto
graph = create_agent(...) # OK
graph = await build_agent(...) # How to do that? I can't await from a module top-level code

```

This is needed for instance to get the tools from an MCPAdapter ( [Connections - Docs by LangChain](https://docs.langchain.com/oss/python/langchain/mcp/connections) ). I couldn’t find a proper answer in the Async LangGraph guide ( [Use the graph API - Docs by LangChain](https://docs.langchain.com/oss/python/langgraph/use-graph-api#async) ).

I’ve tried a hacky setup with `asyncio.run` as recommended by AI but it didn’t work, see PR here: [mcp support by eric-burel · Pull Request #1 · lbke/langgraph-simple-agent-openrouter · GitHub](https://github.com/lbke/langgraph-simple-agent-openrouter/pull/1/changes)

---

<div class="post-metadata">

**Author:** ![keenborder786](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/keenborder786/32/1757_2.png) [@keenborder786](https://forum.langchain.com/u/keenborder786)\
**Post date:** [October 16, 2026, 10:28pm UTC](https://forum.langchain.com/t/async-graph-creation-function/4767/2 "2026-10-16T22:28:35Z")

</div>

Hi @eric-burel, you don’t need to await at module top level. LangGraph Server accepts a graph factory function in `langgraph.json`, and that function can be async. The server calls it for you, so you can `await` inside it.

```python
import contextlib
from langchain.agents import create_agent
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph_sdk.runtime import ServerRuntime

client = MultiServerMCPClient({
    "my_server": {"transport": "streamable_http", "url": "http://localhost:8000/mcp"}
})

@contextlib.asynccontextmanager
async def make_graph(runtime: ServerRuntime):
    if runtime.execution_runtime:
        tools = await client.get_tools()
    else:
        tools = [] # same topology, no MCP call for schema/state reads
    yield create_agent("openai:gpt-4.1", tools)

```

Langraph.json

```json

{
  "dependencies": ["."],
  "graphs": {
    "agent": "./agent.py:make_graph"
  }
}

```

Please checkout the following: [Rebuild graph at runtime - Docs by LangChain](https://docs.langchain.com/langsmith/graph-rebuild#simple-factory)

---

<div class="post-metadata">

**Author:** ![eric-burel](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/eric-burel/32/712_2.png) [@eric-burel](https://forum.langchain.com/u/eric-burel)\
**Post date:** [October 19, 2026, 3:32pm UTC](https://forum.langchain.com/t/async-graph-creation-function/4767/3 "2026-10-19T15:32:45Z")

</div>

Hey @keenborder786 , thanks a lot for pointing me in the right direction, I figured that you need the complete “context manager factory” as you pointed out. This is not super intuitive especially since you can’t just pass an async function and must handle context properly, so I’ve PRed the doc with a proper link in the MCP page : [Update MCP documentation for Agent Server by eric-burel · Pull Request #6838 · langchain-ai/docs · GitHub](https://github.com/langchain-ai/docs/pull/6838)
