UNPKG

purchase-mcp-server

Version:
94 lines (93 loc) 4.09 kB
#!/usr/bin/env node import dotenv from 'dotenv'; // Load environment variables from .env file dotenv.config(); import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { toolDefinitions } from "./tools/schemas.js"; import * as toolHandlers from "./tools/handlers.js"; import * as resourceHandlers from "./resources/handlers.js"; import { resourceDefinitions } from "./resources/resources.js"; import fs from 'fs'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; // Register tools export function registerTools(server) { Object.values(toolDefinitions).forEach((tool) => { const handler = toolHandlers[tool.name]; if (!handler) { throw new Error(`Handler not found for tool: ${tool.name}`); } server.tool(tool.name, tool.description, tool.schema, handler); }); } // Register resources export function registerResources(server) { Object.values(resourceDefinitions).forEach((resource) => { const handler = resourceHandlers[resource.method]; if (!handler) { throw new Error(`Handler not found for resource: ${resource.method}`); } // Removed console.error to avoid interfering with MCP JSON-RPC protocol // Convert URI format from <param> to {param} for ResourceTemplate const templateUri = resource.uri.replace(/<([^>]+)>/g, '{$1}'); const template = new ResourceTemplate(templateUri, { list: undefined, // Not listing all resources }); try { server.resource(resource.name, template, { description: resource.description, mimeType: resource.mimeType, }, async (uri, variables) => { // Removed console.error to avoid interfering with MCP JSON-RPC protocol // Extract the identifier from the variables object // For "user://details/{user_id}", variables will contain { user_id: "..." } // For "vessel://managers/{imo}", variables will contain { imo: "..." } const userId = variables.user_id; const imo = variables.imo; const identifier = (Array.isArray(userId) ? userId[0] : userId) || (Array.isArray(imo) ? imo[0] : imo) || ''; // Removed console.error to avoid interfering with MCP JSON-RPC protocol const result = await handler(identifier); return { contents: [ { uri: uri.toString(), mimeType: resource.mimeType, text: JSON.stringify(result, null, 2), }, ], }; }); // Removed console.error to avoid interfering with MCP JSON-RPC protocol } catch (error) { // Removed console.error to avoid interfering with MCP JSON-RPC protocol throw error; } }); } // Create server instance // Use __dirname to get the correct path to package.json const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const packageJsonPath = join(__dirname, '..', 'package.json'); const packageJson = fs.readFileSync(packageJsonPath, 'utf8'); const packageJsonObject = JSON.parse(packageJson); const server = new McpServer({ name: packageJsonObject.name, version: packageJsonObject.version, }); // Register tools and resources // Removed console.error statements to avoid interfering with MCP JSON-RPC protocol registerTools(server); registerResources(server); async function main() { const transport = new StdioServerTransport(); await server.connect(transport); // Removed console.error to avoid interfering with MCP JSON-RPC protocol } main().catch((error) => { // Removed console.error to avoid interfering with MCP JSON-RPC protocol process.exit(1); });