unity-mcp icon indicating copy to clipboard operation
unity-mcp copied to clipboard

Runtime MCP Operation

Open Scriptwonder opened this issue 1 year ago • 9 comments

Hi Justin, what would it take to run Runtime/Play Mode compilation and adjustment with MCP? I tried projects like LLMR before, and dynamically running adjustments seem to be approachable with Roslyn and LLM(even though it can be really messy), and they basically resemble the idea that with any given input, user can see any live changes during gameplay. I am not an expert in this, so I would love to hear the community's thoughts on this. Thanks!

Scriptwonder avatar Mar 27 '25 17:03 Scriptwonder

I love the idea. Right now everything's mostly running in the editor. The assembly is just editor. Would have to open it up to runtime. Most of the functionality (minus deleting) should carry over no problem.

justinpbarnett avatar Apr 01 '25 01:04 justinpbarnett

I love the idea. Right now everything's mostly running in the editor. The assembly is just editor. Would have to open it up to runtime. Most of the functionality (minus deleting) should carry over no problem.

Just tried the run-time generation. The main problem with Run-time is that every time new script is generated or script is changd, scene and the assembly will reload. This can be solved with using Roslyn, but maybe not a priority now.

Scriptwonder avatar Apr 02 '25 16:04 Scriptwonder

Tested my current Runtime implementation, and found this issue

Image

@msanatan I agree that memory leak check is important, but is it possible to come up with a more elegant solution so that I could actually get my scripts generated in runtime?

Scriptwonder avatar Oct 24 '25 22:10 Scriptwonder

Tested my current Runtime implementation, and found this issue

Image [@msanatan](https://github.com/msanatan) I agree that memory leak check is important, but is it possible to come up with a more elegant solution so that I could actually get my scripts generated in runtime?

FYI, the change is attributed to me because I copied the folder, you added this 3 months ago 😅. That said, these checks won't be enough. I'm not too familiar with the Roslyn integration, but that would give us a syntax tree to more methodologically traverse and validate.

It's possible it does this already, but in a case where we're writing scripts I'd probably just compile them in memory, catch any errors before writing them to the file, and let the LLM try again with the error.

msanatan avatar Oct 25 '25 03:10 msanatan

My mistake! Will look into that and submit a PR soon.

Scriptwonder avatar Oct 25 '25 03:10 Scriptwonder

Just for my knowledge, what do we mean/expect from runtime tools? When I think about it, I imagine that someone is able to bundle MCP for Unity in their game/experience, and our plugin allows LLMs to connect to their game via MCP. Is that what we mean? For e.g. I can create Tic Tac Toe game and use MCP for Unity's runtime tools, Claude or another model can play against me with the tools I've created.

If it that, here are my thoughts about the use case and how we can implement it.

FastMCP (and other MCP SDKs) support dynamic tools https://gofastmcp.com/servers/tools#disabling-tools (a few Discord chats spoke about being able to enable/disable Tools in the UI - something I'd love to see and this feature supports it!). We're able to enable and disable tools at runtime, in my head this can be very cleanly done as an HTTP request, which is something already in development in #295.

So runtime tools would require:

  • User creating a runtime tool
  • Unity plugin sends a request to the MCP server with the tools and resources available/enabled
  • If their MCP client supports dynamic tooling (Claude Code/Desktop, and VS Code definitely do) they'll see their runtime tools

Users can add custom tools, though it somewhat clunkily requires Python and C# code still. However, tools are enabled at startup, and we don't have a way to disable the editor tools easily. Those gaps would cleanly facilitate runtime tools.

What's the approach you have in mind?

msanatan avatar Oct 25 '25 03:10 msanatan

I think I should rephrase this as a "hot-reload feature"? (Sorry, I make it sound like tools that support runtime:) )

With the current MCP, I remember that if you are in the middle of the game with some scene states changed, compiling and reloading the domain would cause the scene to restart. This is Unity's default mechanism, triggered by any little script change. But I want to explore the option to keep the game going even after a script change, which explores more options for fun projects/research.

I am currently exploring options, including using Roslyn or other dynamic hot-reload repos (like this one: https://github.com/focus-creative-games/hybridclr), but I think Roslyn might be the easiest one, since we already have some features using it. let me know what you think.

Scriptwonder avatar Oct 25 '25 04:10 Scriptwonder

Image A bit of update on this, I made this single-script plugin could execute scripts in play mode without interrupting the flow, while also let you save the scripts (because they will be lost once the play mode stops). I am right now following the tool creating .md to create a mcp tool that can have LLM-generated scripts in playtime.

Scriptwonder avatar Oct 25 '25 16:10 Scriptwonder

This has been transferred to a discussion: https://github.com/CoplayDev/unity-mcp/discussions/781

dsarno avatar Feb 18 '26 22:02 dsarno