index.ts•3.13 kB
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
CallToolRequest,
} from '@modelcontextprotocol/sdk/types.js';
// Import all command tools
import { TouchCommands, ControlCommands, ColorCommands, OtherCommands } from './tools/basic-commands.js';
import { MathFunctions, StringFunctions, TypeConversionFunctions, ArrayFunctions } from './tools/standard-library.js';
import { UIControlCommands, UIPropertyCommands, FloatingWindowCommands } from './tools/ui-commands.js';
import { ElementCommands, DeviceCommands, PhoneCommands, SysCommands } from './tools/extension-commands.js';
import { CJsonCommands, DateTimeCommands, FileCommands, TuringCommands } from './tools/plugin-commands.js';
// Create the server
const server = new Server(
{
name: 'mqscript-mcp-server',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// Combine all tools into a single registry
const ALL_TOOLS = {
// Basic Commands - 基础命令
...TouchCommands,
...ControlCommands,
...ColorCommands,
...OtherCommands,
// Standard Library - 标准库函数
...MathFunctions,
...StringFunctions,
...TypeConversionFunctions,
...ArrayFunctions,
// UI Commands - 界面命令
...UIControlCommands,
...UIPropertyCommands,
...FloatingWindowCommands,
// Extension Commands - 扩展命令
...ElementCommands,
...DeviceCommands,
...PhoneCommands,
...SysCommands,
// Plugin Commands - 插件命令
...CJsonCommands,
...DateTimeCommands,
...FileCommands,
...TuringCommands,
};
// Handle list tools request
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: Object.values(ALL_TOOLS).map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema,
})),
};
});
// Handle call tool request
server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest) => {
const { name, arguments: args } = request.params;
const tool = Object.values(ALL_TOOLS).find(t => t.name === name);
if (!tool) {
throw new Error(`Unknown tool: ${name}`);
}
try {
return await tool.handler(args as any || {});
} catch (error) {
throw new Error(`Error executing tool ${name}: ${error instanceof Error ? error.message : String(error)}`);
}
});
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
// Server is running
console.error('MQScript MCP Server running on stdio');
}
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.error('Shutting down MQScript MCP Server...');
await server.close();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.error('Shutting down MQScript MCP Server...');
await server.close();
process.exit(0);
});
main().catch((error) => {
console.error('Failed to start MQScript MCP Server:', error);
process.exit(1);
});