tambahannnya
This commit is contained in:
@@ -3,17 +3,40 @@ import {
|
||||
INodeTypeDescription,
|
||||
IWebhookFunctions,
|
||||
IWebhookResponseData,
|
||||
ILoadOptionsFunctions,
|
||||
INodePropertyOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { getMcpTools } from "../lib/mcp_tool_convert";
|
||||
|
||||
let tools: any[] = []; // ✅ cache global tools
|
||||
// ======================================================
|
||||
// Cache tools per URL
|
||||
// ======================================================
|
||||
const toolsCache = new Map<string, any[]>();
|
||||
|
||||
// ======================================================
|
||||
// Load OpenAPI → MCP Tools
|
||||
// ======================================================
|
||||
async function loadTools(openapiUrl: string, filterTag: string) {
|
||||
tools = await getMcpTools(openapiUrl, filterTag);
|
||||
async function loadTools(openapiUrl: string, filterTag: string, forceRefresh = false): Promise<any[]> {
|
||||
const cacheKey = `${openapiUrl}::${filterTag}`;
|
||||
|
||||
// Jika tidak forceRefresh, gunakan cache
|
||||
if (!forceRefresh && toolsCache.has(cacheKey)) {
|
||||
return toolsCache.get(cacheKey)!;
|
||||
}
|
||||
|
||||
console.log(`[MCP] 🔄 Refreshing tools from ${openapiUrl} ...`);
|
||||
const fetched = await getMcpTools(openapiUrl, filterTag);
|
||||
|
||||
// 🟢 Log jumlah & daftar tools
|
||||
console.log(`[MCP] ✅ Loaded ${fetched.length} tools`);
|
||||
if (fetched.length > 0) {
|
||||
console.log(
|
||||
`[MCP] Tools: ${fetched.map((t: any) => t.name).join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
toolsCache.set(cacheKey, fetched);
|
||||
return fetched;
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -24,7 +47,7 @@ type JSONRPCRequest = {
|
||||
id: string | number;
|
||||
method: string;
|
||||
params?: any;
|
||||
credentials?: any; // ✅ tambahan (inject credential)
|
||||
credentials?: any;
|
||||
};
|
||||
|
||||
type JSONRPCResponse = {
|
||||
@@ -81,10 +104,11 @@ async function executeTool(
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// JSON-RPC Handler
|
||||
// JSON-RPC Handler (per node, per request)
|
||||
// ======================================================
|
||||
async function handleMCPRequest(
|
||||
request: JSONRPCRequest
|
||||
request: JSONRPCRequest,
|
||||
tools: any[]
|
||||
): Promise<JSONRPCResponse> {
|
||||
const { id, method, params, credentials } = request;
|
||||
|
||||
@@ -105,12 +129,23 @@ async function handleMCPRequest(
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
tools: tools.map((t) => ({
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
inputSchema: t.inputSchema,
|
||||
"x-props": t["x-props"],
|
||||
})),
|
||||
tools: tools.map((t) => {
|
||||
const inputSchema =
|
||||
typeof t.inputSchema === "object" && t.inputSchema?.type === "object"
|
||||
? t.inputSchema
|
||||
: {
|
||||
type: "object",
|
||||
properties: {},
|
||||
required: [],
|
||||
};
|
||||
|
||||
return {
|
||||
name: t.name,
|
||||
description: t.description || "No description provided",
|
||||
inputSchema,
|
||||
"x-props": t["x-props"],
|
||||
};
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -184,17 +219,11 @@ export class OpenapiMcpServer implements INodeType {
|
||||
defaults: {
|
||||
name: 'OpenAPI MCP Server'
|
||||
},
|
||||
|
||||
credentials: [
|
||||
{
|
||||
name: "openapiMcpServerCredentials",
|
||||
required: true,
|
||||
},
|
||||
{ name: "openapiMcpServerCredentials", required: true },
|
||||
],
|
||||
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
@@ -203,7 +232,6 @@ export class OpenapiMcpServer implements INodeType {
|
||||
path: '={{$parameter["path"]}}',
|
||||
},
|
||||
],
|
||||
|
||||
properties: [
|
||||
{
|
||||
displayName: "Path",
|
||||
@@ -224,10 +252,47 @@ export class OpenapiMcpServer implements INodeType {
|
||||
type: "string",
|
||||
default: "",
|
||||
placeholder: "mcp | tag",
|
||||
}
|
||||
},
|
||||
// 🟢 Tambahan agar terlihat jumlah tools di UI
|
||||
{
|
||||
displayName: 'Available Tools (auto-refresh)',
|
||||
name: 'toolList',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'refreshToolList',
|
||||
refreshOnOpen: true, // setiap node dibuka auto refresh
|
||||
},
|
||||
default: '',
|
||||
description: 'Daftar tools yang berhasil dimuat dari OpenAPI',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// ==================================================
|
||||
// LoadOptions untuk tampil di dropdown
|
||||
// ==================================================
|
||||
methods = {
|
||||
loadOptions: {
|
||||
// 🟢 otomatis refetch setiap kali node dibuka
|
||||
async refreshToolList(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const openapiUrl = this.getNodeParameter("openapiUrl", 0) as string;
|
||||
const filterTag = this.getNodeParameter("defaultFilter", 0) as string;
|
||||
|
||||
if (!openapiUrl) {
|
||||
return [{ name: "❌ No OpenAPI URL provided", value: "" }];
|
||||
}
|
||||
|
||||
const tools = await loadTools(openapiUrl, filterTag, true); // force refresh
|
||||
|
||||
return tools.map((t) => ({
|
||||
name: t.name,
|
||||
value: t.name,
|
||||
description: t.description,
|
||||
}));
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// ==================================================
|
||||
// WEBHOOK HANDLER
|
||||
// ==================================================
|
||||
@@ -235,9 +300,8 @@ export class OpenapiMcpServer implements INodeType {
|
||||
const openapiUrl = this.getNodeParameter("openapiUrl", 0) as string;
|
||||
const filterTag = this.getNodeParameter("defaultFilter", 0) as string;
|
||||
|
||||
if (!tools.length) {
|
||||
await loadTools(openapiUrl, filterTag);
|
||||
}
|
||||
// 🟢 selalu refresh (agar node terbaru)
|
||||
const tools = await loadTools(openapiUrl, filterTag, true);
|
||||
|
||||
const creds = await this.getCredentials("openapiMcpServerCredentials") as {
|
||||
baseUrl: string;
|
||||
@@ -248,17 +312,17 @@ export class OpenapiMcpServer implements INodeType {
|
||||
|
||||
if (Array.isArray(body)) {
|
||||
const responses = body.map((r) =>
|
||||
handleMCPRequest({ ...r, credentials: creds })
|
||||
handleMCPRequest({ ...r, credentials: creds }, tools)
|
||||
);
|
||||
return {
|
||||
webhookResponse: await Promise.all(responses),
|
||||
};
|
||||
}
|
||||
|
||||
const single = await handleMCPRequest({
|
||||
...(body as JSONRPCRequest),
|
||||
credentials: creds,
|
||||
});
|
||||
const single = await handleMCPRequest(
|
||||
{ ...(body as JSONRPCRequest), credentials: creds },
|
||||
tools
|
||||
);
|
||||
|
||||
return {
|
||||
webhookResponse: single,
|
||||
|
||||
Reference in New Issue
Block a user