tambahan
This commit is contained in:
@@ -19,7 +19,7 @@ interface McpTool {
|
|||||||
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions (without run()).
|
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions (without run()).
|
||||||
* Each tool corresponds to an endpoint, with metadata stored under `x-props`.
|
* Each tool corresponds to an endpoint, with metadata stored under `x-props`.
|
||||||
*/
|
*/
|
||||||
export function convertOpenApiToMcpTools(openApiJson: any, baseUrl: string = ""): McpTool[] {
|
export function convertOpenApiToMcpTools(openApiJson: any): McpTool[] {
|
||||||
const tools: McpTool[] = [];
|
const tools: McpTool[] = [];
|
||||||
const paths = openApiJson.paths || {};
|
const paths = openApiJson.paths || {};
|
||||||
|
|
||||||
@@ -92,9 +92,14 @@ function cleanToolName(name: string): string {
|
|||||||
// const tools = convertOpenApiToMcpTools(openApiJson, "https://api.wibudev.com");
|
// const tools = convertOpenApiToMcpTools(openApiJson, "https://api.wibudev.com");
|
||||||
// console.log(JSON.stringify(tools, null, 2));
|
// console.log(JSON.stringify(tools, null, 2));
|
||||||
|
|
||||||
if (import.meta.main) {
|
export async function getMcpTools(){
|
||||||
const data = await fetch("http://localhost:3000/docs/json");
|
const data = await fetch(`${process.env.BUN_PUBLIC_BASE_URL}/docs/json`);
|
||||||
const openApiJson = await data.json();
|
const openApiJson = await data.json();
|
||||||
const tools = convertOpenApiToMcpTools(openApiJson, "http://localhost:3000");
|
const tools = convertOpenApiToMcpTools(openApiJson);
|
||||||
|
return tools;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (import.meta.main) {
|
||||||
|
const tools = await getMcpTools();
|
||||||
Bun.write("./tools.json", JSON.stringify(tools, null, 2));
|
Bun.write("./tools.json", JSON.stringify(tools, null, 2));
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import tools from "./../../../tools.json";
|
import { getMcpTools } from "../lib/mcp_tool_convert";
|
||||||
|
// import tools from "./../../../tools.json";
|
||||||
|
|
||||||
|
var tools = [] as any[];
|
||||||
|
|
||||||
// =====================
|
// =====================
|
||||||
// MCP Protocol Types
|
// MCP Protocol Types
|
||||||
@@ -151,6 +154,9 @@ export const MCPRoute = new Elysia({
|
|||||||
tags: ["MCP"]
|
tags: ["MCP"]
|
||||||
})
|
})
|
||||||
.post("/mcp", async ({ request, set }) => {
|
.post("/mcp", async ({ request, set }) => {
|
||||||
|
if (!tools.length) {
|
||||||
|
tools = await getMcpTools();
|
||||||
|
}
|
||||||
set.headers["Content-Type"] = "application/json";
|
set.headers["Content-Type"] = "application/json";
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
|
|
||||||
@@ -181,7 +187,10 @@ export const MCPRoute = new Elysia({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Tools list (debug)
|
// Tools list (debug)
|
||||||
.get("/mcp/tools", ({ set }) => {
|
.get("/mcp/tools", async ({ set }) => {
|
||||||
|
if (!tools.length) {
|
||||||
|
tools = await getMcpTools();
|
||||||
|
}
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
return {
|
return {
|
||||||
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
|
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
|
||||||
@@ -204,6 +213,16 @@ export const MCPRoute = new Elysia({
|
|||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
return { status: "ok", timestamp: Date.now(), tools: tools.length };
|
return { status: "ok", timestamp: Date.now(), tools: tools.length };
|
||||||
})
|
})
|
||||||
|
.get("/mcp/init", async ({ set }) => {
|
||||||
|
|
||||||
|
const _tools = await getMcpTools();
|
||||||
|
tools = _tools;
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "MCP initialized",
|
||||||
|
tools: tools.length,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
|
||||||
// CORS
|
// CORS
|
||||||
.options("/mcp", ({ set }) => {
|
.options("/mcp", ({ set }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user