import { Tool } from '@modelcontextprotocol/sdk/types.js';
import { SFExpressClient } from '../sf-express-client.js';
import {
CreateOrderRequestSchema,
CreateOrderRequest,
SFExpressError
} from '../types.js';
export const createOrderTool: Tool = {
name: 'sf_express_create_order',
description: 'Create a new shipping order with SF Express. This tool handles order creation for domestic and international shipments.',
inputSchema: {
type: 'object',
properties: {
orderId: {
type: 'string',
description: 'Unique customer order ID'
},
expressType: {
type: 'string',
enum: ['1', '2', '3', '4', '5'],
description: 'Express service type: 1-Standard Express, 2-Next Day, 3-Same Day, 4-Economic, 5-International'
},
payMethod: {
type: 'string',
enum: ['1', '2', '3'],
description: 'Payment method: 1-Sender pays, 2-Receiver pays, 3-Third party pays'
},
custId: {
type: 'string',
description: 'Customer ID provided by SF Express'
},
consigneeInfo: {
type: 'object',
description: 'Recipient information',
properties: {
contact: {
type: 'object',
properties: {
contact: { type: 'string', description: 'Contact person name' },
tel: { type: 'string', description: 'Phone number' },
mobile: { type: 'string', description: 'Mobile phone number' },
company: { type: 'string', description: 'Company name' }
},
required: ['contact', 'tel']
},
address: {
type: 'object',
properties: {
province: { type: 'string', description: 'Province name' },
city: { type: 'string', description: 'City name' },
county: { type: 'string', description: 'County/District name' },
address: { type: 'string', description: 'Detailed address' },
postCode: { type: 'string', description: 'Postal code' },
tel: { type: 'string', description: 'Phone number' }
},
required: ['province', 'city', 'address']
}
},
required: ['contact', 'address']
},
deliverInfo: {
type: 'object',
description: 'Sender information',
properties: {
contact: {
type: 'object',
properties: {
contact: { type: 'string', description: 'Contact person name' },
tel: { type: 'string', description: 'Phone number' },
mobile: { type: 'string', description: 'Mobile phone number' },
company: { type: 'string', description: 'Company name' }
},
required: ['contact', 'tel']
},
address: {
type: 'object',
properties: {
province: { type: 'string', description: 'Province name' },
city: { type: 'string', description: 'City name' },
county: { type: 'string', description: 'County/District name' },
address: { type: 'string', description: 'Detailed address' },
postCode: { type: 'string', description: 'Postal code' },
tel: { type: 'string', description: 'Phone number' }
},
required: ['province', 'city', 'address']
}
},
required: ['contact', 'address']
},
cargo: {
type: 'array',
description: 'List of items to ship',
items: {
type: 'object',
properties: {
name: { type: 'string', description: 'Item name' },
count: { type: 'number', description: 'Quantity', minimum: 1 },
weight: { type: 'number', description: 'Weight in kg', minimum: 0 },
amount: { type: 'number', description: 'Declared value', minimum: 0 },
currency: { type: 'string', description: 'Currency code', default: 'CNY' },
unit: { type: 'string', description: 'Unit of measurement' }
},
required: ['name', 'count']
},
minItems: 1
},
addedService: {
type: 'array',
description: 'Additional services (optional)',
items: {
type: 'object',
properties: {
name: { type: 'string', description: 'Service name' },
value: { type: 'string', description: 'Service value' }
},
required: ['name']
}
},
remark: {
type: 'string',
description: 'Additional remarks or instructions'
}
},
required: ['orderId', 'expressType', 'payMethod', 'custId', 'consigneeInfo', 'deliverInfo', 'cargo']
}
};
export async function handleCreateOrder(
args: any,
client: SFExpressClient
): Promise<any> {
try {
// Validate input using Zod schema
const validatedRequest = CreateOrderRequestSchema.parse(args);
// Call SF Express API
const result = await client.createOrder(validatedRequest);
return {
success: true,
data: {
orderId: result.orderId,
waybillNo: result.waybillNo,
originCode: result.originCode,
destCode: result.destCode,
filterResult: result.filterResult,
remark: result.remark,
message: 'Order created successfully'
}
};
} catch (error) {
if (error instanceof SFExpressError) {
return {
success: false,
error: {
code: error.errorCode || 'CREATE_ORDER_ERROR',
message: error.message,
details: error.details
}
};
}
// Handle Zod validation errors
if (error.name === 'ZodError') {
return {
success: false,
error: {
code: 'VALIDATION_ERROR',
message: 'Invalid input parameters',
details: error.errors
}
};
}
return {
success: false,
error: {
code: 'UNKNOWN_ERROR',
message: 'An unexpected error occurred while creating the order',
details: error instanceof Error ? error.message : 'Unknown error'
}
};
}
}