import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
const LMS_API_BASE = 'https://api.launchmystore.io/api/v1';
class LaunchMyStoreServer {
private server: Server;
private accessToken: string;
constructor(accessToken: string) {
this.accessToken = accessToken;
this.server = new Server(
{ name: 'launchmystore-custom', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
this.setupHandlers();
}
private async apiRequest(endpoint: string, options: RequestInit = {}) {
const response = await fetch(`${LMS_API_BASE}${endpoint}`, {
...options,
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json',
...options.headers,
},
});
const data = await response.json();
if (!response.ok) {
throw new Error(`API Error: ${data.error || response.statusText}`);
}
return data;
}
private setupHandlers() {
// List available tools
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'get_products',
description: 'List products in the store',
inputSchema: {
type: 'object',
properties: {
search: { type: 'string', description: 'Search by name' },
status: { type: 'string', enum: ['active', 'inactive'] },
limit: { type: 'number', description: 'Max results (default 10)' },
},
},
},
{
name: 'get_orders',
description: 'List orders',
inputSchema: {
type: 'object',
properties: {
status: {
type: 'string',
enum: ['unpaid', 'confirmed', 'shipped', 'delivered', 'canceled']
},
range: {
type: 'string',
enum: ['today', 'this_week', 'this_month', 'lifetime']
},
},
},
},
{
name: 'get_analytics',
description: 'Get store analytics',
inputSchema: {
type: 'object',
properties: {
range: { type: 'string', enum: ['today', 'this_week', 'this_month'] },
},
},
},
],
}));
// Handle tool calls
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
let result: any;
switch (name) {
case 'get_products': {
const params = new URLSearchParams();
if (args?.search) params.set('search', String(args.search));
if (args?.status) params.set('status', String(args.status));
if (args?.limit) params.set('limit', String(args.limit));
result = await this.apiRequest(`/products.json?${params}`);
break;
}
case 'get_orders': {
const params = new URLSearchParams();
if (args?.status) params.set('status', String(args.status));
if (args?.range) params.set('range', String(args.range));
result = await this.apiRequest(`/orders.json?${params}`);
break;
}
case 'get_analytics': {
const params = new URLSearchParams();
if (args?.range) params.set('range', String(args.range));
result = await this.apiRequest(`/analytics.json?${params}`);
break;
}
default:
throw new Error(`Unknown tool: ${name}`);
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('LaunchMyStore MCP server running');
}
}
// Start server
const accessToken = process.env.LMS_ACCESS_TOKEN;
if (!accessToken) {
console.error('LMS_ACCESS_TOKEN environment variable required');
process.exit(1);
}
new LaunchMyStoreServer(accessToken).run().catch(console.error);