This commit is contained in:
bipproduction
2025-11-20 10:34:25 +08:00
parent db3561b6bf
commit 148ad40712
6 changed files with 1093 additions and 104 deletions

View File

@@ -1,5 +1,15 @@
// mcp_tool_convert.ts
import _ from "lodash";
/**
* This file:
* - preserves exported function names: convertOpenApiToMcpTools and getMcpTools
* - improves resilience when parsing OpenAPI objects
* - emits x-props.parameters array so executeTool can act correctly
* - ensures requestBody is represented as a synthetic `body` parameter when necessary
*/
// == Types
interface McpTool {
name: string;
description: string;
@@ -11,22 +21,24 @@ interface McpTool {
tag?: string;
deprecated?: boolean;
summary?: string;
parameters?: any[]; // added to communicate param locations to executor
};
}
/**
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions.
* - filterTag is matched case-insensitively against operation tags (substring)
*/
export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): McpTool[] {
const tools: McpTool[] = [];
if (!openApiJson || typeof openApiJson !== "object") {
console.warn("Invalid OpenAPI JSON");
return tools;
}
const paths = openApiJson.paths || {};
if (Object.keys(paths).length === 0) {
console.warn("No paths found in OpenAPI spec");
return tools;
@@ -34,20 +46,19 @@ export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): M
for (const [path, methods] of Object.entries(paths)) {
if (!path || typeof path !== "string") continue;
if (!methods || typeof methods !== "object") continue;
for (const [method, operation] of Object.entries<any>(methods)) {
const validMethods = ["get", "post", "put", "delete", "patch", "head", "options"];
if (!validMethods.includes(method.toLowerCase())) continue;
if (!operation || typeof operation !== "object") continue;
const tags: string[] = Array.isArray(operation.tags) ? operation.tags : [];
if (!tags.length || !tags.some(t =>
typeof t === "string" && t.toLowerCase().includes(filterTag)
)) continue;
// If filterTag provided, require at least one tag to include it (case-insensitive)
if (filterTag && (!tags.length || !tags.some(t => typeof t === "string" && t.toLowerCase().includes(filterTag.toLowerCase())))) {
continue;
}
try {
const tool = createToolFromOperation(path, method, operation, tags);
@@ -65,7 +76,8 @@ export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): M
}
/**
* Buat MCP tool dari operation OpenAPI
* Create MCP tool from an OpenAPI operation.
* - Ensures x-props.parameters exists and describes path/query/header/cookie/requestBody
*/
function createToolFromOperation(
path: string,
@@ -87,16 +99,69 @@ function createToolFromOperation(
operation.summary ||
`Execute ${method.toUpperCase()} ${path}`;
// ✅ Extract schema berdasarkan method
// Build parameters array for executor
const parameters: any[] = [];
if (Array.isArray(operation.parameters)) {
for (const p of operation.parameters) {
try {
// copy essential fields
const paramEntry: any = {
name: p.name,
in: p.in,
required: !!p.required,
description: p.description,
schema: p.schema || { type: "string" },
};
parameters.push(paramEntry);
} catch (err) {
console.warn("Skipping invalid parameter:", p, err);
}
}
}
// If requestBody exists, synthesize a single `body` parameter so the executor can pick it up.
// We do not try to expand complex requestBody schemas into multiple parameters here — inputSchema covers that.
if (operation.requestBody && typeof operation.requestBody === "object") {
// prefer application/json schema, fallback to first available
const content = operation.requestBody.content || {};
let schemaCandidate: any = null;
const preferred = ["application/json", "multipart/form-data", "application/x-www-form-urlencoded"];
for (const c of preferred) {
if (content[c]?.schema) {
schemaCandidate = content[c].schema;
break;
}
}
if (!schemaCandidate) {
const entries = Object.entries(content);
if (entries.length > 0 && (entries[0] as any)[1]?.schema) {
schemaCandidate = (entries[0] as any)[1].schema;
}
}
// Add synthetic body param (name "body")
parameters.push({
name: "body",
in: "requestBody",
required: !!operation.requestBody.required,
schema: schemaCandidate || { type: "object" },
description: operation.requestBody.description || "Request body",
});
}
// Extract input schema for UI (query/path/header -> properties OR requestBody schema)
let schema;
if (method.toLowerCase() === "get") {
// ✅ Untuk GET, ambil dari parameters (query/path)
if (method.toLowerCase() === "get" || method.toLowerCase() === "delete" || method.toLowerCase() === "head") {
schema = extractParametersSchema(operation.parameters || []);
} else {
// ✅ Untuk POST/PUT/etc, ambil dari requestBody
schema = extractRequestBodySchema(operation);
// if no requestBody but parameters exist, fall back to parameters schema
if (!schema) {
schema = extractParametersSchema(operation.parameters || []);
}
}
const inputSchema = createInputSchema(schema);
return {
@@ -109,6 +174,7 @@ function createToolFromOperation(
tag: tags[0],
deprecated: operation.deprecated || false,
summary: operation.summary,
parameters, // executor will rely on this
},
inputSchema,
};
@@ -119,9 +185,10 @@ function createToolFromOperation(
}
/**
* Extract schema dari parameters (untuk GET requests)
* Extract schema dari parameters (untuk GET/DELETE requests)
* - returns null if no parameters
*/
function extractParametersSchema(parameters: any[]): any {
function extractParametersSchema(parameters: any[]): any | null {
if (!Array.isArray(parameters) || parameters.length === 0) {
return null;
}
@@ -132,7 +199,7 @@ function extractParametersSchema(parameters: any[]): any {
for (const param of parameters) {
if (!param || typeof param !== "object") continue;
// Support path, query, dan header parameters
// Support path, query, dan header parameters
if (["path", "query", "header"].includes(param.in)) {
const paramName = param.name;
if (!paramName || typeof paramName !== "string") continue;
@@ -142,7 +209,7 @@ function extractParametersSchema(parameters: any[]): any {
description: param.description || `${param.in} parameter: ${paramName}`,
};
// ✅ Copy field tambahan dari schema
// copy allowed fields
if (param.schema) {
const allowedFields = ["examples", "example", "default", "enum", "pattern", "minLength", "maxLength", "minimum", "maximum", "format"];
for (const field of allowedFields) {
@@ -171,8 +238,9 @@ function extractParametersSchema(parameters: any[]): any {
/**
* Extract schema dari requestBody (untuk POST/PUT/etc requests)
* - prefers application/json, handles form-data, urlencoded fallbacks
*/
function extractRequestBodySchema(operation: any): any {
function extractRequestBodySchema(operation: any): any | null {
if (!operation.requestBody?.content) {
return null;
}
@@ -203,6 +271,7 @@ function extractRequestBodySchema(operation: any): any {
/**
* Buat input schema yang valid untuk MCP
* - preserves optional flags, required semantics, and nested properties
*/
function createInputSchema(schema: any): any {
const defaultSchema = {
@@ -229,11 +298,10 @@ function createInputSchema(schema: any): any {
if (cleanProp) {
properties[key] = cleanProp;
// ✅ PERBAIKAN: Check optional flag dengan benar
// Check optional flag properly
const isOptional = prop?.optional === true || prop?.optional === "true";
const isInRequired = originalRequired.includes(key);
// ✅ Hanya masukkan ke required jika memang required DAN bukan optional
if (isInRequired && !isOptional) {
required.push(key);
}
@@ -243,6 +311,9 @@ function createInputSchema(schema: any): any {
continue;
}
}
} else if (schema.type === "array" && schema.items) {
// represent top-level array as object with items property to keep inputSchema shape predictable
properties["items"] = cleanProperty(schema.items) || { type: "string" };
}
return {
@@ -259,6 +330,7 @@ function createInputSchema(schema: any): any {
/**
* Bersihkan property dari field custom
* - preserves nested structures, arrays, and combiners (oneOf/anyOf/allOf)
*/
function cleanProperty(prop: any): any | null {
if (!prop || typeof prop !== "object") {
@@ -301,7 +373,7 @@ function cleanProperty(prop: any): any | null {
cleaned.properties[key] = cleanedNested;
}
}
if (Array.isArray(prop.required)) {
cleaned.required = prop.required.filter((r: any) => typeof r === "string");
}
@@ -342,7 +414,7 @@ function cleanToolName(name: string): string {
.replace(/[^a-zA-Z0-9_]/g, "_")
.replace(/_+/g, "_")
.replace(/^_|_$/g, "")
// ❗️ METHOD PREFIX TIDAK DIHAPUS LAGI (agar tidak duplicate)
// keep lowercase and stable
.toLowerCase()
|| "unnamed_tool";
} catch (error) {
@@ -351,30 +423,29 @@ function cleanToolName(name: string): string {
}
}
/**
* Ambil OpenAPI JSON dari endpoint dan konversi ke tools MCP
* - preserves exported name getMcpTools
* - robust error handling and console diagnostics
*/
export async function getMcpTools(url: string, filterTag: string): Promise<McpTool[]> {
try {
console.log(`Fetching OpenAPI spec from: ${url}`);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const openApiJson = await response.json();
const tools = convertOpenApiToMcpTools(openApiJson, filterTag);
console.log(`✅ Successfully generated ${tools.length} MCP tools`);
return tools;
} catch (error) {
console.error("Error fetching MCP tools:", error);
throw error;
}
}