docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class UnityMCPBridge

    Main API for controlling the Unity MCP Bridge lifecycle and querying connection status. The bridge enables MCP clients (like Claude Code) to connect to Unity Editor and invoke registered tools.

    Inheritance
    object
    UnityMCPBridge
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.AI.MCP.Editor
    Assembly: Unity.AI.MCP.Editor.dll
    Syntax
    public static class UnityMCPBridge
    Remarks

    The bridge operates as an IPC server that:

    • Listens for incoming connections from MCP clients via named pipes (Windows) or Unix sockets (Mac/Linux)
    • Routes tool invocation requests to the McpToolRegistry
    • Manages client connections and security validation

    Lifecycle:

    • Automatically started at editor load if enabled in project settings
    • Can be manually controlled via Start() and Stop()
    • Persists across domain reloads (script compilation)

    The bridge can be disabled entirely via Enabled property or project settings. In batch mode, the bridge respects the batchModeEnabled setting and UNITY_MCP_DISABLE_BATCH environment variable.

    Properties

    Enabled

    Gets or sets whether the MCP bridge is enabled in project settings.

    Declaration
    public static bool Enabled { get; set; }
    Property Value
    Type Description
    bool
    Remarks

    When set to true:

    • Creates and starts the bridge instance if not already running
    • Persists the setting to EditorPrefs
    • Bridge will auto-start on editor load

    When set to false:

    • Stops and disposes the bridge instance
    • Persists the setting to EditorPrefs
    • Bridge will not auto-start on editor load

    This property is backed by project settings and changes are saved automatically. Changes take effect immediately.

    IsRunning

    Gets whether the MCP bridge is currently running and accepting connections.

    Declaration
    public static bool IsRunning { get; }
    Property Value
    Type Description
    bool
    Remarks

    Returns true when the bridge has successfully started and is listening for connections. The bridge may stop temporarily during domain reloads (script compilation) and restart automatically.

    MaxDirectConnectionsResolver

    Resolver for the maximum number of concurrent direct (non-gateway) connections. Return -1 for unlimited, or a positive integer for a hard cap.

    Declaration
    public static Func<int> MaxDirectConnectionsResolver { get; set; }
    Property Value
    Type Description
    Func<int>

    Methods

    DisconnectAll()

    Disconnects all active client connections. Used when removing all connections from security settings.

    Declaration
    public static void DisconnectAll()
    Remarks

    This immediately closes all active connections. Clients can attempt to reconnect, but will need to be re-approved through the security validation dialog.

    Does nothing if the bridge is not running or no connections exist.

    DisconnectConnectionByIdentity(ConnectionIdentity)

    Disconnects any active client connections matching the specified identity. Used when revoking a previously-approved connection from security settings.

    Declaration
    public static void DisconnectConnectionByIdentity(ConnectionIdentity identity)
    Parameters
    Type Name Description
    ConnectionIdentity identity

    The connection identity to disconnect

    Remarks

    This immediately closes the connection to any matching client. The client can attempt to reconnect, but will need to be re-approved through the security validation dialog.

    Does nothing if the bridge is not running or no matching connections exist.

    GetActiveIdentityKeys()

    Gets the identity keys of all currently connected clients.

    Declaration
    public static string[] GetActiveIdentityKeys()
    Returns
    Type Description
    string[]

    Array of identity key strings for active connections

    Remarks

    Identity keys are used to distinguish between active connections and historical connection records in settings. Useful for UI displays showing connection status. Returns an empty array if the bridge is not running.

    GetConnectedClientCount()

    Gets the number of currently connected MCP clients.

    Declaration
    public static int GetConnectedClientCount()
    Returns
    Type Description
    int

    The number of active client connections

    Remarks

    This is more efficient than calling GetConnectedClients().Length and provides immediate feedback when new clients connect. Returns 0 if the bridge is not running.

    GetConnectedClients()

    Gets information about all currently connected MCP clients.

    Declaration
    public static ClientInfo[] GetConnectedClients()
    Returns
    Type Description
    ClientInfo[]

    Array of ClientInfo for each connected client, or empty array if none

    Remarks

    Each ClientInfo includes:

    • Client process information (name, PID, executable path)
    • Connection identity (for security validation)
    • Connection timestamp

    Returns an empty array if the bridge is not running or no clients are connected. The array represents a snapshot at the time of the call.

    Init()

    Initializes the MCP bridge at editor load time if enabled in project settings. This method is called automatically by Unity's InitializeOnLoadMethod mechanism.

    Declaration
    [InitializeOnLoadMethod]
    public static void Init()

    NotifyMaxDirectConnectionsPolicyChanged()

    Notifies subscribers that the effective connection policy may have changed. Call after setting MaxDirectConnectionsResolver to a custom value.

    Declaration
    public static void NotifyMaxDirectConnectionsPolicyChanged()

    PrintClientInfo()

    Prints information about all currently connected MCP clients to the console.

    Declaration
    public static void PrintClientInfo()

    PrintToolSchemas()

    Prints all registered MCP tool schemas to the console and copies them to the clipboard. Useful for debugging and understanding available tools.

    Declaration
    public static void PrintToolSchemas()

    ResetMaxDirectConnectionsResolver()

    Restores the default license-aware resolver and notifies subscribers.

    Declaration
    public static void ResetMaxDirectConnectionsResolver()

    Start()

    Manually starts the MCP bridge if it's not already running. If the bridge is not enabled in settings, this will enable it first.

    Declaration
    public static void Start()
    Remarks

    This method is primarily for internal use and testing. In normal operation, the bridge starts automatically when enabled.

    The bridge will not start if:

    • Running in batch mode and batch mode is disabled in settings
    • The UNITY_MCP_DISABLE_BATCH environment variable is set

    Stop()

    Manually stops the MCP bridge, disconnecting all clients. This does not change the Enabled setting.

    Declaration
    public static void Stop()
    Remarks

    The bridge may restart automatically if it's still enabled in settings. To permanently stop the bridge, set Enabled to false instead.

    Events

    MaxDirectConnectionsPolicyChanged

    Raised when the effective max-direct-connections policy may have changed. Subscribers should re-read MaxDirectConnectionsResolver to get the current value.

    Declaration
    public static event Action MaxDirectConnectionsPolicyChanged
    Event Type
    Type Description
    Action
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)