Skip to content

Unity MCP fails with "Invalid JSON format" error when creating C# scripts larger than ~2000 bytes. #136

Closed

Description

@bizzeu

Error Details
Failed to communicate with Unity: Invalid JSON format
Client handler error: Unable to read data from the transport connection

Question: Is this a known limitation? Is there a workaround or planned fix?

Environment: Unity 2022.3 LTS, latest Unity MCP version, Windows

Activity

  1. Scriptwonder commented on Jul 6, 2025

    @Scriptwonder
    Collaborator

    The current TCP buffer is using a buffer of size 8192, but I wonder if the wrapper for the script exceed that limit. Did you find a solution to this?

  2. added
    bug
    on Jul 6, 2025
  3. melkourie commented on Jul 14, 2025

    @melkourie

    The current TCP buffer is using a buffer of size 8192, but I wonder if the wrapper for the script exceed that limit. Did you find a solution to this?

    I too am encountering this buffer issue. I am able to reproduce the issue if I allow Claude to answer with longer scripts it wants to create. If I force Claude to keep the answer short, no issues whatsoever. Claude Code (in paid mode) can have a context of up to 200,000 tokens. Quote from Anthropic "Context Window (Token Limit): The actual "buffer" for Claude's understanding and response generation is measured in tokens, not bytes. The context window for Claude Pro and the API is currently 200,000+ tokens, equivalent to about 500 pages of text or 100 images. However, the free version's context window can vary. "

    My suggestion would be perhaps some form of simple chunking, where you have a stop bit that gets sent when no more data is being sent. That action would then trigger the actual file being written when sending TO Unity FROM Claude. I'm not sure how other integrations handle working with large code and say GPT or other LLMs, but would imagine they all use a common, but simple mechanism to ensure they aren't truncating a response to file on transfer or send. I get no timeouts on the Claude side, so it may be a matter of working within the constraints of MCP.... I'm not that smart.

  4. added
    enhancement
    on Jul 14, 2025
  5. melkourie commented on Jul 14, 2025

    @melkourie

    I found another possible workaround. I routinely when using CC directly instruct it to keep files under a certain limit so that it's faster to debug and troubleshoot, and do this by having in my prompt an instruction that says if a file is over a certain limit in tokens (usually 20,000 is the limit) to intelligently chunk the file into smaller files and use includes or similar..... What seems to be working thus far with your implementation is to tell Claude Desktop to be aware of the 8192 tcp socket size limit. In doing this, Claude responded that it would instead either create smaller files and use includes, and, that it would use helper scripts along with files to auto-generate (user interaction then is to right mouse click in inspector the header section of the object, and then select the auto-generate function). I'm using this now to rapidly prototype terrain and it seems to work well.

    My suggestion, since Claude is intelligent enough to understand packet data sizes, would be to embed into whatever prompt sequence you're sending the limiter statement. Just simply embedding "Keep files under an overall size of 7500 bytes or smaller by using smaller files and includes, or by using helpers scripts, or both". It may even work if you embed that when the first "chat" request is sent. That could serve as a workaround to the TCP send window issue until a more elegant solution is found. Just a thought, it seems to be working for me.

  6. xuguozhi commented on Jul 15, 2025

    @xuguozhi

    I asked Claude Code, and "he" solved this buffer issue

  7. melkourie commented on Jul 15, 2025

    @melkourie

    I asked Claude Code, and "he" solved this buffer issue

    If I may ask - what did you ask, and what was the actual solution ? I've been just using a manual system prompt to ensure staying below 7KB file sizes with pretty good success (it makes Claude work harder, since "he" has to modularize all his scripting)... LOL.

  8. xuguozhi commented on Jul 15, 2025

    @xuguozhi

    I asked Claude Code, and "he" solved this buffer issue

    If I may ask - what did you ask, and what was the actual solution ? I've been just using a manual system prompt to ensure staying below 7KB file sizes with pretty good success (it makes Claude work harder, since "he" has to modularize all his scripting)... LOL.

    I just copy this issue title to CC, then CC modified both MCP server and unity bridge code upgrading buffer to separated buffer

  9. dsarno commented on Aug 30, 2025

    @dsarno
    Collaborator

    Should be fixed in 3.1.0. Please let us know if you see an improved experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions