+ "jsCode": "const qType = typeof query;\nlet mcpUrl = '', toolName = '', args = {};\n\nif (qType === 'object' && query !== null) {\n mcpUrl = query.mcp_url || '';\n toolName = query.tool_name || '';\n args = query.arguments || {};\n} else if (qType === 'string') {\n try {\n const parsed = JSON.parse(query);\n mcpUrl = parsed.mcp_url || '';\n toolName = parsed.tool_name || '';\n args = parsed.arguments || {};\n } catch(e) {\n return 'Parse error: query=' + String(query).substring(0,200) + ' type=' + qType;\n }\n} else {\n return 'Unexpected query type: ' + qType + ' value: ' + String(query).substring(0,200);\n}\n\nif (typeof args === 'string') try { args = JSON.parse(args); } catch(e) {}\nif (!mcpUrl || !toolName) return 'Fehler: mcp_url=' + mcpUrl + ' tool_name=' + toolName;\n\nfunction parseSSE(raw) {\n const str = String(raw);\n let lines = str.split('\\n');\n if (lines.length <= 1) lines = str.split('\\\\n');\n for (let i = lines.length - 1; i >= 0; i--) {\n if (lines[i].startsWith('data: ')) {\n return JSON.parse(lines[i].substring(6));\n }\n }\n return null;\n}\n\ntry {\n let authHeader = null;\n try {\n const row = await helpers.httpRequest({method:'GET',url:'{{SUPABASE_URL}}/rest/v1/mcp_registry?mcp_url=eq.'+encodeURIComponent(mcpUrl)+'&select=auth_type,auth_token&limit=1',headers:{apikey:'{{SUPABASE_SERVICE_KEY}}',Authorization:'Bearer {{SUPABASE_SERVICE_KEY}}'},json:true});\n if (row && row[0] && row[0].auth_type && row[0].auth_type !== 'none' && row[0].auth_token) {\n authHeader = row[0].auth_type === 'bearer' ? 'Bearer '+row[0].auth_token : row[0].auth_token;\n }\n } catch(e) {}\n\n const hdrs = {'Content-Type':'application/json','Accept':'application/json, text/event-stream'};\n if (authHeader) hdrs['Authorization'] = authHeader;\n\n const init = await helpers.httpRequest({\n method: 'POST', url: mcpUrl,\n headers: hdrs,\n body: JSON.stringify({jsonrpc:'2.0',id:1,method:'initialize',params:{protocolVersion:'2024-11-05',capabilities:{},clientInfo:{name:'n8n-claw-bg-checker',version:'1.0'}}}),\n returnFullResponse: true, encoding: 'utf-8', json: false\n });\n const sid = init.headers && init.headers['mcp-session-id'];\n if (sid) hdrs['mcp-session-id'] = sid;\n\n await helpers.httpRequest({\n method: 'POST', url: mcpUrl,\n headers: hdrs,\n body: JSON.stringify({jsonrpc:'2.0',method:'notifications/initialized'}),\n json: false\n });\n\n const listResp = await helpers.httpRequest({\n method: 'POST', url: mcpUrl,\n headers: hdrs,\n body: JSON.stringify({jsonrpc:'2.0',id:2,method:'tools/list',params:{}}),\n json: false\n });\n const listResult = parseSSE(listResp);\n if (listResult && listResult.result && listResult.result.tools) {\n const tool = listResult.result.tools.find(t => t.name === toolName);\n if (tool && tool.inputSchema && tool.inputSchema.required) {\n for (const req of tool.inputSchema.required) {\n if (args[req] === undefined || args[req] === null) {\n args[req] = '';\n }\n }\n }\n }\n\n const resp = await helpers.httpRequest({\n method: 'POST', url: mcpUrl,\n headers: hdrs,\n body: JSON.stringify({jsonrpc:'2.0',id:3,method:'tools/call',params:{name:toolName,arguments:args}}),\n returnFullResponse: true, encoding: 'utf-8', json: false\n });\n\n const result = parseSSE(resp.body || resp);\n if (!result) return 'MCP: Keine Daten. Response: ' + String(resp.body || resp).substring(0,500);\n if (result.error) return 'MCP Error: ' + JSON.stringify(result.error);\n return result.result?.content?.[0]?.text || JSON.stringify(result.result);\n} catch(e) {\n return 'MCP Error: ' + e.message;\n}",
0 commit comments