import { z } from 'zod';
import * as cmd from '../core/commands';
import { createErrorResponse, ErrorCodes } from '../utils/errors';
/**
* Command execution tools
*/
export const RunCommandInputSchema = z.object({
command: z.string(),
timeout: z.number().optional().default(30000),
cwd: z.string().optional(),
});
export async function runCommandTool(input: z.infer<typeof RunCommandInputSchema>) {
try {
if (!cmd.isCommandAllowed(input.command)) {
return {
content: [
createErrorResponse(
ErrorCodes.ACCESS_DENIED,
`Command not allowed: ${input.command}`
),
],
};
}
const result = await cmd.runCommand(input.command, {
timeout: input.timeout,
cwd: input.cwd,
});
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: result.exitCode === 0,
exitCode: result.exitCode,
stdout: result.stdout,
stderr: result.stderr,
},
null,
2
),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.COMMAND_FAILED, String(error))],
};
}
}
export const ListCommandsInputSchema = z.object({});
export async function listCommandsTool(_input: z.infer<typeof ListCommandsInputSchema>) {
try {
const commands = cmd.getAvailableCommands();
return {
content: [
{
type: 'text',
text: JSON.stringify({ success: true, allowedCommands: commands }, null, 2),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}