How should I provide an agent to a LangGraph server?
@pawel-tward
·
2026-04-23
·
via LangChain Forum - Latest posts
hi @wigging How to Provide an Agent to a LangGraph Server The user’s concern is valid - their global caching is necessary with a plain async factory function, because the server calls the factory for every request , including schema introspection (Studio refresh, get_graph , get_schema ), state reads, and actual execution. Without caching they’d pay the Azure OpenAI + MCP initialization cost on every introspection call. However, there’s a better, officially documented pattern. How the server loads your graph From langgraph_cli/schemas.py , the graphs field supports three forms: Module-level compiled object - imported once at server startup, never called again Async context manager factory - called per-request, receives RunnableConfig (legacy) or ServerRuntime (modern) Async function factory - same as above, but returns instead of yielding The server calls your factory in 4 contexts : threads.create_run (actual execution), threads.update , threads.read (state history, used by Studio’s useStream ), and assistants.read (schema introspection). This is why the caching is needed with a plain factory. The recommended modern pattern: ServerRuntime (server v0.7.30+) From langgraph_sdk/runtime.py : import contextlib from langchain.agents import create_agent from langchain_openai import AzureChatOpenAI from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph_sdk.runtime import ServerRuntime llm = AzureChatOpenAI( azure_deployment="your-deployment", azure_endpoint="https://...", api_version="2024-02-01", ) # Lightweight agent for introspection - no MCP connection needed _base_agent = create_agent(llm, tools=[]) @contextlib.asynccontextmanager async def get_agent(runtime: ServerRuntime): if runtime.execution_runtime: # Only connect to MCP during actual runs async with MultiServerMCPClient({...}) as mcp: tools = await mcp.get_tools() yield create_agent(llm, tools=tools) else: # Schema reads, Studio refresh - skip expensive MCP setup yield _base_agent langgraph.json : { "graphs": { "joker_agent": { "path": "./src/joker_agent.py:get_agent", "description": "Joker agent with Azure OpenAI and MCP tools" } } } Why this is better than the global cache Concern Global cache ServerRuntime factory Avoids re-init on every call Yes (cached) Yes ( execution_runtime guard) Handles MCP disconnects No (connection held forever) Yes (fresh per run, teardown after yield) Skips MCP during introspection No Yes Proper cleanup No Yes (code after yield ) The global caching pattern is fragile for MCP specifically: connections can time out while the server keeps running, and the cached agent won’t reconnect. The ServerRuntime context manager solves this by connecting fresh per execution and tearing down cleanly. When to use each pattern Scenario Recommended pattern No async init (sync tools only) Module-level object: graph = create_agent(...) MCP tools, async resources ServerRuntime async context manager (v0.7.30+) Older server, async resources RunnableConfig async context manager Per-user graph customization ServerRuntime factory using runtime.ensure_user()
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。