KChad/Prompt-Injection-RL-environment
1
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""8Echo Environment Client.9 10This module provides the client for connecting to an Echo Environment server.11EchoEnv extends MCPToolClient to provide tool-calling style interactions.12 13Example:14 >>> with EchoEnv(base_url="http://localhost:8000") as env:15 ... env.reset()16 ...17 ... # Discover tools18 ... tools = env.list_tools()19 ... print([t.name for t in tools]) # ['echo_message', 'echo_with_length']20 ...21 ... # Call tools22 ... result = env.call_tool("echo_message", message="Hello!")23 ... print(result) # "Hello!"24 ...25 ... result = env.call_tool("echo_with_length", message="Test")26 ... print(result) # {"message": "Test", "length": 4}27"""28 29from openenv.core.mcp_client import MCPToolClient30 31 32class EchoEnv(MCPToolClient):33 """34 Client for the Echo Environment.35 36 This client provides a simple interface for interacting with the Echo37 Environment via MCP tools. It inherits all functionality from MCPToolClient:38 - `list_tools()`: Discover available tools39 - `call_tool(name, **kwargs)`: Call a tool by name40 - `reset(**kwargs)`: Reset the environment41 - `step(action)`: Execute an action (for advanced use)42 43 Example:44 >>> # Connect to a running server45 >>> with EchoEnv(base_url="http://localhost:8000") as env:46 ... env.reset()47 ...48 ... # List available tools49 ... tools = env.list_tools()50 ... for tool in tools:51 ... print(f"{tool.name}: {tool.description}")52 ...53 ... # Echo a message54 ... result = env.call_tool("echo_message", message="Hello!")55 ... print(result) # "Hello!"56 ...57 ... # Echo with length58 ... result = env.call_tool("echo_with_length", message="Test")59 ... print(result) # {"message": "Test", "length": 4}60 61 Example with Docker:62 >>> # Automatically start container and connect63 >>> env = EchoEnv.from_docker_image("echo-env:latest")64 >>> try:65 ... env.reset()66 ... tools = env.list_tools()67 ... result = env.call_tool("echo_message", message="Hello!")68 ... finally:69 ... env.close()70 71 Example with HuggingFace Space:72 >>> # Run from HuggingFace Space73 >>> env = EchoEnv.from_env("openenv/echo-env")74 >>> try:75 ... env.reset()76 ... result = env.call_tool("echo_message", message="Hello!")77 ... finally:78 ... env.close()79 """80 81 pass # MCPToolClient provides all needed functionality82 