tambahan
This commit is contained in:
30
src/credentials/OpenapiMcpServerCredentials.credentials.ts
Normal file
30
src/credentials/OpenapiMcpServerCredentials.credentials.ts
Normal 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
101
src/lib/mcp_tool_convert.ts
Normal 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;
|
||||
}
|
||||
267
src/nodes/OpenapiMcpServer.ts
Normal file
267
src/nodes/OpenapiMcpServer.ts
Normal 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
17
src/nodes/icon.svg
Normal 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 |
Reference in New Issue
Block a user