coreto-mcp-glpi
Version:
MCP Server para integração CORETO AI com GLPI via tools de tickets
151 lines (132 loc) • 4.18 kB
JavaScript
import { mcpLogger } from '../logger.js'
import { TICKET_STATUS, TICKET_PRIORITY } from '../constants.js'
/**
* Create a new ticket in GLPI
* @param {Object} args - Tool arguments
* @param {Object} tenantContext - Tenant context with GLPI connector
* @returns {Object} Tool execution result
*/
async function createTicket(args, tenantContext) {
const logger = mcpLogger.child({ tool: 'create_ticket' })
try {
const { title, description, priority = '3', category_id = 0 } = args
const { glpiConnector, tenant_id } = tenantContext
// Validate required fields
if (!title || !description) {
throw new Error('Título e descrição são obrigatórios para criar um chamado')
}
if (title.length < 3) {
throw new Error('Título deve ter pelo menos 3 caracteres')
}
if (description.length < 10) {
throw new Error('Descrição deve ter pelo menos 10 caracteres')
}
// Validate priority
const priorityNumber = parseInt(priority)
if (isNaN(priorityNumber) || priorityNumber < 1 || priorityNumber > 5) {
throw new Error('Prioridade deve ser um número entre 1 e 5')
}
logger.info('Creating GLPI ticket', {
tenantId: tenant_id,
title: title.substring(0, 50),
priority: priorityNumber,
categoryId: category_id
})
// Prepare ticket data
const ticketData = {
name: title.trim(),
content: description.trim(),
priority: priorityNumber,
category_id: category_id,
status: 1, // New ticket
type: 1 // Incident
}
// Create ticket via GLPI connector
const response = await glpiConnector.createTicket(ticketData)
if (!response.id) {
throw new Error('Falha ao criar chamado no GLPI - nenhum ID retornado')
}
const ticketId = response.id
logger.info('Ticket created successfully', {
tenantId: tenant_id,
ticketId,
title: title.substring(0, 50)
})
return {
success: true,
ticket_id: ticketId,
message: `Chamado #${ticketId} criado com sucesso!`,
data: {
id: ticketId,
title: title,
status: TICKET_STATUS[1] || 'Novo',
priority: TICKET_PRIORITY[priorityNumber] || 'Média',
created_at: new Date().toISOString(),
category_id: category_id
}
}
} catch (error) {
logger.error('Error creating ticket', {
tenantId: tenantContext.tenant_id,
error: error.message,
args
})
// Return user-friendly error messages
if (error.message.includes('GLPI')) {
return {
success: false,
error: `Erro de conexão com GLPI: ${error.message}`,
code: 'GLPI_CONNECTION_ERROR'
}
}
if (error.message.includes('Unauthorized') || error.message.includes('401')) {
return {
success: false,
error: 'Erro de autenticação com GLPI - verifique as credenciais',
code: 'GLPI_AUTH_ERROR'
}
}
return {
success: false,
error: error.message,
code: 'TICKET_CREATION_ERROR'
}
}
}
// Tool configuration
const createTicketTool = {
name: 'create_ticket',
description: 'Criar um novo chamado no sistema GLPI',
inputSchema: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'Título do chamado (mínimo 3 caracteres)',
minLength: 3,
maxLength: 255
},
description: {
type: 'string',
description: 'Descrição detalhada do problema ou solicitação (mínimo 10 caracteres)',
minLength: 10,
maxLength: 4000
},
priority: {
type: 'string',
enum: ['1', '2', '3', '4', '5'],
description: 'Prioridade do chamado: 1=Muito baixa, 2=Baixa, 3=Média, 4=Alta, 5=Muito alta',
default: '3'
},
category_id: {
type: 'number',
description: 'ID da categoria do chamado no GLPI (0 = sem categoria)',
default: 0,
minimum: 0
}
},
required: ['title', 'description']
},
handler: createTicket
}
export default createTicketTool