Skip to content
Merged
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
15 changes: 12 additions & 3 deletions Server/src/transport/unity_instance_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class UnityInstanceMiddleware(Middleware):
Middleware that manages per-session Unity instance selection.

Stores active instance per session_id and injects it into request state
for all tool calls.
for all tool and resource calls.
"""

def __init__(self):
Expand Down Expand Up @@ -83,8 +83,8 @@ def clear_active_instance(self, ctx) -> None:
with self._lock:
self._active_by_key.pop(key, None)

async def on_call_tool(self, context: MiddlewareContext, call_next):
"""Inject active Unity instance into tool context if available."""
async def _inject_unity_instance(self, context: MiddlewareContext) -> None:
"""Inject active Unity instance into context if available."""
ctx = context.fastmcp_context

active_instance = self.get_active_instance(ctx)
Expand Down Expand Up @@ -129,4 +129,13 @@ async def on_call_tool(self, context: MiddlewareContext, call_next):
ctx.set_state("unity_instance", active_instance)
if session_id is not None:
ctx.set_state("unity_session_id", session_id)

async def on_call_tool(self, context: MiddlewareContext, call_next):
"""Inject active Unity instance into tool context if available."""
await self._inject_unity_instance(context)
return await call_next(context)

async def on_read_resource(self, context: MiddlewareContext, call_next):
"""Inject active Unity instance into resource context if available."""
await self._inject_unity_instance(context)
return await call_next(context)
Comment on lines +138 to 141
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new on_read_resource method lacks test coverage. Since the repository has comprehensive tests for on_call_tool (see test_instance_routing_comprehensive.py), similar tests should be added for on_read_resource to verify that:

  1. The middleware correctly injects unity_instance into resource contexts
  2. Resources can successfully retrieve the active instance via get_unity_instance_from_context
  3. The behavior matches that of tool calls

Copilot uses AI. Check for mistakes.