This commit is contained in:
bipproduction
2025-11-12 10:14:48 +08:00
commit 9f11e74294
17 changed files with 1919 additions and 0 deletions

34
.gitignore vendored Normal file
View File

@@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules
# output
out
dist
*.tgz
# code coverage
coverage
*.lcov
# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# caches
.eslintcache
.cache
*.tsbuildinfo
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

15
README.md Normal file
View File

@@ -0,0 +1,15 @@
# mcp-server
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.3.0. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.

1035
bun.lock Normal file

File diff suppressed because it is too large Load Diff

17
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

1
index.ts Normal file
View File

@@ -0,0 +1 @@
console.log("Hello via Bun!");

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;

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;
}

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;

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

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"
]
}
}

29
package.json Normal file
View File

@@ -0,0 +1,29 @@
{
"name": "n8n-mcp-server",
"type": "module",
"private": true,
"scripts": {
"gen": "bunx tsc && cp package.txt n8n-nodes-openapi-mcp-server/package.json && cp icon.svg n8n-nodes-openapi-mcp-server/nodes/icon.svg"
},
"dependencies": {
"express": "^5.1.0",
"lodash": "^4.17.21",
"n8n-core": "^1.117.1",
"n8n-workflow": "^1.116.0",
"nock": "^14.0.10",
"ssh2": "^1.17.0"
},
"devDependencies": {
"@types/bun": "latest",
"@types/lodash": "^4.17.20",
"@types/express": "^5.0.5",
"@types/node": "^24.10.0",
"@types/ssh2": "^1.15.5",
"prettier": "^3.6.2",
"ts-node": "^10.9.2"
},
"peerDependencies": {
"typescript": "^5"
}
}

23
package.txt 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"
]
}
}

View File

@@ -0,0 +1,30 @@
import { ICredentialType, INodeProperties } from "n8n-workflow";
export class OpenapiMcpServerCredentials implements ICredentialType {
name = "openapiMcpServerCredentials";
displayName = "OpenAPI MCP Server Credentials";
properties: INodeProperties[] = [
{
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,
},
];
}

101
src/lib/mcp_tool_convert.ts Normal file
View File

@@ -0,0 +1,101 @@
import _ from "lodash";
interface McpTool {
name: string;
description: string;
inputSchema: any;
"x-props": {
method: string;
path: string;
operationId?: string;
tag?: string;
deprecated?: boolean;
summary?: string;
};
}
/**
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions (without run()).
* Hanya menyertakan endpoint yang memiliki tag berisi "mcp".
*/
export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): McpTool[] {
const tools: McpTool[] = [];
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<any>(methods as any)) {
const tags: string[] = 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(filterTag))) continue;
const rawName = _.snakeCase(operation.operationId || `${method}_${path}`) || "unnamed_tool";
const name = cleanToolName(rawName);
const description =
operation.description ||
operation.summary ||
`Execute ${method.toUpperCase()} ${path}`;
const schema =
operation.requestBody?.content?.["application/json"]?.schema || {
type: "object",
properties: {},
additionalProperties: true,
};
const tool: McpTool = {
name,
description,
"x-props": {
method: method.toUpperCase(),
path,
operationId: operation.operationId,
tag: tags[0],
deprecated: operation.deprecated || false,
summary: operation.summary,
},
inputSchema: {
...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: string): string {
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
*/
export async function getMcpTools(url: string, filterTag: string) {
const data = await fetch(url);
const openApiJson = await data.json();
const tools = convertOpenApiToMcpTools(openApiJson, filterTag);
return tools;
}

View File

@@ -0,0 +1,267 @@
import {
INodeType,
INodeTypeDescription,
IWebhookFunctions,
IWebhookResponseData,
} from 'n8n-workflow';
import { getMcpTools } from "../lib/mcp_tool_convert";
let tools: any[] = []; // ✅ cache global tools
// ======================================================
// Load OpenAPI → MCP Tools
// ======================================================
async function loadTools(openapiUrl: string, filterTag: string) {
tools = await getMcpTools(openapiUrl, filterTag);
}
// ======================================================
// JSON-RPC Types
// ======================================================
type JSONRPCRequest = {
jsonrpc: "2.0";
id: string | number;
method: string;
params?: any;
credentials?: any; // ✅ tambahan (inject credential)
};
type JSONRPCResponse = {
jsonrpc: "2.0";
id: string | number;
result?: any;
error?: {
code: number;
message: string;
data?: any;
};
};
// ======================================================
// Eksekusi Tool HTTP
// ======================================================
async function executeTool(
tool: any,
args: Record<string, any> = {},
baseUrl: string,
token?: string
) {
const x = tool["x-props"] || {};
const method = (x.method || "GET").toUpperCase();
const path = x.path || `/${tool.name}`;
const url = `${baseUrl}${path}`;
const opts: RequestInit = {
method,
headers: {
"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: JSONRPCRequest
): Promise<JSONRPCResponse> {
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?.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?.baseUrl;
const token = credentials?.token;
const result = await executeTool(
tool,
params?.arguments || {},
baseUrl,
token
);
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
},
};
} catch (err: any) {
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
// ======================================================
export class OpenapiMcpServer implements INodeType {
description: INodeTypeDescription = {
displayName: 'OpenAPI MCP Server',
name: 'openapiMcpServer',
group: ['trigger'],
version: 1,
description: 'Runs an MCP Server inside n8n',
icon: 'file:icon.svg',
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",
},
{
displayName: "Default Filter",
name: "defaultFilter",
type: "string",
default: "",
placeholder: "mcp | tag",
}
],
};
// ==================================================
// WEBHOOK HANDLER
// ==================================================
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const openapiUrl = this.getNodeParameter("openapiUrl", 0) as string;
const filterTag = this.getNodeParameter("defaultFilter", 0) as string;
if (!tools.length) {
await loadTools(openapiUrl, filterTag);
}
const creds = await this.getCredentials("openapiMcpServerCredentials") as {
baseUrl: string;
token: string;
};
const body = this.getBodyData();
if (Array.isArray(body)) {
const responses = body.map((r) =>
handleMCPRequest({ ...r, credentials: creds })
);
return {
webhookResponse: await Promise.all(responses),
};
}
const single = await handleMCPRequest({
...(body as JSONRPCRequest),
credentials: creds,
});
return {
webhookResponse: single,
};
}
}

17
src/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

17
tsconfig.json Normal file
View File

@@ -0,0 +1,17 @@
{
"compilerOptions": {
"lib": ["es2021", "dom"],
"module": "commonjs",
"target": "es2017",
"outDir": "n8n-nodes-openapi-mcp-server",
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["src/**/*.ts"]
}