1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| class MCPToolRegistry { constructor() { this.tools = new Map(); this.capabilities = new Map(); }
registerTool(name, handler, metadata = {}) { if (this.tools.has(name)) { throw new Error(`Tool already registered: ${name}`); }
this.tools.set(name, { handler: handler, metadata: metadata, registeredAt: Date.now() });
this.capabilities.set(name, { name: name, description: metadata.description || 'No description', parameters: metadata.parameters || {}, returns: metadata.returns || {}, category: metadata.category || 'general', version: metadata.version || '1.0', deprecated: metadata.deprecated || false });
console.log(`Tool registered: ${name}`); }
async executeTool(name, parameters) { const tool = this.tools.get(name); if (!tool) { throw new Error(`Tool not found: ${name}`); }
try { if (!this.validateParameters(parameters, tool.metadata.parameters)) { throw new Error('Invalid parameters for tool'); }
const startTime = Date.now(); const result = await tool.handler(parameters); const duration = Date.now() - startTime;
return { success: true, data: result, metadata: { executionTime: duration, toolVersion: tool.metadata.version, requestId: this.generateRequestId() } }; } catch (error) { return { success: false, error: error.message, metadata: { errorType: error.constructor.name, requestId: this.generateRequestId() } }; } }
validateParameters(params, schema) { if (!schema || Object.keys(schema).length === 0) { return true; }
for (const [paramName, paramSchema] of Object.entries(schema)) { if (paramSchema.required && params[paramName] === undefined) { return false; }
if (params[paramName] !== undefined) { const value = params[paramName]; const expectedType = paramSchema.type;
if (expectedType && typeof value !== expectedType) { return false; }
if (paramSchema.min && value < paramSchema.min) { return false; }
if (paramSchema.max && value > paramSchema.max) { return false; }
if (typeof value === 'string') { if (paramSchema.minLength && value.length < paramSchema.minLength) { return false; }
if (paramSchema.maxLength && value.length > paramSchema.maxLength) { return false; } } } }
return true; }
getAvailableTools() { return Array.from(this.capabilities.values()); }
getToolDetails(name) { return this.capabilities.get(name); }
searchTools(query) { const tools = this.getAvailableTools(); return tools.filter(tool => tool.name.toLowerCase().includes(query.toLowerCase()) || tool.description.toLowerCase().includes(query.toLowerCase()) || tool.category.toLowerCase().includes(query.toLowerCase()) ); }
generateRequestId() { return `req_${Date.now()}_${Math.random().toString(36).substr(2, 8)}`; } }
const registry = new MCPToolRegistry();
registry.registerTool('weather.query', async (params) => { const { location, unit = 'celsius', forecast_days = 1 } = params;
const weatherData = { location: location, temperature: Math.floor(Math.random() * 30) + 15, humidity: Math.floor(Math.random() * 50) + 30, condition: ['sunny', 'cloudy', 'rainy'][Math.floor(Math.random() * 3)]);
return weatherData; }, { description: '查询指定地点的天气信息', category: 'weather', version: '1.0', parameters: { location: { type: 'string', required: true, description: '城市名称或坐标' }, unit: { type: 'string', required: false, default: 'celsius', description: '温度单位(celsius/fahrenheit)' }, forecast_days: { type: 'number', required: false, default: 1, min: 1, max: 7, description: '预报天数(1-7)' } }, returns: { type: 'object', properties: { location: 'string', temperature: 'number', humidity: 'number', condition: 'string' } } });
registry.registerTool('database.query', async (params) => { const { query, parameters = [], connection_id } = params;
const mockResults = [ { id: 1, name: 'John Doe', email: 'john@example.com' }, { id: 2, name: 'Jane Smith', email: 'jane@example.com' } ];
return { results: mockResults, rowCount: mockResults.length, executionTime: 42 }; }, { description: '执行数据库查询操作', category: 'database', version: '1.0', parameters: { query: { type: 'string', required: true, maxLength: 1000, description: 'SQL查询语句' }, parameters: { type: 'array', required: false, default: [], description: '查询参数数组' }, connection_id: { type: 'string', required: true, description: '数据库连接ID' } } });
|