Skip to content

Conversation

@MyNameisPI
Copy link
Contributor

@MyNameisPI MyNameisPI commented Dec 5, 2025

  • Extract common logic from on_call_tool to _inject_unity_instance method.
  • Add on_read_resource to inject unity_instance into resource contexts.
  • Fix resources unable to get unity_instance from context after set_active_instance. Resources cannot get unity_instance from context #431

Summary by CodeRabbit

  • Refactor
    • Improved context handling infrastructure to support both tool and resource operations consistently, expanding runtime support capabilities across all operation types.

✏️ Tip: You can customize this high-level summary in your review settings.

)

- Extract common logic from on_call_tool to _inject_unity_instance method.
- Add on_read_resource to inject unity_instance into resource contexts.
- Fix resources unable to get unity_instance from context after set_active_instance.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 5, 2025

Walkthrough

The PR refactors the UnityInstanceMiddleware class by extracting the Unity instance injection logic into a private helper method and extending middleware support from tool calls to resource calls through a new on_read_resource handler.

Changes

Cohort / File(s) Summary
Middleware refactoring
Server/src/transport/unity_instance_middleware.py
Added private _inject_unity_instance() helper to consolidate common injection logic; created new on_read_resource() public method for resource context injection; updated on_call_tool() to delegate to helper; expanded class docstring to document both tool and resource support.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Areas requiring attention:
    • Verify the extracted _inject_unity_instance() helper maintains identical behavior and error handling as the original inlined logic
    • Confirm async/await patterns are correctly applied in both on_call_tool() and on_read_resource() methods
    • Validate that the permissive PluginHub resolution fallback is preserved in the helper

Possibly related issues

Poem

🐰 A helper emerges from duplication's den,
Injecting grace for resources and tools again,
Middleware refined, now cleaner and bright,
One path shared, unified might! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: adding middleware support for resource context injection, which directly aligns with the PR's primary objective of enabling resource contexts to access unity_instance.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2c65c76 and 161bd46.

📒 Files selected for processing (1)
  • Server/src/transport/unity_instance_middleware.py (3 hunks)
🔇 Additional comments (4)
Server/src/transport/unity_instance_middleware.py (4)

45-45: LGTM! Documentation accurately reflects the expanded scope.

The docstring correctly indicates that the middleware now handles both tool and resource calls.


133-137: Clean middleware delegation pattern.

The implementation correctly calls the injection helper and proceeds with the middleware chain. The on_call_tool hook is the correct middleware method for fastmcp v2.13.0, used for tool-execution-specific logic with the proper signature and control flow.


138-141: Correctly extends middleware support to resource calls.

This implementation properly enables resources to access unity_instance from context. The on_read_resource hook is the correct operation-specific middleware hook in fastmcp v2.13.0 for intercepting resource-read requests. The pattern of awaiting _inject_unity_instance(context) and then returning await call_next(context) is consistent with documented middleware behavior.


86-132: Good refactoring to eliminate duplication.

The extraction of common injection logic into a private helper is clean and follows DRY principles. The error handling is comprehensive with proper special-casing for system exceptions.

The permissive error handling is intentional and well-documented: if PluginHub session resolution fails, the middleware still injects active_instance into context to support stdio transport and hybrid mode scenarios. This approach prevents middleware from breaking requests while allowing downstream handlers to work with at least the instance identifier, even if HTTP routing information (session_id) is unavailable. Verify that tool and resource handlers appropriately handle the case where unity_session_id is absent from context.


</blockquote></details>

</blockquote></details>

</details>

<!-- tips_start -->

---

Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=CoplayDev/unity-mcp&utm_content=432)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<sub>Comment `@coderabbitai help` to get the list of available commands and usage tips.</sub>

<!-- tips_end -->

<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKPR1AGxJcAYvAAeXACCtPTM8KFeAO7OJPbY3Nz4FLiQAGbJkBTS+NgUDHFMGDT+qfAYImLw+FgAFGYALADMAIwAlJCQBgByjgKUXM0ATJ0GAKo2ADJcsLi43IgcAPRLROqw2AIaTMxLAML43B5osgAiJBJL2BjqsmDMDNxL3NgeHkvDo2OIA5AAsrJumg2EgrABJUYAZVy+TiAioGAYsC4aQCYGyiBhBTARRKuDA5Uq4hqo2gzlIqXhmCRXGY2iwXUhuGo2EW/G4ZFGNgu8BIUUobNqGBqcQ8SBotA6XUmKhIHkFwvIkDFiAlUoMe2y1Do6E4kCGAAY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YAA5MgXgNSN2oXmmXfex4HpC1Y0ldqZ5vwFCQUUgqe6INCqPUoE9zgz0Xr/WNGoqIiB5EfbyrVRmDLXtvR4e9wIHNCsw14D9Rbv0Ht/Y2sDQHUNGoYDrIWBwJJoFIJZxtUHW2wY7QemliG+0LQTbE9I1xiqiYyNcdOWBkat1IPQVEXhLYXHTNgYKtBtZySFkwGLpAWyQmHYkIW9WIuNb1s15QpBUbusM6loVC4a79tQwATVyIwSi7asCPwAALLLwrxfiNE4JxG65F6aiB4F5hqTDMuO3euHn61QVr8XD3HtQ6eg812r3ejcx5itqHTumuzQez9gqAC6SaDIptsFW19k2hXGjQLaBoBoDS/IYC0NI/ywUGmaGkAAHAIA0TQSAkCtCQBoxoWhoGNCQNAQxbTY4EA0EgQxzlpANF835Qxp6gtoFCtIBOGBpDp7QSzgPuG4FsCBrzBkIctLR7joYAhgUGgly0BocOGBvPpzD20DBbT49+bjj5LQLnS9ELac5BoBAvJaL8406PjRo7QCr0IQwGAGj5x9g80WBskA8u+Y8HgmTahlRSn7P2DCYuyDiigeKwO7PRfoIAA== -->

<!-- internal state end -->

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #431 by extending middleware support to inject unity_instance into resource contexts, ensuring resources can access the active Unity instance after set_active_instance is called.

Key Changes:

  • Extracted common injection logic into a reusable _inject_unity_instance method
  • Added on_read_resource middleware hook to support resource operations
  • Refactored on_call_tool to use the extracted injection method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +138 to 141
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)
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.
@dsarno
Copy link
Collaborator

dsarno commented Dec 5, 2025

This looks good @MyNameisPI , thanks for the catch and fix!

@dsarno dsarno merged commit b09e48a into CoplayDev:main Dec 5, 2025
7 checks passed
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