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()).
|
||||
* 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 paths = openApiJson.paths || {};
|
||||
|
||||
@@ -92,9 +92,14 @@ function cleanToolName(name: string): string {
|
||||
// const tools = convertOpenApiToMcpTools(openApiJson, "https://api.wibudev.com");
|
||||
// console.log(JSON.stringify(tools, null, 2));
|
||||
|
||||
if (import.meta.main) {
|
||||
const data = await fetch("http://localhost:3000/docs/json");
|
||||
export async function getMcpTools(){
|
||||
const data = await fetch(`${process.env.BUN_PUBLIC_BASE_URL}/docs/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));
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
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
|
||||
@@ -151,6 +154,9 @@ export const MCPRoute = new Elysia({
|
||||
tags: ["MCP"]
|
||||
})
|
||||
.post("/mcp", async ({ request, set }) => {
|
||||
if (!tools.length) {
|
||||
tools = await getMcpTools();
|
||||
}
|
||||
set.headers["Content-Type"] = "application/json";
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
|
||||
@@ -181,7 +187,10 @@ export const MCPRoute = new Elysia({
|
||||
})
|
||||
|
||||
// Tools list (debug)
|
||||
.get("/mcp/tools", ({ set }) => {
|
||||
.get("/mcp/tools", async ({ set }) => {
|
||||
if (!tools.length) {
|
||||
tools = await getMcpTools();
|
||||
}
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
return {
|
||||
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
|
||||
@@ -204,6 +213,16 @@ export const MCPRoute = new Elysia({
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
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
|
||||
.options("/mcp", ({ set }) => {
|
||||
|
||||
Reference in New Issue
Block a user