Skip to content

[RFC 003] Add MCP (Model Context Protocol) support - Phase 1#290

Closed
Darktex wants to merge 2 commits into
mainfrom
rfc-003-mcp-support
Closed

[RFC 003] Add MCP (Model Context Protocol) support - Phase 1#290
Darktex wants to merge 2 commits into
mainfrom
rfc-003-mcp-support

Conversation

@Darktex

@Darktex Darktex commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR implements RFC 003: MCP (Model Context Protocol) support for OpenEnv - Phase 1.

  • Add strongly-typed MCP types: Tool, ToolError, ListToolsAction, CallToolAction, ListToolsObservation, CallToolObservation
  • Add MCPEnvironment base class for MCP-enabled environments with reserved tool name validation
  • Add WebSocket MCP message handler for JSON-RPC protocol (tools/list, tools/call)
  • Update echo_env as reference implementation with FastMCP tools

Key Design Decisions

  1. WebSocket-only transport: MCP uses type: "mcp" WebSocket messages (no HTTP /mcp endpoint)
  2. Per-environment MCP client: Each environment instance has its own MCP client
  3. Dual API support: step(ListToolsAction/CallToolAction) for training, WebSocket /mcp for production
  4. Reserved tool names: Hard error if tools named reset, step, state, or close are registered (protects dual API boundary)

Files Changed

Core MCP Infrastructure:

  • src/openenv/core/env_server/mcp_types.py - Tool, ToolError, actions/observations, WS message types
  • src/openenv/core/env_server/mcp_environment.py - MCPEnvironment base class
  • src/openenv/core/env_server/http_server.py - WebSocket MCP handler
  • src/openenv/core/env_server/__init__.py - Exports

Echo Environment Reference:

  • envs/echo_env/server/mcp_server.py - FastMCP tool definitions
  • envs/echo_env/server/echo_environment.py - MCP action support

Tests:

  • tests/core/test_mcp/test_mcp_types.py - 18 tests
  • tests/core/test_mcp/test_mcp_environment.py - 12 tests

Test plan

  • All 30 MCP tests pass: PYTHONPATH=src:envs pytest tests/core/test_mcp/ -v
  • MCP imports work correctly from package
  • Reserved tool names are validated

Implements RFC 003 for MCP integration in OpenEnv:

Core MCP Infrastructure:
- Add mcp_types.py with Tool, ToolError, ListToolsAction, CallToolAction,
  and corresponding observations
- Add MCPEnvironment base class with reserved tool name validation,
  timeout handling, and proper error categorization
- Add WebSocket MCP message handler for JSON-RPC via "type: mcp" messages

Echo Environment Reference Implementation:
- Add mcp_server.py with echo_message and echo_with_length tools
- Update echo_environment.py with MCP action support while maintaining
  backwards compatibility with EchoAction

Design decisions:
- WebSocket-only for MCP (no HTTP /mcp endpoint)
- Per-environment MCP client lifecycle
- Both step() and WebSocket /mcp paths supported
- reset() NOT required for list_tools()
- Reserved tool names (reset, step, state, close) raise hard error

Adds fastmcp>=2.0.0 dependency and comprehensive tests.
The directory name 'mcp' was shadowing the external mcp package
during pytest collection, causing 'No module named mcp.types' errors.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 13, 2026
@Darktex

Darktex commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in favor of updating PR #224

@Darktex Darktex closed this Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant