Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/source/environments/posttrain.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ Through `step(CallToolAction(...))`, an episode ends when the agent calls
`submit`, after `max_steps` tool calls (default 100) or after the task's time limit.
The step and time limits give reward 0.

Direct `/mcp` calls, including `env.call_tool`, bypass the step and time limits
and do not return the final step observation with reward and `service_spend`.
Direct `/mcp` calls, including `env.call_tool`, follow the environment's step and
time limits. They still return only the JSON-RPC tool result, not the final step
observation with reward and `service_spend`.

If the score job itself fails, times out or is cancelled, the episode ends
unscored: `reward` is `None` and `episode_end.reason` is `score_failed`, since
Expand Down
2 changes: 1 addition & 1 deletion docs/source/tutorials/mcp-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,4 @@ You will see the discovery call, two tool invocations, and an error case printed
- **MCP in both modes**: [Simulation vs Production](../guides/simulation-vs-production) covers `step()` vs `step_async()`, the `call_tool()` convenience path, and common debugging questions.
- **A richer MCP environment** — [`envs/finqa_env/`](https://github.com/openenv/OpenEnv/tree/main/envs/finqa_env) shows tool calls participating in episode progression, rewards, and terminal submission — not just a stateless echo.
- **Design rationale** — [RFC 003](https://github.com/openenv/OpenEnv/blob/main/rfcs/003-mcp-support.md) explains why OpenEnv picked MCP as the agent boundary and how tool-calling and CodeAct styles share the same plumbing.
- **Serving tools to an external agent** — the `/mcp` JSON-RPC endpoint is available alongside `/ws` on any MCP environment server. Point an MCP-compatible client at it for production inference without going through the step loop. This direct path bypasses reward computation, step counts, and episode termination, and it exposes only registered MCP tools — not `reset`, `step`, or `state`.
- **Serving tools to an external agent** — the `/mcp` JSON-RPC endpoint is available alongside `/ws` on any MCP environment server. Point an MCP-compatible client at it for production inference. When an environment overrides `step_async()`, direct tool calls use that lifecycle for reward computation, step counts, and episode termination; otherwise they retain the direct MCP dispatch. The endpoint exposes only registered MCP tools — not `reset`, `step`, or `state`.
48 changes: 43 additions & 5 deletions src/openenv/core/env_server/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
WebSocket,
WebSocketDisconnect,
)
from fastmcp.client.client import CallToolResult
from pydantic import ValidationError

from ._utils import overrides_method
from .interfaces import Environment
from .mcp_environment import get_server_tools
from .mcp_environment import get_server_tools, MCPEnvironment
from .mcp_types import (
CallToolAction,
CallToolObservation,
Expand Down Expand Up @@ -1481,12 +1482,27 @@ async def call_mcp_style_step(action: Action) -> Observation:
request_id=request_id,
)

if hasattr(_env, "_mode_tools") and any(
obs: Observation | None = None
uses_step_lifecycle = isinstance(
_env, MCPEnvironment
) and overrides_method(_env.step_async, MCPEnvironment.step_async)
is_mode_tool = hasattr(_env, "_mode_tools") and any(
m is not None for m in _env._mode_tools.get(tool_name, {})
):
)
if uses_step_lifecycle:
obs = await call_mcp_style_step(
CallToolAction(tool_name=tool_name, arguments=arguments)
)
elif is_mode_tool:
obs = await _env._async_handle_call_tool(
CallToolAction(tool_name=tool_name, arguments=arguments)
)
if obs is not None:
if not isinstance(obs, CallToolObservation):
return JsonRpcResponse.success(
result=None,
request_id=request_id,
)
if obs.error:
err_code = (
JsonRpcErrorCode.INVALID_PARAMS
Expand All @@ -1502,8 +1518,30 @@ async def call_mcp_style_step(action: Action) -> Observation:
obs.error.message,
request_id=request_id,
)
result = obs.result
if (
uses_step_lifecycle
and not is_mode_tool
and isinstance(result, CallToolResult)
and not result.is_error
):
if None in _env._mode_tools.get(tool_name, {}):
result = result.data
elif result.data is not None:
result = result.data
elif result.structured_content is not None:
structured_content = result.structured_content
fastmcp_meta = (result.meta or {}).get("fastmcp")
if (
isinstance(fastmcp_meta, dict)
and fastmcp_meta.get("wrap_result") is True
):
structured_content = structured_content.get(
"result"
)
result = structured_content
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return JsonRpcResponse.success(
result=_make_json_serializable(obs.result),
result=_make_json_serializable(result),
request_id=request_id,
)

Expand Down Expand Up @@ -2002,7 +2040,7 @@ async def get_schemas() -> SchemaResponse:
async def mcp_endpoint(request_raw: Request) -> Dict[str, Any]:
"""MCP JSON-RPC endpoint for production mode.

Bypasses step() overhead and provides direct access to MCP tools.
Uses an environment's overridden step lifecycle when present.
Supports tools/list and tools/call methods.
"""
# Parse JSON manually to handle parse errors gracefully
Expand Down
Loading