unity-editor-mcp
Version:
MCP server for Unity Editor integration - enables AI assistants to control Unity Editor
76 lines (65 loc) • 2.47 kB
JavaScript
import { BaseToolHandler } from '../base/BaseToolHandler.js';
export class ModifyMaterialToolHandler extends BaseToolHandler {
constructor(unityConnection) {
super(
'modify_material',
'Modify properties of an existing material',
{
type: 'object',
properties: {
materialPath: {
type: 'string',
description: 'Asset path to the material (must start with Assets/ and end with .mat)'
},
properties: {
type: 'object',
description: 'Material properties to modify (e.g., {"_Color": [1,0,0,1], "_Metallic": 0.5})'
},
shader: {
type: 'string',
description: 'Change the shader (e.g., "Standard", "Unlit/Color")'
}
},
required: ['materialPath', 'properties']
}
);
this.unityConnection = unityConnection;
}
validate(params) {
// Call parent validation for required fields
super.validate(params);
const { materialPath, properties, shader } = params;
// Validate materialPath format
if (!materialPath.startsWith('Assets/') || !materialPath.endsWith('.mat')) {
throw new Error('materialPath must start with Assets/ and end with .mat');
}
// Validate properties
if (typeof properties !== 'object' || properties === null || Array.isArray(properties)) {
throw new Error('properties must be an object');
}
if (Object.keys(properties).length === 0) {
throw new Error('properties cannot be empty');
}
// Validate shader when provided
if (shader !== undefined && shader === '') {
throw new Error('shader cannot be empty when provided');
}
}
async execute(params) {
const {
materialPath,
properties,
shader
} = params;
// Ensure connected
if (!this.unityConnection.isConnected()) {
await this.unityConnection.connect();
}
const result = await this.unityConnection.sendCommand('modify_material', {
materialPath,
properties,
shader
});
return result;
}
}