Skip to content

[Bug]: execute_code tool fails with BOM character (U+FEFF) in code parameter #1186

@stanLy490

Description

@stanLy490

What happened?

Description

The execute_code tool consistently fails with CodeDom compilation error when the code parameter contains a UTF-8 BOM character (U+FEFF, ) at the beginning.

Error

{
  "success": false,
  "message": "Compilation failed",
  "data": {
    "errors": ["Line 1: "],
    "compiler": "codedom"
  }
}
Steps to Reproduce
Call execute_code with any valid C# code:
{
  "action": "execute",
  "code": "return \"hello\";"
}
Observe compilation failure with BOM error
Expected Behavior
Code should compile and execute successfully. The BOM character should be stripped before compilation.

Root Cause
The code parameter arrives with a leading U+FEFF (BOM) character. This likely occurs during JSON serialization/deserialization in the MCP protocol layer. The ExecuteCode.HandleExecute method does not strip BOM before passing the code to WrapUserCode → CodeDomCompile.

Suggested Fix
In ExecuteCode.cs, add BOM stripping at the entry point:

string code = @params["code"]?.ToString()?.TrimStart('\uFEFF');
Environment
MCP for Unity: main branch (78ee541841)
Unity Version: 2022.3.x (URP)
OS: Windows 10
MCP Client: opencode with stdio transport

### Reproduction steps

## Reproduction Steps

1. Open Window > MCP for Unity
2. Click Start Server
3. In MCP client, call execute_code tool with any valid C# code:
   ```json
   { "action": "execute", "code": "return \"hello\";" }

### Unity version

2022.3.8f1

### MCP for Unity package version

v9.7.1

### Python server version

_No response_

### MCP client

Other (specify in description)

### Transport

stdio

### OS

Windows

### Relevant logs / console output

```text

Checks

  • I searched existing issues and did not find a duplicate
  • I included logs / steps to reproduce

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions