Compare commits

..

14 Commits

Author SHA1 Message Date
d7e77da16a upd: api detail pengaduan jenna ai 2025-11-26 14:29:00 +08:00
decf6dd972 Merge pull request 'upd: dashboard admin' (#39) from amalia/26-nov-25 into main
Reviewed-on: http://wibugit.wibudev.com/wibu/jenna-mcp/pulls/39
2025-11-26 12:14:55 +08:00
5b72f1a9cc upd: dashboard admin
Deskripsi:
- login input
- login redirect sesuai dg akses
- tampilan jika tidak ada data ttd pada setting desa
- disable button pada list kategori pengaduan dg value id == lainnya
- disable button aksi pada list role dg value id == developer
- tidak menampilkan list data menu akses pada modal tambah dan edi role
- tampilan list permission pada table role
- order data permission yg telah terpilih sesuai dengan data json menu

NO Issues
2025-11-26 12:14:09 +08:00
acb5ae7cd1 Merge pull request 'amalia/25-nov-25' (#38) from amalia/25-nov-25 into main
Reviewed-on: http://wibugit.wibudev.com/wibu/jenna-mcp/pulls/38
2025-11-25 17:27:07 +08:00
6bdb0246c9 upd: api upload
Deskripsi:
- summary upload file form data

No Issues'
2025-11-25 16:47:47 +08:00
3f68f212cd upd: api jenna ai
Deskripsi:
- api upsert warga pada create pengaduan
- tampilan detail pengaduan jika tidak ada gambar

NO Issues
2025-11-25 16:17:44 +08:00
bipproduction
e0236a907f tambahan 2025-11-25 15:11:22 +08:00
e4189d40e9 Merge pull request 'amalia/25-nov-25' (#37) from amalia/25-nov-25 into main
Reviewed-on: http://wibugit.wibudev.com/wibu/jenna-mcp/pulls/37
2025-11-25 15:01:35 +08:00
94e7604afb upd: api jenna ai
Deskripsi:
- tambah pengaduan

NO Issues
2025-11-25 15:01:01 +08:00
a253d40d19 upd: api jenna ai
Deskripsi:
- tambah pengaduan

NO Issues
2025-11-25 14:58:40 +08:00
26c7357ca3 Merge pull request 'amalia/25-nov-25' (#36) from amalia/25-nov-25 into main
Reviewed-on: http://wibugit.wibudev.com/wibu/jenna-mcp/pulls/36
2025-11-25 14:05:46 +08:00
15c5140902 upd: dashboard admin
Deskripsi:
- nama field pada modal edit dan tambah role user

No Issues
2025-11-25 12:17:03 +08:00
c5b1452955 upd: dashboard admin
Deskripsi:
- tambah role user
- edit role user

No Issues
2025-11-25 12:15:29 +08:00
e1431fafb2 Merge pull request 'amalia/24-nov-25' (#35) from amalia/24-nov-25 into main
Reviewed-on: http://wibugit.wibudev.com/wibu/jenna-mcp/pulls/35
2025-11-24 17:41:22 +08:00
16 changed files with 1481 additions and 448 deletions

250
bak/mcp_route.ts.txt Normal file
View File

@@ -0,0 +1,250 @@
import { Elysia } from "elysia";
import { getMcpTools } from "../lib/mcp_tool_convert";
var tools = [] as any[];
const OPENAPI_URL = process.env.BUN_PUBLIC_BASE_URL + "/docs/json";
const FILTER_TAG = "mcp";
if (!process.env.BUN_PUBLIC_BASE_URL) {
throw new Error("BUN_PUBLIC_BASE_URL environment variable is not set");
}
// =====================
// MCP Protocol Types
// =====================
type JSONRPCRequest = {
jsonrpc: "2.0";
id: string | number;
method: string;
params?: any;
};
type JSONRPCResponse = {
jsonrpc: "2.0";
id: string | number;
result?: any;
error?: {
code: number;
message: string;
data?: any;
};
};
// =====================
// Tool Executor
// =====================
export async function executeTool(
tool: any,
args: Record<string, any> = {},
baseUrl: 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" },
};
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,
};
}
// =====================
// MCP Handler (Async)
// =====================
async function handleMCPRequestAsync(
request: JSONRPCRequest
): Promise<JSONRPCResponse> {
const { id, method, params } = request;
switch (method) {
case "initialize":
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: { tools: {} },
serverInfo: { name: "elysia-mcp-server", version: "1.0.0" },
},
};
case "tools/list":
return {
jsonrpc: "2.0",
id,
result: {
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
description,
inputSchema,
"x-props": x,
})),
},
};
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 =
process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
const result = await executeTool(tool, params?.arguments || {}, baseUrl);
const data = result.data.data;
const isObject = typeof data === "object" && data !== null;
return {
jsonrpc: "2.0",
id,
result: {
content: [
isObject
? { type: "json", data: data }
: { type: "text", text: JSON.stringify(data || result.data || result) },
],
},
};
} catch (error: any) {
return {
jsonrpc: "2.0",
id,
error: { code: -32603, message: error.message },
};
}
}
case "ping":
return { jsonrpc: "2.0", id, result: {} };
default:
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method '${method}' not found` },
};
}
}
// =====================
// Elysia MCP Server
// =====================
export const MCPRoute = new Elysia({
tags: ["MCP Server"]
})
.post("/mcp", async ({ request, set }) => {
if (!tools.length) {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
}
set.headers["Content-Type"] = "application/json";
set.headers["Access-Control-Allow-Origin"] = "*";
try {
const body = await request.json();
if (!Array.isArray(body)) {
const res = await handleMCPRequestAsync(body);
return res;
}
const results = await Promise.all(
body.map((req) => handleMCPRequestAsync(req))
);
return results;
} catch (error: any) {
set.status = 400;
return {
jsonrpc: "2.0",
id: null,
error: {
code: -32700,
message: "Parse error",
data: error.message,
},
};
}
})
// Tools list (debug)
.get("/mcp/tools", async ({ set }) => {
if (!tools.length) {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
}
set.headers["Access-Control-Allow-Origin"] = "*";
return {
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
description,
inputSchema,
"x-props": x,
})),
};
})
// MCP status
.get("/mcp/status", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "active", timestamp: Date.now() };
})
// Health check
.get("/health", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "ok", timestamp: Date.now(), tools: tools.length };
})
.get("/mcp/init", async ({ set }) => {
const _tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
tools = _tools;
return {
success: true,
message: "MCP initialized",
tools: tools.length,
};
})
// CORS
.options("/mcp", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
})
.options("/mcp/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
});

381
bak/mcp_tool_convert.ts.txt Normal file
View File

@@ -0,0 +1,381 @@
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.
*/
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;
}
for (const [path, methods] of Object.entries(paths)) {
if (!path || typeof path !== "string") continue;
if (path.startsWith("/mcp")) 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;
try {
const tool = createToolFromOperation(path, method, operation, tags);
if (tool) {
tools.push(tool);
}
} catch (error) {
console.error(`Error creating tool for ${method.toUpperCase()} ${path}:`, error);
continue;
}
}
}
return tools;
}
/**
* Buat MCP tool dari operation OpenAPI
*/
function createToolFromOperation(
path: string,
method: string,
operation: any,
tags: string[]
): McpTool | null {
try {
const rawName = _.snakeCase(operation.operationId || `${method}_${path}`) || "unnamed_tool";
const name = cleanToolName(rawName);
if (!name || name === "unnamed_tool") {
console.warn(`Invalid tool name for ${method} ${path}`);
return null;
}
const description =
operation.description ||
operation.summary ||
`Execute ${method.toUpperCase()} ${path}`;
// ✅ Extract schema berdasarkan method
let schema;
if (method.toLowerCase() === "get") {
// ✅ Untuk GET, ambil dari parameters (query/path)
schema = extractParametersSchema(operation.parameters || []);
} else {
// ✅ Untuk POST/PUT/etc, ambil dari requestBody
schema = extractRequestBodySchema(operation);
}
const inputSchema = createInputSchema(schema);
return {
name,
description,
"x-props": {
method: method.toUpperCase(),
path,
operationId: operation.operationId,
tag: tags[0],
deprecated: operation.deprecated || false,
summary: operation.summary,
},
inputSchema,
};
} catch (error) {
console.error(`Failed to create tool from operation:`, error);
return null;
}
}
/**
* Extract schema dari parameters (untuk GET requests)
*/
function extractParametersSchema(parameters: any[]): any {
if (!Array.isArray(parameters) || parameters.length === 0) {
return null;
}
const properties: any = {};
const required: string[] = [];
for (const param of parameters) {
if (!param || typeof param !== "object") continue;
// ✅ Support path, query, dan header parameters
if (["path", "query", "header"].includes(param.in)) {
const paramName = param.name;
if (!paramName || typeof paramName !== "string") continue;
properties[paramName] = {
type: param.schema?.type || "string",
description: param.description || `${param.in} parameter: ${paramName}`,
};
// ✅ Copy field tambahan dari schema
if (param.schema) {
const allowedFields = ["examples", "example", "default", "enum", "pattern", "minLength", "maxLength", "minimum", "maximum", "format"];
for (const field of allowedFields) {
if (param.schema[field] !== undefined) {
properties[paramName][field] = param.schema[field];
}
}
}
if (param.required === true) {
required.push(paramName);
}
}
}
if (Object.keys(properties).length === 0) {
return null;
}
return {
type: "object",
properties,
required,
};
}
/**
* Extract schema dari requestBody (untuk POST/PUT/etc requests)
*/
function extractRequestBodySchema(operation: any): any {
if (!operation.requestBody?.content) {
return null;
}
const content = operation.requestBody.content;
const contentTypes = [
"application/json",
"multipart/form-data",
"application/x-www-form-urlencoded",
"text/plain",
];
for (const contentType of contentTypes) {
if (content[contentType]?.schema) {
return content[contentType].schema;
}
}
for (const [_, value] of Object.entries<any>(content)) {
if (value?.schema) {
return value.schema;
}
}
return null;
}
/**
* Buat input schema yang valid untuk MCP
*/
function createInputSchema(schema: any): any {
const defaultSchema = {
type: "object",
properties: {},
additionalProperties: false,
};
if (!schema || typeof schema !== "object") {
return defaultSchema;
}
try {
const properties: any = {};
const required: string[] = [];
const originalRequired = Array.isArray(schema.required) ? schema.required : [];
if (schema.properties && typeof schema.properties === "object") {
for (const [key, prop] of Object.entries<any>(schema.properties)) {
if (!key || typeof key !== "string") continue;
try {
const cleanProp = cleanProperty(prop);
if (cleanProp) {
properties[key] = cleanProp;
// ✅ PERBAIKAN: Check optional flag dengan benar
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);
}
}
} catch (error) {
console.error(`Error cleaning property ${key}:`, error);
continue;
}
}
}
return {
type: "object",
properties,
required,
additionalProperties: false,
};
} catch (error) {
console.error("Error creating input schema:", error);
return defaultSchema;
}
}
/**
* Bersihkan property dari field custom
*/
function cleanProperty(prop: any): any | null {
if (!prop || typeof prop !== "object") {
return { type: "string" };
}
try {
const cleaned: any = {
type: prop.type || "string",
};
const allowedFields = [
"description",
"examples",
"example",
"default",
"enum",
"pattern",
"minLength",
"maxLength",
"minimum",
"maximum",
"format",
"multipleOf",
"exclusiveMinimum",
"exclusiveMaximum",
];
for (const field of allowedFields) {
if (prop[field] !== undefined && prop[field] !== null) {
cleaned[field] = prop[field];
}
}
if (prop.properties && typeof prop.properties === "object") {
cleaned.properties = {};
for (const [key, value] of Object.entries(prop.properties)) {
const cleanedNested = cleanProperty(value);
if (cleanedNested) {
cleaned.properties[key] = cleanedNested;
}
}
if (Array.isArray(prop.required)) {
cleaned.required = prop.required.filter((r: any) => typeof r === "string");
}
}
if (prop.items) {
cleaned.items = cleanProperty(prop.items);
}
if (Array.isArray(prop.oneOf)) {
cleaned.oneOf = prop.oneOf.map(cleanProperty).filter(Boolean);
}
if (Array.isArray(prop.anyOf)) {
cleaned.anyOf = prop.anyOf.map(cleanProperty).filter(Boolean);
}
if (Array.isArray(prop.allOf)) {
cleaned.allOf = prop.allOf.map(cleanProperty).filter(Boolean);
}
return cleaned;
} catch (error) {
console.error("Error cleaning property:", error);
return null;
}
}
/**
* Bersihkan nama tool
*/
function cleanToolName(name: string): string {
if (!name || typeof name !== "string") {
return "unnamed_tool";
}
try {
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, "")
|| "unnamed_tool";
} catch (error) {
console.error("Error cleaning tool name:", error);
return "unnamed_tool";
}
}
/**
* Ambil OpenAPI JSON dari endpoint dan konversi ke tools MCP
*/
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;
}
}

View File

@@ -206,9 +206,12 @@ export default function DesaSetting({ permissions }: { permissions: JsonValue[]
{
v.name == "TTD"
?
<Anchor href="#" onClick={() => { setViewImg(v.value); setOpenedPreview(true); }} underline="always">
Lihat
</Anchor>
v.value ?
<Anchor href="#" onClick={() => { setViewImg(v.value); setOpenedPreview(true); }} underline="always">
Lihat
</Anchor>
:
"-"
:
v.value
}

View File

@@ -329,7 +329,7 @@ export default function KategoriPengaduan({ permissions }: { permissions: JsonVa
size="sm"
style={{ boxShadow: "0 0 8px rgba(0,255,200,0.2)" }}
onClick={() => chooseEdit({ data: v })}
disabled={!permissions.includes("setting.kategori_pengaduan.edit")}
disabled={!permissions.includes("setting.kategori_pengaduan.edit") || v.id == "lainnya"}
>
<IconEdit size={20} />
</ActionIcon>
@@ -344,7 +344,7 @@ export default function KategoriPengaduan({ permissions }: { permissions: JsonVa
setDataDelete(v.id);
openDelete();
}}
disabled={!permissions.includes("setting.kategori_pengaduan.delete")}
disabled={!permissions.includes("setting.kategori_pengaduan.delete") || v.id == "lainnya"}
>
<IconTrash size={20} />
</ActionIcon>

View File

@@ -1,5 +1,5 @@
import { groupPermissions } from "@/lib/groupPermission";
import { Button, Stack, Text } from "@mantine/core";
import { Anchor, Flex, Stack, Text } from "@mantine/core";
import { useState } from "react";
interface Node {
@@ -14,7 +14,7 @@ function RenderNode({ node }: { node: Node }) {
return (
<Stack pl="md" gap={6}>
{/* Title */}
<Text fw={600}>- {node.label}</Text>
<Text size="sm">- {node.label}</Text>
{/* Children */}
{sub.map((child: any, i) => (
@@ -24,6 +24,22 @@ function RenderNode({ node }: { node: Node }) {
);
}
function RenderNode2({ node }: { node: Node }) {
const sub = Object.values(node.children || {});
return (
<Flex direction={"row"} wrap={'wrap'} gap={6}>
{/* Title */}
<Text size="sm">{node.label},</Text>
{/* Children */}
{sub.map((child: any, i) => (
<RenderNode2 key={i} node={child} />
))}
</Flex>
);
}
export default function PermissionRole({ permissions }: { permissions: string[] }) {
const [showAll, setShowAll] = useState(false);
if (!permissions?.length) return <Text c="dimmed">-</Text>;
@@ -32,7 +48,7 @@ export default function PermissionRole({ permissions }: { permissions: string[]
const rootNodes = Object.values(groups);
return (
<Stack gap="lg">
<Stack gap="sm">
{
showAll ?
rootNodes.map((node: any, idx) => (
@@ -40,18 +56,12 @@ export default function PermissionRole({ permissions }: { permissions: string[]
))
:
rootNodes.slice(0, 2).map((node: any, idx) => (
<RenderNode key={idx} node={node} />
<RenderNode2 key={idx} node={node} />
))
}
<Button
variant="subtle"
size="xs"
onClick={() => setShowAll(!showAll)}
w="fit-content"
ml="md"
>
<Anchor size="xs" onClick={() => setShowAll(!showAll)} >
{showAll ? "View less" : "View more"}
</Button>
</Anchor>
</Stack>
);
}

View File

@@ -16,112 +16,148 @@ export default function PermissionTree({
selected: string[];
onChange: (val: string[]) => void;
}) {
const [open, setOpen] = useState<Record<string, boolean>>({});
// Ambil semua child dari node
const [openNodes, setOpenNodes] = useState<Record<string, boolean>>({});
const toggle = (key: string) => {
setOpen((prev) => ({ ...prev, [key]: !prev[key] }));
};
function toggleNode(label: string) {
setOpenNodes(prev => ({ ...prev, [label]: !prev[label] }));
}
// Ambil semua key dari node termasuk semua keturunannya
const collectKeys = (n: Node): string[] => {
if (!n.children) return [n.key];
return [n.key, ...n.children.flatMap(collectKeys)];
};
const checkState = (node: Node): { all: boolean; some: boolean } => {
const children = node.children || [];
// Jika tidak ada anak → nilai hanya berdasarkan dirinya sendiri
if (children.length === 0) {
const checked = selected.includes(node.key);
return { all: checked, some: checked };
function getAllChildKeys(node: Node): string[] {
let result: string[] = [];
if (node.children) {
node.children.forEach((c) => {
result.push(c.key);
result = [...result, ...getAllChildKeys(c)];
});
}
// Rekursif ke anak
let all = selected.includes(node.key);
let some = selected.includes(node.key);
for (const c of children) {
const childState = checkState(c);
if (!childState.all) all = false;
if (childState.some) some = true;
return result;
}
// Dapatkan parentKey, jika ada
function getParentKey(key: string) {
const split = key.split(".");
if (split.length <= 1) return null;
split.pop();
return split.join(".");
}
// Update parent ke atas secara rekursif
function updateParent(next: string[], parentKey: string | null): string[] {
if (!parentKey) return next;
const allChildKeys = findAllChildKeysFromKey(parentKey);
const selectedChild = allChildKeys.filter((c) => next.includes(c));
if (selectedChild.length === 0) {
// Semua child uncheck → parent uncheck
next = next.filter((x) => x !== parentKey);
} else if (selectedChild.length === allChildKeys.length) {
// Semua child check → parent check
if (!next.includes(parentKey)) {
next.push(parentKey);
}
} else {
// Sebagian child check → parent intermediate (checked = true, rendered sebagai indeterminate)
if (!next.includes(parentKey)) {
next.push(parentKey);
}
}
return { all, some };
};
// Untuk ordering sesuai urutan JSON
const getOrderedKeys = (nodes: Node[]): string[] =>
nodes.flatMap((n) => [n.key, ...getOrderedKeys(n.children || [])]);
// Rekursif naik ke atas
return updateParent(next, getParentKey(parentKey));
}
// dapatkan child dari string key
function findAllChildKeysFromKey(parentKey: string) {
const list: string[] = [];
const RenderNode = ({ node }: { node: Node }) => {
const children = node.children || [];
function traverse(nodes: Node[]) {
nodes.forEach((n) => {
if (n.key.startsWith(parentKey + ".") && n.key !== parentKey) {
list.push(n.key);
}
if (n.children) traverse(n.children);
});
}
const state = checkState(node); // ← gunakan recursive evaluator
traverse(permissionConfig.menus);
return list;
}
const isChecked = state.all;
const isIndeterminate = !state.all && state.some;
const RenderMenu = ({ menu }: { menu: Node }) => {
const hasChild = menu.children && menu.children.length > 0;
const open = openNodes[menu.label] ?? false;
const childKeys = getAllChildKeys(menu);
const isChecked = selected.includes(menu.key);
const isIndeterminate =
!isChecked &&
selected.some(
(x) =>
typeof x === "string" &&
x.startsWith(menu.key + ".")
);
const showChildren = open[node.key] ?? false;
function handleCheck() {
let next = [...selected];
// Ambil semua key anak + parent
const collectKeys = (n: Node): string[] => {
if (!n.children) return [n.key];
return [n.key, ...n.children.flatMap(collectKeys)];
};
if (childKeys.length > 0) {
// klik parent
if (!isChecked) {
next = [...new Set([...next, menu.key, ...childKeys])];
} else {
next = next.filter((x) => x !== menu.key && !childKeys.includes(x));
}
const allKeys = collectKeys(node);
const toggleCheck = (checked: boolean) => {
let updated = new Set(selected);
if (checked) {
// parent + semua child
allKeys.forEach((k) => updated.add(k));
} else {
// hilangkan parent + semua child
allKeys.forEach((k) => updated.delete(k));
next = updateParent(next, getParentKey(menu.key));
onChange(next);
return;
}
// ⬇⬇⬇ PERBAIKAN PENTING ⬇⬇⬇
//
// Jika node indeterminate → parent harus tetap ada di selected
//
if (isIndeterminate) {
updated.add(node.key);
}
// Jika semua child tercentang → parent harus checked
// klik child
if (isChecked) {
updated.add(node.key);
next = next.filter((x) => x !== menu.key);
} else {
next.push(menu.key);
}
onChange([...updated]);
};
next = updateParent(next, getParentKey(menu.key));
onChange(next);
}
return (
<Stack gap={4} pl="xs">
<Group wrap="nowrap">
{children.length > 0 ? (
<ActionIcon variant="subtle" onClick={() => toggle(node.key)}>
{showChildren ? <IconChevronDown size={16} /> : <IconChevronRight size={16} />}
<Stack gap={4}>
<Group gap="xs">
{menu.children && menu.children.length > 0 ? (
<ActionIcon
variant="subtle"
onClick={() => toggleNode(menu.label)}
>
{openNodes[menu.label] ? (
<IconChevronDown size={16} />
) : (
<IconChevronRight size={16} />
)}
</ActionIcon>
) : (
<div style={{ width: 24 }} />
<div style={{ width: 28 }} />
)}
<Checkbox
label={node.label}
label={menu.label}
checked={isChecked}
indeterminate={isIndeterminate}
onChange={(e) => toggleCheck(e.target.checked)}
onChange={handleCheck}
/>
</Group>
{children.length > 0 && (
<Collapse in={showChildren}>
{menu.children && (
<Collapse in={open}>
<Stack gap={4} pl="md">
{children.map((c) => (
<RenderNode key={c.key} node={c} />
{menu.children.map((child) => (
<RenderMenu key={child.key} menu={child} />
))}
</Stack>
</Collapse>
@@ -130,14 +166,11 @@ export default function PermissionTree({
);
};
return (
<Stack>
<Text size="sm">Hak Akses</Text>
{permissionConfig.menus.map((menu: Node) => (
<RenderNode key={menu.key} node={menu} />
{permissionConfig.menus.filter((menu: Node) => !menu.key.startsWith("api") && !menu.key.startsWith("credential")).map((menu: Node) => (
<RenderMenu key={menu.key} menu={menu} />
))}
</Stack>
);

View File

@@ -18,10 +18,18 @@ import { IconEdit, IconPlus, IconTrash } from "@tabler/icons-react";
import type { JsonValue } from "generated/prisma/runtime/library";
import { useState } from "react";
import useSWR from "swr";
import listMenu from "../lib/listPermission.json";
import notification from "./notificationGlobal";
import PermissionRole from "./PermissionRole";
import PermissionTree from "./PermissionTree";
interface MenuNode {
key: string;
label: string;
default: boolean;
children?: MenuNode[];
}
export default function UserRoleSetting({ permissions }: { permissions: JsonValue[] }) {
const [btnDisable, setBtnDisable] = useState(true);
const [btnLoading, setBtnLoading] = useState(false);
@@ -72,13 +80,13 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
});
notification({
title: "Success",
message: "Your user have been saved",
message: "Your role have been saved",
type: "success",
});
} else {
notification({
title: "Error",
message: "Failed to create user ",
message: "Failed to create role",
type: "error",
});
}
@@ -86,7 +94,7 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
console.error(error);
notification({
title: "Error",
message: "Failed to create user",
message: "Failed to create role",
type: "error",
});
} finally {
@@ -97,19 +105,19 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
async function handleEdit() {
try {
setBtnLoading(true);
const res = await apiFetch.api.pengaduan.category.update.post(dataEdit);
const res = await apiFetch.api.user["role-update"].post(dataEdit as any);
if (res.status === 200) {
mutate();
close();
notification({
title: "Success",
message: "Your category have been saved",
message: "Your role have been saved",
type: "success",
});
} else {
notification({
title: "Error",
message: "Failed to edit category",
message: "Failed to edit role",
type: "error",
});
}
@@ -117,7 +125,7 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
console.error(error);
notification({
title: "Error",
message: "Failed to edit category",
message: "Failed to edit role",
type: "error",
});
} finally {
@@ -156,16 +164,10 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
}
}
function chooseEdit({
data,
}: {
data: {
id: string;
name: string;
permissions: [];
};
}) {
setDataEdit(data);
function chooseEdit({ data }: { data: { id: string; name: string; permissions: []; }; }) {
setDataEdit({
id: data.id, name: data.name, permissions: data.permissions ? data.permissions : []
});
open();
}
@@ -185,7 +187,27 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
}
}
console.log("dataTambah", dataTambah);
function buildOrderList(menus: MenuNode[]): string[] {
const list: string[] = [];
const traverse = (nodes: MenuNode[]) => {
nodes.forEach((node) => {
list.push(node.key);
if (node.children) traverse(node.children);
});
};
traverse(menus);
return list;
}
function sortByJsonOrder(arrayData: string[]): string[] {
const orderList = buildOrderList(listMenu.menus);
return arrayData.sort((a, b) => {
return orderList.indexOf(a) - orderList.indexOf(b);
});
}
useShallowEffect(() => {
if (dataEdit.name.length > 0) {
@@ -200,11 +222,11 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
opened={opened}
onClose={close}
title={"Edit"}
centered
overlayProps={{ backgroundOpacity: 0.55, blur: 3 }}
size={"lg"}
>
<Stack gap="ld">
<Input.Wrapper label="Edit Kategori">
<Input.Wrapper label="Nama Role">
<Input
value={dataEdit.name}
onChange={(e) =>
@@ -216,6 +238,12 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
}
/>
</Input.Wrapper>
<PermissionTree
selected={dataEdit.permissions}
onChange={(permissions) => {
setDataEdit({ ...dataEdit, permissions: sortByJsonOrder(permissions) as never[] });
}}
/>
<Group justify="center" grow>
<Button variant="light" onClick={close}>
Batal
@@ -223,7 +251,11 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
<Button
variant="filled"
onClick={handleEdit}
disabled={btnDisable}
disabled={
btnDisable ||
dataEdit.name.length < 1 ||
dataEdit.permissions?.length < 1
}
loading={btnLoading}
>
Simpan
@@ -238,10 +270,11 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
onClose={closeTambah}
title={"Tambah"}
overlayProps={{ backgroundOpacity: 0.55, blur: 3 }}
size={"lg"}
>
<Stack gap="ld">
<Input.Wrapper
label="Nama"
label="Nama Role"
description=""
error={error.name ? "Field is required" : ""}
>
@@ -259,7 +292,7 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
<PermissionTree
selected={dataTambah.permissions}
onChange={(permissions) => {
setDataTambah({ ...dataTambah, permissions: permissions as never[] });
setDataTambah({ ...dataTambah, permissions: sortByJsonOrder(permissions) as never[] });
}}
/>
<Group justify="center" grow>
@@ -342,11 +375,11 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
{list.length > 0 ? (
list?.map((v: any) => (
<Table.Tr key={v.id}>
<Table.Td>{v.name}</Table.Td>
<Table.Td w={"150"}>{v.name}</Table.Td>
<Table.Td>
<PermissionRole permissions={v.permissions} />
</Table.Td>
<Table.Td>
<Table.Td w={"100"}>
<Group>
<Tooltip label={permissions.includes('setting.user_role.edit') ? "Edit Role" : "Edit Role - Anda tidak memiliki akses"}>
<ActionIcon
@@ -354,7 +387,7 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
size="sm"
style={{ boxShadow: "0 0 8px rgba(0,255,200,0.2)" }}
onClick={() => chooseEdit({ data: v })}
disabled={!permissions.includes('setting.user_role.edit')}
disabled={!permissions.includes('setting.user_role.edit') || v.id == "developer"}
>
<IconEdit size={20} />
</ActionIcon>
@@ -369,7 +402,7 @@ export default function UserRoleSetting({ permissions }: { permissions: JsonValu
setDataDelete(v.id);
openDelete();
}}
disabled={!permissions.includes('setting.user_role.delete')}
disabled={!permissions.includes('setting.user_role.delete') || v.id == "developer"}
>
<IconTrash size={20} />
</ActionIcon>

View File

@@ -107,19 +107,19 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
async function handleEdit() {
try {
setBtnLoading(true);
const res = await apiFetch.api.pengaduan.category.update.post(dataEdit);
const res = await apiFetch.api.user.update.post(dataEdit);
if (res.status === 200) {
mutate();
close();
notification({
title: "Success",
message: "Your category have been saved",
message: "Your data have been saved",
type: "success",
});
} else {
notification({
title: "Error",
message: "Failed to edit category",
message: "Failed to edit user",
type: "error",
});
}
@@ -127,7 +127,7 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
console.error(error);
notification({
title: "Error",
message: "Failed to edit category",
message: "Failed to edit user2",
type: "error",
});
} finally {
@@ -222,9 +222,10 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
overlayProps={{ backgroundOpacity: 0.55, blur: 3 }}
>
<Stack gap="ld">
<Input.Wrapper label="Edit Kategori">
<Input.Wrapper label="Nama">
<Input
value={dataEdit.name}
error={error.name ? "Field is required" : ""}
onChange={(e) =>
onValidation({
kat: "name",
@@ -234,6 +235,51 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
}
/>
</Input.Wrapper>
<Select
label="Role"
placeholder="Pilih Role"
data={listRole.map((r: any) => ({
value: r.id,
label: r.name,
}))}
value={dataEdit.roleId || null}
error={error.roleId ? "Field is required" : ""}
onChange={(_value, option) => {
onValidation({
kat: "roleId",
value: option?.value,
aksi: "edit",
});
}}
/>
<Input.Wrapper label="Phone" description="">
<Input
value={dataEdit.phone}
onChange={(e) =>
onValidation({
kat: "phone",
value: e.target.value,
aksi: "edit",
})
}
/>
</Input.Wrapper>
<Input.Wrapper
label="Email"
description=""
error={error.email ? "Field is required" : ""}
>
<Input
value={dataEdit.email}
onChange={(e) =>
onValidation({
kat: "email",
value: e.target.value,
aksi: "edit",
})
}
/>
</Input.Wrapper>
<Group justify="center" grow>
<Button variant="light" onClick={close}>
Batal
@@ -434,7 +480,7 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
size="sm"
style={{ boxShadow: "0 0 8px rgba(0,255,200,0.2)" }}
onClick={() => chooseEdit({ data: v })}
disabled={!permissions.includes('setting.user.edit')}
disabled={!permissions.includes('setting.user.edit') || v.roleId == "developer"}
>
<IconEdit size={20} />
</ActionIcon>
@@ -449,7 +495,7 @@ export default function UserSetting({ permissions }: { permissions: JsonValue[]
setDataDelete(v.id);
openDelete();
}}
disabled={!permissions.includes('setting.user.delete')}
disabled={!permissions.includes('setting.user.delete') || v.roleId == "developer"}
>
<IconTrash size={20} />
</ActionIcon>

View File

@@ -24,41 +24,41 @@
},
{
"key": "pengaduan.antrian",
"label": "Antrian",
"label": "Detail pengaduan dengan status antrian",
"default": true,
"children": [
{
"key": "pengaduan.antrian.tolak",
"label": "Menolak",
"label": "Menolak pengaduan",
"default": true
},
{
"key": "pengaduan.antrian.terima",
"label": "Menerima",
"label": "Menerima pengaduan",
"default": true
}
]
},
{
"key": "pengaduan.diterima",
"label": "Diterima",
"label": "Detail pengaduan dengan status diterima",
"default": true,
"children": [
{
"key": "pengaduan.diterima.dikerjakan",
"label": "Dikerjakan",
"label": "Menegerjakan pengaduan",
"default": true
}
]
},
{
"key": "pengaduan.dikerjakan",
"label": "Dikerjakan",
"label": "Detail pengaduan dengan status dikerjakan",
"default": true,
"children": [
{
"key": "pengaduan.dikerjakan.selesai",
"label": "Diselesaikan",
"label": "Menyelesaikan pengaduan",
"default": true
}
]
@@ -77,34 +77,34 @@
},
{
"key": "pelayanan.antrian",
"label": "Antrian",
"label": "Detail pelayanan dengan status antrian",
"default": true,
"children": [
{
"key": "pelayanan.antrian.tolak",
"label": "Menolak",
"label": "Menolak pelayanan",
"default": true
},
{
"key": "pelayanan.antrian.terima",
"label": "Menerima",
"label": "Menerima pelayanan",
"default": true
}
]
},
{
"key": "pelayanan.diterima",
"label": "Diterima",
"label": "Detail pelayanan dengan status diterima",
"default": true,
"children": [
{
"key": "pelayanan.diterima.tolak",
"label": "Menolak",
"label": "Menolak pelayanan",
"default": true
},
{
"key": "pelayanan.diterima.setujui",
"label": "Menyetujui",
"label": "Menyetujui pelayanan",
"default": true
}
]
@@ -300,7 +300,7 @@
"default": true,
"children": [
{
"key": "credential.viewØ",
"key": "credential.view",
"label": "View List",
"default": true
}

View File

@@ -1,20 +1,50 @@
import clientRoutes from "@/clientRoutes";
import {
Button,
Container,
Group,
PasswordInput,
Stack,
Text,
TextInput,
} from "@mantine/core";
import { useState } from "react";
import apiFetch from "../lib/apiFetch";
import clientRoutes from "@/clientRoutes";
export default function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false);
function navigateToRoute(akses: string) {
switch (akses) {
case "dashboard":
window.location.href = clientRoutes["/scr/dashboard/dashboard-home"];
break;
case "pengaduan":
window.location.href = clientRoutes["/scr/dashboard/pengaduan/list"];
break;
case "warga":
window.location.href = clientRoutes["/scr/dashboard/warga/list-warga"];
break;
case "credential":
window.location.href = clientRoutes["/scr/dashboard/credential/credential"];
break;
case "setting":
window.location.href = clientRoutes["/scr/dashboard/setting/detail-setting"];
break;
case "api_key":
window.location.href = clientRoutes["/scr/dashboard/apikey/apikey"];
break;
case "pelayanan":
window.location.href = clientRoutes["/scr/dashboard/pelayanan-surat/list-pelayanan"];
break;
default:
window.location.href = clientRoutes["/scr/dashboard"];
break;
}
}
const handleSubmit = async () => {
setLoading(true);
try {
@@ -25,7 +55,7 @@ export default function Login() {
if (response.data?.token) {
localStorage.setItem("token", response.data.token);
window.location.href = clientRoutes["/scr/dashboard"];
navigateToRoute(response.data.akses || "dashboard");
return;
}
@@ -48,7 +78,7 @@ export default function Login() {
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<TextInput
<PasswordInput
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}

View File

@@ -263,9 +263,18 @@ function DetailDataPengaduan({ data, onAction }: { data: any, onAction: () => vo
<IconPhotoScan size={20} />
<Text size="md">Gambar</Text>
</Group>
<Anchor href="#" onClick={() => { }}>
Lihat Gambar
</Anchor>
{
data?.image != null && data?.image != ""
?
<Anchor href="#" onClick={() => { }}>
Lihat Gambar
</Anchor>
:
<Text size="md" c="white">
-
</Text>
}
</Flex>
</Stack>
</Grid.Col>

View File

@@ -16,8 +16,11 @@ interface McpTool {
/**
* Convert OpenAPI 3.x JSON spec into MCP-compatible tool definitions.
* * @param openApiJson OpenAPI JSON specification object.
* @param filterTag A string or array of strings. Operations must match at least one tag
* (case-insensitive partial match).
*/
export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): McpTool[] {
export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string | string[]): McpTool[] {
const tools: McpTool[] = [];
if (!openApiJson || typeof openApiJson !== "object") {
@@ -25,6 +28,15 @@ export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): M
return tools;
}
// Cast filterTag to an array and normalize to lowercase for comparison
const filterTags = _.castArray(filterTag)
.filter(t => typeof t === "string" && t.trim() !== "")
.map(t => t.toLowerCase());
if (filterTags.length === 0) {
console.warn("Filter tag is empty or invalid. Returning all tools with tags.");
}
const paths = openApiJson.paths || {};
if (Object.keys(paths).length === 0) {
@@ -34,7 +46,6 @@ export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): M
for (const [path, methods] of Object.entries(paths)) {
if (!path || typeof path !== "string") continue;
if (path.startsWith("/mcp")) continue;
if (!methods || typeof methods !== "object") continue;
@@ -45,10 +56,19 @@ export function convertOpenApiToMcpTools(openApiJson: any, filterTag: string): M
if (!operation || typeof operation !== "object") continue;
const tags: string[] = Array.isArray(operation.tags) ? operation.tags : [];
const lowerCaseTags = tags.map(t => typeof t === "string" ? t.toLowerCase() : "");
if (!tags.length || !tags.some(t =>
typeof t === "string" && t.toLowerCase().includes(filterTag)
)) continue;
// ✅ MODIFIKASI: Pengecekan filterTags
if (filterTags.length > 0) {
const isTagMatch = lowerCaseTags.some(opTag =>
filterTags.some(fTag => opTag.includes(fTag))
);
if (!isTagMatch) continue;
} else if (tags.length === 0) {
// Jika tidak ada filter, hanya proses operation yang memiliki tags
continue;
}
try {
const tool = createToolFromOperation(path, method, operation, tags);
@@ -75,18 +95,20 @@ function createToolFromOperation(
tags: string[]
): McpTool | null {
try {
const rawName = _.snakeCase(operation.operationId || `${method}_${path}`) || "unnamed_tool";
const name = cleanToolName(rawName);
const rawName = _.snakeCase(`${operation.operationId}` || `${method}_${path}`) || "unnamed_tool";
const name = _.snakeCase(cleanToolName(operation.summary)) || cleanToolName(rawName);
if (!name || name === "unnamed_tool") {
console.warn(`Invalid tool name for ${method} ${path}`);
return null;
}
const description =
let description =
operation.description ||
operation.summary ||
`Execute ${method.toUpperCase()} ${path}`;
operation.summary;
description += `\n
Execute ${method.toUpperCase()} ${path}`;
// ✅ Extract schema berdasarkan method
let schema;
@@ -343,9 +365,8 @@ function cleanToolName(name: string): string {
.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, "")
// ❗️ METHOD PREFIX TIDAK DIHAPUS LAGI (agar tidak duplicate)
.toLowerCase()
|| "unnamed_tool";
} catch (error) {
console.error("Error cleaning tool name:", error);
@@ -353,10 +374,14 @@ function cleanToolName(name: string): string {
}
}
/**
* Ambil OpenAPI JSON dari endpoint dan konversi ke tools MCP
* * @param url URL of the OpenAPI spec.
* @param filterTag A string or array of strings. Operations must match at least one tag
* (case-insensitive partial match).
*/
export async function getMcpTools(url: string, filterTag: string): Promise<McpTool[]> {
export async function getMcpTools(url: string, filterTag: string | string[]): Promise<McpTool[]> {
try {
console.log(`Fetching OpenAPI spec from: ${url}`);
@@ -370,12 +395,12 @@ export async function getMcpTools(url: string, filterTag: string): Promise<McpTo
const openApiJson = await response.json();
const tools = convertOpenApiToMcpTools(openApiJson, filterTag);
console.log(`✅ Successfully generated ${tools.length} MCP tools`);
const filterStr = _.castArray(filterTag).join(", ");
console.log(`✅ Successfully generated ${tools.length} MCP tools for tags: [${filterStr}]`);
return tools;
} catch (error) {
console.error("Error fetching MCP tools:", error);
throw error;
}
}
}

View File

@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { prisma } from '@/server/lib/prisma'
import { jwt as jwtPlugin, type JWTPayloadSpec } from '@elysiajs/jwt'
import Elysia, { t, type Cookie, type HTTPHeaders, type StatusMap } from 'elysia'
import { type ElysiaCookie } from 'elysia/cookies'
import { prisma } from '@/server/lib/prisma'
const secret = process.env.JWT_SECRET
if (!secret) {
@@ -75,6 +75,15 @@ async function login({
const user = await prisma.user.findUnique({
where: { email },
select: {
id: true,
password: true,
Role: {
select: {
permissions: true
}
}
}
})
if (!user) {
@@ -87,6 +96,12 @@ async function login({
return { message: 'Invalid password' }
}
const rawPermissions = user.Role?.permissions;
const akses = Array.isArray(rawPermissions)
? rawPermissions[0]?.toString()
: undefined;
const token = await issueToken({
jwt,
cookie,
@@ -94,7 +109,7 @@ async function login({
role: 'user',
expiresAt: Math.floor(Date.now() / 1000) + NINETY_YEARS,
})
return { token }
return { token, akses }
} catch (error) {
console.error('Error logging in:', error)
return {
@@ -146,7 +161,7 @@ const Auth = new Elysia({
detail: {
summary: 'logout',
description: 'Logout (clear token cookie)',
},
}
)

View File

@@ -1,250 +1,476 @@
// server/mcpServer.ts
import { Elysia } from "elysia";
import { getMcpTools } from "../lib/mcp_tool_convert";
var tools = [] as any[];
const OPENAPI_URL = process.env.BUN_PUBLIC_BASE_URL + "/docs/json";
const FILTER_TAG = "mcp";
/**
* Refactored Elysia-based MCP server
* - Fixes inconsistent "text/json" handling by normalizing response extraction
* - Robust executeTool: supports path/query/header/cookie/body params (if provided in x-props)
* - Proper baseUrl/path normalization and URLSearchParams building (repeated keys for arrays)
* - Consistent MCP content conversion: always returns either { type: 'json', data } or { type: 'text', text }
* - Safer error handling, batch support, Promise.allSettled to avoid full failure on single-item error
* - Lightweight in-memory tools cache with explicit init endpoint (keeps original behavior)
*/
/* -------------------------
Environment & Globals
------------------------- */
if (!process.env.BUN_PUBLIC_BASE_URL) {
throw new Error("BUN_PUBLIC_BASE_URL environment variable is not set");
throw new Error("BUN_PUBLIC_BASE_URL environment variable is not set");
}
// =====================
// MCP Protocol Types
// =====================
const OPENAPI_URL = `${process.env.BUN_PUBLIC_BASE_URL.replace(/\/+$/, "")}/docs/json`;
const FILTER_TAG = "mcp";
let tools: any[] = [];
/* -------------------------
MCP Types
------------------------- */
type JSONRPCRequest = {
jsonrpc: "2.0";
id: string | number;
method: string;
params?: any;
jsonrpc: "2.0";
id: string | number;
method: string;
params?: any;
credentials?: any;
};
type JSONRPCResponse = {
jsonrpc: "2.0";
id: string | number;
result?: any;
error?: {
code: number;
message: string;
data?: any;
};
jsonrpc: "2.0";
id: string | number | null;
result?: any;
error?: { code: number; message: string; data?: any };
};
// =====================
// Tool Executor
// =====================
/* -------------------------
Helpers
------------------------- */
/** Ensure baseUrl doesn't end with slash; ensure path begins with slash */
function joinBasePath(base: string, path: string) {
const normalizedBase = base.replace(/\/+$/, "");
const normalizedPath = path ? (path.startsWith("/") ? path : `/${path}`) : "";
return `${normalizedBase}${normalizedPath}`;
}
/** Serialize query object to repeated-key QS when arrays provided */
function buildQueryString(q: Record<string, any>): string {
const parts: string[] = [];
for (const [k, v] of Object.entries(q)) {
if (v === undefined || v === null) continue;
if (Array.isArray(v)) {
for (const item of v) {
parts.push(`${encodeURIComponent(k)}=${encodeURIComponent(String(item))}`);
}
} else if (typeof v === "object") {
parts.push(`${encodeURIComponent(k)}=${encodeURIComponent(JSON.stringify(v))}`);
} else {
parts.push(`${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`);
}
}
return parts.length ? `?${parts.join("&")}` : "";
}
/** Safely extract "useful" payload from a fetch result:
* Prefer resp.data if present, otherwise resp itself.
* If resp is a string, keep as string.
*/
function extractRaw(result: { data: any } | any) {
// If result shaped as { data: ... } prefer inner .data
if (result && typeof result === "object" && "data" in result) {
return result.data;
}
return result;
}
/** Convert various payloads into MCP content shape */
function convertToMcpContent(payload: any) {
if (typeof payload === "string") {
return { type: "text", text: payload };
}
if (payload == null) {
return { type: "text", text: String(payload) };
}
// If payload looks like an image/audio wrapper produced by converter
if (payload?.__mcp_type === "image" && payload.base64) {
return { type: "image", data: payload.base64, mimeType: payload.mimeType || "image/png" };
}
if (payload?.__mcp_type === "audio" && payload.base64) {
return { type: "audio", data: payload.base64, mimeType: payload.mimeType || "audio/mpeg" };
}
// If already an object/array → return JSON
if (typeof payload === "object") {
return { type: "json", data: payload };
}
// Fallback — stringify
try {
return { type: "text", text: JSON.stringify(payload) };
} catch {
return { type: "text", text: String(payload) };
}
}
/* -------------------------
executeTool (robust)
------------------------- */
/**
* Execute a tool converted from OpenAPI -> expected x-props shape:
* x-props may contain:
* - method, path
* - parameters: [{ name, in, required? }]
*
* If x.parameters present, we inspect args and place them accordingly.
*/
export async function executeTool(
tool: any,
args: Record<string, any> = {},
baseUrl: string
tool: any,
args: Record<string, any> = {},
baseUrl: string
) {
const x = tool["x-props"] || {};
const x = tool["x-props"] || {};
const method = (x.method || "GET").toUpperCase();
const method = (x.method || "GET").toUpperCase();
const path = x.path || `/${tool.name}`;
const url = `${baseUrl}${path}`;
// Start with provided path (may contain {param})
let path = x.path ?? `/${tool.name}`;
const opts: RequestInit = {
method,
headers: { "Content-Type": "application/json" },
};
// Headers, cookies, query, body collection
const headers: Record<string, any> = {
"Content-Type": "application/json",
...(x.defaultHeaders || {}),
};
const query: Record<string, any> = {};
const cookies: string[] = [];
let bodyPayload: any = undefined;
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
opts.body = JSON.stringify(args || {});
// If parameters described, map args accordingly
if (Array.isArray(x.parameters)) {
for (const p of x.parameters) {
try {
const name: string = p.name;
const value = args?.[name];
// skip undefined unless required — we let API validate required semantics
if (value === undefined) continue;
switch (p.in) {
case "path":
if (path.includes(`{${name}}`)) {
path = path.replace(new RegExp(`{${name}}`, "g"), encodeURIComponent(String(value)));
} else {
// fallback to query
query[name] = value;
}
break;
case "query":
query[name] = value;
break;
case "header":
headers[name] = value;
break;
case "cookie":
cookies.push(`${name}=${value}`);
break;
case "body":
case "requestBody":
bodyPayload = value;
break;
default:
// unknown location -> place into body
bodyPayload = bodyPayload ?? {};
bodyPayload[name] = value;
break;
}
} catch (err) {
// best-effort: skip problematic param
console.warn(`[MCP] Skipping parameter ${String(p?.name)} due to error:`, err);
}
}
} else {
// no param descriptions: assume all args are body
bodyPayload = Object.keys(args || {}).length ? args : undefined;
}
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();
if (cookies.length) {
headers["Cookie"] = cookies.join("; ");
}
return {
success: res.ok,
status: res.status,
method,
path,
data,
};
// Build full URL
const urlBase = baseUrl || process.env.BUN_PUBLIC_BASE_URL!;
let url = joinBasePath(urlBase, path);
const qs = buildQueryString(query);
if (qs) url += qs;
// Build RequestInit
const opts: RequestInit & { headers?: Record<string, any> } = { method, headers };
// Body handling for applicable methods
const bodyMethods = new Set(["POST", "PUT", "PATCH", "DELETE"]);
const contentTypeLower = (headers["Content-Type"] || "").toLowerCase();
if (bodyMethods.has(method) && bodyPayload !== undefined) {
// Support tiny formdata marker shape: { __formdata: true, entries: [ [k,v], ... ] }
if (bodyPayload && bodyPayload.__formdata === true && Array.isArray(bodyPayload.entries)) {
const form = new FormData();
for (const [k, v] of bodyPayload.entries) {
form.append(k, v as any);
}
// Let fetch set boundary
delete opts.headers!["Content-Type"];
opts.body = form as any;
} else if (contentTypeLower.includes("application/x-www-form-urlencoded")) {
opts.body = new URLSearchParams(bodyPayload as Record<string, string>).toString();
} else if (contentTypeLower.includes("multipart/form-data")) {
// If caller explicitly requested multipart but didn't pass FormData — convert object to form
const form = new FormData();
if (typeof bodyPayload === "object") {
for (const [k, v] of Object.entries(bodyPayload)) {
form.append(k, (v as any) as any);
}
} else {
form.append("payload", String(bodyPayload));
}
delete opts.headers!["Content-Type"];
opts.body = form as any;
} else {
// Default JSON
opts.body = JSON.stringify(bodyPayload);
}
}
// Execute fetch
console.log(`[MCP] → ${method} ${url}`);
const res = await fetch(url, opts);
const resContentType = (res.headers.get("content-type") || "").toLowerCase();
let data: any;
try {
if (resContentType.includes("application/json")) {
data = await res.json();
} else {
data = await res.text();
}
} catch (err) {
// fallback to text
try {
data = await res.text();
} catch {
data = null;
}
}
return {
success: res.ok,
status: res.status,
method,
url,
path,
headers: res.headers,
data,
};
}
// =====================
// MCP Handler (Async)
// =====================
async function handleMCPRequestAsync(
request: JSONRPCRequest
): Promise<JSONRPCResponse> {
const { id, method, params } = request;
/* -------------------------
JSON-RPC Handler
------------------------- */
async function handleMCPRequestAsync(request: JSONRPCRequest): Promise<JSONRPCResponse> {
const { id, method, params } = request;
switch (method) {
case "initialize":
const makeError = (code: number, message: string, data?: any): JSONRPCResponse => ({
jsonrpc: "2.0",
id: id ?? null,
error: { code, message, data },
});
switch (method) {
case "initialize":
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: { tools: {} },
serverInfo: { name: "elysia-mcp-server", version: "1.0.0" },
},
};
case "tools/list":
return {
jsonrpc: "2.0",
id,
result: {
tools: tools.map((t) => {
const inputSchema =
typeof t.inputSchema === "object" && t.inputSchema?.type === "object"
? t.inputSchema
: { type: "object", properties: {}, required: [] };
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: { tools: {} },
serverInfo: { name: "elysia-mcp-server", version: "1.0.0" },
},
name: t.name,
description: t.description || "No description provided",
inputSchema,
"x-props": t["x-props"],
};
}),
},
};
case "tools/list":
return {
jsonrpc: "2.0",
id,
result: {
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
description,
inputSchema,
"x-props": x,
})),
},
};
case "tools/call": {
const toolName = params?.name;
const tool = tools.find((t) => t.name === toolName);
if (!tool) return makeError(-32601, `Tool '${toolName}' not found`);
case "tools/call": {
const toolName = params?.name;
const tool = tools.find((t) => t.name === toolName);
try {
const baseUrl = (params?.credentials?.baseUrl as string) || process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
const args = params?.arguments || {};
if (!tool) {
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Tool '${toolName}' not found` },
};
}
const result = await executeTool(tool, args, baseUrl);
try {
const baseUrl =
process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
const result = await executeTool(tool, params?.arguments || {}, baseUrl);
const data = result.data.data;
const isObject = typeof data === "object" && data !== null;
// Extract the meaningful payload (prefer nested .data if present)
const raw = extractRaw(result.data);
return {
jsonrpc: "2.0",
id,
result: {
content: [
isObject
? { type: "json", data: data }
: { type: "text", text: JSON.stringify(data || result.data || result) },
],
},
};
} catch (error: any) {
return {
jsonrpc: "2.0",
id,
error: { code: -32603, message: error.message },
};
}
}
// Normalize content shape consistently:
const contentItem = convertToMcpContent(raw ?? result.data ?? result);
case "ping":
return { jsonrpc: "2.0", id, result: {} };
default:
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method '${method}' not found` },
};
return {
jsonrpc: "2.0",
id,
result: {
content: [contentItem],
},
};
} catch (err: any) {
// avoid leaking secrets — small debug
const dbg = { message: err?.message };
return makeError(-32603, err?.message ?? "Internal error", dbg);
}
}
case "ping":
return { jsonrpc: "2.0", id, result: {} };
default:
return makeError(-32601, `Method '${method}' not found`);
}
}
// =====================
// Elysia MCP Server
// =====================
export const MCPRoute = new Elysia({
tags: ["MCP Server"]
})
.post("/mcp", async ({ request, set }) => {
if (!tools.length) {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
}
set.headers["Content-Type"] = "application/json";
set.headers["Access-Control-Allow-Origin"] = "*";
/* -------------------------
Elysia App & Routes
------------------------- */
export const MCPRoute = new Elysia({ tags: ["MCP Server"] })
.post("/mcp", async ({ request, set }) => {
set.headers["Content-Type"] = "application/json";
set.headers["Access-Control-Allow-Origin"] = "*";
try {
const body = await request.json();
// Lazy load the tools (keeps previous behavior)
if (!tools.length) {
try {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
} catch (err) {
console.error("[MCP] Failed to load tools during lazy init:", err);
}
}
if (!Array.isArray(body)) {
const res = await handleMCPRequestAsync(body);
return res;
}
try {
const body = await request.json();
const results = await Promise.all(
body.map((req) => handleMCPRequestAsync(req))
);
return results;
} catch (error: any) {
set.status = 400;
return {
// If batch array -> allSettled for resilience
if (Array.isArray(body)) {
const promises = body.map((req: JSONRPCRequest) => handleMCPRequestAsync(req));
const settled = await Promise.allSettled(promises);
const responses = settled.map((s) =>
s.status === "fulfilled"
? s.value
: ({
jsonrpc: "2.0",
id: null,
error: {
code: -32700,
message: "Parse error",
data: error.message,
code: -32000,
message: "Unhandled handler error",
data: String((s as PromiseRejectedResult).reason),
},
};
}
})
} as JSONRPCResponse)
);
return responses;
}
// Tools list (debug)
.get("/mcp/tools", async ({ set }) => {
if (!tools.length) {
const single = await handleMCPRequestAsync(body as JSONRPCRequest);
return single;
} catch (err: any) {
set.status = 400;
return {
jsonrpc: "2.0",
id: null,
error: { code: -32700, message: "Parse error", data: err?.message ?? String(err) },
} as JSONRPCResponse;
}
})
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
}
set.headers["Access-Control-Allow-Origin"] = "*";
return {
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
description,
inputSchema,
"x-props": x,
})),
};
})
/* Debug / management endpoints */
.get("/mcp/tools", async ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
if (!tools.length) {
try {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
} catch (err) {
console.error("[MCP] Failed to load tools for /mcp/tools:", err);
}
}
return {
tools: tools.map((t) => ({
name: t.name,
description: t.description,
inputSchema: t.inputSchema,
"x-props": t["x-props"],
})),
};
})
// MCP status
.get("/mcp/status", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "active", timestamp: Date.now() };
})
.get("/mcp/status", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "active", timestamp: Date.now() };
})
// Health check
.get("/health", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "ok", timestamp: Date.now(), tools: tools.length };
})
.get("/mcp/init", async ({ set }) => {
.get("/health", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "ok", timestamp: Date.now(), tools: tools.length };
})
const _tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
tools = _tools;
return {
success: true,
message: "MCP initialized",
tools: tools.length,
};
})
// Force re-init (useful for admin / CI)
.get("/mcp/init", async ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
try {
tools = await getMcpTools(OPENAPI_URL, FILTER_TAG);
return { success: true, message: "MCP initialized", tools: tools.length };
} catch (err) {
set.status = 500;
return { success: false, message: "Failed to initialize tools", error: String(err) };
}
})
// CORS
.options("/mcp", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
})
.options("/mcp/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
});
/* CORS preflight */
.options("/mcp", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
})
.options("/mcp/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
});
/* -------------------------
End
------------------------- */

View File

@@ -1,5 +1,6 @@
import Elysia, { t } from "elysia"
import type { StatusPengaduan } from "generated/prisma"
import _ from "lodash"
import { v4 as uuidv4 } from "uuid"
import { getLastUpdated } from "../lib/get-last-updated"
import { mimeToExtension } from "../lib/mimetypeToExtension"
@@ -107,11 +108,11 @@ const PengaduanRoute = new Elysia({
// --- PENGADUAN ---
.post("/create", async ({ body }) => {
const { judulPengaduan, detailPengaduan, lokasi, namaGambar, kategoriId, wargaId, noTelepon } = body
const { judulPengaduan, detailPengaduan, lokasi, namaGambar, kategoriId, namaWarga, noTelepon } = body
let imageFix = namaGambar
const noPengaduan = await generateNoPengaduan()
let idCategoryFix = kategoriId
let idWargaFix = wargaId
let idWargaFix = ""
if (idCategoryFix) {
const category = await prisma.categoryPengaduan.findUnique({
@@ -138,38 +139,25 @@ const PengaduanRoute = new Elysia({
idCategoryFix = "lainnya"
}
const warga = await prisma.warga.findUnique({
const nomorHP = normalizePhoneNumber({ phone: noTelepon })
const dataWarga = await prisma.warga.upsert({
where: {
id: wargaId,
phone: nomorHP
},
create: {
name: namaWarga,
phone: nomorHP,
},
update: {
name: namaWarga,
},
select: {
id: true
}
})
if (!warga) {
const nomorHP = normalizePhoneNumber({ phone: noTelepon })
const cariWarga = await prisma.warga.findUnique({
where: {
phone: nomorHP,
}
})
idWargaFix = dataWarga.id
if (!cariWarga) {
const wargaCreate = await prisma.warga.create({
data: {
name: wargaId,
phone: nomorHP,
},
select: {
id: true
}
})
idWargaFix = wargaCreate.id
} else {
idWargaFix = cariWarga.id
}
}
const pengaduan = await prisma.pengaduan.create({
data: {
@@ -228,9 +216,9 @@ const PengaduanRoute = new Elysia({
description: "ID atau nama kategori pengaduan (contoh: kebersihan, keamanan, lainnya)"
})),
wargaId: t.Optional(t.String({
namaWarga: t.Optional(t.String({
examples: ["budiman"],
description: "ID unik warga yang melapor (jika sudah terdaftar)"
description: "Nama warga yang melapor"
})),
noTelepon: t.String({
@@ -242,23 +230,7 @@ const PengaduanRoute = new Elysia({
detail: {
summary: "Buat Pengaduan Warga",
description: `
Endpoint ini digunakan untuk membuat data pengaduan (laporan) baru dari warga.
Alur proses:
1. Sistem memvalidasi kategori pengaduan berdasarkan ID.
- Jika ID kategori tidak ditemukan, sistem akan mencari berdasarkan nama kategori.
- Jika tetap tidak ditemukan, kategori akan diset menjadi "lainnya".
2. Sistem memvalidasi data warga berdasarkan ID.
- Jika warga tidak ditemukan, sistem akan mencari berdasarkan nomor telepon.
- Jika tetap tidak ditemukan, data warga baru akan dibuat secara otomatis.
3. Sistem menghasilkan nomor pengaduan unik (noPengaduan).
4. Data pengaduan akan disimpan ke database, termasuk judul, detail, lokasi, gambar (opsional), dan data warga.
5. Sistem juga membuat catatan riwayat awal pengaduan dengan deskripsi "Pengaduan dibuat".
Respon:
- success: true jika pengaduan berhasil dibuat.
- message: berisi pesan sukses dan nomor pengaduan yang dapat digunakan untuk melacak status pengaduan.`,
description: `Endpoint ini digunakan untuk membuat data pengaduan (laporan) baru dari warga`,
tags: ["mcp"]
}
})
@@ -361,7 +333,7 @@ Respon:
const dataHistory = await prisma.historyPengaduan.findMany({
where: {
idPengaduan: id,
idPengaduan: data?.id,
},
select: {
id: true,
@@ -377,23 +349,20 @@ Respon:
}
})
const dataHistoryFix = dataHistory.map((item) => {
return {
id: item.id,
deskripsi: item.deskripsi,
status: item.status,
createdAt: item.createdAt.toLocaleString("id-ID", {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false
}),
idUser: item.idUser,
nameUser: item.User?.name,
}
})
const dataHistoryFix = dataHistory.map((item: any) => ({
..._.omit(item, ["User", "createdAt"]),
nameUser: item.User?.name,
createdAt: item.createdAt.toLocaleString("id-ID", {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false
}),
}))
const warga = {
name: data?.Warga?.name,
@@ -545,8 +514,8 @@ Respon:
folder: t.String(),
}),
detail: {
summary: "Upload File",
description: "Tool untuk upload file ke Seafile",
summary: "Upload File (FormData)",
description: "Tool untuk upload file ke folder tujuan dengan memakai FormData",
tags: ["mcp"],
consumes: ["multipart/form-data"]
},

View File

@@ -159,6 +159,9 @@ const UserRoute = new Elysia({
const data = await prisma.role.findMany({
where: {
isActive: true
},
orderBy: {
name: "asc"
}
})
return data
@@ -193,11 +196,11 @@ const UserRoute = new Elysia({
}
})
.post("role-create", async ({ body }) => {
const { name, permission } = body;
const { name, permissions } = body;
const create = await prisma.role.create({
data: {
name,
permissions: permission
permissions: permissions
}
});
@@ -208,7 +211,7 @@ const UserRoute = new Elysia({
}, {
body: t.Object({
name: t.String({ minLength: 1, error: "name is required" }),
permission: t.Array(t.Any(), { minItems: 1, error: "permission is required" })
permissions: t.Any(),
}),
detail: {
summary: "create-role",
@@ -216,14 +219,14 @@ const UserRoute = new Elysia({
}
})
.post("/role-update", async ({ body }) => {
const { id, name, permission } = body;
const { id, name, permissions } = body;
const update = await prisma.role.update({
where: {
id
},
data: {
name,
permissions: permission
permissions
}
});
@@ -235,7 +238,7 @@ const UserRoute = new Elysia({
body: t.Object({
id: t.String({ minLength: 1, error: "id is required" }),
name: t.String({ minLength: 1, error: "name is required" }),
permission: t.Array(t.String(), { minItems: 1, error: "permission is required" })
permissions: t.Any()
}),
detail: {
summary: "update-role",