Skip to content

MCP: Audit and harden native MCP server implementation#1290

Draft
MichaelFisher1997 wants to merge 1 commit into
Redot-Engine:masterfrom
MichaelFisher1997:mcp-audit
Draft

MCP: Audit and harden native MCP server implementation#1290
MichaelFisher1997 wants to merge 1 commit into
Redot-Engine:masterfrom
MichaelFisher1997:mcp-audit

Conversation

@MichaelFisher1997

@MichaelFisher1997 MichaelFisher1997 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

A comprehensive audit and hardening of the native MCP (Model Context Protocol) server module (modules/mcp/). The module was functional but had several bugs, spec compliance gaps, and unimplemented actions advertised in the tool schemas.

Bugfixes

  • Capture crash: Null-deref in capture action when viewport returns no image (headless/first-frame) or PNG encoding fails
  • Double-wrapped errors: Pre-init tools/* requests returned a JSON-RPC error nested inside a result envelope instead of a top-level error. Fixed via a process_string override that gates pre-init requests and notifications

Protocol compliance

  • Updated protocol version from 2024-11-05 to 2025-06-18 with proper version negotiation (echoes client version if supported)
  • Moved initialized flag from initialize response to notifications/initialized per spec lifecycle
  • Declared tools.listChanged capability explicitly

Security & robustness

  • Hardened validate_path: segment-based .. traversal check, applied consistently to all path-accepting actions (previously instance_path, resource paths, and code_intel paths were unvalidated)
  • Fixed bridge concurrency: update() no longer drops an active connection mid-command
  • Improved send_command: chunked reads via CharString accumulation with single-pass UTF-8 decode

New functionality

  • code_intel search action: recursive .gd file content search
  • project_config add_input action: writes InputEventKey to project.godot with runtime InputMap registration
  • project_config add_autoload action: writes autoload entry to project.godot

Quality

  • MCP tool annotations (readOnlyHint, destructiveHint, etc.) on all 5 tools per 2025-06-18 spec
  • Clarified project_config schema descriptions (which params each action uses)
  • Improved _ensure_callback_exists word-boundary detection (handles static func, annotation prefixes)
  • Added signal-exists validation before connect() in scene_action
  • Filled in class reference docs for MCPServer, MCPBridge, MCPProtocol
  • Removed dead code (_make_text_content)

Testing

All changes manually tested end-to-end with a live MCP handshake (--mcp-server --headless) across all 5 tools:

Tool Actions verified
code_intel get_symbols, get_docs, validate, search
game_control wait, capture, inspect_live, click, trigger_action, type
project_config get_info, list_files, run, stop, output, read_file_res, create_file_res, set_setting, add_input, add_autoload
resource_action inspect, inspect_asset, create, modify, duplicate
scene_action get_node, create, add, set_prop, instance, reparent, remove, connect

Pre-init gating verified: single requests return JSON-RPC error, notifications are silently dropped.

Notes

  • AI tools were used during development, in accordance with the project AI Policy. All changes have been manually tested and reviewed.
  • Built and tested on Linux (linuxbsd platform, editor target).

Checklist

  • Code compiles (scons platform=linuxbsd target=editor)
  • Manually tested all advertised tool actions
  • Class reference documentation updated
  • Pre-commit hooks passed (clang-format, codespell, copyright-headers, header-guards, file-format)
  • CodeRabbit review addressed

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5f4a0f2c-585e-41c3-852c-e19c6c23f51d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Fixes several bugs, improves spec compliance, and fills in missing
functionality across the MCP module.

Bugfixes:
- Fix null-deref crash in capture action when viewport returns no image
  or PNG encoding fails (mcp_bridge.cpp)
- Fix double-wrapped JSON-RPC error responses: pre-init tools/* requests
  now return a proper top-level error via process_string override instead
  of being nested inside a result envelope (mcp_protocol.{h,cpp})
- Fix double-wrapping on INVALID_PARAMS paths in tools/call handler

Protocol compliance:
- Update protocol version from 2024-11-05 to 2025-06-18
- Add version negotiation: echo client version if supported, else return
  latest (mcp_protocol.cpp)
- Move initialized flag from initialize response to notifications/initialized
  per spec lifecycle requirements
- Declare tools.listChanged capability explicitly

Security and robustness:
- Harden validate_path: segment-based .. check instead of substring match,
  applied consistently to all path-accepting actions (instance_path,
  resource paths, code_intel paths were previously unvalidated)
- Fix bridge concurrency: update() no longer drops an active connection
  mid-command when a new peer connects
- Improve send_command: chunked reads via CharString accumulation with
  single-pass UTF-8 decode to handle multi-byte chars across TCP boundaries

New functionality:
- Implement code_intel search action (recursive .gd file content search)
- Implement project_config add_input action (writes InputEventKey to
  project.godot with runtime InputMap registration)
- Implement project_config add_autoload action (writes autoload entry
  to project.godot)

Quality improvements:
- Add MCP tool annotations (readOnlyHint, destructiveHint, etc.) to all
  5 tools per 2025-06-18 spec
- Clarify project_config schema descriptions (which params each action
  uses) to prevent agent confusion
- Improve _ensure_callback_exists: word-boundary detection handles
  static func and annotation prefixes
- Add signal-exists validation before connect() in scene_action
- Remove dead code (_make_text_content)
- Fill in class reference documentation for MCPServer, MCPBridge,
  MCPProtocol

AI tools were used in the development of this contribution, in
accordance with the project AI Policy. All changes have been manually
tested end-to-end with a live MCP handshake across all 5 tools.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants