@kinginsun/mcp-aitep
Version:
A CLI tool for retrieving PDE (Permitted Daily Exposure) reports for Active Pharmaceutical Ingredients (APIs) via Model Context Protocol.
96 lines (95 loc) • 3.17 kB
JavaScript
// Add basic error handling
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
import { RequestPayloadSchema } from "./types.js";
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
process.exit(1);
});
const server = new Server({
name: "mcp-aitep",
version: "0.0.9",
}, {
capabilities: {
resources: {},
tools: {},
},
});
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "AITEP_PDE_Report",
description: "Retrieves the PDE (Permitted Daily Exposure) report in JSON format for a given API (Active Pharmaceutical Ingredient)",
inputSchema: {
type: "object",
properties: {
ingredient: {
type: "string",
description: "Name of active pharmaceutical ingredient (required)",
},
},
required: ["ingredient"],
},
},
],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
const validatedArgs = RequestPayloadSchema.parse(args);
if (request.params.name === "AITEP_PDE_Report") {
const apiKey = process.env.AITEP_API_KEY;
if (!apiKey) {
return {
content: [
{
type: "text",
text: "AITEP_API_KEY environment variable is not set",
},
],
isError: true,
};
}
const response = await fetch("https://aitep.probot.hk/api/hkpma/mcp_api_detail", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(validatedArgs),
});
const result = await response.json();
const { code, data, msg } = result;
if (code != 1) {
return {
content: [
{
type: "text",
text: msg,
},
],
isError: true,
};
}
return {
content: [{ type: "text", text: JSON.stringify(data) }],
isError: false,
};
}
throw new Error("Tool not found");
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});