Resources Server APIs
Resources Server APIs
A Resources Server implements the Environment interface related to: its tools, state, and reward logic.
Agent Server context
The Agent is the behavior that decides what to do next, typically with different components like a Harness and a policy-model. The Agent Server is the HTTP transport for two internal methods:
run()is exposed as/runand implements part of the episode orchestration associated with the Resources Server seed session, delegation of the Agent loop to responses() and call to the verifier.responses()is exposed as/v1/responsesand implements Agent behavior. Depending on the Agent Server, this can be a complete agent harness or one policy-model call.
In simple_agent, run() delegates the Agent loop to responses() before requesting final verification. In gymnasium_agent, run() owns the model-and-environment loop and does not invoke responses() to the tool calling.
Resources Server Protocol Landscape
NeMo Gym ships two framework-standard base protocols for coordinating an episode between the Agent and the Environment. Agent behavior and rollout orchestration are implemented by responses() and run() behind the Agent Server’s HTTP endpoints; the Environment is implemented by the Resources Server.
Each protocol has a matching built-in Agent Server: simple_agent for the /seed_session and /verify interface, and gymnasium_agent for the /reset and /step interface.
Pick one base protocol and build the Resources Server and Agent Server to match. GymnasiumServer exposes /reset and /step and does not expose /verify. SimpleResourcesServer defines /seed_session and /verify but can be extended with additional environment-specific routes.
The two base protocols do not describe every concrete Agent–Environment interface in the repository. Some environments extend the seed/verify protocol with custom transition endpoints. Other environments are embedded inside the Agent and do not use a Resources Server during /run.
Seed/Verify Protocol Extensions
Several environments keep the /seed_session and /verify lifecycle while adding bespoke transition APIs. These extensions require a matching Agent Server; sharing an endpoint name such as /step does not make their request and response contracts interchangeable.
Some Agent Servers also use variants of the seed/verify sequence—for example, skipping a no-op /seed_session, treating it as optional, or calling /verify without tool execution. These are Agent Server behaviors built around the seed/verify API rather than new Resources Server base classes.
Agent-Embedded Environments
Some Agent Servers embed the environment or an external benchmark harness in their own process. Examples include OSWorld, Tau2, SWE wrappers, Harbor, PinchBench, and Verifiers. Their /run implementation computes reward without calling a Resources Server, so they do not implement a Resources Server episode protocol.
Action and Tool Transports
The episode protocol defines initialization, transitions, and scoring. The action transport separately defines how the Agent affects the Environment during the episode.
Seed/Verify Protocol: seed_session() and verify()
The seed/verify protocol brackets the Agent harness with environment initialization and final verification:
For the built-in simple_agent, run() performs the lifecycle orchestration and responses() implements the Agent loop. The Agent Server exposes those methods as /run and /v1/responses, respectively.
Lifecycle API
SimpleResourcesServer exposes:
bodyinseed_session()contains fields used to initialize an episode. Define aBaseSeedSessionRequestsubclass for fields such as an initial board, task ID, or sandbox image. The default implementation is a no-op.body.responses_create_paramsinverify()is the original model request.body.responseinverify()is the completedNeMoGymResponse, including the trajectory assembled by the Agent Server.- Custom task metadata requires a
BaseVerifyRequestsubclass. Declare fields such asanswerorexpected_state; undeclared fields are not retained by the base Pydantic model. request: Request, when added to either method signature, provides access to the per-rollout session ID.
Direct HTTP Tool-Calling Protocol
The seed/verify episode protocol does not itself define how the Agent acts on the environment. simple_agent supplies a direct HTTP function-tool protocol inside responses():
- The input row declares function schemas in
responses_create_params.tools. - The Agent Server sends those schemas to the Model Server.
- The model returns a structured
function_callwithname,arguments, andcall_id. simple_agentparsesargumentsand sendsPOST /{name}to the Resources Server.- The Resources Server’s response becomes a
function_call_outputassociated with the samecall_id. responses()sends the updated trajectory back to the model and repeats until the model returns a final response or the step limit is reached.
The tool name in the input schema must match the Resources Server route:
For a model response such as:
simple_agent calls POST /square with {"value": 4}. The Agent Server performs this dispatch; the Resources Server only implements the endpoint.
Resources Server tools can also be exposed over MCP. MCP changes the tool transport, not the episode protocol: the rollout can still use /seed_session and /verify. See MCP Resources Server.
Minimal Seed/Verify Environment
The complete server below combines the tool route with final trajectory verification:
Choose the seed/verify protocol when:
- The Agent harness should own model calls, tool dispatch, retries, or other orchestration.
- Resources Server routes should be exposed directly as model tools.
- One final reward should be computed from the completed trajectory or final environment state.
- You are pairing the environment with
simple_agentor a custom Agent Server that implements/seed_session, an Agent harness, and/verify.
Gymnasium Protocol: reset() and step()
The Gymnasium protocol models the environment as a transition function. The Agent Server sends every model response to the Resources Server:
gymnasium_agent never calls /verify. GymnasiumServer does not register a /verify endpoint; /step provides both transitions and rewards.
Transition API
Subclass GymnasiumServer and implement step(). Override reset() when the environment needs initialization or an initial observation.
actionis the complete structured model response for the current step. It can contain assistant messages, reasoning items, or function calls.metadatacontains extra top-level fields from the input row, such asanswer,category, or task settings. It does not includeresponses_create_params.session_ididentifies the rollout and keys per-episode state. Stateless environments can ignore it.
reset() returns (observation, info). A non-None observation is appended as a user message before the first model call.
step() returns (observation, reward, terminated, truncated, info):
observation: the next user message, orNonewhen no message is needed.reward: reward for this step.gymnasium_agentsums rewards across the episode.terminated:Truewhen the episode reaches a natural terminal state.truncated:Truewhen a limit or timeout cuts the episode short.info: diagnostic metadata and optionaltool_outputsfor the Agent Server.
Tool Calls as Gymnasium Actions
gymnasium_agent does not dispatch a function call to POST /{name}. Instead:
- The model returns a
NeMoGymResponse. - The Agent Server sends that response to
/stepasaction. step()interprets text or structured function calls and executes the corresponding environment operation.- For function calls,
step()returns results ininfo["tool_outputs"]. - The Agent Server converts those results into
function_call_outputitems for the next model call.
Tool schemas still come from responses_create_params.tools, but they describe the actions the model can emit. The Resources Server does not need matching HTTP tool routes because step() performs the dispatch.
Choose the Gymnasium protocol when:
- The Resources Server must process every model response before the episode continues.
- The environment decides the next observation and stopping condition.
- Rewards are naturally assigned per transition.
- The model response itself is the environment action.
- You are pairing the environment with
gymnasium_agentor a custom Agent Server that implements the/resetand/steploop.
State Management
Both episode protocols use the Resources Server’s session middleware. It assigns a unique session_id cookie, and the Agent Server preserves that cookie across Resources Server calls in the same rollout.
Store rollout-specific state under the session ID so concurrent episodes remain isolated. State can range from:
- Stateless: grade an answer directly from the response and task metadata.
- Lightweight: store a counter, game board, or conversation state in memory.
- Heavyweight: associate each session with a browser, database, or Docker container.
Seed/Verify State
SimpleResourcesServer does not define a state container. Add one and read the session ID from the FastAPI request:
Tool endpoints can read the same request.session[SESSION_ID_KEY], so /seed_session, tool calls, and /verify operate on the same environment instance.
Gymnasium State
GymnasiumServer provides self.session_state and passes the session ID to reset() and step():
Do not put rollout-specific mutable data in an unkeyed server attribute.
Session Cleanup
- Seed/verify protocol: remove custom state in
verify(), preferably in afinallyblock.SimpleResourcesServerdoes not automatically clear custom state. - Gymnasium protocol: when
step()returnsterminated=Trueortruncated=True,GymnasiumServercallsclose_session()and removes the entry fromself.session_state. - Heavyweight state: override
close_session(), release the external resource, and callawait super().close_session(session_id).
Ensure a Gymnasium environment returns a terminal or truncated result before the Agent Server’s max_steps is exhausted. Also use timeouts or stale-session reclamation so client failures cannot leak external resources indefinitely.
YAML Configuration
The Resources Server definition has the same YAML shape for both episode protocols. The paired Agent Server determines which protocol is used.
Seed/verify
Gymnasium
The Agent Server’s resources_server.name must match the top-level Resources Server instance name.
Next Steps
- Build a seed/verify environment in Single-Step Environment.
- Add per-rollout state in Stateful Environment.
- Expose Resources Server tools through MCP Resources Server.
- Review common reward designs in Verification Patterns.
- See a multi-step Gymnasium implementation in
resources_servers/blackjack.