Skip to content

Commit 13b8fb2

Browse files
sakurachanclaude
andcommitted
Fix CodeRabbit feedback: reconnection fallback and annotations safety
Address 3 CodeRabbit review comments: 1. Critical: Guard reconnection fallback to prevent wrong instance routing - When instance_id is set but rediscovery fails, now raises ConnectionError - Added 'from e' to preserve exception chain for better debugging - Prevents silently connecting to different Unity instance - Ensures multi-instance routing integrity 2. Minor: Guard __annotations__ access in resource registration - Use getattr(func, '__annotations__', {}) instead of direct access - Prevents AttributeError for functions without type hints 3. Minor: Remove unused get_type_hints import - Clean up unused import in resources/__init__.py All changes applied to both Server/ and MCPForUnity/ directories. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 73f8206 commit 13b8fb2

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

MCPForUnity/UnityMcpServer~/src/resources/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
import inspect
66
from pathlib import Path
7-
from typing import get_type_hints
87

98
from fastmcp import FastMCP
109
from telemetry_decorator import telemetry_resource
@@ -105,13 +104,13 @@ def register_all_resources(mcp: FastMCP):
105104
if has_other_params:
106105
fixed_wrapper.__signature__ = sig.replace(parameters=fixed_params)
107106
fixed_wrapper.__annotations__ = {
108-
k: v for k, v in func.__annotations__.items()
107+
k: v for k, v in getattr(func, '__annotations__', {}).items()
109108
if k != 'unity_instance'
110109
}
111110
else:
112111
fixed_wrapper.__signature__ = inspect.Signature(parameters=[])
113112
fixed_wrapper.__annotations__ = {
114-
k: v for k, v in func.__annotations__.items()
113+
k: v for k, v in getattr(func, '__annotations__', {}).items()
115114
if k == 'return'
116115
}
117116

MCPForUnity/UnityMcpServer~/src/unity_connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def read_status_file() -> dict | None:
346346

347347
# Fallback to generic port discovery if instance-specific discovery failed
348348
if new_port is None:
349+
if self.instance_id:
350+
raise ConnectionError(
351+
f"Unity instance '{self.instance_id}' could not be rediscovered"
352+
) from e
349353
new_port = PortDiscovery.discover_unity_port()
350354

351355
if new_port != self.port:

Server/resources/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
import inspect
66
from pathlib import Path
7-
from typing import get_type_hints
87

98
from fastmcp import FastMCP
109
from telemetry_decorator import telemetry_resource
@@ -105,13 +104,13 @@ def register_all_resources(mcp: FastMCP):
105104
if has_other_params:
106105
fixed_wrapper.__signature__ = sig.replace(parameters=fixed_params)
107106
fixed_wrapper.__annotations__ = {
108-
k: v for k, v in func.__annotations__.items()
107+
k: v for k, v in getattr(func, '__annotations__', {}).items()
109108
if k != 'unity_instance'
110109
}
111110
else:
112111
fixed_wrapper.__signature__ = inspect.Signature(parameters=[])
113112
fixed_wrapper.__annotations__ = {
114-
k: v for k, v in func.__annotations__.items()
113+
k: v for k, v in getattr(func, '__annotations__', {}).items()
115114
if k == 'return'
116115
}
117116

Server/unity_connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def read_status_file() -> dict | None:
346346

347347
# Fallback to generic port discovery if instance-specific discovery failed
348348
if new_port is None:
349+
if self.instance_id:
350+
raise ConnectionError(
351+
f"Unity instance '{self.instance_id}' could not be rediscovered"
352+
) from e
349353
new_port = PortDiscovery.discover_unity_port()
350354

351355
if new_port != self.port:

0 commit comments

Comments
 (0)