Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Langchain ai Langgraph Server Runtime Context

From Leeroopedia
Attribute Value
Type Principle
Knowledge Sources LangGraph
Domains SDK, Runtime, Server, Authentication
Last Updated 2026-02-11 15:00 GMT

Overview

Server runtime context provides graph factory functions with access context, authenticated user information, persistent storage, and (during execution) the graph run context when the LangGraph Agent Server constructs a graph.

Description

When the LangGraph Agent Server needs to access a graph, it calls a graph factory function and passes a `ServerRuntime` instance that tells the factory why the graph is being constructed and provides the runtime resources it needs. This is distinct from the client-side `Runtime` object injected into nodes -- `ServerRuntime` operates at the factory level, before the graph is even compiled.

The server accesses graphs in four distinct contexts, defined by the `AccessContext` literal type:

  • `"threads.create_run"` -- Full graph execution via `graph.astream`. The graph run context is available.
  • `"threads.update"` -- State update via `graph.aupdate_state`. Nodes and edges do not execute; only channel writers are applied.
  • `"threads.read"` -- State inspection via `graph.aget_state` or `graph.aget_state_history`. The graph structure is used to determine pending tasks.
  • `"assistants.read"` -- Schema and visualization introspection via `graph.aget_graph`, `graph.aget_subgraphs`, or `graph.aget_schemas`. Used by Studio UI and protocol integrations (MCP, A2A).

The `ServerRuntime` type is a union of `_ExecutionRuntime` (for `threads.create_run` contexts) and `_ReadRuntime` (for all other contexts). Both share a common base that provides `access_context`, `user` (the authenticated user or `None`), and `store` (a `BaseStore` instance for persistence). Only `_ExecutionRuntime` additionally provides `context` -- the graph run context typed by the graph's `context_schema`.

The `execution_runtime` property enables safe narrowing: it returns the `_ExecutionRuntime` instance if the current runtime supports execution, or `None` otherwise. The `ensure_user()` method returns the authenticated user or raises `PermissionError` if no custom auth is configured. All runtime instances are frozen, slot-based dataclasses, making them immutable and safe to share.

Usage

Use `ServerRuntime` as the parameter type for graph factory functions deployed on the LangGraph Agent Server. Check `access_context` to conditionally set up expensive resources only during execution. Use the `execution_runtime` property to safely access the graph run context. Use `ensure_user()` to enforce authentication in user-specific graph configurations. The graph topology returned by the factory must be identical regardless of access context, since the server uses the same graph structure for execution, state reads, and introspection.

Theoretical Basis

The `ServerRuntime` design implements the context object pattern from enterprise application architecture: a single object encapsulates all the information a component needs from its runtime environment, reducing the number of parameters that must be passed through call chains.

The distinction between `_ExecutionRuntime` and `_ReadRuntime` via a union type applies algebraic data types to enforce at the type level that certain resources (like the graph run context) are only available in execution scenarios. The `execution_runtime` property provides a type-narrowing accessor that mirrors Rust's `Option` or TypeScript's discriminated union patterns.

The requirement that the graph topology be identical across all access contexts follows the structural consistency principle: the server needs a stable graph structure for schema introspection and state reconstruction, regardless of whether the graph is being executed, read, or visualized. The factory may vary resource setup (connecting tools, initializing clients) but must return topologically equivalent graphs.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment