unity-mcp
Version:
Model Context Protocol server for Unity.
85 lines (84 loc) • 2.37 kB
JavaScript
import fs from 'fs/promises';
import { z } from 'zod';
import { ProjectService } from './project-service.js';
export const listProjectsInputSchema = z.object({});
export async function handleListProjects({}) {
try {
const projectService = new ProjectService();
const projects = await projectService.listProjects();
return {
content: [
{
type: 'text',
text: JSON.stringify(projects, null, 2)
}
]
};
}
catch (error) {
console.error('Error handling list projects request:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({ error: 'Failed to list Unity projects' }, null, 2)
}
],
isError: true
};
}
}
export const listFilesInProjectSchema = z.object({
projectName: z.string()
});
export async function handleListFilesInProject({ projectName }) {
try {
const projectService = new ProjectService();
const files = await projectService.listFilesInProject(projectName);
return {
content: [
{
type: 'text',
text: JSON.stringify(files, null, 2)
}
]
};
}
catch (error) {
console.error('Error handling list files request:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({ error: 'Failed to list files in the Unity project.' }, null, 2)
}
],
isError: true
};
}
}
export async function handleReadFile({ path }) {
try {
const data = await fs.readFile(path, 'utf-8');
return {
content: [
{
type: 'text',
text: data
}
]
};
}
catch (error) {
console.error('Error reading file:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({ error: `Failed to read file at ${path}.` }, null, 2)
}
],
isError: true
};
}
}