OpenEnv icon indicating copy to clipboard operation
OpenEnv copied to clipboard

Enforce episode step and time limits for direct MCP tool calls

Open burtenshaw opened this issue 1 month ago • 3 comments

Episode step and wall-clock limits currently apply through the Gym/WebSocket step path, but direct tool calls through /mcp bypass that accounting. In posttrain_env, repeated call_tool calls leave step_count at zero, and do not deliver the final step observation carrying reward and service_spend. The broker budget still applies.

Requested by @sergiopaniego in https://github.com/openenv/OpenEnv/pull/1500#pullrequestreview-5670918419 (point 5). The experimental env remains private and documents step(CallToolAction(...)) as its orchestration path.

Follow-up scope:

  • Define and enforce consistent episode step/time limits for direct MCP and step-driven tool calls, without double counting step dispatch.
  • End the episode and clean up live work when a limit is reached; preserve final reward/spend reporting for orchestration.
  • Add regression coverage for direct MCP, step dispatch, and calls after episode end, without real provider spend.
  • Keep reset/state/simulation controls outside the agent MCP surface.

This is episode lifecycle enforcement, separate from request rate limiting and IP filtering in #1360. No core change is proposed in #1500; coordinate the design with RFC 014 before implementation.

burtenshaw avatar Oct 27 '26 16:10 burtenshaw

I'd like to help with this follow-up. On main at 3dfda7df, direct MCP tool dispatch can bypass the environment's step/step_async overrides; in posttrain_env, those overrides perform episode accounting and attach the final reward and service_spend observation. This is a source trace, not a runtime reproduction.

Before implementation, to coordinate with RFC 014:

  1. Should direct MCP calls and step(CallToolAction(...)) share one environment-owned lifecycle path, counting each tool call once and enforcing both step and wall-clock limits, including cleanup and calls after episode end?
  2. How should orchestration receive the terminal observation when a direct MCP call ends the episode? Should the MCP tool result retain its existing shape, with the final observation retained for infrastructure, or is another contract preferred? I would keep reset/state/simulation controls off the agent MCP surface.

The validation plan would cover direct MCP, step dispatch, no double counting, both limits, cleanup and post-end calls, using real local client/server transport and offline fixtures without provider spend. Nothing is implemented or tested yet. Happy to follow your preferred design.

leninworld avatar Oct 28 '26 20:10 leninworld

A concrete proposal for the two questions @leninworld raised, from a read of main at 1f9a7638.

This isn't limited to posttrain_env. Several MCP environments do their episode accounting in step/step_async, and direct /mcp calls skip all of it:

  • finqa_env: the max_steps cutoff and returning the final observation after the episode ends
  • tau2_env: _finish, which sets done and reward
  • coding_tools_env: done and reward after submit
  • posttrain_env: the step and time limits, service_spend and episode_end

1. One lifecycle path. In the /mcp tools/call handler (http_server.py, around lines 1474–1560), when the environment overrides step or step_async, send the call through the existing call_mcp_style_step(CallToolAction(...)). That's the same dispatch WebSocket step uses, and the handler already uses it at around line 1511 for environments without an MCP client. Environments that don't override step keep today's direct dispatch, so nothing changes for them. Each tool call runs through step exactly once, so nothing is counted twice, and tools/list stays uncounted. Core adds no limits of its own: limits, cleanup and calls after the end stay with the environment, as posttrain_env._finish and finqa_env._final_observation already handle them.

2. Terminal observation. Keep result as the tool result, unchanged. When the step's observation has done=True, also return done, reward and metadata (service_spend, episode_end) under the result's _meta, for example _meta["openenv"]. MCP allows _meta on results, so existing clients ignore it. The other option is to leave the /mcp response untouched and have orchestration read the final observation the environment keeps, through step or /state.

Questions before anyone builds this:

  1. Should this be automatic for every environment that overrides step, or opt-in through a class attribute? Automatic fixes the four environments above in one change. Opt-in is narrower.
  2. _meta or the kept observation? _meta shows the reward to the agent on its last call. The episode is over by then, but if you'd rather keep reward off the agent's MCP surface entirely, the kept observation avoids that.
  3. RFC 014 lists "changing /mcp" as out of scope. Is a dispatch change that keeps the wire shape (plus an optional _meta) acceptable here, or does it need an RFC 003 amendment?

Tests would use a small test-only MCP environment with max_steps=2 and a fake clock, driven through the real FastAPI server, with no provider calls. They'd cover direct /mcp calls, WebSocket step, both mixed in one episode (no double counting), both limits, calls after the end and tools/list.

@leninworld offered first. If you're working on it, I'm glad to review. If not, I can open the PR once the questions above are settled.

Aafimalek avatar Nov 22 '26 07:11 Aafimalek

Thanks @Aafimalek, that matches what I traced, and the list of affected environments is helpful. I'm working on the implementation and will open a PR.

I'll keep the first PR to part 1: in the /mcp tools/call handler, when the environment overrides MCPEnvironment.step or step_async, send the call through call_mcp_style_step(CallToolAction(...)). Environments that don't override step keep today's direct dispatch. The /mcp response stays as it is, so the wire shape doesn't change. I'll leave the terminal-observation question (_meta or the kept observation) for a follow-up once the maintainers pick one.

Tests will follow your outline: a small test-only MCP environment with max_steps=2 and a fake clock, driven through the FastAPI server, covering direct /mcp calls, WebSocket step, both mixed in one episode without double counting, calls after the end and tools/list. Your review would be very welcome.

leninworld avatar Nov 22 '26 16:11 leninworld