tamabahan baru

This commit is contained in:
bipproduction
2025-11-12 01:13:06 +08:00
commit dda4a65584
5 changed files with 333 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenapiMcpServerCredentials = void 0;
class OpenapiMcpServerCredentials {
constructor() {
this.name = "openapiMcpServerCredentials";
this.displayName = "OpenAPI MCP Server Credentials";
this.properties = [
{
displayName: "Base URL",
name: "baseUrl",
type: "string",
default: "",
placeholder: "https://api.example.com",
description: "Masukkan URL dasar API tanpa garis miring di akhir",
required: true,
},
{
displayName: "Bearer Token",
name: "token",
type: "string",
default: "",
typeOptions: { password: true },
description: "Masukkan token autentikasi Bearer (tanpa 'Bearer ' di depannya)",
required: true,
},
];
}
}
exports.OpenapiMcpServerCredentials = OpenapiMcpServerCredentials;

79
lib/mcp_tool_convert.js Normal file
View File

@@ -0,0 +1,79 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertOpenApiToMcpTools = convertOpenApiToMcpTools;
exports.getMcpTools = getMcpTools;
const lodash_1 = __importDefault(require("lodash"));
/**
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions (without run()).
* Hanya menyertakan endpoint yang memiliki tag berisi "mcp".
*/
function convertOpenApiToMcpTools(openApiJson) {
var _a, _b, _c;
const tools = [];
const paths = openApiJson.paths || {};
for (const [path, methods] of Object.entries(paths)) {
// ✅ skip semua path internal MCP
if (path.startsWith("/mcp"))
continue;
for (const [method, operation] of Object.entries(methods)) {
const tags = Array.isArray(operation.tags) ? operation.tags : [];
// ✅ exclude semua yang tidak punya tag atau tag-nya tidak mengandung "mcp"
if (!tags.length || !tags.some(t => t.toLowerCase().includes("mcp")))
continue;
const rawName = lodash_1.default.snakeCase(operation.operationId || `${method}_${path}`) || "unnamed_tool";
const name = cleanToolName(rawName);
const description = operation.description ||
operation.summary ||
`Execute ${method.toUpperCase()} ${path}`;
const schema = ((_c = (_b = (_a = operation.requestBody) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b["application/json"]) === null || _c === void 0 ? void 0 : _c.schema) || {
type: "object",
properties: {},
additionalProperties: true,
};
const tool = {
name,
description,
"x-props": {
method: method.toUpperCase(),
path,
operationId: operation.operationId,
tag: tags[0],
deprecated: operation.deprecated || false,
summary: operation.summary,
},
inputSchema: Object.assign(Object.assign({}, schema), { additionalProperties: true, $schema: "http://json-schema.org/draft-07/schema#" }),
};
tools.push(tool);
}
}
return tools;
}
/**
* Bersihkan nama agar valid untuk digunakan sebagai tool name
* - hapus karakter spesial
* - ubah slash jadi underscore
* - hilangkan prefix umum (get_, post_, api_, dll)
* - rapikan underscore berganda
*/
function cleanToolName(name) {
return name
.replace(/[{}]/g, "")
.replace(/[^a-zA-Z0-9_]/g, "_")
.replace(/_+/g, "_")
.replace(/^_|_$/g, "")
.replace(/^(get|post|put|delete|patch|api)_/i, "")
.replace(/^(get_|post_|put_|delete_|patch_|api_)+/gi, "")
.replace(/(^_|_$)/g, "");
}
/**
* Ambil OpenAPI JSON dari endpoint dan konversi ke tools MCP
*/
async function getMcpTools(url) {
const data = await fetch(url);
const openApiJson = await data.json();
const tools = convertOpenApiToMcpTools(openApiJson);
return tools;
}

184
nodes/OpenapiMcpServer.js Normal file
View File

@@ -0,0 +1,184 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenapiMcpServer = void 0;
const mcp_tool_convert_1 = require("../lib/mcp_tool_convert");
let tools = []; // ✅ cache global tools
// ======================================================
// Load OpenAPI → MCP Tools
// ======================================================
async function loadTools(openapiUrl) {
tools = await (0, mcp_tool_convert_1.getMcpTools)(openapiUrl);
}
// ======================================================
// Eksekusi Tool HTTP
// ======================================================
async function executeTool(tool, args = {}, baseUrl, token) {
const x = tool["x-props"] || {};
const method = (x.method || "GET").toUpperCase();
const path = x.path || `/${tool.name}`;
const url = `${baseUrl}${path}`;
const opts = {
method,
headers: Object.assign({ "Content-Type": "application/json" }, (token ? { Authorization: `Bearer ${token}` } : {})),
};
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
opts.body = JSON.stringify(args || {});
}
const res = await fetch(url, opts);
const contentType = res.headers.get("content-type") || "";
const data = contentType.includes("application/json")
? await res.json()
: await res.text();
return {
success: res.ok,
status: res.status,
method,
path,
data,
};
}
// ======================================================
// JSON-RPC Handler
// ======================================================
async function handleMCPRequest(request) {
const { id, method, params, credentials } = request;
switch (method) {
case "initialize":
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: { tools: {} },
serverInfo: { name: "n8n-mcp-server", version: "1.0.0" },
},
};
case "tools/list":
return {
jsonrpc: "2.0",
id,
result: {
tools: tools.map((t) => ({
name: t.name,
description: t.description,
inputSchema: t.inputSchema,
"x-props": t["x-props"],
})),
},
};
case "tools/call": {
const toolName = params === null || params === void 0 ? void 0 : params.name;
const tool = tools.find((t) => t.name === toolName);
if (!tool) {
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Tool '${toolName}' not found` },
};
}
try {
const baseUrl = credentials === null || credentials === void 0 ? void 0 : credentials.baseUrl;
const token = credentials === null || credentials === void 0 ? void 0 : credentials.token;
const result = await executeTool(tool, (params === null || params === void 0 ? void 0 : params.arguments) || {}, baseUrl, token);
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
},
};
}
catch (err) {
return {
jsonrpc: "2.0",
id,
error: { code: -32603, message: err.message },
};
}
}
case "ping":
return { jsonrpc: "2.0", id, result: {} };
default:
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method '${method}' not found` },
};
}
}
// ======================================================
// NODE MCP TRIGGER
// ======================================================
class OpenapiMcpServer {
constructor() {
this.description = {
displayName: 'OpenAPI MCP Server',
name: 'openapiMcpServer',
group: ['trigger'],
version: 1,
description: 'Runs an MCP Server inside n8n',
icon: 'fa:server',
defaults: {
name: 'OpenAPI MCP Server'
},
credentials: [
{
name: "openapiMcpServerCredentials",
required: true,
},
],
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: '={{$parameter["path"]}}',
},
],
properties: [
{
displayName: "Path",
name: "path",
type: "string",
default: "mcp",
},
{
displayName: "OpenAPI URL",
name: "openapiUrl",
type: "string",
default: "",
placeholder: "https://example.com/openapi.json",
},
],
};
}
// ==================================================
// WEBHOOK HANDLER
// ==================================================
async webhook() {
const openapiUrl = this.getNodeParameter("openapiUrl", 0);
if (!tools.length) {
await loadTools(openapiUrl);
}
const creds = await this.getCredentials("openapiMcpServerCredentials");
const body = this.getBodyData();
if (Array.isArray(body)) {
const responses = body.map((r) => handleMCPRequest(Object.assign(Object.assign({}, r), { credentials: creds })));
return {
webhookResponse: await Promise.all(responses),
};
}
const single = await handleMCPRequest(Object.assign(Object.assign({}, body), { credentials: creds }));
return {
webhookResponse: single,
};
}
}
exports.OpenapiMcpServer = OpenapiMcpServer;

17
nodes/icon.svg Normal file
View File

@@ -0,0 +1,17 @@
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128" aria-label="Icon AB square">
<defs>
<style>
.bg { fill: #111827; rx: 20; }
.letters { fill: #f9fafb; font-family: "Inter", "Segoe UI", Roboto, sans-serif; font-weight: 800; font-size: 56px; }
</style>
</defs>
<!-- rounded square background -->
<rect class="bg" width="128" height="128" rx="20" ry="20"/>
<!-- letters -->
<text class="letters" x="64" y="78" text-anchor="middle" dominant-baseline="middle">mcp</text>
</svg>

After

Width:  |  Height:  |  Size: 550 B

23
package.json Normal file
View File

@@ -0,0 +1,23 @@
{
"name": "n8n-nodes-openapi-mcp-server",
"version": "1.1.2",
"keywords": [
"n8n",
"n8n-nodes"
],
"author": {
"name": "makuro",
"phone": "6289697338821"
},
"license": "ISC",
"description": "",
"n8n": {
"nodes": [
"nodes/OpenapiMcpServer.js"
],
"n8nNodesApiVersion": 1,
"credentials": [
"credentials/OpenapiMcpServerCredentials.credentials.js"
]
}
}